mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-21 12:41:32 +00:00
Add error propagation test
This commit is contained in:
@ -230,7 +230,7 @@ impl WasmTypeList for Infallible {
|
||||
unreachable!()
|
||||
}
|
||||
fn types() -> &'static [Type] {
|
||||
unreachable!()
|
||||
&[]
|
||||
}
|
||||
#[allow(non_snake_case)]
|
||||
unsafe fn call<Rets: WasmTypeList>(
|
||||
|
49
lib/runtime/tests/error_propagation.rs
Normal file
49
lib/runtime/tests/error_propagation.rs
Normal file
@ -0,0 +1,49 @@
|
||||
#[test]
|
||||
fn error_propagation() {
|
||||
use std::convert::Infallible;
|
||||
use wabt::wat2wasm;
|
||||
use wasmer_runtime::{compile, error::RuntimeError, imports, Ctx, Func};
|
||||
|
||||
static WAT: &'static str = r#"
|
||||
(module
|
||||
(type (;0;) (func))
|
||||
(import "env" "ret_err" (func $ret_err (type 0)))
|
||||
(func $call_panic
|
||||
call $ret_err
|
||||
)
|
||||
(export "call_err" (func $call_panic))
|
||||
)
|
||||
"#;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct ExitCode {
|
||||
code: i32,
|
||||
}
|
||||
|
||||
fn ret_err(_ctx: &mut Ctx) -> Result<Infallible, ExitCode> {
|
||||
Err(ExitCode { code: 42 })
|
||||
}
|
||||
|
||||
let wasm = wat2wasm(WAT).unwrap();
|
||||
|
||||
let module = compile(&wasm).unwrap();
|
||||
|
||||
let instance = module
|
||||
.instantiate(&imports! {
|
||||
"env" => {
|
||||
"ret_err" => Func::new(ret_err),
|
||||
},
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
let foo: Func<(), ()> = instance.func("call_err").unwrap();
|
||||
|
||||
let result = foo.call();
|
||||
|
||||
if let Err(RuntimeError::Error { data }) = result {
|
||||
let exit_code = data.downcast::<ExitCode>().unwrap();
|
||||
assert_eq!(exit_code.code, 42);
|
||||
} else {
|
||||
panic!("didn't return RuntimeError::Error")
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user