xref: /wasmtime-44.0.1/examples/wasip1/main.rs (revision 4ac219fd)
1 //! Example of instantiating a wasm module which uses WASI imports.
2 
3 /*
4 You can execute this example with:
5     cmake examples/
6     cargo run --example wasip1
7 */
8 
9 use wasmtime::*;
10 use wasmtime_wasi::WasiCtx;
11 
main() -> Result<()>12 fn main() -> Result<()> {
13     // Define the WASI functions globally on the `Config`.
14     let engine = Engine::default();
15     let mut linker = Linker::new(&engine);
16     wasmtime_wasi::p1::add_to_linker_sync(&mut linker, |s| s)?;
17 
18     // Create a WASI context and put it in a Store; all instances in the store
19     // share this context. `WasiCtxBuilder` provides a number of ways to
20     // configure what the target program will have access to.
21     let wasi = WasiCtx::builder().inherit_stdio().inherit_args().build_p1();
22     let mut store = Store::new(&engine, wasi);
23 
24     // Instantiate our module with the imports we've created, and run it.
25     let module = Module::from_file(&engine, "target/wasm32-wasip1/debug/wasi.wasm")?;
26     linker.module(&mut store, "", &module)?;
27     linker
28         .get_default(&mut store, "")?
29         .typed::<(), ()>(&store)?
30         .call(&mut store, ())?;
31 
32     Ok(())
33 }
34