MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / main

Function main

examples/wasip2/main.rs:32–85  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

30}
31
32fn main() -> Result<()> {
33 // Define the WASI functions globally on the `Config`.
34 let engine = Engine::default();
35 let mut linker = Linker::new(&engine);
36 wasmtime_wasi::p2::add_to_linker_sync(&mut linker)?;
37
38 // Create a WASI context and put it in a Store; all instances in the store
39 // share this context. `WasiCtx` provides a number of ways to
40 // configure what the target program will have access to.
41 let wasi = WasiCtx::builder().inherit_stdio().inherit_args().build();
42 let state = ComponentRunStates {
43 wasi_ctx: wasi,
44 resource_table: ResourceTable::new(),
45 };
46 let mut store = Store::new(&engine, state);
47
48 // Instantiate our component with the imports we've created, and run it.
49 let component = Component::from_file(&engine, "target/wasm32-wasip2/debug/wasi.wasm")?;
50 let command = Command::instantiate(&mut store, &component, &linker)?;
51 let program_result = command.wasi_cli_run().call_run(&mut store)?;
52 if program_result.is_err() {
53 std::process::exit(1)
54 }
55
56 // Alternatively, instead of using `Command`, just instantiate it as a normal component
57 // New states
58 let wasi = WasiCtx::builder().inherit_stdio().inherit_args().build();
59 let state = ComponentRunStates {
60 wasi_ctx: wasi,
61 resource_table: ResourceTable::new(),
62 };
63 let mut store = Store::new(&engine, state);
64 // Instantiate it as a normal component
65 let instance = linker.instantiate(&mut store, &component)?;
66 // Get the index for the exported interface
67 let interface_idx = instance
68 .get_export_index(&mut store, None, "wasi:cli/run@0.2.0")
69 .expect("Cannot get `wasi:cli/run@0.2.0` interface");
70 // Get the index for the exported function in the exported interface
71 let parent_export_idx = Some(&interface_idx);
72 let func_idx = instance
73 .get_export_index(&mut store, parent_export_idx, "run")
74 .expect("Cannot get `run` function in `wasi:cli/run@0.2.0` interface");
75 let func = instance
76 .get_func(&mut store, func_idx)
77 .expect("Unreachable since we've got func_idx");
78 // As the `run` function in `wasi:cli/run@0.2.0` takes no argument and return a WASI result that correspond to a `Result<(), ()>`
79 // Reference:
80 // * https://github.com/WebAssembly/wasi-cli/blob/main/wit/run.wit
81 // * Documentation for [Func::typed](https://docs.rs/wasmtime/latest/wasmtime/component/struct.Func.html#method.typed) and [ComponentNamedList](https://docs.rs/wasmtime/latest/wasmtime/component/trait.ComponentNamedList.html)
82 let typed = func.typed::<(), (Result<(), ()>,)>(&store)?;
83 let (result,) = typed.call(&mut store, ())?;
84 result.map_err(|_| wasmtime::format_err!("error"))
85}

Callers

nothing calls this directly

Calls 13

call_runMethod · 0.80
newFunction · 0.50
add_to_linker_syncFunction · 0.50
builderFunction · 0.50
instantiateFunction · 0.50
buildMethod · 0.45
inherit_argsMethod · 0.45
inherit_stdioMethod · 0.45
instantiateMethod · 0.45
expectMethod · 0.45
get_export_indexMethod · 0.45
get_funcMethod · 0.45

Tested by

no test coverage detected