Add wasmtime::UnknownImportError (#5509)

This adds a new error type `UnknownImportError` which will be returned
(wrapped in an `anyhow::Error`) by `Linker::instantiate{,_async,_pre}`
if a module has an unresolvable import.

This error type is also used by `Linker::define_unknown_imports_as_traps`;
any resulting traps will also downcast to `UnknownImportError`.

Closes #5416
This commit is contained in:
Lann
2023-01-03 14:01:57 -05:00
committed by GitHub
parent c9c7d4991c
commit 69b7ecf90e
2 changed files with 85 additions and 25 deletions

View File

@@ -23,6 +23,24 @@ fn link_undefined() -> Result<()> {
Ok(())
}
#[test]
fn test_unknown_import_error() -> Result<()> {
let mut store = Store::<()>::default();
let linker = Linker::new(store.engine());
let module = Module::new(
store.engine(),
r#"(module (import "unknown-module" "unknown-name" (func)))"#,
)?;
let err = linker
.instantiate(&mut store, &module)
.expect_err("should fail");
let unknown_import: UnknownImportError = err.downcast()?;
assert_eq!(unknown_import.module(), "unknown-module");
assert_eq!(unknown_import.name(), "unknown-name");
unknown_import.ty().unwrap_func();
Ok(())
}
#[test]
fn link_twice_bad() -> Result<()> {
let mut store = Store::<()>::default();
@@ -366,7 +384,8 @@ fn test_trapping_unknown_import() -> Result<()> {
.get_func(&mut store, "run")
.expect("expected a run func in the module");
assert!(run_func.call(&mut store, &[], &mut []).is_err());
let err = run_func.call(&mut store, &[], &mut []).unwrap_err();
assert!(err.is::<UnknownImportError>());
// "other" does not call the import function, so it should not trap
let other_func = instance