* 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?
60 lines
2.1 KiB
Rust
60 lines
2.1 KiB
Rust
//! Translation of hello example
|
|
|
|
use anyhow::{ensure, Context as _, Result};
|
|
use std::rc::Rc;
|
|
use wasmtime::*;
|
|
|
|
struct HelloCallback;
|
|
|
|
impl Callable for HelloCallback {
|
|
fn call(&self, _params: &[Val], _results: &mut [Val]) -> Result<(), Trap> {
|
|
println!("Calling back...");
|
|
println!("> Hello World!");
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
fn main() -> Result<()> {
|
|
// Configure the initial compilation environment, creating the global
|
|
// `Store` structure. Note that you can also tweak configuration settings
|
|
// with a `Config` and an `Engine` if desired.
|
|
println!("Initializing...");
|
|
let store = Store::default();
|
|
|
|
// Compile the wasm binary into an in-memory instance of a `Module`.
|
|
println!("Compiling module...");
|
|
let wat = r#"
|
|
(module
|
|
(func $hello (import "" "hello"))
|
|
(func (export "run") (call $hello))
|
|
)
|
|
"#;
|
|
let module = Module::new(&store, wat).context("> Error compiling module!")?;
|
|
|
|
// Here we handle the imports of the module, which in this case is our
|
|
// `HelloCallback` type and its associated implementation of `Callback.
|
|
println!("Creating callback...");
|
|
let hello_type = FuncType::new(Box::new([]), Box::new([]));
|
|
let hello_func = Func::new(&store, hello_type, Rc::new(HelloCallback));
|
|
|
|
// Once we've got that all set up we can then move to the instantiation
|
|
// phase, pairing together a compiled module as well as a set of imports.
|
|
// Note that this is where the wasm `start` function, if any, would run.
|
|
println!("Instantiating module...");
|
|
let imports = vec![hello_func.into()];
|
|
let instance = Instance::new(&module, &imports).context("> Error instantiating module!")?;
|
|
|
|
// Next we poke around a bit to extract the `run` function from the module.
|
|
println!("Extracting export...");
|
|
let exports = instance.exports();
|
|
ensure!(!exports.is_empty(), "> Error accessing exports!");
|
|
let run_func = exports[0].func().context("> Error accessing exports!")?;
|
|
|
|
// And last but not least we can call it!
|
|
println!("Calling export...");
|
|
run_func.call(&[])?;
|
|
|
|
println!("Done.");
|
|
Ok(())
|
|
}
|