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 wasmi::*;
// In this simple example we are going to compile the below Wasm source,
// instantiate a Wasm module from it and call its exported "hello" function.
fn main() -> Result<(), wasmi::Error> {
let wasm = r#"
(module
(import "host" "hello" (func $host_hello (param i32)))
(func (export "hello")
(call $host_hello (i32.const 3))
)
)
"#;
// 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();
// Now we can compile the above Wasm module with the given Wasm source.
let module = Module::new(&engine, wasm)?;
// Wasm objects operate within the context of a Wasm `Store`.
//
// Each `Store` has a type parameter to store host specific data.
// In this example the host state is a simple `u32` type with value `42`.
type HostState = u32;
let mut store = Store::new(&engine, 42);
// A linker can be used to instantiate Wasm modules.
// The job of a linker is to satisfy the Wasm module's imports.
let mut linker = <Linker<HostState>>::new(&engine);
// We are required to define all imports before instantiating a Wasm module.
linker.func_wrap("host", "hello", |caller: Caller<'_, HostState>, param: i32| {
println!("Got {param} from WebAssembly and my host state is: {}", caller.data());
});
let instance = linker
.instantiate(&mut store, &module)?
.start(&mut store)?;
// Now we can finally query the exported "hello" function and call it.
instance
.get_typed_func::<(), ()>(&store, "hello")?
.call(&mut store, ())?;
Ok(())
}§Crate Features
| Feature | Crates | Description |
|---|---|---|
std | wasmiwasmi_corewasmi_irwasmi_collections | Enables usage of Rust’s standard library. This may have some performance advantages when enabled. Disabling this feature makes Wasmi compile on platforms that do not provide Rust’s standard library such as many embedded platforms. Enabled by default. |
wat | wasmi | Enables support to parse Wat encoded Wasm modules. Enabled by default. |
simd | wasmiwasmi_corewasmi_irwasmi_cli | Enables support for the Wasm simd and relaxed-simd proposals. Note that this may introduce execution overhead and increased memory consumption for Wasm executions that do not need Wasm simd functionality. Disabled by default. |
hash-collections | wasmiwasmi_collections | Enables use of hash-map based collections in Wasmi internals. This might yield performance improvements in some use cases. Disabled by default. |
prefer-btree-collections | wasmiwasmi_collections | Enforces use of btree-map based collections in Wasmi internals. This may yield performance improvements and memory consumption decreases in some use cases. Also it enables Wasmi to run on platforms that have no random source. Disabled by default. |
extra-checks | wasmi | Enables extra runtime checks in the Wasmi executor. Expected execution overhead is ~20%. Enable this if your focus is on safety. Disable this for maximum execution performance. Disabled by default. |
Modules§
- core
Deprecated - Definitions from the
wasmi_corecrate. - errors
- Defines some errors that may occur upon interaction with Wasmi.
- state
Deprecated - Contains type states for the
LinkerBuilderconstruction process.
Structs§
- Caller
- Represents the caller’s context when creating a host function via
Func::wrap. - Config
- Configuration for an
Engine. - Custom
Section - A Wasm custom section.
- Custom
Sections Iter - An iterator over the custom sections of a Wasm module.
- Enforced
Limits - Stores customizable limits for the
Enginewhen parsing or compiling Wasm modules. - Engine
- The Wasmi interpreter.
- Engine
Weak - A weak reference to an
Engine. - Error
- The generic Wasmi root error type.
- Export
- An exported WebAssembly value.
- Export
Type - A descriptor for an exported WebAssembly value of a
Module. - Exports
Iter - An iterator over the
Externdeclarations of anInstance. - Extern
Ref - Represents an opaque reference to any data within WebAssembly.
- F32
- A 32-bit
f32type. - F64
- A 64-bit
f64type. - Func
- A Wasm or host function reference.
- Func
Type - A Wasm function descriptor.
- 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 Deprecated - A partially instantiated
Instancewhere thestartfunction has not yet been executed. - Linker
- A linker used to define module imports and instantiate module instances.
- Linker
Builder Deprecated - A linker used to define module imports and instantiate module instances.
- Memory
- A Wasm linear memory reference.
- Memory
Type - A Wasm memory descriptor.
- Memory
Type Builder - A
MemoryTypebuilder. - 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
Call Host Trap - State required to resume a
Funcinvocation after a host trap. - Resumable
Call OutOf Fuel - State required to resume a
Funcinvocation after a host trap. - Stack
Limits Deprecated - 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>. - Store
Limits - Provides limits for a
Store. - Store
Limits Builder - Used to build
StoreLimits. - Table
- A Wasm table reference.
- Table
Type - A Wasm table descriptor.
- Typed
Func - A typed
Funcinstance. - Typed
Resumable Call Host Trap - State required to resume a
TypedFuncinvocation. - Typed
Resumable Call OutOf Fuel - State required to resume a
TypedFuncinvocation after running out of fuel. - V128
- The Wasm
simdproposal’sv128type.
Enums§
- Call
Hook - Argument to the callback set by
Store::call_hookto indicate why the callback was invoked. - Compilation
Mode - The chosen mode of Wasm to Wasmi bytecode compilation.
- Extern
- An external item to a WebAssembly module.
- Extern
Type - The type of an
Externitem. - Mutability
- The mutability of a global variable.
- Ref
- A nullable reference type.
- Resumable
Call - Returned by calling a
Funcin a resumable way. - Trap
Code - Error type which can be thrown by wasm code or by host environment.
- Typed
Resumable Call - Returned by calling a
TypedFuncin a resumable way. - Val
- Runtime representation of a Wasm value.
- ValType
- Type of a value.
Traits§
- AsContext
- A trait used to get shared access to a
Storein Wasmi. - AsContext
Mut - A trait used to get exclusive access to a
Storein Wasmi. - Into
Func - Closures and functions that can be used as host functions.
- Read
- Types implementing this trait act as byte streams.
- Resource
Limiter - Used by hosts to limit resource consumption of instances.
- 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.
- WasmTy
- Types that can be used as parameters or results of host functions.
- Wasm
TyList - A list of
WasmTytypes.