Skip to content

Commit

Permalink
Add error propagation test
Browse files Browse the repository at this point in the history
  • Loading branch information
lachlansneff committed Apr 22, 2019
1 parent 14325c9 commit ff9de18
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/runtime-core/src/typed_func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ impl WasmTypeList for Infallible {
unreachable!()
}
fn types() -> &'static [Type] {
unreachable!()
&[]
}
#[allow(non_snake_case)]
unsafe fn call<Rets: WasmTypeList>(
Expand Down
49 changes: 49 additions & 0 deletions lib/runtime/tests/error_propagation.rs
Original file line number Diff line number Diff line change
@@ -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")
}
}

0 comments on commit ff9de18

Please sign in to comment.