`funcref`s are implemented as `NonNull<VMCallerCheckedAnyfunc>`. This should be more efficient than using a `VMExternRef` that points at a `VMCallerCheckedAnyfunc` because it gets rid of an indirection, dynamic allocation, and some reference counting. Note that the null function reference is *NOT* a null pointer; it is a `VMCallerCheckedAnyfunc` that has a null `func_ptr` member. Part of #929
32 lines
775 B
Rust
32 lines
775 B
Rust
use anyhow::{Context as _, Result};
|
|
use wasmtime::*;
|
|
|
|
#[test]
|
|
fn test_invoke_func_via_table() -> Result<()> {
|
|
let store = Store::default();
|
|
|
|
let wat = r#"
|
|
(module
|
|
(func $f (result i64) (i64.const 42))
|
|
|
|
(table (export "table") 1 1 anyfunc)
|
|
(elem (i32.const 0) $f)
|
|
)
|
|
"#;
|
|
let module = Module::new(store.engine(), wat).context("> Error compiling module!")?;
|
|
let instance = Instance::new(&store, &module, &[]).context("> Error instantiating module!")?;
|
|
|
|
let f = instance
|
|
.get_table("table")
|
|
.unwrap()
|
|
.get(0)
|
|
.unwrap()
|
|
.funcref()
|
|
.unwrap()
|
|
.unwrap()
|
|
.clone();
|
|
let result = f.call(&[]).unwrap();
|
|
assert_eq!(result[0].unwrap_i64(), 42);
|
|
Ok(())
|
|
}
|