wasmtime

A standalone runtime for WebAssembly

A Bytecode Alliance project

About

This crate is the Rust embedding API for the [Wasmtime] project: a cross-platform engine for running WebAssembly programs. Notable features of Wasmtime are:

Example

An example of using the Wasmtime embedding API for running a small WebAssembly module might look like:

```rust use anyhow::Result; use wasmtime::*;

fn main() -> Result<()> { // Modules can be compiled through either the text or binary format let engine = Engine::default(); let wat = r#" (module (import "host" "hostfunc" (func $hosthello (param i32)))

        (func (export "hello")
            i32.const 3
            call $host_hello)
    )
"#;
let module = Module::new(&engine, wat)?;

// Create a `Linker` which will be later used to instantiate this module.
// Host functionality is defined by name within the `Linker`.
let mut linker = Linker::new(&engine);
linker.func_wrap("host", "host_func", |caller: Caller<'_, u32>, param: i32| {
    println!("Got {} from WebAssembly", param);
    println!("my host state is: {}", caller.data());
})?;

// All wasm objects operate within the context of a "store". Each
// `Store` has a type parameter to store host-specific data, which in
// this case we're using `4` for.
let mut store = Store::new(&engine, 4);
let instance = linker.instantiate(&mut store, &module)?;
let hello = instance.get_typed_func::<(), ()>(&mut store, "hello")?;

// And finally we can call the wasm!
hello.call(&mut store, ())?;

Ok(())

} ```

More examples and information can be found in the wasmtime crate's online documentation as well.

Documentation

📚 Read the Wasmtime guide here! 📚

The wasmtime guide is the best starting point to learn about what Wasmtime can do for you or help answer your questions about Wasmtime. If you're curious in contributing to Wasmtime, it can also help you do that!