mirror of
https://github.com/pezkuwichain/revive.git
synced 2026-04-22 13:48:08 +00:00
Add support for esm and cjs modules
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
var Module = {
|
||||
stdinData: "",
|
||||
stdoutCallback: null,
|
||||
stderrCallback: null,
|
||||
|
||||
// Function to set a callback for stdout
|
||||
setStdoutCallback: function(callback) {
|
||||
this.stdoutCallback = callback;
|
||||
},
|
||||
|
||||
// Function to set a callback for stderr
|
||||
setStderrCallback: function(callback) {
|
||||
this.stderrCallback = callback;
|
||||
},
|
||||
|
||||
// Function to set input data for stdin
|
||||
setStdinData: function(data) {
|
||||
this.stdinData = data;
|
||||
},
|
||||
|
||||
// `preRun` is called before the program starts running
|
||||
preRun: function() {
|
||||
// Define a custom stdin function
|
||||
function customStdin() {
|
||||
if (Module.stdinData.length === 0) {
|
||||
return null; // End of input (EOF)
|
||||
}
|
||||
const char = Module.stdinData.charCodeAt(0);
|
||||
Module.stdinData = Module.stdinData.slice(1); // Remove the character from input
|
||||
return char;
|
||||
}
|
||||
|
||||
// Define a custom stdout function
|
||||
function customStdout(char) {
|
||||
if (Module.stdoutCallback) {
|
||||
Module.stdoutCallback(String.fromCharCode(char));
|
||||
}
|
||||
}
|
||||
|
||||
// Define a custom stderr function
|
||||
function customStderr(char) {
|
||||
if (Module.stderrCallback) {
|
||||
Module.stderrCallback(String.fromCharCode(char));
|
||||
}
|
||||
}
|
||||
|
||||
FS.init(customStdin, customStdout, customStderr);
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,60 @@
|
||||
mergeInto(LibraryManager.library, {
|
||||
soljson_compile: function(inputPtr, inputLen) {
|
||||
const inputJson = UTF8ToString(inputPtr, inputLen);
|
||||
const output = Module.solc.compile(inputJson)
|
||||
return stringToNewUTF8(output)
|
||||
},
|
||||
soljson_version: function() {
|
||||
var version = Module.solc.version();
|
||||
return stringToNewUTF8(version)
|
||||
},
|
||||
resolc_compile: function(inputPtr, inputLen) {
|
||||
const { Worker } = require('worker_threads');
|
||||
const deasync = require('deasync');
|
||||
|
||||
var inputJson = UTF8ToString(inputPtr, inputLen);
|
||||
|
||||
function compileWithWorker(inputJson, callback) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const worker = new Worker(new URL('./worker.js', import.meta.url), {
|
||||
type: 'module',
|
||||
});
|
||||
|
||||
// Listen for messages from the worker
|
||||
worker.on('message', (message) => {
|
||||
resolve(message.output); // Resolve the promise with the output
|
||||
callback(null, message.output);
|
||||
worker.terminate(); // Terminate the worker after processing
|
||||
});
|
||||
|
||||
// Listen for errors from the worker
|
||||
worker.on('error', (error) => {
|
||||
reject(error);
|
||||
callback(error);
|
||||
worker.terminate();
|
||||
});
|
||||
|
||||
// Send the input JSON to the worker
|
||||
worker.postMessage(inputJson);
|
||||
});
|
||||
}
|
||||
let result = null;
|
||||
let error = null;
|
||||
|
||||
// Use deasync to block until promise resolves
|
||||
compileWithWorker(inputJson, function (err, res) {
|
||||
error = err;
|
||||
result = res;
|
||||
});
|
||||
// TODO: deasync is not present in browsers, another solution needs to be implemented
|
||||
deasync.loopWhile(() => result === null && error === null);
|
||||
|
||||
if (error) {
|
||||
const errorJson = JSON.stringify({ type: 'error', message: error.message || "Unknown error" });
|
||||
return stringToNewUTF8(errorJson)
|
||||
}
|
||||
|
||||
const resultJson = JSON.stringify({ type: 'success', data: result });
|
||||
return stringToNewUTF8(resultJson);
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user