mirror of
https://github.com/pezkuwichain/revive.git
synced 2026-06-13 10:41:05 +00:00
Remove async calls from revive
This commit is contained in:
@@ -9,8 +9,8 @@ RUSTFLAGS_EMSCRIPTEN := \
|
|||||||
-Clink-arg=-sALLOW_MEMORY_GROWTH \
|
-Clink-arg=-sALLOW_MEMORY_GROWTH \
|
||||||
-Clink-arg=-sEXPORTED_RUNTIME_METHODS=FS,callMain,stringToNewUTF8,cwrap \
|
-Clink-arg=-sEXPORTED_RUNTIME_METHODS=FS,callMain,stringToNewUTF8,cwrap \
|
||||||
-Clink-arg=-sMODULARIZE \
|
-Clink-arg=-sMODULARIZE \
|
||||||
-Clink-arg=-sEXPORT_ES6 \
|
|
||||||
-Clink-arg=-sEXPORT_NAME=createRevive \
|
-Clink-arg=-sEXPORT_NAME=createRevive \
|
||||||
|
-Clink-arg=-sWASM_ASYNC_COMPILATION=0 \
|
||||||
-Clink-arg=--js-library=js/embed/soljson_interface.js \
|
-Clink-arg=--js-library=js/embed/soljson_interface.js \
|
||||||
-Clink-arg=--pre-js=js/embed/pre.js
|
-Clink-arg=--pre-js=js/embed/pre.js
|
||||||
|
|
||||||
|
|||||||
@@ -9,52 +9,35 @@ mergeInto(LibraryManager.library, {
|
|||||||
return stringToNewUTF8(version)
|
return stringToNewUTF8(version)
|
||||||
},
|
},
|
||||||
resolc_compile: function(inputPtr, inputLen) {
|
resolc_compile: function(inputPtr, inputLen) {
|
||||||
const { Worker } = require('worker_threads');
|
|
||||||
const deasync = require('deasync');
|
|
||||||
|
|
||||||
var inputJson = UTF8ToString(inputPtr, inputLen);
|
var inputJson = UTF8ToString(inputPtr, inputLen);
|
||||||
|
const path = require('path');
|
||||||
|
const createRevive = require(path.resolve(__dirname, './resolc.js'));
|
||||||
|
const revive = createRevive();
|
||||||
|
|
||||||
function compileWithWorker(inputJson, callback) {
|
revive.setStdinData(inputJson);
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const worker = new Worker(new URL('./worker.js', import.meta.url), {
|
|
||||||
type: 'module',
|
|
||||||
});
|
|
||||||
|
|
||||||
// Listen for messages from the worker
|
let stdoutString = "";
|
||||||
worker.on('message', (message) => {
|
revive.setStdoutCallback(function(char) {
|
||||||
resolve(message.output); // Resolve the promise with the output
|
if (char.charCodeAt(0) === '\n') {
|
||||||
callback(null, message.output);
|
exit
|
||||||
worker.terminate(); // Terminate the worker after processing
|
}
|
||||||
});
|
stdoutString += char;
|
||||||
|
|
||||||
// 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) {
|
let stderrString = "";
|
||||||
const errorJson = JSON.stringify({ type: 'error', message: error.message || "Unknown error" });
|
revive.setStderrCallback(function(char) {
|
||||||
return stringToNewUTF8(errorJson)
|
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,9 +3,7 @@
|
|||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "Revive compiler",
|
"description": "Revive compiler",
|
||||||
"main": "run_revive.js",
|
"main": "run_revive.js",
|
||||||
"type": "module",
|
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"deasync": "^0.1.15",
|
|
||||||
"solc": "^0.8.28"
|
"solc": "^0.8.28"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@@ -16,7 +14,6 @@
|
|||||||
"@babel/core": "^7.26.0",
|
"@babel/core": "^7.26.0",
|
||||||
"@babel/preset-env": "^7.26.0",
|
"@babel/preset-env": "^7.26.0",
|
||||||
"@rollup/plugin-babel": "^6.0.4",
|
"@rollup/plugin-babel": "^6.0.4",
|
||||||
"@rollup/plugin-node-resolve": "^15.3.0",
|
|
||||||
"rollup": "^4.27.3",
|
"rollup": "^4.27.3",
|
||||||
"rollup-plugin-copy": "^3.5.0"
|
"rollup-plugin-copy": "^3.5.0"
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-10
@@ -1,12 +1,12 @@
|
|||||||
import babel from '@rollup/plugin-babel';
|
const babel = require('@rollup/plugin-babel');
|
||||||
import copy from 'rollup-plugin-copy';
|
const copy = require('rollup-plugin-copy');
|
||||||
import resolve from '@rollup/plugin-node-resolve';
|
const resolve = require('@rollup/plugin-node-resolve'); // Add this if resolve is not already imported
|
||||||
|
|
||||||
const outputDirCJS = 'dist/revive-cjs';
|
const outputDirCJS = 'dist/revive-cjs';
|
||||||
const outputDirESM = 'dist/revive-esm';
|
const outputDirESM = 'dist/revive-esm';
|
||||||
|
|
||||||
export default {
|
module.exports = {
|
||||||
input: ['src/resolc.js', 'src/worker.js'], // Adjust this to your main entry file
|
input: ['src/resolc.js'],
|
||||||
output: [
|
output: [
|
||||||
{
|
{
|
||||||
dir: outputDirCJS,
|
dir: outputDirCJS,
|
||||||
@@ -26,10 +26,10 @@ export default {
|
|||||||
}),
|
}),
|
||||||
resolve(),
|
resolve(),
|
||||||
copy({
|
copy({
|
||||||
targets: [
|
targets: [
|
||||||
{ src: 'src/resolc.wasm', dest: outputDirCJS },
|
{ src: 'src/resolc.wasm', dest: outputDirCJS },
|
||||||
{ src: 'src/resolc.wasm', dest: outputDirESM },
|
{ src: 'src/resolc.wasm', dest: outputDirESM },
|
||||||
],
|
],
|
||||||
})
|
}),
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|||||||
+3
-4
@@ -1,6 +1,5 @@
|
|||||||
import solc from 'solc';
|
const solc = require('solc');
|
||||||
// Import the Emscripten module
|
const createRevive = require('./dist/revive-cjs/resolc.js');
|
||||||
import createRevive from './dist/revive-esm/resolc.js';
|
|
||||||
|
|
||||||
const compilerStandardJsonInput = {
|
const compilerStandardJsonInput = {
|
||||||
language: 'Solidity',
|
language: 'Solidity',
|
||||||
@@ -31,7 +30,7 @@ const compilerStandardJsonInput = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
async function runCompiler() {
|
async function runCompiler() {
|
||||||
const m = await createRevive();
|
const m = createRevive();
|
||||||
m.solc = solc;
|
m.solc = solc;
|
||||||
|
|
||||||
// Set input data for stdin
|
// Set input data for stdin
|
||||||
|
|||||||
@@ -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 });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
Reference in New Issue
Block a user