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:
Alex Crichton
2021-06-03 09:10:53 -05:00
committed by GitHub
parent a5a28b1c5b
commit 7a1b7cdf92
233 changed files with 13349 additions and 11997 deletions

View File

@@ -6,23 +6,42 @@
use anyhow::Result;
use wasmtime::*;
struct MyState {
name: String,
count: usize,
}
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`.
// First the wasm module needs to be compiled. This is done with a global
// "compilation environment" within an `Engine`. Note that engines can be
// further configured through `Config` if desired instead of using the
// default like this is here.
println!("Compiling module...");
let module = Module::from_file(store.engine(), "examples/hello.wat")?;
let engine = Engine::default();
let module = Module::from_file(&engine, "examples/hello.wat")?;
// Here we handle the imports of the module, which in this case is our
// `HelloCallback` type and its associated implementation of `Callback.
// After a module is compiled we create a `Store` which will contain
// instantiated modules and other items like host functions. A Store
// contains an arbitrary piece of host information, and we use `MyState`
// here.
println!("Initializing...");
let mut store = Store::new(
&engine,
MyState {
name: "hello, world!".to_string(),
count: 0,
},
);
// Our wasm module we'll be instantiating requires one imported function.
// the function takes no parameters and returns no results. We create a host
// implementation of that function here, and the `caller` parameter here is
// used to get access to our original `MyState` value.
println!("Creating callback...");
let hello_func = Func::wrap(&store, || {
let hello_func = Func::wrap(&mut store, |mut caller: Caller<'_, MyState>| {
println!("Calling back...");
println!("> Hello World!");
println!("> {}", caller.data().name);
caller.data_mut().count += 1;
});
// Once we've got that all set up we can then move to the instantiation
@@ -30,15 +49,15 @@ fn main() -> Result<()> {
// Note that this is where the wasm `start` function, if any, would run.
println!("Instantiating module...");
let imports = [hello_func.into()];
let instance = Instance::new(&store, &module, &imports)?;
let instance = Instance::new(&mut store, &module, &imports)?;
// Next we poke around a bit to extract the `run` function from the module.
println!("Extracting export...");
let run = instance.get_typed_func::<(), ()>("run")?;
let run = instance.get_typed_func::<(), (), _>(&mut store, "run")?;
// And last but not least we can call it!
println!("Calling export...");
run.call(())?;
run.call(&mut store, ())?;
println!("Done.");
Ok(())