mirror of
https://github.com/pezkuwichain/revive.git
synced 2026-04-22 12:38:03 +00:00
Remove async calls from revive
This commit is contained in:
@@ -9,52 +9,35 @@ mergeInto(LibraryManager.library, {
|
||||
return stringToNewUTF8(version)
|
||||
},
|
||||
resolc_compile: function(inputPtr, inputLen) {
|
||||
const { Worker } = require('worker_threads');
|
||||
const deasync = require('deasync');
|
||||
|
||||
var inputJson = UTF8ToString(inputPtr, inputLen);
|
||||
const path = require('path');
|
||||
const createRevive = require(path.resolve(__dirname, './resolc.js'));
|
||||
const revive = createRevive();
|
||||
|
||||
function compileWithWorker(inputJson, callback) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const worker = new Worker(new URL('./worker.js', import.meta.url), {
|
||||
type: 'module',
|
||||
});
|
||||
revive.setStdinData(inputJson);
|
||||
|
||||
// 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;
|
||||
let stdoutString = "";
|
||||
revive.setStdoutCallback(function(char) {
|
||||
if (char.charCodeAt(0) === '\n') {
|
||||
exit
|
||||
}
|
||||
stdoutString += char;
|
||||
});
|
||||
// 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)
|
||||
let stderrString = "";
|
||||
revive.setStderrCallback(function(char) {
|
||||
stderrString += char;
|
||||
});
|
||||
|
||||
// Call main on the new instance
|
||||
const result = revive.callMain(['--recursive-process']);
|
||||
|
||||
if (result) {
|
||||
const error = JSON.stringify({ type: 'error', message: stderrString || "Unknown error" });
|
||||
return stringToNewUTF8(error);
|
||||
} else {
|
||||
const json = JSON.stringify({ type: 'success', data: stdoutString });
|
||||
return stringToNewUTF8(json)
|
||||
}
|
||||
|
||||
const resultJson = JSON.stringify({ type: 'success', data: result });
|
||||
return stringToNewUTF8(resultJson);
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user