1 //! Example of instantiating a wasm module which uses WASI preview1 imports
2 //! implemented through the async preview2 WASI implementation.
3 
4 /*
5 You can execute this example with:
6     cmake examples/
7     cargo run --example wasip1-async
8 */
9 
10 use wasmtime::Result;
11 use wasmtime::{Engine, Linker, Module, Store};
12 use wasmtime_wasi::WasiCtx;
13 use wasmtime_wasi::p1::{self, WasiP1Ctx};
14 
15 #[tokio::main]
main() -> Result<()>16 async fn main() -> Result<()> {
17     let engine = Engine::default();
18 
19     // Add the WASIp1 APIs to the linker
20     let mut linker: Linker<WasiP1Ctx> = Linker::new(&engine);
21     p1::add_to_linker_async(&mut linker, |t| t)?;
22 
23     // Add capabilities (e.g. filesystem access) to the WASI preview2 context
24     // here. Here only stdio is inherited, but see docs of `WasiCtx` for
25     // more.
26     let wasi_ctx = WasiCtx::builder().inherit_stdio().build_p1();
27 
28     let mut store = Store::new(&engine, wasi_ctx);
29 
30     // Instantiate our 'Hello World' wasm module.
31     // Note: This is a module built against the preview1 WASI API.
32     let module = Module::from_file(&engine, "target/wasm32-wasip1/debug/wasi.wasm")?;
33     let func = linker
34         .module_async(&mut store, "", &module)
35         .await?
36         .get_default(&mut store, "")?
37         .typed::<(), ()>(&store)?;
38 
39     // Invoke the WASI program default function.
40     func.call_async(&mut store, ()).await?;
41 
42     Ok(())
43 }
44