| 11 | // Crate a synchronous Func, call it directly: |
| 12 | #[test] |
| 13 | fn call_wrapped_func() -> Result<()> { |
| 14 | let wat = r#" |
| 15 | (component |
| 16 | (import "f" (func $f)) |
| 17 | |
| 18 | (core func $f_lower |
| 19 | (canon lower (func $f)) |
| 20 | ) |
| 21 | (core module $m |
| 22 | (import "" "" (func $f)) |
| 23 | |
| 24 | (func $export |
| 25 | (call $f) |
| 26 | ) |
| 27 | |
| 28 | (export "export" (func $export)) |
| 29 | ) |
| 30 | (core instance $i (instantiate $m |
| 31 | (with "" (instance |
| 32 | (export "" (func $f_lower)) |
| 33 | )) |
| 34 | )) |
| 35 | (func (export "export") |
| 36 | (canon lift |
| 37 | (core func $i "export") |
| 38 | ) |
| 39 | ) |
| 40 | ) |
| 41 | "#; |
| 42 | |
| 43 | let engine = Engine::default(); |
| 44 | let component = Component::new(&engine, wat)?; |
| 45 | |
| 46 | let mut linker = Linker::<State>::new(&engine); |
| 47 | linker |
| 48 | .root() |
| 49 | .func_wrap("f", |_, _: ()| -> Result<()> { Ok(()) })?; |
| 50 | |
| 51 | let mut store = Store::new(&engine, State::default()); |
| 52 | store.call_hook(sync_call_hook); |
| 53 | let inst = linker |
| 54 | .instantiate(&mut store, &component) |
| 55 | .expect("instantiate"); |
| 56 | |
| 57 | let export = inst |
| 58 | .get_typed_func::<(), ()>(&mut store, "export") |
| 59 | .expect("looking up `export`"); |
| 60 | |
| 61 | export.call(&mut store, ())?; |
| 62 | |
| 63 | let s = store.into_data(); |
| 64 | assert_eq!(s.calls_into_host, 1); |
| 65 | assert_eq!(s.returns_from_host, 1); |
| 66 | assert_eq!(s.calls_into_wasm, 1); |
| 67 | assert_eq!(s.returns_from_wasm, 1); |
| 68 | |
| 69 | Ok(()) |
| 70 | } |