* Support parsing the text format in `wasmtime` crate This commit adds support to the `wasmtime::Module` type to parse the text format. This is often quite convenient to support in testing or tinkering with the runtime. Additionally the `wat` parser is pretty lightweight and easy to add to builds, so it's relatively easy for us to support as well! The exact manner that this is now supported comes with a few updates to the existing API: * A new optional feature of the `wasmtime` crate, `wat`, has been added. This is enabled by default. * The `Module::new` API now takes `impl AsRef<[u8]>` instead of just `&[u8]`, and when the `wat` feature is enabled it will attempt to interpret it either as a wasm binary or as the text format. Note that this check is quite cheap since you just check the first byte. * A `Module::from_file` API was added as a convenience to parse a file from disk, allowing error messages for `*.wat` files on disk to be a bit nicer. * APIs like `Module::new_unchecked` and `Module::validate` remain unchanged, they require the binary format to be called. The intention here is to make this as convenient as possible for new developers of the `wasmtime` crate. By changing the default behavior though this has ramifications such as, for example, supporting the text format implicitly through the C API now. * Handle review comments * Update more tests to avoid usage of `wat` crate * Go back to unchecked for now in wasm_module_new Looks like C# tests rely on this?
67 lines
1.6 KiB
Rust
67 lines
1.6 KiB
Rust
use std::cell::RefCell;
|
|
use std::rc::Rc;
|
|
use wasmtime::*;
|
|
|
|
#[test]
|
|
fn test_import_calling_export() {
|
|
const WAT: &str = r#"
|
|
(module
|
|
(type $t0 (func))
|
|
(import "" "imp" (func $.imp (type $t0)))
|
|
(func $run call $.imp)
|
|
(func $other)
|
|
(export "run" (func $run))
|
|
(export "other" (func $other))
|
|
)
|
|
"#;
|
|
|
|
struct Callback {
|
|
pub other: RefCell<Option<Func>>,
|
|
}
|
|
|
|
impl Callable for Callback {
|
|
fn call(&self, _params: &[Val], _results: &mut [Val]) -> Result<(), Trap> {
|
|
self.other
|
|
.borrow()
|
|
.as_ref()
|
|
.expect("expected a function ref")
|
|
.call(&[])
|
|
.expect("expected function not to trap");
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
let store = Store::default();
|
|
let module = Module::new(&store, WAT).expect("failed to create module");
|
|
|
|
let callback = Rc::new(Callback {
|
|
other: RefCell::new(None),
|
|
});
|
|
|
|
let callback_func = Func::new(
|
|
&store,
|
|
FuncType::new(Box::new([]), Box::new([])),
|
|
callback.clone(),
|
|
);
|
|
|
|
let imports = vec![callback_func.into()];
|
|
let instance =
|
|
Instance::new(&module, imports.as_slice()).expect("failed to instantiate module");
|
|
|
|
let exports = instance.exports();
|
|
assert!(!exports.is_empty());
|
|
|
|
let run_func = exports[0]
|
|
.func()
|
|
.expect("expected a run func in the module");
|
|
|
|
*callback.other.borrow_mut() = Some(
|
|
exports[1]
|
|
.func()
|
|
.expect("expected an other func in the module")
|
|
.clone(),
|
|
);
|
|
|
|
run_func.call(&[]).expect("expected function not to trap");
|
|
}
|