You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Thank you for this nice package. I am currently trying to make it work with my wasm file in the browser. Here is some background:
I have a bash script that constantly listens to stdin and accumulates user input until a sequence of chars are encountered (____END____ in my case), the tty behavior is like below:
Until I send SIGINT or similar. Now I converted this to a docker container (with openssl and all other deps with Alpine base image) and run:
docker run -it --rm --name app-test my-repo:my-tag
which replicates the behavior of the bash script 1 to 1. Next I converted the container to a wasm using container2wasm package using c2w binary. And run the resulting wasm using wasmtime:
wasmtime /home/user/path/to/out_alpine.wasm
and the above perfectly replicates the behavior of the bash script. So all 3 steps work. Next is using your package Wasmer to make it work in the browser. So on the main thread side I have this:
import{init,Wasmer}from"./wasmer-sdk.mjs";//same as https://unpkg.com/@wasmer/[email protected]/dist/index.mjs, I also have wasmer_js_bg.wasm under the same path 'static/js/wasmer_js_bg.wasm'letwasmInstance=null;letwasmStdin=null;letdecoder=newTextDecoder();letencoder=newTextEncoder();letstdoutStream=newWritableStream({write(chunk){postMessage({type: 'output',data: decoder.decode(chunk)+"--test"});}});letstderrStream=newWritableStream({write(chunk){postMessage({type: 'error',data: decoder.decode(chunk)+"--err-test"});}});(async()=>{awaitinit({log: "trace"});try{// Fetch the WASM file as binary dataconstresponse=awaitfetch("/static/js/out_alpine.wasm");if(!response.ok){thrownewError(`Failed to fetch WASM: ${response.statusText}`);}constbinaryFile=newUint8Array(awaitresponse.arrayBuffer());constwasmModule=awaitWasmer.fromFile(binaryFile);const{ entrypoint }=wasmModule;//wasmInstance = await entrypoint.run({args: ["/bin/bash", "./myscript.sh"], cwd:"/app"}); doesnt work//wasmInstance = await entrypoint.run({stdin: (new TextEncoder).encode('{"a":1,"b":2}____END____')}); doesnt workwasmInstance=awaitentrypoint.run();//await wasmInstance.wait(); throws null pointer passed to rustwasmStdin=wasmInstance.stdin?.getWriter();wasmInstance.stdout?.pipeTo(stdoutStream);wasmInstance.stderr?.pipeTo(stderrStream);}catch(err){postMessage({type: "error",data: err.message||"Unexpected Error!"});}})().catch(err=>postMessage({type: "error",data: err.message||"Unexpected Error!"}));onmessage=async(event)=>{const{ input }=event.data;try{if(wasmStdin){awaitwasmStdin.write(encoder.encode(input));//await wasmStdin.close(); to force EOF and doing encoder.encode(input + "\n") does not work}else{thrownewError("WASM stdin is not initialized.");}}catch(err){console.error("Error sending input:",err.message);postMessage({type: 'error',data: err.message||"Unexpected Error!"});}};
above does not throw an error, and in fact loads the wasm as I can see from the debug log, which I attached. My only problem is, when I postMessage to the worker, whatever I post is reflected back. I checked if wasmInstance.stdin or wasmInstance.stdout return the same pointer but no, they return different objects. So it all seems to be working accept the fact that when I postMessage "abc____END_____", I get back "abc____END_____--test" into the DOM.
Why is stdin seems to be piped to stdout? What am I missing?
I attached the DEBUG log. wasmer.log
Many thanks!
The text was updated successfully, but these errors were encountered:
Thank you for this nice package. I am currently trying to make it work with my wasm file in the browser. Here is some background:
I have a bash script that constantly listens to stdin and accumulates user input until a sequence of chars are encountered (
____END____
in my case), the tty behavior is like below:Until I send
SIGINT
or similar. Now I converted this to a docker container (with openssl and all other deps with Alpine base image) and run:which replicates the behavior of the bash script 1 to 1. Next I converted the container to a wasm using container2wasm package using
c2w
binary. And run the resulting wasm usingwasmtime
:and the above perfectly replicates the behavior of the bash script. So all 3 steps work. Next is using your package Wasmer to make it work in the browser. So on the main thread side I have this:
and on the worker side:
above does not throw an error, and in fact loads the wasm as I can see from the debug log, which I attached. My only problem is, when I
postMessage
to the worker, whatever I post is reflected back. I checked ifwasmInstance.stdin
orwasmInstance.stdout
return the same pointer but no, they return different objects. So it all seems to be working accept the fact that when IpostMessage
"abc____END_____"
, I get back"abc____END_____--test"
into the DOM.Why is stdin seems to be piped to stdout? What am I missing?
I attached the DEBUG log.
wasmer.log
Many thanks!
The text was updated successfully, but these errors were encountered: