This commit is contained in:
Sebastian Miasojed
2024-11-18 10:18:21 +01:00
parent f57ab96eed
commit cece20deb1
11 changed files with 56 additions and 186 deletions
+15
View File
@@ -0,0 +1,15 @@
{
"name": "revive",
"version": "1.0.0",
"description": "Revive compiler",
"main": "run_revive.js",
"type": "module",
"dependencies": {
"deasync": "^0.1.15",
"solc": "^0.8.28"
},
"scripts": {
"init": "cp -r ../target/wasm32-unknown-emscripten/release/resolc.{js,wasm} .",
"test": "npm run init && node run_revive.js"
}
}
-16
View File
File diff suppressed because one or more lines are too long
BIN
View File
Binary file not shown.
+29
View File
@@ -0,0 +1,29 @@
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
import solc from 'solc';
// Import the Emscripten module
import ModuleFactory from './resolc.js';
async function runCompiler() {
const Module = await ModuleFactory();
Module.solc = solc;
// Create input Solidity source code
const input = `
// SPDX-License-Identifier: MIT
pragma solidity ^0.8;
contract Baseline {
function baseline() public payable {}
}`;
// Write the input Solidity code to the Emscripten file system
Module.FS.writeFile('./input.sol', input);
// Compile the Solidity source code
Module.callMain(['./input.sol', '-O3','--bin']);
}
runCompiler().catch(err => {
console.error('Error:', err);
});
-49
View File
@@ -1,49 +0,0 @@
import { readFileSync } from 'fs';
import { fileURLToPath } from 'url';
import path from 'path';
import vm from 'vm';
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
// Import the Emscripten module
import ModuleFactory from './resolc.mjs';
async function initializeSolc() {
// Load soljson.js
const soljsonPath = path.join('./', 'soljson.js');
const soljsonCode = readFileSync(soljsonPath, 'utf8');
// Create a new VM context and run soljson.js in it
const soljsonContext = { Module: {} };
vm.createContext(soljsonContext); // Create a new context
vm.runInContext(soljsonCode, soljsonContext); // Execute soljson.js in the new context
// Return the initialized soljson module
return soljsonContext.Module;
}
async function runCompiler() {
const soljson = await initializeSolc();
const Module = await ModuleFactory();
// Expose soljson in the Module context
Module.soljson = soljson;
// Create input Solidity source code
const input = `
// SPDX-License-Identifier: MIT
pragma solidity ^0.8;
contract Baseline {
function baseline() public payable {}
}`;
// Write the input Solidity code to the Emscripten file system
Module.FS.writeFile('./input.sol', input);
// Compile the Solidity source code
Module.callMain(['./input.sol', '-O3','--bin']);
}
runCompiler().catch(err => {
console.error('Error:', err);
});
-111
View File
File diff suppressed because one or more lines are too long
+3 -3
View File
@@ -1,11 +1,11 @@
mergeInto(LibraryManager.library, {
soljson_compile: function(inputPtr, inputLen) {
var inputJson = UTF8ToString(inputPtr, inputLen);
var output = Module.soljson.cwrap('solidity_compile', 'string', ['string'])(inputJson);
const inputJson = UTF8ToString(inputPtr, inputLen);
const output = Module.solc.compile(inputJson)
return stringToNewUTF8(output)
},
soljson_version: function() {
var version = Module.soljson.cwrap("solidity_version", "string", [])();
var version = Module.solc.version();
return stringToNewUTF8(version)
},
resolc_compile: function(inputPtr, inputLen) {
+2 -2
View File
@@ -1,9 +1,9 @@
// worker.js
// nodejs version
const { parentPort } = require('worker_threads');
import { parentPort } from 'worker_threads';
parentPort.on('message', async (inputJson) => {
const { default: ModuleFactory } = await import('./resolc.mjs');
const { default: ModuleFactory } = await import('./resolc.js');
const newModule = await ModuleFactory();
// Create a virtual file for stdin