| 4 | |
| 5 | #[test] |
| 6 | fn test_import_calling_export() { |
| 7 | const WAT: &str = r#" |
| 8 | (module |
| 9 | (type $t0 (func)) |
| 10 | (import "" "imp" (func $.imp (type $t0))) |
| 11 | (func $run call $.imp) |
| 12 | (func $other) |
| 13 | (export "run" (func $run)) |
| 14 | (export "other" (func $other)) |
| 15 | ) |
| 16 | "#; |
| 17 | |
| 18 | let mut store = Store::<Option<Func>>::default(); |
| 19 | let module = Module::new(store.engine(), WAT).expect("failed to create module"); |
| 20 | |
| 21 | let func_ty = FuncType::new(store.engine(), None, None); |
| 22 | let callback_func = Func::new(&mut store, func_ty, move |mut caller, _, _| { |
| 23 | caller |
| 24 | .data() |
| 25 | .unwrap() |
| 26 | .call(&mut caller, &[], &mut []) |
| 27 | .expect("expected function not to trap"); |
| 28 | Ok(()) |
| 29 | }); |
| 30 | |
| 31 | let imports = vec![callback_func.into()]; |
| 32 | let instance = Instance::new(&mut store, &module, imports.as_slice()) |
| 33 | .expect("failed to instantiate module"); |
| 34 | |
| 35 | let run_func = instance |
| 36 | .get_func(&mut store, "run") |
| 37 | .expect("expected a run func in the module"); |
| 38 | |
| 39 | let other_func = instance |
| 40 | .get_func(&mut store, "other") |
| 41 | .expect("expected an other func in the module"); |
| 42 | *store.data_mut() = Some(other_func); |
| 43 | |
| 44 | run_func |
| 45 | .call(&mut store, &[], &mut []) |
| 46 | .expect("expected function not to trap"); |
| 47 | } |
| 48 | |
| 49 | #[test] |
| 50 | fn test_returns_incorrect_type() -> Result<()> { |