Files
wasmtime/crates/api/tests/traps.rs
Alex Crichton 1fe76ef9e3 Remove the need for HostRef<Module>
This commit continues previous work and also #708 by removing the need
to use `HostRef<Module>` in the API of the `wasmtime` crate. The API
changes performed here are:

* The `Module` type is now itself internally reference counted.
* The `Module::store` function now returns the `Store` that was used to
  create a `Module`
* Documentation for `Module` and its methods have been expanded.
2020-01-08 12:46:18 -08:00

48 lines
1.3 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 = HostRef::new(Func::new(&store, hello_type, Rc::new(HelloCallback)));
let imports = vec![hello_func.into()];
let instance = Instance::new(&store, &module, imports.as_slice())
.map_err(|e| format!("failed to instantiate module: {:?}", e))?;
let run_func = instance.exports()[0]
.func()
.expect("expected function export");
let e = run_func
.borrow()
.call(&[])
.err()
.expect("error calling function");
assert_eq!(e.message(), "test 123");
Ok(())
}