wasmtime: Option to return default values for unknown imports (#6010)

Similar to the `--trap-unknown-imports` option, which defines unknown function
imports with functions that trap when called, this new
`--default-values-unknown-imports` option defines unknown function imports with
a function that returns the default values for the result types (either zero or
null depending on the value type).
This commit is contained in:
Nick Fitzgerald
2023-03-13 14:39:30 -07:00
committed by GitHub
parent e2a6fe99c2
commit 90c9bec225
3 changed files with 102 additions and 1 deletions

View File

@@ -390,3 +390,37 @@ fn test_trapping_unknown_import() -> Result<()> {
Ok(())
}
#[test]
fn test_default_value_unknown_import() -> Result<()> {
const WAT: &str = r#"
(module
(import "unknown" "func" (func $unknown_func (result i64 f32 externref)))
(func (export "run") (result i64 f32 externref)
call $unknown_func
)
)
"#;
let mut store = Store::<()>::default();
let module = Module::new(store.engine(), WAT).expect("failed to create module");
let mut linker = Linker::new(store.engine());
linker.define_unknown_imports_as_default_values(&module)?;
let instance = linker.instantiate(&mut store, &module)?;
// "run" calls an import function which will not be defined, so it should
// return default values.
let run_func = instance
.get_func(&mut store, "run")
.expect("expected a run func in the module");
let mut results = vec![Val::I32(1), Val::I32(2), Val::I32(3)];
run_func.call(&mut store, &[], &mut results)?;
assert_eq!(results[0].i64(), Some(0));
assert_eq!(results[1].f32(), Some(0.0));
assert!(results[2].externref().unwrap().is_none());
Ok(())
}