Expand description
tinywasm provides a small, portable WebAssembly interpreter with support for
the WebAssembly MVP, WebAssembly 2.0, and a growing set of newer proposals.
It is designed to stay lightweight while still being practical to embed in
applications, tools, and no_std + alloc environments.
§Features
std
Enables parsing from files and streams. Enabled by default.log
Enables integration with thelogcrate. Enabled by default.parser
Enables the bundledtinywasm-parsercrate and top-level parse helpers. Enabled by default.archive
Enables serialization and deserialization of compiled modules in the internaltwasmformat. Enabled by default.canonicalize-nans
Canonicalizes NaN values to a single representation. Enabled by default.debug
DerivesDebugfor runtime types. Enabled by default.parallel-parser
Parallelizes function parsing and validation across threads whenstdis enabled. Enabled by default.guest-debug
Exposes module-internal by-index inspection APIs (*_by_index).simd-x86
Enables x86-specific SIMD intrinsics for selected operations and usesunsafeinternally.
With default features disabled, tinywasm only depends on core, alloc, and libm.
By disabling std, you can use tinywasm in no_std environments. This requires
a custom allocator and removes support for parsing from files and streams, but otherwise the API is the same.
§Getting Started
The easiest way to get started is to use the crate::parse_bytes function to load a
WebAssembly module from bytes. This will parse the module and validate it, returning
a Module that can be used to instantiate the module.
use tinywasm::{ModuleInstance, Store};
// Load a module from bytes
let wasm = include_bytes!("../../../examples/wasm/add.wasm");
let module = tinywasm::parse_bytes(wasm)?;
// # Create a new store
// Stores are used to allocate objects like functions and globals
let mut store = Store::default();
// # Instantiate the module
// This will allocate the module and its globals into the store
// and execute the module's start function.
let instance = ModuleInstance::instantiate(&mut store, &module, None)?;
// # Get a typed handle to the exported "add" function
// Alternatively, you can use `instance.func_untyped` to get an untyped handle
// that takes and returns [`WasmValue`]s
let func = instance.func::<(i32, i32), i32>(&mut store, "add")?;
let res = func.call(&mut store, (1, 2))?;
assert_eq!(res, 3);For non-default runtime behavior, construct a Store with a custom Engine
and engine::Config to control stack sizing, fuel accounting, memory backends,
and trap-on-OOM behavior.
For more examples, see the examples directory.
§Imports
To provide imports to a module, you can use the Imports struct.
This struct allows you to register custom functions, globals, memories, tables,
and other modules to be linked into the module when it is instantiated.
See the Imports documentation for more information.
Re-exports§
pub use engine::Engine;pub use engine::LazyLinearMemory;pub use engine::LinearMemory;pub use engine::MemoryBackend;pub use engine::PagedMemory;pub use engine::StackConfig;pub use engine::VecMemory;
Modules§
- engine
- Global configuration for the WebAssembly interpreter
- parser
parser - Re-export of
tinywasm_parser. Requiresparserfeature. - types
- Re-export of
tinywasm_types.
Structs§
- Extern
Name - Name of an import
- Func
Context - The context of a host-function call
- Func
Execution - Resumable execution for an untyped function call.
- Func
Execution Typed - Resumable execution for a typed function call.
- Function
- A function handle
- Function
Typed - A typed function handle
- Global
- A global instance in a store.
- Host
Function - A host function
- Imports
- Imports for a module instance
- Lazy
Linear Memory - A linear memory wrapper that materializes its inner backend on first access.
- Memory
- A memory instance in a store.
- Memory
Backend - Configures how runtime memory instances are created.
- Memory
Cursor std - A cursor over a
Memoryinstance. - Module
- A
TinyWasmWebAssembly Module - Module
Instance - An instantiated WebAssembly module
- Paged
Memory - A sparse chunked linear memory.
- Store
- Global state that can be manipulated by WebAssembly programs
- Table
- A table instance in a store.
- VecMemory
- A contiguous
Vec<u8>-backed linear memory. - Wasm
Tuple Chain - A helper type for using tuples of arbitrary number of elements as function parameters or results, by concatenating the Wasm types of each element.
Enums§
- Error
- Errors that can occur for
TinyWasmoperations - Exec
Progress - Progress for fuel-limited function execution.
- Extern
- An external import value.
- Extern
Item - A typed view over an exported extern value.
- Linking
Error - Errors that can occur when linking a WebAssembly module
- Parse
Error parser - Errors that can occur when parsing a WebAssembly module
- Trap
- A WebAssembly trap
Traits§
- Linear
Memory - Backend storage for a linear memory
- ToWasm
Types - Describes the WebAssembly value types produced by a Rust value or tuple shape.
Functions§
- parse_
bytes parser - Parse a module from bytes
- parse_
file parserandstd - Parse a module from a file. Requires the
stdfeature. - parse_
stream parserandstd - Parse a module from a stream. Requires
parserandstdfeatures.
Type Aliases§
- Result
- A wrapper around
core::result::Resultfor tinywasm operations