* Move all examples to a top-level directory This commit moves all API examples (Rust and C) to a top-level `examples` directory. This is intended to make it more discoverable and conventional as to where examples are located. Additionally all examples are now available in both Rust and C to see how to execute the example in the language you're familiar with. The intention is that as more languages are supported we'd add more languages as examples here too. Each example is also accompanied by either a `*.wat` file which is parsed as input, or a Rust project in a `wasm` folder which is compiled as input. A simple driver crate was also added to `crates/misc` which executes all the examples on CI, ensuring the C and Rust examples all execute successfully.
50 lines
1.8 KiB
Rust
50 lines
1.8 KiB
Rust
//! Small example of how to instantiate a wasm module that imports one function,
|
|
//! showing how you can fill in host functionality for a wasm module.
|
|
|
|
// You can execute this example with `cargo run --example hello`
|
|
|
|
use anyhow::Result;
|
|
use wasmtime::*;
|
|
|
|
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 module = Module::from_file(&store, "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.
|
|
println!("Creating callback...");
|
|
let hello_func = Func::wrap0(&store, || {
|
|
println!("Calling back...");
|
|
println!("> Hello World!");
|
|
});
|
|
|
|
// 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 = [hello_func.into()];
|
|
let instance = Instance::new(&module, &imports)?;
|
|
|
|
// Next we poke around a bit to extract the `run` function from the module.
|
|
println!("Extracting export...");
|
|
let run = instance
|
|
.get_export("run")
|
|
.and_then(|e| e.func())
|
|
.ok_or(anyhow::format_err!("failed to find `run` function export"))?
|
|
.get0::<()>()?;
|
|
|
|
// And last but not least we can call it!
|
|
println!("Calling export...");
|
|
run()?;
|
|
|
|
println!("Done.");
|
|
Ok(())
|
|
}
|