mirror of
https://github.com/pezkuwichain/revive.git
synced 2026-06-15 10:21:06 +00:00
Add stdin support
This commit is contained in:
@@ -10,7 +10,8 @@ RUSTFLAGS_EMSCRIPTEN := \
|
|||||||
-Clink-arg=-sEXPORTED_RUNTIME_METHODS=FS,callMain,stringToNewUTF8,cwrap \
|
-Clink-arg=-sEXPORTED_RUNTIME_METHODS=FS,callMain,stringToNewUTF8,cwrap \
|
||||||
-Clink-arg=-sMODULARIZE \
|
-Clink-arg=-sMODULARIZE \
|
||||||
-Clink-arg=-sEXPORT_ES6 \
|
-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
|
install: install-bin install-npm
|
||||||
|
|
||||||
|
|||||||
@@ -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);
|
||||||
|
},
|
||||||
|
};
|
||||||
+23
-30
@@ -3,26 +3,7 @@ const require = createRequire(import.meta.url);
|
|||||||
import solc from 'solc';
|
import solc from 'solc';
|
||||||
|
|
||||||
// Import the Emscripten module
|
// Import the Emscripten module
|
||||||
import ModuleFactory from './resolc.js';
|
import Module 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']);
|
|
||||||
}
|
|
||||||
|
|
||||||
const compilerStandardJsonInput = {
|
const compilerStandardJsonInput = {
|
||||||
language: 'Solidity',
|
language: 'Solidity',
|
||||||
@@ -52,21 +33,33 @@ const compilerStandardJsonInput = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
async function runCompilerWithStandardJson() {
|
async function runCompiler() {
|
||||||
const Module = await ModuleFactory();
|
const m = await Module();
|
||||||
Module.solc = solc;
|
m.solc = solc;
|
||||||
|
|
||||||
// Write the input Solidity code to the Emscripten file system
|
// Set input data for stdin
|
||||||
Module.FS.writeFile('/in', JSON.stringify(compilerStandardJsonInput));
|
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
|
// Compile the Solidity source code
|
||||||
Module.callMain(['--standard-json']);
|
let x = m.callMain(['--standard-json']);
|
||||||
|
console.log(stdoutString)
|
||||||
|
console.error(stderrString)
|
||||||
}
|
}
|
||||||
|
|
||||||
runCompiler().catch(err => {
|
runCompiler().catch(err => {
|
||||||
console.error('Error:', err);
|
console.error('Error:', err);
|
||||||
});
|
});
|
||||||
|
|
||||||
runCompilerWithStandardJson().catch(err => {
|
|
||||||
console.error('Error:', err);
|
|
||||||
});
|
|
||||||
|
|||||||
Reference in New Issue
Block a user