Expand description
The wasmi
virtual machine definitions.
These closely mirror the WebAssembly specification definitions.
The overall structure is heavily inspired by the wasmtime
virtual
machine architecture.
§Example
The following example shows a “Hello, World!”-like example of creating
a Wasm module from some initial .wat
contents, defining a simple host
function and calling the exported Wasm function.
The example was inspired by Wasmtime’s API example.
use anyhow::{anyhow, Result};
use wasmi::*;
fn main() -> Result<()> {
// First step is to create the Wasm execution engine with some config.
// In this example we are using the default configuration.
let engine = Engine::default();
let wat = r#"
(module
(import "host" "hello" (func $host_hello (param i32)))
(func (export "hello")
(call $host_hello (i32.const 3))
)
)
"#;
// Wasmi does not yet support parsing `.wat` so we have to convert
// out `.wat` into `.wasm` before we compile and validate it.
let wasm = wat::parse_str(&wat)?;
let module = Module::new(&engine, &mut &wasm[..])?;
// 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 are using `42` for.
type HostState = u32;
let mut store = Store::new(&engine, 42);
let host_hello = Func::wrap(&mut store, |caller: Caller<'_, HostState>, param: i32| {
println!("Got {param} from WebAssembly");
println!("My host state is: {}", caller.data());
});
// In order to create Wasm module instances and link their imports
// and exports we require a `Linker`.
let mut linker = <Linker<HostState>>::new();
// Instantiation of a Wasm module requires defining its imports and then
// afterwards we can fetch exports by name, as well as asserting the
// type signature of the function with `get_typed_func`.
//
// Also before using an instance created this way we need to start it.
linker.define("host", "hello", host_hello)?;
let instance = linker
.instantiate(&mut store, &module)?
.start(&mut store)?;
let hello = instance.get_typed_func::<(), ()>(&store, "hello")?;
// And finally we can call the wasm!
hello.call(&mut store, ())?;
Ok(())
}
Modules§
- core
- Definitions from the
wasmi_core
crate. - errors
- Defines some errors that may occur upon interaction with
wasmi
.
Structs§
- Caller
- Represents the caller’s context when creating a host function via
Func::wrap
. - Config
- Configuration for an
Engine
. - Engine
- The
wasmi
interpreter. - Export
- An exported WebAssembly value.
- Export
Type - A descriptor for an exported WebAssembly value of a
Module
. - Exports
Iter - An iterator over the
Extern
declarations of anInstance
. - Func
- A Wasm or host function reference.
- Func
Type - A function type representing a function’s parameter and result types.
- Global
- A Wasm global variable reference.
- Global
Type - The type of a global variable.
- Import
Type - A descriptor for an imported value into a Wasm
Module
. - Instance
- An instantiated WebAssembly
Module
. - Instance
Pre - A partially instantiated
Instance
where thestart
function has not yet been executed. - Linker
- A linker used to define module imports and instantiate module instances.
- Memory
- A Wasm linear memory reference.
- Memory
Type - The memory type of a linear memory.
- Module
- A parsed and validated WebAssembly module.
- Module
Exports Iter - An iterator over the exports of a
Module
. - Module
Imports Iter - An iterator over the imports of a
Module
. - Resumable
Invocation - State required to resume a
Func
invocation. - Stack
Limits - The configured limits of the Wasm stack.
- Store
- The store that owns all data associated to Wasm modules.
- Store
Context - A temporary handle to a
&Store<T>
. - Store
Context Mut - A temporary handle to a
&mut Store<T>
. - Table
- A Wasm table reference.
- Table
Type - A descriptor for a
Table
instance. - Typed
Func - A typed
Func
instance. - Typed
Resumable Invocation - State required to resume a
TypedFunc
invocation.
Enums§
- Error
- An error that may occur upon operating on Wasm modules or module instances.
- Extern
- An external item to a WebAssembly module.
- Extern
Type - The type of an
Extern
item. - Mutability
- The mutability of a global variable.
- Resumable
Call - Returned by calling a
Func
in a resumable way. - Typed
Resumable Call - Returned by calling a
TypedFunc
in a resumable way.
Traits§
- AsContext
- A trait used to get shared access to a
Store
inwasmi
. - AsContext
Mut - A trait used to get exclusive access to a
Store
inwasmi
. - Into
Func - Closures and functions that can be used as host functions.
- Read
- Types implementing this trait act as byte streams.
- Wasm
Params - The typed parameters of a
TypedFunc
. - Wasm
Results - The typed results of a
TypedFunc
. - WasmRet
- Types and type sequences that can be used as return values of host functions.
- Wasm
Type - Types that can be used as parameters or results of host functions.
- Wasm
Type List - A list of
WasmType
types.