diff --git a/.github/workflows/build-revive-wasm.yml b/.github/workflows/build-revive-wasm.yml index e848516..90bc2f8 100644 --- a/.github/workflows/build-revive-wasm.yml +++ b/.github/workflows/build-revive-wasm.yml @@ -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: diff --git a/.gitignore b/.gitignore index 1c4929d..6422124 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,4 @@ artifacts tmp package-lock.json /*.html -/js/src/resolc.* -/js/dist/ /build diff --git a/Makefile b/Makefile index c99c885..2b08368 100644 --- a/Makefile +++ b/Makefile @@ -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) diff --git a/js/embed/soljson_interface.js b/js/embed/soljson_interface.js index 40cabcb..cefff55 100644 --- a/js/embed/soljson_interface.js +++ b/js/embed/soljson_interface.js @@ -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 = ""; diff --git a/js/examples/node/resolc.js b/js/examples/node/resolc.js new file mode 120000 index 0000000..ed95f38 --- /dev/null +++ b/js/examples/node/resolc.js @@ -0,0 +1 @@ +../../../target/wasm32-unknown-emscripten/release/resolc.js \ No newline at end of file diff --git a/js/examples/node/resolc.wasm b/js/examples/node/resolc.wasm new file mode 120000 index 0000000..79f581c --- /dev/null +++ b/js/examples/node/resolc.wasm @@ -0,0 +1 @@ +../../../target/wasm32-unknown-emscripten/release/resolc.wasm \ No newline at end of file diff --git a/js/run_revive.js b/js/examples/node/run_revive.js similarity index 89% rename from js/run_revive.js rename to js/examples/node/run_revive.js index 9c1c32d..bd6b10c 100644 --- a/js/run_revive.js +++ b/js/examples/node/run_revive.js @@ -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 => { diff --git a/js/examples/web/index.html b/js/examples/web/index.html new file mode 100644 index 0000000..3123a26 --- /dev/null +++ b/js/examples/web/index.html @@ -0,0 +1,35 @@ + + + + + + Web Worker Example + + + + +

Revive Compilation Output

+

+    
+
+
+
diff --git a/js/examples/web/resolc.js b/js/examples/web/resolc.js
new file mode 120000
index 0000000..ed95f38
--- /dev/null
+++ b/js/examples/web/resolc.js
@@ -0,0 +1 @@
+../../../target/wasm32-unknown-emscripten/release/resolc.js
\ No newline at end of file
diff --git a/js/examples/web/resolc.wasm b/js/examples/web/resolc.wasm
new file mode 120000
index 0000000..79f581c
--- /dev/null
+++ b/js/examples/web/resolc.wasm
@@ -0,0 +1 @@
+../../../target/wasm32-unknown-emscripten/release/resolc.wasm
\ No newline at end of file
diff --git a/js/examples/web/worker.js b/js/examples/web/worker.js
new file mode 100644
index 0000000..68cd0b7
--- /dev/null
+++ b/js/examples/web/worker.js
@@ -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});
+};
diff --git a/js/package.json b/js/package.json
index b38dade..9de29b2 100644
--- a/js/package.json
+++ b/js/package.json
@@ -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"
   }
 }
diff --git a/js/rollup.config.js b/js/rollup.config.js
deleted file mode 100644
index 399d2f1..0000000
--- a/js/rollup.config.js
+++ /dev/null
@@ -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 },
-      ],
-    }),
-  ],
-};
diff --git a/package.json b/package.json
index ebee435..71dacf7 100644
--- a/package.json
+++ b/package.json
@@ -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",