safx/zig-tiny-wasm-runtime
A tiny Web Assembly interpreter written in Zig. The interpreter supports Web Assembly Core Specification 2.0
A tiny WebAssembly interpreter written in Zig. The interpreter supports WebAssembly Core Specification 2.0 draft including SIMD operations. This project is intended for personal understanding of the WebAssembly specification. Do not use in production environment.
# Build the main interpreter
zig build
# Build the spec test runner
zig build spec_test
# Or use the Makefile
make build
make build-spec-test
# Run a WebAssembly binary file
./zig-out/bin/zig-wasm-interp somefile.wasm -r function_name -a 32
# Run with verbose output
./zig-out/bin/zig-wasm-interp somefile.wasm -v -r function_name -a i32:42 -a f64:3.14
# Use the spec test runner for text format files
./zig-out/bin/spec_test test.wast -v
This project now supports the new WebAssembly 2.0 test suite from spectec.
# Download and setup WebAssembly 2.0 test files
make setup-tests
# This will:
# - Clone the spectec repository
# - Copy compatible .wast test files to wasm_tests/
# - Filter out unsupported advanced features
# Run all WebAssembly spec tests
make test
# Run specific test file
python3 ./run_spectec_tests.py wasm_tests/address.wast
# Run with verbose output
python3 ./run_spectec_tests.py --verbose wasm_tests/
# Stop on first failure
python3 ./run_spectec_tests.py --failfast wasm_tests/
The codebase is organized into four main modules:
src/core/
): WebAssembly types, instructions, and basic structuressrc/decode/
): Binary format parsing and module loadingsrc/text_decode/
): Text format (.wast/.wat) parsingsrc/validate/
): WebAssembly validation rulessrc/runtime/
): Execution engine and interpreter# Development
make help # Show available commands
make build # Build the interpreter
make build-spec-test # Build spec test runner
make clean # Clean all build artifacts
# Testing
make setup-spectec # Clone/update spectec repository
make setup-tests # Setup WebAssembly 2.0 test files
make test # Run WebAssembly spec tests
make clean-tests # Remove test files
# Manual testing
zig build test # Run unit tests
zig fmt src/ # Format source code
This interpreter provides comprehensive WebAssembly 2.0 support with 223/223 test cases passing (100% pass rate):
Vector/SIMD Instructions
Bulk Memory Instructions
memory.copy
, memory.fill
, memory.init
, data.drop
Multi-Value Results
Reference Types
funcref
)externref
)ref.null
, ref.func
, ref.is_null
, ref.as_non_null
br_on_null
, br_on_non_null
)Non-Trapping Conversions
Sign Extension Instructions
Relaxed SIMD Extensions
Multi-Memory Support
Tail Calls
return_call
- direct tail callsreturn_call_indirect
- indirect tail calls return_call_ref
- reference-based tail callsAdvanced Type System
Exception Handling
throw
and throw_ref
instructionstry_table
structured exception handlingMemory64
Only one advanced WebAssembly feature is not yet supported:
This is an educational project for understanding WebAssembly internals. Feel free to explore and learn from the code structure.