Move @parity/resolc from js-revive (#296)

- Move npm package from paritytech/js-revive 
- Rename package to `@parity/resolc`
This commit is contained in:
PG Herveou
2025-04-30 17:24:52 +02:00
committed by GitHub
parent f6a412eef4
commit e07d0f0cb7
49 changed files with 940 additions and 75 deletions
+1
View File
@@ -0,0 +1 @@
../../../../target/wasm32-unknown-emscripten/release/resolc.js
+1
View File
@@ -0,0 +1 @@
../../../../target/wasm32-unknown-emscripten/release/resolc.wasm
+32
View File
@@ -0,0 +1,32 @@
const soljson = require("solc/soljson");
const createRevive = require("./resolc.js");
async function compile(standardJsonInput) {
if (!standardJsonInput) {
throw new Error("Input JSON for the Solidity compiler is required.");
}
// Initialize the compiler
const compiler = createRevive();
compiler.soljson = soljson;
// Provide input to the compiler
compiler.writeToStdin(JSON.stringify(standardJsonInput));
// Run the compiler
compiler.callMain(["--standard-json"]);
// Collect output
const stdout = compiler.readFromStdout();
const stderr = compiler.readFromStderr();
// Check for errors and throw if stderr exists
if (stderr) {
throw new Error(`Compilation failed: ${stderr}`);
}
// Return the output if no errors
return stdout;
}
module.exports = { compile };
+38
View File
@@ -0,0 +1,38 @@
const { compile } = require("./revive.js");
const compilerStandardJsonInput = {
language: "Solidity",
sources: {
"MyContract.sol": {
content: `
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
contract MyContract {
function greet() public pure returns (string memory) {
return "Hello";
}
}
`,
},
},
settings: {
optimizer: {
enabled: true,
runs: 200,
},
outputSelection: {
"*": {
"*": ["abi"],
},
},
},
};
async function runCompiler() {
let output = await compile(compilerStandardJsonInput);
console.log("Output: " + output);
}
runCompiler().catch((err) => {
console.error("Error:", err);
});
+53
View File
@@ -0,0 +1,53 @@
<!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");
const standardJsonInput = {
language: "Solidity",
sources: {
contract: {
content: "contract MyContract { function f() public { } }",
},
},
settings: {
optimizer: {
enabled: true,
runs: 200,
},
outputSelection: {
"*": {
"*": ["abi"],
},
},
},
};
worker.addEventListener(
"message",
function (e) {
outputElement.textContent = e.data.output;
},
false,
);
worker.postMessage(JSON.stringify(standardJsonInput));
</script>
</body>
</html>
+1
View File
@@ -0,0 +1 @@
../../../../target/wasm32-unknown-emscripten/release/resolc.wasm
+1
View File
@@ -0,0 +1 @@
../../../../target/wasm32-unknown-emscripten/release/resolc_web.js
+14
View File
@@ -0,0 +1,14 @@
importScripts("./resolc_web.js");
// Handle messages from the main thread
onmessage = async function (e) {
const m = createRevive();
// Set input data for stdin
m.writeToStdin(e.data);
// Compile the Solidity source code
m.callMain(["--standard-json"]);
postMessage({ output: m.readFromStdout() || m.readFromStderr() });
};