mirror of
https://github.com/pezkuwichain/revive.git
synced 2026-04-29 19:27:56 +00:00
JS: Fix encoding conversion from utf16 to utf8 (#131)
This commit is contained in:
committed by
GitHub
parent
2cb8f82266
commit
afe44ad21b
+26
-21
@@ -1,47 +1,52 @@
|
||||
var Module = {
|
||||
stdinData: "",
|
||||
stdoutCallback: null,
|
||||
stderrCallback: null,
|
||||
stdinData: null,
|
||||
stdinDataPosition: 0,
|
||||
stdoutData: [],
|
||||
stderrData: [],
|
||||
|
||||
// Function to set a callback for stdout
|
||||
setStdoutCallback: function(callback) {
|
||||
this.stdoutCallback = callback;
|
||||
// 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;
|
||||
},
|
||||
|
||||
// Function to set a callback for stderr
|
||||
setStderrCallback: function(callback) {
|
||||
this.stderrCallback = callback;
|
||||
// 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;
|
||||
},
|
||||
|
||||
// Function to set input data for stdin
|
||||
setStdinData: function(data) {
|
||||
this.stdinData = data;
|
||||
writeToStdin: function(data) {
|
||||
const encoder = new TextEncoder();
|
||||
this.stdinData = encoder.encode(data);
|
||||
this.stdinDataPosition = 0;
|
||||
},
|
||||
|
||||
// `preRun` is called before the program starts running
|
||||
preRun: function() {
|
||||
// Define a custom stdin function
|
||||
function customStdin() {
|
||||
if (Module.stdinData.length === 0) {
|
||||
if (!Module.stdinData || Module.stdinDataPosition >= Module.stdinData.length) {
|
||||
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;
|
||||
return Module.stdinData[Module.stdinDataPosition++];
|
||||
}
|
||||
|
||||
// Define a custom stdout function
|
||||
function customStdout(char) {
|
||||
if (Module.stdoutCallback) {
|
||||
Module.stdoutCallback(String.fromCharCode(char));
|
||||
}
|
||||
Module.stdoutData.push(char);
|
||||
}
|
||||
|
||||
// Define a custom stderr function
|
||||
function customStderr(char) {
|
||||
if (Module.stderrCallback) {
|
||||
Module.stderrCallback(String.fromCharCode(char));
|
||||
}
|
||||
Module.stderrData.push(char);
|
||||
}
|
||||
|
||||
FS.init(customStdin, customStdout, customStderr);
|
||||
|
||||
@@ -24,28 +24,17 @@ mergeInto(LibraryManager.library, {
|
||||
} else {
|
||||
throw new Error('Unknown environment: Unable to load resolc.js');
|
||||
}
|
||||
revive.setStdinData(inputJson);
|
||||
|
||||
let stdoutString = "";
|
||||
revive.setStdoutCallback(function(char) {
|
||||
if (char.charCodeAt(0) === '\n') {
|
||||
exit;
|
||||
}
|
||||
stdoutString += char;
|
||||
});
|
||||
|
||||
let stderrString = "";
|
||||
revive.setStderrCallback(function(char) {
|
||||
stderrString += char;
|
||||
});
|
||||
revive.writeToStdin(inputJson);
|
||||
|
||||
// Call main on the new instance
|
||||
const result = revive.callMain(['--recursive-process']);
|
||||
|
||||
if (result) {
|
||||
const stderrString = revive.readFromStderr();
|
||||
const error = JSON.stringify({ type: 'error', message: stderrString || "Unknown error" });
|
||||
return stringToNewUTF8(error);
|
||||
} else {
|
||||
const stdoutString = revive.readFromStdout();
|
||||
const json = JSON.stringify({ type: 'success', data: stdoutString });
|
||||
return stringToNewUTF8(json);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user