Skip to content

Instantly share code, notes, and snippets.

@alexcrichton
Created July 14, 2020 15:24
Show Gist options
  • Save alexcrichton/49d60787351aa04434044ff3e49d94fc to your computer and use it in GitHub Desktop.
Save alexcrichton/49d60787351aa04434044ff3e49d94fc to your computer and use it in GitHub Desktop.
fn main() {
let n = 42;
println!("fib({}) = {}", n, fib(n));
}
fn fib(n: u32) -> u32 {
if n <= 2 {
1
} else {
fib(n - 1) + fib(n - 2)
}
}
use wasmtime::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut config = Config::new();
config.profiler(ProfilingStrategy::JitDump)?;
let engine = Engine::new(&config);
let store = Store::new(&engine);
let module = Module::from_file(&engine, "./fib.wasm")?;
let instance = Instance::new(&store, &module, &[])?;
let start = instance.get_func("main").unwrap();
let start = start.get2::<i32, i32, i32>()?;
let f = if false {
Func::wrap(&store, move || {
start(0, 0)?;
Ok(())
})
} else {
let ty = FuncType::new(Box::new([]), Box::new([]));
Func::new(&store, ty, move |_, _, _| {
start(0, 0)?;
Ok(())
})
};
f.call(&[]).unwrap();
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment