I have a C CLI program that I compiled to wasm with Emscripten. I'm trying to run that program each time a button is pressed, reading from a textbox into stdin and from stdout to another textbox. However, I can only call Module.callMain() successfully once. Each subsequent invocation does not execute. How to properly setup this?
Here is the HTML code:
<!doctype html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>title</title>
</head>
<body>
<textarea id="input" rows="8"></textarea>
<button onclick="exec()">run</button>
<textarea id="output" rows="8"></textarea>
<script>
var input_box = document.getElementById('input');
var input_text = "";
let i = 0;
var Module = {
noInitialRun: true,
noExitRuntime: true,
print: (function() {
var element = document.getElementById('output');
if (element) element.value = ''; // clear browser cache
return (...args) => {
var text = args.join(' ');
console.log(text);
if (element) {
element.value += text + "\n";
element.scrollTop = element.scrollHeight; // focus on bottom
}
};
})(),
stdin: (function() {
return () => {
if (i >= input_text.length) {
return null;
} else {
return input_text.charCodeAt(i++);
}
};
})(),
};
function exec() {
console.log(i);
i = 0;
input_text = input_box.value;
console.log(input_text);
Module.callMain([]);
}
</script>
<script async type="text/javascript" src="main.js"></script>
</body>
</html>
The .js file is the one output from emscripten without modifications.
Here are the flags used to compile:
-s 'EXIT_RUNTIME=0' -s EXPORTED_RUNTIME_METHODS='[\"callMain\",\"run\"]' -s ENVIRONMENT=web