I have a C program that takes one argument (a char array / string) via command line and also reads from stdin. I've compiled it to JavaScript using emscripten. This was successful and I can run it just like the normal C program using node.js:
emcc -O2 translate.c
node translate.js "foo" < bar.txt
As you can see, I'm providing the string "foo" as an argument and the contents of bar.txt as stdin. Now I want this to be a self-contained HTML file.
By changing the output to HTML:
emcc -O2 translate.c -o trans.html
I provide the argument by adding arguments: ['foo'],
to the definitions in var Module
. This works as expected, the program receives the argument correctly.
Now, how do I provide the stdin input to this program? I don't need to do this dynamically. It would be fine to just declare a string somewhere in the HTML with the required stdin content.
Edit
Just found a solution that works for me. In the JS file for the generated HTML, there is a default input handler which prompt()
s the user when no other input method is defined. Just edit the variable result
or call your own function:
} else if (typeof window != 'undefined' &&
typeof window.prompt == 'function') {
// Browser.
// REPLACE THIS CODE:
result = window.prompt('Input: '); // returns null on cancel
if (result !== null) {
result += '\n';
}
Rather than editing the output of Emscripten, you could monkey patch the Window object
Not wonderful, but I would deem this less of a hack than editing the Emscripten-generated Javascript.
A way would be to use the Emscripten Filesystem API, for example by calling FS.init in the Module preRun function, passing custom functions to be used for standard input, output and error.
The functions are quite low-level: they each deal with one character at a time as an ASCII code. If you have strings you want to pass in, you would have to iterate over the characters of the string yourself. I suspect charCodeAt would be helpful. To output strings from stdout or stderr, then I suspect fromCharCode would be helpful.
Example (not very well tested!) implementations using each are below.
According the question "Edit" , I made my function , thx a lot.
Just hope the code below can help someone else.
comment run(); in the end of emscript
result = areaInput();
// As the question mentionedadd the code below in your html file to activate run() in emscript
remember io textareas in your html
<textarea id="input" cols="80" rows="30"></textarea>
<textarea id="output" cols="80" rows="30"></textarea>
and a button
<button onclick="execEmscript();">run</button>