Files
wasmtime/crates/api/tests/traps.rs
Alex Crichton 420dcd76fd Don't require Store in Instance constructor (#810)
* Don't require `Store` in `Instance` constructor

This can be inferred from the `Module` argument. Additionally add a
`store` accessor to an `Instance` in case it's needed to instantiate
another `Module`.

cc #708

* Update more constructors

* Fix a doctest

* Don't ignore store in `wasm_instance_new`

* Run rustfmt
2020-01-13 17:50:57 -06:00

44 lines
1.2 KiB
Rust

use std::rc::Rc;
use wasmtime::*;
use wat::parse_str;
#[test]
fn test_trap_return() -> Result<(), String> {
struct HelloCallback;
impl Callable for HelloCallback {
fn call(&self, _params: &[Val], _results: &mut [Val]) -> Result<(), Trap> {
Err(Trap::new("test 123"))
}
}
let store = Store::default();
let binary = parse_str(
r#"
(module
(func $hello (import "" "hello"))
(func (export "run") (call $hello))
)
"#,
)
.map_err(|e| format!("failed to parse WebAssembly text source: {}", e))?;
let module =
Module::new(&store, &binary).map_err(|e| format!("failed to compile module: {}", e))?;
let hello_type = FuncType::new(Box::new([]), Box::new([]));
let hello_func = Func::new(&store, hello_type, Rc::new(HelloCallback));
let imports = vec![hello_func.into()];
let instance = Instance::new(&module, &imports)
.map_err(|e| format!("failed to instantiate module: {:?}", e))?;
let run_func = instance.exports()[0]
.func()
.expect("expected function export");
let e = run_func.call(&[]).err().expect("error calling function");
assert_eq!(e.message(), "test 123");
Ok(())
}