Expand description
Safe, wasmi/wasmtime-shaped Rust bindings for the Wasm3 interpreter.
This crate wraps the raw wasm3x-sys C-API in a small, safe, and efficient
API modeled after wasmi and wasmtime:
Enginewraps a shared Wasm3 environment and holds theConfig.Storeowns all runtime state of the modules instantiated into it.Moduleis a parsed module that can be instantiated many times.Linkerprovides host functions and instantiates modules.Instance,Func/TypedFunc,Memory, andGlobalgive access to the instantiated module.
§Example
use wasm3x::{Engine, Store, Module, Linker};
let engine = Engine::default();
let mut store = Store::new(&engine, ());
let module = Module::new(&engine, &wasm_bytes()).unwrap();
let linker = Linker::<()>::new(&engine);
let instance = linker.instantiate_and_start(&mut store, &module).unwrap();
let add = instance.get_typed_func::<(i32, i32), i32>(&store, "add").unwrap();
assert_eq!(add.call(&mut store, (2, 3)).unwrap(), 5);§Differences from wasmi/wasmtime (Wasm3 limitations)
- Function lookup is runtime-global by name. Wasm3 finds exported functions across all modules loaded into the runtime; there is no per-instance namespace and no lookup by index. This matches the common one-module-per-store usage.
- No import/export enumeration. Wasm3 does not expose a module’s imports
or exports, so there is no
Module::imports()/exports(). Host functions are provided through aLinkerand linked by name. - Host functions receive a
Callerbut cannot re-enter Wasm. A host function gets aCaller<'_, T>for&mut Thost-data and linearMemoryaccess, but — unlike Wasmtime — it may not call back into Wasm: Wasm3’s public call entry points run from the base of the runtime stack and would corrupt the suspended frame. Host functions also return at most one value (Wasm3’s signature format encodes a single result); Wasm functions you call may be multi-value. - Light validation. Wasm3 is not a fully validating runtime; treat
Module::newvalidation as best-effort. - Single-threaded.
EngineandStoreare neitherSendnorSync.
Structs§
- Caller
- The context passed as the first argument to every host function.
- Config
- Configuration for an
Engine. - Engine
- A Wasm3 execution environment, shared by all stores created from it.
- Error
- An error that can occur when working with the Wasm3 interpreter.
- Func
- A callable Wasm function belonging to a
Store. - Func
Type - The signature (parameter and result types) of a Wasm function.
- Global
- A handle to an exported global variable of an
Instance. - Global
Type - The type of a global variable: its value type and mutability.
- Instance
- A handle to an instantiated module within a
Store. - Linker
- A registry of host functions used to satisfy module imports and instantiate
Modules into aStore. - Memory
- A handle to the linear memory of a
Store. - Module
- A parsed Wasm module that can be instantiated into a
Store. - Store
- Owns the runtime state (memory, globals, tables, compiled code) of the
modules instantiated into it, together with arbitrary host
data. - Store
Context - A shared view of a
Store’s runtime, produced byAsContext::as_context. - Store
Context Mut - An exclusive view of a
Store’s runtime, produced byAsContextMut::as_context_mut. Used by handle methods that mutate. SeeAsContextfor why this uniform view exists. - Typed
Func - A statically typed view of a
Funcwith compile-time known parameter and result types. Type checks happen once, at construction.
Enums§
- Compilation
Mode - The strategy used to compile Wasm functions to Wasm3’s internal bytecode.
- Mutability
- Whether a global variable can be mutated after instantiation.
- Val
- A runtime Wasm value.
- ValType
- The type of a Wasm value.
Traits§
- AsContext
- A type that can yield a shared
StoreContext:Store<T>, a context, or aCaller, plus&/&mutreferences to any of them. - AsContext
Mut - A type that can yield an exclusive
StoreContextMut. - Into
Func - Rust closures and functions usable as Wasm3 host functions.
- Wasm
Params - Types usable as the parameter list of a
TypedFuncor host function. - Wasm
Results - Types usable as the result list of a
TypedFuncor host function. - WasmRet
- Types usable as the return value of a host function: a value, a tuple of
values, or a
Resultthereof. - WasmTy
- A primitive Wasm type usable as a typed function parameter or result.