/wasm-by-hand

Small cookbook examples of writing WebAssembly by hand in .wast format

Primary LanguageTypeScriptMIT LicenseMIT

WebAssembly by Hand

A little experiment with hand writing WebAssembly using s-expression syntax (.wat).

The project is organized as a test suite that demonstrates different aspects of the WebAssembly syntax.

Run the test suite with annotated examples

git clone https://github.com/rhmoller/wasm-by-hand.git
cd wasm-by-hand
npm install
npm test

And you should see a report like this

PASS  src/fizzbuzz.test.ts
 ✓ Prints fizzbuzz (139ms)

PASS  src/control-flow.test.ts
 control flow
   ✓ loops from 0 to 9 (4ms)
   ✓ counts from 0 to 5 (6ms)
   ✓ demonstrates if-then-else (2ms)

PASS  src/wat-tag.test.ts
 wat-tag
   ✓ Compiles an empty module (65ms)
   ✓ Compiles the square function (81ms)
   ✓ counts to 5 (73ms)

PASS  src/wasm-by-hand.test.ts
 ✓ compiles empty module (10ms)
 ✓ squares the input number (20ms)
 ✓ counter increments number for each invocation (21ms)
 ✓ exports the global constant (7ms)
 ✓ Writes a string to memory (7ms)

Learn more about WebAssembly

Mozilla has a great introduction to the format here Understanding WebAssembly text format

References