xref: /wasmtime-44.0.1/examples/gcd.rs (revision 331b0dee)
1 //! Example of instantiating of the WebAssembly module and invoking its exported
2 //! function.
3 
4 // You can execute this example with `cargo run --example gcd`
5 
6 use anyhow::Result;
7 use wasmtime::*;
8 
9 fn main() -> Result<()> {
10     // Load our WebAssembly (parsed WAT in our case), and then load it into a
11     // `Module` which is attached to a `Store` cache. After we've got that we
12     // can instantiate it.
13     let mut store = Store::<()>::default();
14     let module = Module::from_file(store.engine(), "examples/gcd.wat")?;
15     let instance = Instance::new(&mut store, &module, &[])?;
16 
17     // Invoke `gcd` export
18     let gcd = instance.get_typed_func::<(i32, i32), i32, _>(&mut store, "gcd")?;
19 
20     println!("gcd(6, 27) = {}", gcd.call(&mut store, (6, 27))?);
21     Ok(())
22 }
23