#wasi #module #process #stdout #wasmer #tokio #pseudo-processes

wasi-process

A library to run wasi modules as pseudo-processes

6 releases

0.3.2 Jun 4, 2021
0.3.1 Apr 12, 2021
0.3.0 Jan 23, 2021
0.2.1 Jan 6, 2021
0.1.0 Jun 27, 2020

#640 in Operating systems

MIT license

22KB
509 lines

A library to run wasi modules as pseudo-processes.

use wasmer_wasi::{WasiEnv, WasiState, WasiVersion};
use wasi_process::WasiProcess;
let store = wasmer::Store::default();
let wasm = include_bytes!("../helloworld.wasm"); // just write(1, "Hello, World!\n", 14)
let module = wasmer::Module::new(&store, wasm)?;
let mut state = WasiState::new("progg");
wasi_process::add_stdio(&mut state);
state.args(&["foo", "bar"]);
let imports = wasmer_wasi::generate_import_object_from_env(
    &store,
    WasiEnv::new(state.build()?),
    wasmer_wasi::get_wasi_version(&module, false).unwrap_or(WasiVersion::Latest),
);
let instance = wasmer::Instance::new(&module, &imports)?;
let mut wasi = WasiProcess::new(&instance, wasi_process::MaxBufSize::default())?;
let mut stdout = wasi.stdout.take().unwrap();
wasi.spawn();
let mut out = String::new();
stdout.read_to_string(&mut out).await?;
assert_eq!(out, "Hello, World!\n");

Dependencies

~11–19MB
~263K SLoC