-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathfuncref_callbacks.rs
More file actions
124 lines (105 loc) · 4.6 KB
/
funcref_callbacks.rs
File metadata and controls
124 lines (105 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use eyre::Result;
use tinywasm::{FuncContext, HostFunction, Imports, ModuleInstance, Store, types::FuncRef};
const LHS: i32 = 5;
const RHS: i32 = 3;
fn main() -> Result<()> {
run_passed_funcref_example()?;
run_returned_funcref_example()?;
Ok(())
}
fn run_passed_funcref_example() -> Result<()> {
// Host receives funcref and calls it via an exported proxy.
const WASM: &str = r#"
(module
(import "host" "call_this" (func $host_callback_caller (param funcref)))
(import "host" "mul" (func $host_mul (param $x i32) (param $y i32) (result i32)))
(func $tell_host_to_call (export "tell_host_to_call")
(call $host_callback_caller (ref.func $add))
(call $host_callback_caller (ref.func $sub))
(call $host_callback_caller (ref.func $host_mul))
)
(type $binop (func (param i32 i32) (result i32)))
(table 3 funcref)
(elem (i32.const 0) $add $sub $host_mul)
(func $add (param $x i32) (param $y i32) (result i32)
local.get $x
local.get $y
i32.add
)
(func $sub (param $x i32) (param $y i32) (result i32)
local.get $x
local.get $y
i32.sub
)
(table $callback_register 1 funcref)
(func (export "call_binop_by_ref") (param funcref i32 i32) (result i32)
(table.set $callback_register (i32.const 0) (local.get 0))
(call_indirect $callback_register (type $binop) (local.get 1)(local.get 2)(i32.const 0))
)
)
"#;
let wasm = wat::parse_str(WASM).expect("failed to parse wat");
let module = tinywasm::parse_bytes(&wasm)?;
let mut store = Store::default();
let mul = HostFunction::from(&mut store, |_, (lhs, rhs): (i32, i32)| -> tinywasm::Result<i32> { Ok(lhs * rhs) });
let call_this =
HostFunction::from(&mut store, |mut ctx: FuncContext<'_>, func_ref: FuncRef| -> tinywasm::Result<()> {
// Host cannot call a funcref directly, so it routes through Wasm.
let call_by_ref = ctx.module().func::<(FuncRef, i32, i32), i32>(ctx.store(), "call_binop_by_ref")?;
let _result = call_by_ref.call(ctx.store_mut(), (func_ref, LHS, RHS))?;
Ok(())
});
let mut imports = Imports::new();
imports.define("host", "call_this", call_this).define("host", "mul", mul);
let instance = ModuleInstance::instantiate(&mut store, &module, Some(imports))?;
let caller = instance.func::<(), ()>(&store, "tell_host_to_call")?;
caller.call(&mut store, ())?;
Ok(())
}
fn run_returned_funcref_example() -> Result<()> {
// Wasm returns funcref values, host executes them through the same proxy.
const WASM: &str = r#"
(module
(import "host" "mul" (func $host_mul (param $x i32) (param $y i32) (result i32)))
(type $binop (func (param i32 i32) (result i32)))
(table 3 funcref)
(elem (i32.const 0) $add $sub $host_mul)
(func $add (param $x i32) (param $y i32) (result i32)
local.get $x
local.get $y
i32.add
)
(func $sub (param $x i32) (param $y i32) (result i32)
local.get $x
local.get $y
i32.sub
)
(func $ref_to_funcs (export "what_should_host_call") (result funcref funcref funcref)
(ref.func $add)
(ref.func $sub)
(ref.func $host_mul)
)
(table $callback_register 1 funcref)
(func $call (export "call_binop_by_ref") (param funcref i32 i32) (result i32)
(table.set $callback_register (i32.const 0) (local.get 0))
(call_indirect $callback_register (type $binop) (local.get 1)(local.get 2)(i32.const 0))
)
)
"#;
let wasm = wat::parse_str(WASM).expect("failed to parse wat");
let module = tinywasm::parse_bytes(&wasm)?;
let mut store = Store::default();
let mut imports = Imports::new();
let mul = HostFunction::from(&mut store, |_, (lhs, rhs): (i32, i32)| -> tinywasm::Result<i32> { Ok(lhs * rhs) });
imports.define("host", "mul", mul);
let instance = ModuleInstance::instantiate(&mut store, &module, Some(imports))?;
let (add_ref, sub_ref, mul_ref) = {
let get_funcrefs = instance.func::<(), (FuncRef, FuncRef, FuncRef)>(&store, "what_should_host_call")?;
get_funcrefs.call(&mut store, ())?
};
let call_by_ref = instance.func::<(FuncRef, i32, i32), i32>(&store, "call_binop_by_ref")?;
for func_ref in [add_ref, sub_ref, mul_ref] {
let _result = call_by_ref.call(&mut store, (func_ref, LHS, RHS))?;
}
Ok(())
}