Skip to content

Commit

Permalink
Merge pull request #123 from smiasojed/web
Browse files Browse the repository at this point in the history
Add web worker compatibility
  • Loading branch information
smiasojed authored Nov 29, 2024
2 parents 93f1deb + 9a150b1 commit 54d154a
Show file tree
Hide file tree
Showing 14 changed files with 114 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-revive-wasm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:

env:
CARGO_TERM_COLOR: always
REVIVE_WASM_INSTALL_DIR: ${{ github.workspace }}/js/dist/revive-cjs
REVIVE_WASM_INSTALL_DIR: ${{ github.workspace }}/target/wasm32-unknown-emscripten/release
EMSCRIPTEN_VERSION: 3.1.64

jobs:
Expand Down
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,4 @@ artifacts
tmp
package-lock.json
/*.html
/js/src/resolc.*
/js/dist/
/build
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ install-npm:
install-wasm:
RUSTFLAGS='$(RUSTFLAGS_EMSCRIPTEN)' cargo build --target wasm32-unknown-emscripten -p revive-solidity --release --no-default-features
npm install
npm run build:revive

# install-revive: Build and install to the directory specified in REVIVE_INSTALL_DIR
ifeq ($(origin REVIVE_INSTALL_DIR), undefined)
Expand Down
16 changes: 13 additions & 3 deletions js/embed/soljson_interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
var revive = createRevive();
} else {
throw new Error('Unknown environment: Unable to load resolc.js');
}
revive.setStdinData(inputJson);

let stdoutString = "";
Expand Down
1 change: 1 addition & 0 deletions js/examples/node/resolc.js
1 change: 1 addition & 0 deletions js/examples/node/resolc.wasm
6 changes: 3 additions & 3 deletions js/run_revive.js → js/examples/node/run_revive.js
Original file line number Diff line number Diff line change
@@ -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',
Expand Down Expand Up @@ -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 => {
Expand Down
35 changes: 35 additions & 0 deletions js/examples/web/index.html
Original file line number Diff line number Diff line change
@@ -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>
1 change: 1 addition & 0 deletions js/examples/web/resolc.js
1 change: 1 addition & 0 deletions js/examples/web/resolc.wasm
52 changes: 52 additions & 0 deletions js/examples/web/worker.js
Original file line number Diff line number Diff line change
@@ -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});
};
15 changes: 5 additions & 10 deletions js/package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
{
"name": "revive",
"version": "1.0.0",
"description": "Revive compiler",
"main": "run_revive.js",
"private": true,
"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"
"fetch:soljson": "wget https://binaries.soliditylang.org/wasm/soljson-v0.8.28+commit.7893614a.js -O ./examples/web/soljson.js",
"example:web": "npm run fetch:soljson && http-server ./examples/web/",
"example: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"
}
}
33 changes: 0 additions & 33 deletions js/rollup.config.js

This file was deleted.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"private": true,
"scripts": {
"test:cli": "npm run test -w crates/solidity/src/tests/cli-tests",
"build:revive": "npm run build -w js",
"test:revive": "npm run test -w js"
"test:revive": "npm run example:node -w js"
},
"workspaces": [
"crates/solidity/src/tests/cli-tests",
Expand Down

0 comments on commit 54d154a

Please sign in to comment.