wasmerio / wasmer-js

Monorepo for Javascript WebAssembly packages by Wasmer

Home Page:https://wasmerio.github.io/wasmer-js/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to send stdin after program has been started?

richardanaya opened this issue · comments

I'm trying to make a terminal app that runs in browser with wasmer-js, i'm trying to look at how the wasm_bindgen apis can be used to write to a terminal after it's been started (not the initial stdin).

I would also like to know how to send stdin after the program has started.

This feature is unfortunately not supported by Wasmer-JS, and for a rather subtle but in-hindsight obvious reason: code running in a JS/WASM runtime is single-threaded, but C programs like bash etc. use a "blocking" API to access stdin. There is no async/await in WASM and there is no mechanism to suspend the currently executing stack frame. Even if the WASM program is running in a Web Worker, it will not be able to handle messages (sent e.g. by postMessage from the UI thread) until the program has exited.

That said, there is a roundabout way to achieve a semblance of a blocking stream using JS's SharedArrayBuffer and Atomics. I have successfully used this approach in wasi-kernel; the trouble with wasi-kernel is that it is based on the older TypeScript-based version of Wasmer-JS. I am working on doing the same in Rust but have not gotten very far, mostly due to my inexperience in Rust. If you're interested, you can look at this branch in my fork of Wasmer-JS.