A small WebAssembly runtime in C.
This is a small integer-only runtime intended for learning and experiments.
- WebAssembly binary format v1 only
- Supported sections: type / function / export / code
- Only the
i32value type is supported - Function results are limited to no result or one
i32result - Supported instructions:
i32.const, local get/set/tee, i32 arithmetic, comparisons, bit operations, shifts,call,return,drop,select,block,loop, resultlessif/else,br,br_if - Unsupported: import / memory / global / table / float / SIMD / multi-value / host functions
- This is not a strict validator; safely running malformed wasm is out of scope
make./wasmrun file.wasm [export] [i32 args...]When export is omitted, main is called. If main is not found, _start is tried.
Prebuilt .wasm files are included in examples, so you can run them directly.
./wasmrun examples/add.wasm add 40 2
# 42
./wasmrun examples/fact.wasm fact 5
# 120
./wasmrun examples/max.wasm max 3 -5
# 3
./wasmrun examples/max.wasm max -2 7
# 7After editing a .wat file, regenerate the matching .wasm with wat2wasm.
wat2wasm is part of WABT (The WebAssembly Binary Toolkit). Install it if needed:
# macOS / Homebrew
brew install wabt
# Debian / Ubuntu
sudo apt install wabtTo build WABT from source:
git clone --recursive https://github.com/WebAssembly/wabt
cd wabt
mkdir build
cd build
cmake ..
cmake --build .Regenerate individual examples:
wat2wasm examples/add.wat -o examples/add.wasm
wat2wasm examples/fact.wat -o examples/fact.wasm
wat2wasm examples/max.wat -o examples/max.wasmRegenerate all examples:
for f in examples/*.wat; do wat2wasm "$f" -o "${f%.wat}.wasm"; doneInclude wasmrun.h from another C program. The implementation is header-only.
#include "wasmrun.h"
Wasmrun m;
wasmrun_init(&m);
wasmrun_load(&m, wasm_bytes, wasm_size);
int32_t args[] = { 40, 2 };
int32_t result;
int has_result;
wasmrun_call_export(&m, "add", args, &result, &has_result);
wasmrun_free(&m);Compile:
cc -std=c99 -I. your_app.c -o your_appIncluded C sample:
cc -std=c99 -Wall -Wextra -O2 examples/simple.c -o simple
./simple
# 42