Remove async calls from revive

This commit is contained in:
Sebastian Miasojed
2024-11-26 22:03:57 +01:00
parent 1eb1083d40
commit b65fa2e42c
6 changed files with 38 additions and 91 deletions
+1 -1
View File
@@ -9,8 +9,8 @@ RUSTFLAGS_EMSCRIPTEN := \
-Clink-arg=-sALLOW_MEMORY_GROWTH \
-Clink-arg=-sEXPORTED_RUNTIME_METHODS=FS,callMain,stringToNewUTF8,cwrap \
-Clink-arg=-sMODULARIZE \
-Clink-arg=-sEXPORT_ES6 \
-Clink-arg=-sEXPORT_NAME=createRevive \
-Clink-arg=-sWASM_ASYNC_COMPILATION=0 \
-Clink-arg=--js-library=js/embed/soljson_interface.js \
-Clink-arg=--pre-js=js/embed/pre.js
+24 -41
View File
@@ -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);
},
});
-3
View File
@@ -3,9 +3,7 @@
"version": "1.0.0",
"description": "Revive compiler",
"main": "run_revive.js",
"type": "module",
"dependencies": {
"deasync": "^0.1.15",
"solc": "^0.8.28"
},
"scripts": {
@@ -16,7 +14,6 @@
"@babel/core": "^7.26.0",
"@babel/preset-env": "^7.26.0",
"@rollup/plugin-babel": "^6.0.4",
"@rollup/plugin-node-resolve": "^15.3.0",
"rollup": "^4.27.3",
"rollup-plugin-copy": "^3.5.0"
}
+10 -10
View File
@@ -1,12 +1,12 @@
import babel from '@rollup/plugin-babel';
import copy from 'rollup-plugin-copy';
import resolve from '@rollup/plugin-node-resolve';
const babel = require('@rollup/plugin-babel');
const copy = require('rollup-plugin-copy');
const resolve = require('@rollup/plugin-node-resolve'); // Add this if resolve is not already imported
const outputDirCJS = 'dist/revive-cjs';
const outputDirESM = 'dist/revive-esm';
export default {
input: ['src/resolc.js', 'src/worker.js'], // Adjust this to your main entry file
module.exports = {
input: ['src/resolc.js'],
output: [
{
dir: outputDirCJS,
@@ -26,10 +26,10 @@ export default {
}),
resolve(),
copy({
targets: [
{ src: 'src/resolc.wasm', dest: outputDirCJS },
{ src: 'src/resolc.wasm', dest: outputDirESM },
],
})
targets: [
{ src: 'src/resolc.wasm', dest: outputDirCJS },
{ src: 'src/resolc.wasm', dest: outputDirESM },
],
}),
],
};
+3 -4
View File
@@ -1,6 +1,5 @@
import solc from 'solc';
// Import the Emscripten module
import createRevive from './dist/revive-esm/resolc.js';
const solc = require('solc');
const createRevive = require('./dist/revive-cjs/resolc.js');
const compilerStandardJsonInput = {
language: 'Solidity',
@@ -31,7 +30,7 @@ const compilerStandardJsonInput = {
};
async function runCompiler() {
const m = await createRevive();
const m = createRevive();
m.solc = solc;
// Set input data for stdin
-32
View File
@@ -1,32 +0,0 @@
import { parentPort } from 'worker_threads';
parentPort.on('message', async (inputJson) => {
const { default: createRevive } = await import(new URL('./resolc.js', import.meta.url));
const revive = await createRevive();
revive.setStdinData(inputJson);
let stdoutString = "";
revive.setStdoutCallback(function(char) {
if (char.charCodeAt(0) === '\n') {
console.log("new line")
exit
}
stdoutString += char;
});
let stderrString = "";
revive.setStderrCallback(function(char) {
stderrString += char;
});
// Call main on the new instance
const output = revive.callMain(['--recursive-process']);
if (stderrString.length > 0) {
// If /err is not empty, throw an error with its content
throw new Error(stderrString);
} else {
parentPort.postMessage({ output: stdoutString });
}
});