Files
wasmtime/examples/wasi/main.rs
Pat Hickey 0f5bdc6497 only wasi_cap_std_sync and wasi_tokio need to define WasiCtxBuilders (#2917)
* wasmtime-wasi: re-exporting this WasiCtxBuilder was shadowing the right one

wasi-common's WasiCtxBuilder is really only useful wasi_cap_std_sync and
wasi_tokio to implement their own Builder on top of.

This re-export of wasi-common's is 1. not useful and 2. shadow's the
re-export of the right one in sync::*.

* wasi-common: eliminate WasiCtxBuilder, make the builder methods on WasiCtx instead

* delete wasi-common::WasiCtxBuilder altogether

just put those methods directly on &mut WasiCtx.

As a bonus, the sync and tokio WasiCtxBuilder::build functions
are no longer fallible!

* bench fixes

* more test fixes
2021-05-21 12:59:39 -05:00

43 lines
1.3 KiB
Rust

//! Example of instantiating of instantiating a wasm module which uses WASI
//! imports.
// You can execute this example with `cargo run --example wasi`
use anyhow::Result;
use wasmtime::*;
use wasmtime_wasi::sync::{Wasi, WasiCtxBuilder};
fn main() -> Result<()> {
tracing_subscriber::FmtSubscriber::builder()
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.with_ansi(true)
.init();
// Define the WASI functions globally on the `Config`.
let mut config = Config::default();
Wasi::add_to_config(&mut config);
let store = Store::new(&Engine::new(&config)?);
// Set the WASI context in the store; all instances in the store share this context.
// `WasiCtxBuilder` provides a number of ways to configure what the target program
// will have access to.
assert!(Wasi::set_context(
&store,
WasiCtxBuilder::new()
.inherit_stdio()
.inherit_args()?
.build()
)
.is_ok());
let mut linker = Linker::new(&store);
// Instantiate our module with the imports we've created, and run it.
let module = Module::from_file(store.engine(), "target/wasm32-wasi/debug/wasi.wasm")?;
linker.module("", &module)?;
linker.get_default("")?.typed::<(), ()>()?.call(())?;
Ok(())
}