Path: blob/main/tests/all/import_calling_export.rs
1691 views
#![cfg(not(miri))]12use wasmtime::*;34#[test]5fn test_import_calling_export() {6const WAT: &str = r#"7(module8(type $t0 (func))9(import "" "imp" (func $.imp (type $t0)))10(func $run call $.imp)11(func $other)12(export "run" (func $run))13(export "other" (func $other))14)15"#;1617let mut store = Store::<Option<Func>>::default();18let module = Module::new(store.engine(), WAT).expect("failed to create module");1920let func_ty = FuncType::new(store.engine(), None, None);21let callback_func = Func::new(&mut store, func_ty, move |mut caller, _, _| {22caller23.data()24.unwrap()25.call(&mut caller, &[], &mut [])26.expect("expected function not to trap");27Ok(())28});2930let imports = vec![callback_func.into()];31let instance = Instance::new(&mut store, &module, imports.as_slice())32.expect("failed to instantiate module");3334let run_func = instance35.get_func(&mut store, "run")36.expect("expected a run func in the module");3738let other_func = instance39.get_func(&mut store, "other")40.expect("expected an other func in the module");41*store.data_mut() = Some(other_func);4243run_func44.call(&mut store, &[], &mut [])45.expect("expected function not to trap");46}4748#[test]49fn test_returns_incorrect_type() -> Result<()> {50const WAT: &str = r#"51(module52(import "env" "evil" (func $evil (result i32)))53(func (export "run") (result i32)54(call $evil)55)56)57"#;5859let mut store = Store::<()>::default();60let module = Module::new(store.engine(), WAT)?;6162let func_ty = FuncType::new(store.engine(), None, Some(ValType::I32));63let callback_func = Func::new(&mut store, func_ty, |_, _, results| {64// Evil! Returns I64 here instead of promised in the signature I32.65results[0] = Val::I64(228);66Ok(())67});6869let imports = vec![callback_func.into()];70let instance = Instance::new(&mut store, &module, imports.as_slice())?;7172let run_func = instance73.get_func(&mut store, "run")74.expect("expected a run func in the module");7576let mut result = [Val::I32(0)];77let trap = run_func78.call(&mut store, &[], &mut result)79.expect_err("the execution should fail");80assert!(format!("{trap:?}").contains("function attempted to return an incompatible value"));81Ok(())82}838485