Skip to content

Commit

Permalink
Add all resolc dependencies to resolc_web.js file (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
smiasojed authored Feb 11, 2025
1 parent 374563b commit 79ec4dd
Show file tree
Hide file tree
Showing 20 changed files with 488 additions and 387 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build-revive-wasm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ jobs:
path: |
${{ env.REVIVE_WASM_INSTALL_DIR }}/resolc.js
${{ env.REVIVE_WASM_INSTALL_DIR }}/resolc.wasm
${{ env.REVIVE_WASM_INSTALL_DIR }}/resolc_web.js
retention-days: 1

test-revive-wasm:
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ install-npm:

install-wasm: install-npm
cargo build --target wasm32-unknown-emscripten -p revive-solidity --release --no-default-features
npm run build:package

install-llvm-builder:
cargo install --path crates/llvm-builder
Expand Down
53 changes: 53 additions & 0 deletions js/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const fs = require("fs");
const path = require("path");
const { minify } = require("terser");

const SOLJSON_URI =
"https://binaries.soliditylang.org/wasm/soljson-v0.8.28+commit.7893614a.js";
const RESOLC_WASM_URI = "http://127.0.0.1:8080/resolc.wasm";
const RESOLC_WASM_TARGET_DIR = path.join(
__dirname,
"../target/wasm32-unknown-emscripten/release",
);
const RESOLC_JS = path.join(RESOLC_WASM_TARGET_DIR, "resolc.js");
const RESOLC_WEB_JS = path.join(RESOLC_WASM_TARGET_DIR, "resolc_web.js");

const resolcJs = fs.readFileSync(RESOLC_JS, "utf-8");

const packedJsContent = `
if (typeof importScripts === "function") {
importScripts("${SOLJSON_URI}");
var moduleArgs = {
wasmBinary: (function () {
var xhr = new XMLHttpRequest();
xhr.open("GET", "${RESOLC_WASM_URI}", false);
xhr.responseType = "arraybuffer";
xhr.send(null);
return new Uint8Array(xhr.response);
})(),
soljson: Module
};
} else {
console.log("Not a WebWorker, skipping Soljson and WASM loading.");
}
${resolcJs}
createRevive = createRevive.bind(null, moduleArgs);
`;

minify(packedJsContent)
.then((minifiedJs) => {
if (minifiedJs.error) {
console.error("Error during minification:", minifiedJs.error);
process.exit(1);
}

fs.writeFileSync(RESOLC_WEB_JS, minifiedJs.code, "utf-8");
console.log(`Combined script written to ${RESOLC_WEB_JS}`);
})
.catch((err) => {
console.error("Minification failed:", err);
process.exit(1);
});
129 changes: 80 additions & 49 deletions js/e2e/web.test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
const { test, expect } = require('@playwright/test');
const fs = require('fs');
const path = require('path');
const { test, expect } = require("@playwright/test");
const fs = require("fs");
const path = require("path");

function loadFixture(fixture) {
const fixturePath = path.resolve(__dirname, `../fixtures/${fixture}`);
return JSON.parse(fs.readFileSync(fixturePath, 'utf-8'));
return JSON.parse(fs.readFileSync(fixturePath, "utf-8"));
}

async function loadTestPage(page) {
await page.goto("http://127.0.0.1:8080");
const outputElement = page.locator("#output");
await outputElement.waitFor({ state: "visible" });
await page.setContent("");
}

async function runWorker(page, input) {
return await page.evaluate((input) => {
return new Promise((resolve, reject) => {
const worker = new Worker('worker.js');
const worker = new Worker("worker.js");
worker.postMessage(JSON.stringify(input));

worker.onmessage = (event) => {
Expand All @@ -26,69 +33,87 @@ async function runWorker(page, input) {
}, input);
}

test('should successfully compile valid Solidity code in the browser', async ({ page }) => {
await page.goto("http://127.0.0.1:8080");
await page.setContent("");
const standardInput = loadFixture('storage.json')
test("should successfully compile valid Solidity code in browser", async ({
page,
}) => {
await loadTestPage(page);
const standardInput = loadFixture("storage.json");
const result = await runWorker(page, standardInput);
expect(typeof result).toBe('string');

expect(typeof result).toBe("string");
let output = JSON.parse(result);
expect(output).toHaveProperty('contracts');
expect(output.contracts['fixtures/storage.sol']).toHaveProperty('Storage');
expect(output.contracts['fixtures/storage.sol'].Storage).toHaveProperty('abi');
expect(output.contracts['fixtures/storage.sol'].Storage).toHaveProperty('evm');
expect(output.contracts['fixtures/storage.sol'].Storage.evm).toHaveProperty('bytecode');
expect(output).toHaveProperty("contracts");
expect(output.contracts["fixtures/storage.sol"]).toHaveProperty("Storage");
expect(output.contracts["fixtures/storage.sol"].Storage).toHaveProperty(
"abi",
);
expect(output.contracts["fixtures/storage.sol"].Storage).toHaveProperty(
"evm",
);
expect(output.contracts["fixtures/storage.sol"].Storage.evm).toHaveProperty(
"bytecode",
);
});

test('should successfully compile large valid Solidity code in the browser', async ({ page }) => {
await page.goto("http://127.0.0.1:8080");
await page.setContent("");
const standardInput = loadFixture('token.json')
test("should successfully compile large valid Solidity code in browser", async ({
page,
browserName,
}) => {
if (browserName === "firefox") {
// Skipping tests with large contracts on Firefox due to out-of-memory issues.
test.skip();
}
await loadTestPage(page);
const standardInput = loadFixture("token.json");
const result = await runWorker(page, standardInput);
expect(typeof result).toBe('string');

expect(typeof result).toBe("string");
let output = JSON.parse(result);
expect(output).toHaveProperty('contracts');
expect(output.contracts['fixtures/token.sol']).toHaveProperty('MyToken');
expect(output.contracts['fixtures/token.sol'].MyToken).toHaveProperty('abi');
expect(output.contracts['fixtures/token.sol'].MyToken).toHaveProperty('evm');
expect(output.contracts['fixtures/token.sol'].MyToken.evm).toHaveProperty('bytecode');
expect(output).toHaveProperty("contracts");
expect(output.contracts["fixtures/token.sol"]).toHaveProperty("MyToken");
expect(output.contracts["fixtures/token.sol"].MyToken).toHaveProperty("abi");
expect(output.contracts["fixtures/token.sol"].MyToken).toHaveProperty("evm");
expect(output.contracts["fixtures/token.sol"].MyToken.evm).toHaveProperty(
"bytecode",
);
});

test('should throw an error for invalid Solidity code in the browser', async ({ page }) => {
await page.goto("http://127.0.0.1:8080");
await page.setContent("");
const standardInput = loadFixture('invalid_contract_content.json')
test("should throw an error for invalid Solidity code in browser", async ({
page,
}) => {
await loadTestPage(page);
const standardInput = loadFixture("invalid_contract_content.json");
const result = await runWorker(page, standardInput);

expect(typeof result).toBe('string');
expect(typeof result).toBe("string");
let output = JSON.parse(result);
expect(output).toHaveProperty('errors');
expect(output).toHaveProperty("errors");
expect(Array.isArray(output.errors)).toBeTruthy(); // Check if it's an array
expect(output.errors.length).toBeGreaterThan(0);
expect(output.errors[0]).toHaveProperty('type');
expect(output.errors[0].type).toContain('ParserError');
expect(output.errors[0]).toHaveProperty("type");
expect(output.errors[0].type).toContain("ParserError");
});

test('should return not found error for missing imports in the browser', async ({page}) => {
await page.goto("http://127.0.0.1:8080");
await page.setContent("");
const standardInput = loadFixture('missing_import.json')
test("should return not found error for missing imports in browser", async ({
page,
}) => {
await loadTestPage(page);
const standardInput = loadFixture("missing_import.json");
const result = await runWorker(page, standardInput);
expect(typeof result).toBe('string');

expect(typeof result).toBe("string");
let output = JSON.parse(result);
expect(output).toHaveProperty('errors');
expect(output).toHaveProperty("errors");
expect(Array.isArray(output.errors)).toBeTruthy(); // Check if it's an array
expect(output.errors.length).toBeGreaterThan(0);
expect(output.errors[0]).toHaveProperty('message');
expect(output.errors[0].message).toContain('Source "nonexistent/console.sol" not found');
expect(output.errors[0]).toHaveProperty("message");
expect(output.errors[0].message).toContain(
'Source "nonexistent/console.sol" not found',
);
});

test('should successfully compile a valid Solidity contract that instantiates another contract in the browser', async ({ page }) => {
await page.goto("http://127.0.0.1:8080");
await page.setContent("");
await loadTestPage(page);
const standardInput = loadFixture('instantiate.json')
const result = await runWorker(page, standardInput);

Expand All @@ -105,9 +130,15 @@ test('should successfully compile a valid Solidity contract that instantiates an
expect(output.contracts['fixtures/instantiate.sol'].MainContract.evm).toHaveProperty('bytecode');
});

test('should successfully compile a valid Solidity contract that instantiates the token contracts in the browser', async ({ page }) => {
await page.goto("http://127.0.0.1:8080");
await page.setContent("");
test('should successfully compile a valid Solidity contract that instantiates the token contracts in the browser', async ({
page,
browserName,
}) => {
if (browserName === "firefox") {
// Skipping tests with large contracts on Firefox due to out-of-memory issues.
test.skip();
}
await loadTestPage(page);
const standardInput = loadFixture('instantiate_tokens.json')
const result = await runWorker(page, standardInput);

Expand Down
97 changes: 50 additions & 47 deletions js/embed/pre.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,57 @@
var Module = {
stdinData: null,
stdinDataPosition: 0,
stdoutData: [],
stderrData: [],
Module.stdinData = null;
Module.stdinDataPosition = 0;
Module.stdoutData = [];
Module.stderrData = [];

// Function to read and return all collected stdout data as a string
readFromStdout: function() {
if (!this.stdoutData.length) return "";
const decoder = new TextDecoder('utf-8');
const data = decoder.decode(new Uint8Array(this.stdoutData));
this.stdoutData = [];
return data;
},
// Method to read all collected stdout data
Module.readFromStdout = function () {
if (!Module.stdoutData.length) return "";
const decoder = new TextDecoder("utf-8");
const data = decoder.decode(new Uint8Array(Module.stdoutData));
Module.stdoutData = [];
return data;
};

// Function to read and return all collected stderr data as a string
readFromStderr: function() {
if (!this.stderrData.length) return "";
const decoder = new TextDecoder('utf-8');
const data = decoder.decode(new Uint8Array(this.stderrData));
this.stderrData = [];
return data;
},
// Method to read all collected stderr data
Module.readFromStderr = function () {
if (!Module.stderrData.length) return "";
const decoder = new TextDecoder("utf-8");
const data = decoder.decode(new Uint8Array(Module.stderrData));
Module.stderrData = [];
return data;
};

// Function to set input data for stdin
writeToStdin: function(data) {
const encoder = new TextEncoder();
this.stdinData = encoder.encode(data);
this.stdinDataPosition = 0;
},
// Method to write data to stdin
Module.writeToStdin = function (data) {
const encoder = new TextEncoder();
Module.stdinData = encoder.encode(data);
Module.stdinDataPosition = 0;
};

// `preRun` is called before the program starts running
preRun: function() {
// Define a custom stdin function
function customStdin() {
if (!Module.stdinData || Module.stdinDataPosition >= Module.stdinData.length) {
return null; // End of input (EOF)
}
return Module.stdinData[Module.stdinDataPosition++];
}
// Override the `preRun` method to customize file system initialization
Module.preRun = Module.preRun || [];
Module.preRun.push(function () {
// Custom stdin function
function customStdin() {
if (
!Module.stdinData ||
Module.stdinDataPosition >= Module.stdinData.length
) {
return null; // End of input (EOF)
}
return Module.stdinData[Module.stdinDataPosition++];
}

// Define a custom stdout function
function customStdout(char) {
Module.stdoutData.push(char);
}
// Custom stdout function
function customStdout(char) {
Module.stdoutData.push(char);
}

// Define a custom stderr function
function customStderr(char) {
Module.stderrData.push(char);
}
// Custom stderr function
function customStderr(char) {
Module.stderrData.push(char);
}

FS.init(customStdin, customStdout, customStderr);
},
};
// Initialize the FS (File System) with custom handlers
FS.init(customStdin, customStdout, customStderr);
});
Loading

0 comments on commit 79ec4dd

Please sign in to comment.