From b65fa2e42c5e0f8bb3ed1a7057e3a6148b1fc5a3 Mon Sep 17 00:00:00 2001 From: Sebastian Miasojed Date: Tue, 26 Nov 2024 22:03:57 +0100 Subject: [PATCH] Remove async calls from revive --- Makefile | 2 +- js/embed/soljson_interface.js | 65 +++++++++++++---------------------- js/package.json | 3 -- js/rollup.config.js | 20 +++++------ js/run_revive.js | 7 ++-- js/src/worker.js | 32 ----------------- 6 files changed, 38 insertions(+), 91 deletions(-) delete mode 100644 js/src/worker.js diff --git a/Makefile b/Makefile index 96749a9..c99c885 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/js/embed/soljson_interface.js b/js/embed/soljson_interface.js index 44f1a64..b053d49 100644 --- a/js/embed/soljson_interface.js +++ b/js/embed/soljson_interface.js @@ -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); }, }); diff --git a/js/package.json b/js/package.json index 125e236..b64f4e6 100644 --- a/js/package.json +++ b/js/package.json @@ -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" } diff --git a/js/rollup.config.js b/js/rollup.config.js index b46d773..60bf31c 100644 --- a/js/rollup.config.js +++ b/js/rollup.config.js @@ -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 }, + ], + }), ], }; diff --git a/js/run_revive.js b/js/run_revive.js index 3c16085..a4cbc85 100644 --- a/js/run_revive.js +++ b/js/run_revive.js @@ -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 diff --git a/js/src/worker.js b/js/src/worker.js deleted file mode 100644 index 24a7b00..0000000 --- a/js/src/worker.js +++ /dev/null @@ -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 }); - } -});