wasm.runtime #
WebAssembly runtime
wasm.runtime loads, validates, and executes WebAssembly binaries entirely in V. It has no dependency on Node.js, Wasmer, Wasmtime, or a native WebAssembly library.
Command line
./v -b wasm -o hello.wasm examples/hello_world.v
./v run hello.wasm
./v hello.wasm
./v -b wasm run examples/hello_world.v
./v run examples/wasm_codegen/add.v > add.wasm
./v wasm --invoke add add.wasm 20 22
The default entry point is _start. --invoke NAME calls a named export with scalar arguments and prints each result on its own line. Integers accept decimal and 0x notation. Options precede the file; arguments after the file belong to the guest. Guest proc_exit supplies the process exit code. Invalid binaries, unresolved imports, and execution traps produce a diagnostic and exit 1.
--fuel N limits instructions per call, including nested guest calls. The default is 100 million; --fuel 0 disables the instruction limit. The runner uses V's existing tool cache, so its first invocation compiles the interpreter with V. Running a binary never invokes another Wasm engine.
Embedding
import os
import wasm.runtime
fn main() {
module_ := runtime.load(os.read_bytes('add.wasm')!)!
mut vm := runtime.instantiate(module_, [], runtime.Config{})!
result := vm.call('add', [runtime.Value.i32(20), runtime.Value.i32(22)])!
assert result[0].as_i32() == 42
}
Load a module once and instantiate it repeatedly. Instances have independent memory, globals, tables, and segment state. Instantiation executes the binary's start section, if present; call('_start', []) is a separate operation. module_.exports lists exports and module_.signature(name) returns a function's parameter and result types. module_.imports() lists required function imports. get_global and set_global access exports.
Supply imports as typed V callbacks. Module and field names are matched separately. Argument and result types are checked at the host boundary; scalar values retain their exact bits.
import wasm.runtime
fn double(mut vm runtime.Instance, args []runtime.Value) ![]runtime.Value {
_ = vm
// A callback can also access guest memory through read_memory/write_memory.
return [runtime.Value.i32(args[0].as_i32() * 2)]
}
// Pass this list as instantiate's second argument.
imports := [runtime.HostFunction{
module: 'host'
name: 'double'
signature: runtime.Signature{ parameters: [.i32], results: [.i32] }
callback: double
}]
Guest addresses are offsets into linear memory, never host pointers. read_memory returns a checked copy; write_memory checks the entire destination range. Host callbacks are trusted V code and decide which resources the guest can access. Calls on one instance must be sequential; reentrant calls on the same instance return an error.
wasm.runtime.wasi.imports(argv) supplies the runner's WASI Preview 1 subset: fd_read, fd_write, proc_exit, argument access, and an empty environment. It exposes stdin/stdout/stderr, without filesystem or network access. fd_read skips empty buffers and returns the bytes read so far on EOF, including zero if stdin is already exhausted. proc_exit returns a wasi.Exit error to an embedding application without terminating the host process.
Execution and supported features
The interpreter decodes instructions and validates operand types once when loading. Structured branches become direct targets with known stack heights. Guest calls use an explicit stack; ordinary instructions and guest calls do not allocate argument or result arrays.
Supported features include scalar i32/i64/f32/f64 operations, structured control flow, multi-value blocks and returns, direct and indirect calls, mutable globals, one 32-bit linear memory, one funcref table, active/passive segments, bulk memory/table operations, sign extension, and saturating float conversions. Function imports are supported. Memory, table, and global imports, externref, SIMD, threads, exceptions, GC, memory64, and the component model are currently rejected explicitly.
Bounds violations, integer division by zero/overflow, invalid float conversions, null indirect calls, and unreachable return errors. Configurable limits bound memory, tables, operand values, call depth, and instruction fuel. Defaults allow 256 MiB of memory, one million table entries or stack values, and 1024 call frames. Fuel is renewed for each exported call and for instantiation's start function. It counts guest instructions, not time spent in host callbacks or bulk operations. Loading accepts binaries up to 64 MiB, with at most one million decoded instructions and locals across the module. Individual vectors and validation stacks also have bounded sizes.
After a trap, call-stack state is discarded, but guest memory and globals retain any writes that preceded it. A fresh instance restores initial state. V-generated modules may leave their own linear-memory stack pointer modified after a trap; recreate those instances before continuing.
Tests
./v -silent test vlib/wasm/runtime/
./v -silent cmd/tools/vwasm_test.v
The tests exercise generated modules and malformed binaries without an external Wasm runtime.
fn instantiate #
fn instantiate(mod &Module, imports []HostFunction, config Config) !Instance
instantiate links host functions, initializes memory and tables, and runs the start section.
fn load #
fn load(bytes []u8) !&Module
load decodes and validates a core WebAssembly binary, rejecting unsupported extensions.
fn Value.f32 #
fn Value.f32(value f32) Value
f32 creates an f32 value.
fn Value.f64 #
fn Value.f64(value f64) Value
f64 creates an f64 value.
fn Value.i32 #
fn Value.i32(value int) Value
i32 creates an i32 value.
fn Value.i64 #
fn Value.i64(value i64) Value
i64 creates an i64 value.
fn ValueType.from #
fn ValueType.from[W](input W) !ValueType
from returns the ValueType value of an integer or of a field name, or an error if there is none.
type Callback #
type Callback = fn (mut Instance, []Value) ![]Value
enum ValueType #
enum ValueType as u8 {
i32 = 0x7f
i64 = 0x7e
f32 = 0x7d
f64 = 0x7c
funcref = 0x70
}
struct Config #
struct Config {
pub:
max_memory_pages int = 4096
max_table_elements int = 1000000
max_stack_values int = 1000000
max_call_depth int = 1024
fuel u64 = 100000000
}
Config bounds guest resources. Fuel is renewed for each exported call; zero disables it.
struct Export #
struct Export {
pub:
name string
kind u8
index int
}
struct HostFunction #
struct HostFunction {
pub:
module string
name string
signature Signature
callback Callback = missing_callback
}
HostFunction supplies a typed function import. Names are matched separately.
struct Instance #
struct Instance {
mod &Module
config Config
mut:
hosts []Callback
globals []Global
memory []u8
table []u64
data [][]u8
elements [][]u64
remaining_fuel u64
running bool
}
Instance owns the mutable state of one module. Calls on one instance are sequential.
fn (Instance) read_memory #
fn (instance &Instance) read_memory(offset u32, size u32) ![]u8
read_memory returns a copy of a checked range of guest memory.
fn (Instance) write_memory #
fn (mut instance Instance) write_memory(offset u32, data []u8) !
write_memory copies bytes into a checked range of guest memory.
fn (Instance) get_global #
fn (instance &Instance) get_global(name string) !Value
get_global reads an exported global.
fn (Instance) set_global #
fn (mut instance Instance) set_global(name string, value Value) !
set_global writes a mutable exported global after checking its value type.
fn (Instance) call #
fn (mut instance Instance) call(name string, args []Value) ![]Value
call invokes an exported function, checking argument and result types at the host boundary.
struct Module #
struct Module {
pub:
exports []Export
mut:
types []Signature
functions []Function
import_count int
globals []Global
memory ?Limits
table ?Limits
data []Segment
elements []Segment
start int = -1
data_count int = -1
decoded_instructions int
}
Module is validated, decoded bytecode and can be instantiated repeatedly.
fn (Module) signature #
fn (mod &Module) signature(name string) !Signature
signature returns the type of an exported function.
fn (Module) imports #
fn (mod &Module) imports() []HostFunction
imports lists the function imports and their required signatures, without callbacks.
struct Signature #
struct Signature {
pub:
parameters []ValueType
results []ValueType
}
struct Value #
struct Value {
pub:
typ ValueType
bits u64
}
Value holds a WebAssembly scalar without losing integer or floating-point bits.
fn (Value) as_i32 #
fn (v Value) as_i32() int
as_i32 returns the signed i32 representation.
fn (Value) as_i64 #
fn (v Value) as_i64() i64
as_i64 returns the signed i64 representation.
fn (Value) as_f32 #
fn (v Value) as_f32() f32
as_f32 returns the f32 representation.
fn (Value) as_f64 #
fn (v Value) as_f64() f64
as_f64 returns the f64 representation.