wasmerio / wasmer-go

🐹🕸️ WebAssembly runtime for Go
https://pkg.go.dev/github.com/wasmerio/wasmer-go
MIT License
2.82k stars 161 forks source link

support setting bytes for stdin #405

Open tiziano88 opened 8 months ago

tiziano88 commented 8 months ago

Motivation

It is quite common to have to provide a pre-determined stdin value to a Wasm module, without having to expose a full file system to it.

Proposed solution

leverage this function:

https://docs.wasmtime.dev/c-api/wasi_8h.html#a525abd98ade58887a969b796ea05468e

would require modifying

https://github.com/wasmerio/wasmer-go/blob/ca60a4524acf4fc518ced3bfd818d960804a0bee/wasmer/packaged/include/wasmer.h#L705-L707

and

https://github.com/wasmerio/wasmer-go/blob/ca60a4524acf4fc518ced3bfd818d960804a0bee/wasmer/wasi.go#L196-L202

Alternatives

The best alternative at the moment is passing the value as argument on the command line, but that's not great for large and / or binary values.

jaime-amate commented 7 months ago

I'm currently facing the same issue on my project. It would be quite handy having somehow a setStdIn function.

Could you explain on more further details about the workaround to pass input data to the wasi module execution?

tiziano88 commented 7 months ago

For the time being I just serialize the data as a string (base64 if necessary) and pass it as the first command line argument to the Wasm module, which then reads it as argv[1]

jaime-amate commented 7 months ago

Thanks for the explanation, I think this wouldn't even work in my use case :(

I'm trying to embed a .wasm module compiled by Javy and according to this issue, it doesn't currently expose APIs to read environment variables or command line arguments.

tiziano88 commented 7 months ago

Right, in that case fixing this issue wouldn't help you, since this is specific to WASI, which AFAICT you are not using. I think your best bet is to declare a custom function for the Wasm module to read the data into its own memory