mirror of
https://github.com/pezkuwichain/revive.git
synced 2026-04-27 17:28:01 +00:00
Add web worker compatibility
This commit is contained in:
@@ -14,6 +14,4 @@ artifacts
|
||||
tmp
|
||||
package-lock.json
|
||||
/*.html
|
||||
/js/src/resolc.*
|
||||
/js/dist/
|
||||
/build
|
||||
|
||||
@@ -10,10 +10,20 @@ mergeInto(LibraryManager.library, {
|
||||
},
|
||||
resolc_compile: function(inputPtr, inputLen) {
|
||||
const inputJson = UTF8ToString(inputPtr, inputLen);
|
||||
const path = require('path');
|
||||
const createRevive = require(path.resolve(__dirname, './resolc.js'));
|
||||
const revive = createRevive();
|
||||
|
||||
// Check if running in a web worker or node.js
|
||||
if (typeof importScripts === 'function') {
|
||||
// Running in a web worker
|
||||
importScripts('./resolc.js');
|
||||
var revive = createRevive()
|
||||
} else if (typeof require === 'function') {
|
||||
// Running in Node.js
|
||||
const path = require('path');
|
||||
createRevive = require(path.resolve(__dirname, './resolc.js')); // `createRevive` is returned from the required module
|
||||
var revive = createRevive();
|
||||
} else {
|
||||
throw new Error('Unknown environment: Unable to load resolc.js');
|
||||
}
|
||||
revive.setStdinData(inputJson);
|
||||
|
||||
let stdoutString = "";
|
||||
|
||||
Symlink
+1
@@ -0,0 +1 @@
|
||||
../../../target/wasm32-unknown-emscripten/release/resolc.js
|
||||
Symlink
+1
@@ -0,0 +1 @@
|
||||
../../../target/wasm32-unknown-emscripten/release/resolc.wasm
|
||||
@@ -1,5 +1,5 @@
|
||||
const soljson = require('solc/soljson');
|
||||
const createRevive = require('./dist/revive-cjs/resolc.js');
|
||||
const createRevive = require('./resolc.js');
|
||||
|
||||
const compilerStandardJsonInput = {
|
||||
language: 'Solidity',
|
||||
@@ -52,8 +52,8 @@ async function runCompiler() {
|
||||
|
||||
// Compile the Solidity source code
|
||||
let x = m.callMain(['--standard-json']);
|
||||
console.log("Stdout: " + stdoutString)
|
||||
console.error("Stderr: " + stderrString)
|
||||
console.log("Stdout: " + stdoutString);
|
||||
console.error("Stderr: " + stderrString);
|
||||
}
|
||||
|
||||
runCompiler().catch(err => {
|
||||
@@ -0,0 +1,35 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>Web Worker Example</title>
|
||||
<style>
|
||||
/* Ensure the pre tag wraps long lines */
|
||||
pre {
|
||||
white-space: pre-wrap; /* Wrap long lines */
|
||||
word-wrap: break-word; /* Break long words */
|
||||
max-width: 100%; /* Optional: Ensures it doesn't overflow container */
|
||||
overflow-wrap: break-word; /* Another method for wrapping */
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Revive Compilation Output</h1>
|
||||
<pre id="output"></pre>
|
||||
<script>
|
||||
var outputElement = document.getElementById('output');
|
||||
var worker = new Worker('./worker.js');
|
||||
worker.addEventListener('message', function (e) {
|
||||
const output = e.data.output
|
||||
outputElement.textContent = output;
|
||||
}, false);
|
||||
|
||||
worker.postMessage({
|
||||
contractCode: 'contract C { function f() public { } }',
|
||||
})
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
Symlink
+1
@@ -0,0 +1 @@
|
||||
../../../target/wasm32-unknown-emscripten/release/resolc.js
|
||||
Symlink
+1
@@ -0,0 +1 @@
|
||||
../../../target/wasm32-unknown-emscripten/release/resolc.wasm
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,52 @@
|
||||
|
||||
importScripts('./soljson.js');
|
||||
importScripts('./resolc.js');
|
||||
|
||||
// Handle messages from the main thread
|
||||
onmessage = async function (e) {
|
||||
const contractCode = e.data.contractCode
|
||||
const sourceCode = {
|
||||
language: 'Solidity',
|
||||
sources: {
|
||||
contract: {
|
||||
content: contractCode,
|
||||
}
|
||||
},
|
||||
settings: {
|
||||
optimizer: {
|
||||
enabled: true,
|
||||
runs: 200,
|
||||
},
|
||||
outputSelection: {
|
||||
'*': {
|
||||
'*': ['abi'],
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
const m = createRevive();
|
||||
|
||||
m.soljson = Module;
|
||||
|
||||
// Set input data for stdin
|
||||
m.setStdinData(JSON.stringify(sourceCode));
|
||||
|
||||
var stdoutString = "";
|
||||
m.setStdoutCallback(function(char) {
|
||||
if (char.charCodeAt(0) === '\n') {
|
||||
console.log("new line")
|
||||
exit
|
||||
}
|
||||
stdoutString += char;
|
||||
});
|
||||
|
||||
var stderrString = "";
|
||||
m.setStderrCallback(function(char) {
|
||||
stderrString += char;
|
||||
});
|
||||
|
||||
// Compile the Solidity source code
|
||||
m.callMain(['--standard-json']);
|
||||
|
||||
postMessage({output: stdoutString || stderrString});
|
||||
};
|
||||
+4
-8
@@ -2,19 +2,15 @@
|
||||
"name": "revive",
|
||||
"version": "1.0.0",
|
||||
"description": "Revive compiler",
|
||||
"main": "run_revive.js",
|
||||
"main": "resolc.js",
|
||||
"dependencies": {
|
||||
"solc": "^0.8.28"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "mkdir -p src && cp ../target/wasm32-unknown-emscripten/release/resolc.js ../target/wasm32-unknown-emscripten/release/resolc.wasm ./src && npx rollup -c",
|
||||
"test": "npm run build && node run_revive.js"
|
||||
"start-web": "http-server ./examples/web/",
|
||||
"start-node": "node ./examples//node/run_revive.js"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.26.0",
|
||||
"@babel/preset-env": "^7.26.0",
|
||||
"@rollup/plugin-babel": "^6.0.4",
|
||||
"rollup": "^4.27.3",
|
||||
"rollup-plugin-copy": "^3.5.0"
|
||||
"http-server": "^14.1.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
const babel = require('@rollup/plugin-babel');
|
||||
const copy = require('rollup-plugin-copy');
|
||||
|
||||
const outputDirCJS = 'dist/revive-cjs';
|
||||
const outputDirESM = 'dist/revive-esm';
|
||||
|
||||
module.exports = {
|
||||
input: ['src/resolc.js'],
|
||||
output: [
|
||||
{
|
||||
dir: outputDirCJS,
|
||||
format: 'cjs',
|
||||
exports: 'auto',
|
||||
},
|
||||
{
|
||||
dir: outputDirESM,
|
||||
format: 'esm',
|
||||
},
|
||||
],
|
||||
plugins: [
|
||||
babel({
|
||||
exclude: 'node_modules/**',
|
||||
presets: ['@babel/preset-env'],
|
||||
babelHelpers: 'inline',
|
||||
}),
|
||||
copy({
|
||||
targets: [
|
||||
{ src: 'src/resolc.wasm', dest: outputDirCJS },
|
||||
{ src: 'src/resolc.wasm', dest: outputDirESM },
|
||||
],
|
||||
}),
|
||||
],
|
||||
};
|
||||
Reference in New Issue
Block a user