Skip to main content

Crate toywasmx

Crate toywasmx 

Source
Expand description

Safe, Wasmtime/Wasmi-inspired Rust bindings for the toywasm WebAssembly interpreter.

toywasm is a small WebAssembly interpreter written in C. This crate wraps it behind an idiomatic, RAII-based API built on the raw FFI in toywasmx_sys.

§Differences from Wasmtime/Wasmi

The API deliberately does not mimic Wasmtime/Wasmi where toywasm’s model differs — it follows toywasm instead of adding ceremony:

  • There is no Engine type. Wasmtime/Wasmi use an Engine to hold configuration and compiled artifacts, but toywasm selects its entire configuration at compile time (via the toywasmx-sys Cargo features), so there is nothing for a runtime engine object to carry.
  • A Module is created within a Store, not from an engine, because toywasm ties a module to the store’s memory context.

§Example

use toywasmx::{Linker, Module, Store, Val};

let store = Store::new();
let wasm = wat::parse_str(
    r#"(module (func (export "add") (param i32 i32) (result i32)
           local.get 0 local.get 1 i32.add))"#,
)?;
let module = Module::new(&store, &wasm)?;
let mut instance = Linker::new().instantiate(&store, module)?;

let add = instance.get_func("add")?;
let mut results = [Val::I32(0)];
add.call(&mut instance, &[Val::I32(2), Val::I32(3)], &mut results)?;
assert_eq!(results[0], Val::I32(5));

Re-exports§

pub use toywasmx_sys as sys;

Structs§

Func
A handle to an exported function, obtained from Instance::get_func.
FuncType
The signature of a Wasm function: its parameter and result types.
Global
A handle to an exported global, obtained from Instance::get_global.
GlobalType
The type of a Wasm global: its value type and mutability.
Instance
A live instance of a Module.
Linker
Accumulates host-function definitions and instantiates modules against them.
Memory
A handle to an exported linear memory, obtained from Instance::get_memory.
MemoryType
The type of a Wasm linear memory: its page limits.
Module
A decoded and validated Wasm module.
Store
A Store owns the memory context in which modules are loaded and instantiated.

Enums§

Error
An error produced while loading, instantiating, or running a module.
Mutability
Whether a Wasm global is mutable.
Val
A typed Wasm value.
ValType
A Wasm value type.

Type Aliases§

Result
The result type used throughout this crate.