Program examples
Complete, small WAST modules. Each shows a feature in context rather than as an isolated expression, because most of WAST's sharp edges only appear when declarations, control flow, and lowering interact.
How to read an example
Every page has the same shape:
- The module, with the non-obvious decisions marked as
//comments on the lines they apply to. Read the code first; the comments are the annotations you would want from someone who already debugged it. (WAST has line comments only —/* */is a syntax error.) - Prose explaining why the language behaves that way: what the WebAssembly-level constraint is, what breaks under the alternative design, and what the choice costs you.
- Expected result, and links to the governing rules.
The pages are explanatory. The authoritative specification is the source of truth for grammar, type rules, feature gates, and required lowering; where a page and the spec disagree, the spec wins. For syntax lookup without the commentary, use the cheatsheet. For things that do not work yet, see limitations.
Each module below is editable and compiles in the browser. Changing an example and watching the emitted WAT change is usually faster than reading about it.
Running them yourself
Most examples export a zero-argument check() -> i32: 0 means the program observed the expected behavior, and a nonzero value identifies which unexpected path it took.
wastc build program.wast -o program.wat
wasmtime run --invoke check program.wat
The repository's black-box test harness uses the same convention. Note that a module compiling and passing wasm-tools validate proves only that it is well-formed WebAssembly, not that it means what the source says — for anything touching control flow or evaluation order, run it.
Choose a program
- Counter and globals — mutable globals, and why identifier case decides which namespace a name resolves in.
- Range sum and structured loops —
foras sugar over a counter, versus a labelled loop carrying state as a block parameter. - GC narrowing —
istests, why they only narrow immutable locals, and thebr_on_castlowering. - Mutable arrays — nominal GC array types and indexed access.
- Raw WebAssembly operations — the
wasm.<opcode>(...)escape hatch and the opcode catalog. - Linear memory and function tables — byte-addressed memory versus reference tables, both named explicitly.
- WASI Preview 1 entry point — imports,
start, and process exit.