use anyhow::Result; use wasmtime::*; // https://docs.wasmtime.dev/examples-rust-gcd.html fn main() -> Result<()> { // Load our WebAssembly (parsed WAT in our case), and then load it into a // `Module` which is attached to a `Store` cache. After we've got that we // can instantiate it. let mut store = Store::<()>::default(); let module = Module::new(store.engine(), r#" (module (func $gcd (param i32 i32) (result i32) (local i32) block ;; label = @1 block ;; label = @2 local.get 0 br_if 0 (;@2;) local.get 1 local.set 2 br 1 (;@1;) end loop ;; label = @2 local.get 1 local.get 0 local.tee 2 i32.rem_u local.set 0 local.get 2 local.set 1 local.get 0 br_if 0 (;@2;) end end local.get 2 ) (export "gcd" (func $gcd)) ) "#)?; let instance = Instance::new(&mut store, &module, &[])?; // Invoke `gcd` export let gcd = instance.get_typed_func::<(i32, i32), i32>(&mut store, "gcd")?; println!("gcd(6, 27) = {}", gcd.call(&mut store, (6, 27))?); Ok(()) }