wasmtime icon indicating copy to clipboard operation
wasmtime copied to clipboard

C binding: wasi std out to string

Open ShuP1 opened this issue 3 years ago • 5 comments

Feature

Rust library can bind WritePipe to WasiCtx stdout which can be far simpler and effective than OS files went dealing with multiple short-lived instances.

It is not currently possible with C/C++ bindings

Benefit

  • Avoid useless error-prone and slow interactions by filesystem
  • Allow simpler integration in freestanding targets

Implementation

Leverage existing wasi_common::pipe::WritePipe and ReadPipe on the rust side to implemented a binding in c-api crate.

Alternatives

Export some basic filesystem operations in c binding

ShuP1 avatar Jul 03 '22 16:07 ShuP1

+1 for this feature.

When embedding Wasmtime in C (or C++), and enabling WASI extensions for a Wasm module, there is no way to capture stdout into a string. The only alternative seems to use [wasi.h]wasi_config_set_stdout_file(), which brings many other drawbacks (ie: no buffering, slow down, etc.)

gzurl avatar Aug 05 '22 12:08 gzurl

Is this the blocker issue for https://github.com/bytecodealliance/wasmtime-go/issues/34 and https://github.com/bytecodealliance/wasmtime-py/issues/34? (cc @alexcrichton)

I would love to switch to wasmtime for my projects but this is preventing me from doing so. We also need the ability to set a string for stdin as well. I'm not sure if there is a separate issue for that.

guregu avatar Oct 31 '22 06:10 guregu

Yes this is required for those issues.

alexcrichton avatar Oct 31 '22 15:10 alexcrichton

The binding for stdout/stderr is a bit more complicated

First this api needs to return a new opaque struct for retrieve output after store drop.

Furthermore there is 2 properties of WritePipe than should probably be accessible:

  • bounded or not:
    • provide a &mut [u8] for preallocated bounded size
    • use an internal Vec<u8> for unpredictable unbounded size
  • shared: (note: internally buffer is always in Arc<RwLock<T>>)

ShuP1 avatar Nov 02 '22 15:11 ShuP1

Side question: is stdin streaming a desired feature ?

wasi_read_pipe_t in;
wasi_config_set_stdin_pipe(wasi_config, &in);
...
wasi_read_pipe_write(&in, &binary);
wasmtime_func_call(...); // consume stdin
wasi_read_pipe_write(&in, &binary);
wasmtime_func_call(...);
wasi_read_pipe_delete(&in);

ShuP1 avatar Nov 02 '22 15:11 ShuP1