cryptool-org / wasm-webterm

xterm.js addon to run WebAssembly binaries (supports WASI + Emscripten)
https://cryptool.org/webterm
53 stars 11 forks source link

stdin input #3

Open ppacory opened 1 year ago

ppacory commented 1 year ago
#include <stdio.h>

int main()
{
    char name[200];
    fgets(name, 200, stdin);
    printf("You entered: %s", name);
    return 0;
}
$ emcc test.c -o test.js -s EXPORT_NAME='EmscrJSR_test' -s ENVIRONMENT=web,worker -s FILESYSTEM=1 -s MODULARIZE=1 -s EXPORTED_RUNTIME_METHODS=callMain,FS,TTY -s INVOKE_RUN=0 -s EXIT_RUNTIME=1 -s EXPORT_ES6=0 -s USE_ES6_IMPORT_META=0 -s ALLOW_MEMORY_GROWTH=1

copy the created files test.wasm and test.js into your predelivery folder or drag&drop them into the terminal window. You can now execute the command "test" in the terminal and it should ask you for input.

I've done all, it's working but call a window.prompt in the browser ! is there a way to prompt stdin in xterm terminal ?

z11labs commented 1 year ago

Hello and thank you for reaching out :)

If you are being prompted, it means that the default WasmRunner was used. It is being used if there is no support for Atomics or SharedArrayBuffers. The prompt is necessary in that case because it stops further WebAssembly execution until the user enters something.

To read STDIN from the xterm.js Terminal, you have to enable the WasmWorker. It runs on another thread and uses Atomics and SharedArrayBuffers to stop its execution while the user enters something into the terminal.

To enable SharedArrayBuffers it is necessary to configure your server or web bundler to use custom HTTPS headers for cross-origin isolation. This should also enable the WasmWorker. You can see if it worked in the terminal welcome message. It should say:

Backend: WebWorker.

Somehow the live demo currently does not support workers. We'll look into that. Until then you can try the Webpack example (as it has the headers configured) or drop your binaries into a development version of the OpenSSL app.

Best regards from the CrypTool team

z11labs commented 1 year ago

We enabled WasmWorker again in the live demo.