* Move `Func` to its own file * Support `Func` imports with zero shims This commit extends the `Func` type in the `wasmtime` crate with static `wrap*` constructors. The goal of these constructors is to create a `Func` type which has zero shims associated with it, creating as small of a layer as possible between wasm code and calling imported Rust code. This is achieved by creating an `extern "C"` shim function which matches the ABI of what Cranelift will generate, and then the host function is passed directly into an `InstanceHandle` to get called later. This also enables enough inlining opportunities that LLVM will be able to see all functions and inline everything to the point where your function is called immediately from wasm, no questions asked.
62 lines
2.2 KiB
Rust
62 lines
2.2 KiB
Rust
use std::collections::HashMap;
|
|
use wasmtime::*;
|
|
|
|
/// Return an instance implementing the "spectest" interface used in the
|
|
/// spec testsuite.
|
|
pub fn instantiate_spectest(store: &Store) -> HashMap<&'static str, Extern> {
|
|
let mut ret = HashMap::new();
|
|
|
|
let func = Func::wrap0(store, || {});
|
|
ret.insert("print", Extern::Func(func));
|
|
|
|
let func = Func::wrap1(store, |val: i32| println!("{}: i32", val));
|
|
ret.insert("print_i32", Extern::Func(func));
|
|
|
|
let func = Func::wrap1(store, |val: i64| println!("{}: i64", val));
|
|
ret.insert("print_i64", Extern::Func(func));
|
|
|
|
let func = Func::wrap1(store, |val: f32| println!("{}: f32", val));
|
|
ret.insert("print_f32", Extern::Func(func));
|
|
|
|
let func = Func::wrap1(store, |val: f64| println!("{}: f64", val));
|
|
ret.insert("print_f64", Extern::Func(func));
|
|
|
|
let func = Func::wrap2(store, |i: i32, f: f32| {
|
|
println!("{}: i32", i);
|
|
println!("{}: f32", f);
|
|
});
|
|
ret.insert("print_i32_f32", Extern::Func(func));
|
|
|
|
let func = Func::wrap2(store, |f1: f64, f2: f64| {
|
|
println!("{}: f64", f1);
|
|
println!("{}: f64", f2);
|
|
});
|
|
ret.insert("print_f64_f64", Extern::Func(func));
|
|
|
|
let ty = GlobalType::new(ValType::I32, Mutability::Const);
|
|
let g = Global::new(store, ty, Val::I32(666)).unwrap();
|
|
ret.insert("global_i32", Extern::Global(g));
|
|
|
|
let ty = GlobalType::new(ValType::I64, Mutability::Const);
|
|
let g = Global::new(store, ty, Val::I64(666)).unwrap();
|
|
ret.insert("global_i64", Extern::Global(g));
|
|
|
|
let ty = GlobalType::new(ValType::F32, Mutability::Const);
|
|
let g = Global::new(store, ty, Val::F32(0x4426_8000)).unwrap();
|
|
ret.insert("global_f32", Extern::Global(g));
|
|
|
|
let ty = GlobalType::new(ValType::F64, Mutability::Const);
|
|
let g = Global::new(store, ty, Val::F64(0x4084_d000_0000_0000)).unwrap();
|
|
ret.insert("global_f64", Extern::Global(g));
|
|
|
|
let ty = TableType::new(ValType::FuncRef, Limits::new(10, Some(20)));
|
|
let table = Table::new(store, ty, Val::AnyRef(AnyRef::Null)).unwrap();
|
|
ret.insert("table", Extern::Table(table));
|
|
|
|
let ty = MemoryType::new(Limits::new(1, Some(2)));
|
|
let memory = Memory::new(store, ty);
|
|
ret.insert("memory", Extern::Memory(memory));
|
|
|
|
return ret;
|
|
}
|