From f36d62ca73aa97497c72531dd634ecc72383df29 Mon Sep 17 00:00:00 2001 From: Sebastian Miasojed Date: Wed, 20 Nov 2024 10:27:57 +0100 Subject: [PATCH] Add stdin support --- Makefile | 3 ++- js/pre.js | 49 ++++++++++++++++++++++++++++++++++++++++++++ js/run_revive.js | 53 +++++++++++++++++++++--------------------------- 3 files changed, 74 insertions(+), 31 deletions(-) create mode 100644 js/pre.js diff --git a/Makefile b/Makefile index 0b8f97c..5b08eda 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,8 @@ RUSTFLAGS_EMSCRIPTEN := \ -Clink-arg=-sEXPORTED_RUNTIME_METHODS=FS,callMain,stringToNewUTF8,cwrap \ -Clink-arg=-sMODULARIZE \ -Clink-arg=-sEXPORT_ES6 \ - -Clink-arg=--js-library=js/soljson_interface.js + -Clink-arg=--js-library=js/soljson_interface.js \ + -Clink-arg=--pre-js=js/pre.js install: install-bin install-npm diff --git a/js/pre.js b/js/pre.js new file mode 100644 index 0000000..745723c --- /dev/null +++ b/js/pre.js @@ -0,0 +1,49 @@ +var Module = { + stdinData: "", + stdoutCallback: null, + stderrCallback: null, + + // Function to set a callback for stdout + setStdoutCallback: function(callback) { + this.stdoutCallback = callback; + }, + + // Function to set a callback for stderr + setStderrCallback: function(callback) { + this.stderrCallback = callback; + }, + + // Function to set input data for stdin + setStdinData: function(data) { + this.stdinData = data; + }, + + // `preRun` is called before the program starts running + preRun: function() { + // Define a custom stdin function + function customStdin() { + if (Module.stdinData.length === 0) { + return null; // End of input (EOF) + } + const char = Module.stdinData.charCodeAt(0); + Module.stdinData = Module.stdinData.slice(1); // Remove the character from input + return char; + } + + // Define a custom stdout function + function customStdout(char) { + if (Module.stdoutCallback) { + Module.stdoutCallback(String.fromCharCode(char)); + } + } + + // Define a custom stderr function + function customStderr(char) { + if (Module.stderrCallback) { + Module.stderrCallback(String.fromCharCode(char)); + } + } + + FS.init(customStdin, customStdout, customStderr); + }, +}; diff --git a/js/run_revive.js b/js/run_revive.js index 550a5d9..6a68e93 100644 --- a/js/run_revive.js +++ b/js/run_revive.js @@ -3,26 +3,7 @@ const require = createRequire(import.meta.url); import solc from 'solc'; // Import the Emscripten module -import ModuleFactory from './resolc.js'; - -// Solidity source code -const input = ` - // SPDX-License-Identifier: MIT - pragma solidity ^0.8; - contract Baseline { - function baseline() public payable {} - }`; - -async function runCompiler() { - const Module = await ModuleFactory(); - Module.solc = solc; - - // 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']); -} +import Module from './resolc.js'; const compilerStandardJsonInput = { language: 'Solidity', @@ -52,21 +33,33 @@ const compilerStandardJsonInput = { }, }; -async function runCompilerWithStandardJson() { - const Module = await ModuleFactory(); - Module.solc = solc; +async function runCompiler() { + const m = await Module(); + m.solc = solc; - // Write the input Solidity code to the Emscripten file system - Module.FS.writeFile('/in', JSON.stringify(compilerStandardJsonInput)); + // Set input data for stdin + m.setStdinData(JSON.stringify(compilerStandardJsonInput)); + + var stdoutString = ""; + m.setStdoutCallback(function(char) { + if (char.charCodeAt(0) === '\n') { + console.log("new line") + exit + } + stdoutString += char; + }); + + var stderrString = ""; + m.setStderrCallback(function(error) { + stderrString += char; + }); // Compile the Solidity source code - Module.callMain(['--standard-json']); + let x = m.callMain(['--standard-json']); + console.log(stdoutString) + console.error(stderrString) } runCompiler().catch(err => { console.error('Error:', err); }); - -runCompilerWithStandardJson().catch(err => { - console.error('Error:', err); -});