Implement RFC 11: Redesigning Wasmtime's APIs (#2897)
Implement Wasmtime's new API as designed by RFC 11. This is quite a large commit which has had lots of discussion externally, so for more information it's best to read the RFC thread and the PR thread.
This commit is contained in:
@@ -1,6 +1,4 @@
|
||||
use anyhow::Result;
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
use wasmtime::*;
|
||||
|
||||
#[test]
|
||||
@@ -16,39 +14,38 @@ fn test_import_calling_export() {
|
||||
)
|
||||
"#;
|
||||
|
||||
let store = Store::default();
|
||||
let mut store = Store::<Option<Func>>::default();
|
||||
let module = Module::new(store.engine(), WAT).expect("failed to create module");
|
||||
|
||||
let other = Rc::new(RefCell::new(None::<Func>));
|
||||
let other2 = Rc::downgrade(&other);
|
||||
|
||||
let callback_func = Func::new(&store, FuncType::new(None, None), move |_, _, _| {
|
||||
other2
|
||||
.upgrade()
|
||||
.unwrap()
|
||||
.borrow()
|
||||
.as_ref()
|
||||
.expect("expected a function ref")
|
||||
.call(&[])
|
||||
.expect("expected function not to trap");
|
||||
Ok(())
|
||||
});
|
||||
|
||||
let imports = vec![callback_func.into()];
|
||||
let instance =
|
||||
Instance::new(&store, &module, imports.as_slice()).expect("failed to instantiate module");
|
||||
|
||||
let run_func = instance
|
||||
.get_func("run")
|
||||
.expect("expected a run func in the module");
|
||||
|
||||
*other.borrow_mut() = Some(
|
||||
instance
|
||||
.get_func("other")
|
||||
.expect("expected an other func in the module"),
|
||||
let callback_func = Func::new(
|
||||
&mut store,
|
||||
FuncType::new(None, None),
|
||||
move |mut caller, _, _| {
|
||||
caller
|
||||
.data()
|
||||
.unwrap()
|
||||
.call(&mut caller, &[])
|
||||
.expect("expected function not to trap");
|
||||
Ok(())
|
||||
},
|
||||
);
|
||||
|
||||
run_func.call(&[]).expect("expected function not to trap");
|
||||
let imports = vec![callback_func.into()];
|
||||
let instance = Instance::new(&mut store, &module, imports.as_slice())
|
||||
.expect("failed to instantiate module");
|
||||
|
||||
let run_func = instance
|
||||
.get_func(&mut store, "run")
|
||||
.expect("expected a run func in the module");
|
||||
|
||||
let other_func = instance
|
||||
.get_func(&mut store, "other")
|
||||
.expect("expected an other func in the module");
|
||||
*store.data_mut() = Some(other_func);
|
||||
|
||||
run_func
|
||||
.call(&mut store, &[])
|
||||
.expect("expected function not to trap");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -62,11 +59,11 @@ fn test_returns_incorrect_type() -> Result<()> {
|
||||
)
|
||||
"#;
|
||||
|
||||
let store = Store::default();
|
||||
let mut store = Store::<()>::default();
|
||||
let module = Module::new(store.engine(), WAT)?;
|
||||
|
||||
let callback_func = Func::new(
|
||||
&store,
|
||||
&mut store,
|
||||
FuncType::new(None, Some(ValType::I32)),
|
||||
|_, _, results| {
|
||||
// Evil! Returns I64 here instead of promised in the signature I32.
|
||||
@@ -76,14 +73,14 @@ fn test_returns_incorrect_type() -> Result<()> {
|
||||
);
|
||||
|
||||
let imports = vec![callback_func.into()];
|
||||
let instance = Instance::new(&store, &module, imports.as_slice())?;
|
||||
let instance = Instance::new(&mut store, &module, imports.as_slice())?;
|
||||
|
||||
let run_func = instance
|
||||
.get_func("run")
|
||||
.get_func(&mut store, "run")
|
||||
.expect("expected a run func in the module");
|
||||
|
||||
let trap = run_func
|
||||
.call(&[])
|
||||
.call(&mut store, &[])
|
||||
.expect_err("the execution should fail")
|
||||
.downcast::<Trap>()?;
|
||||
assert!(trap
|
||||
|
||||
Reference in New Issue
Block a user