Add support for esm and cjs modules

This commit is contained in:
Sebastian Miasojed
2024-11-25 10:25:15 +01:00
parent 892c9b5fbe
commit 4e024655a1
14 changed files with 149 additions and 157 deletions
+49
View File
@@ -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);
},
};