Light & fast WAT compiler: docs, repl
Useful for backends, JIT compilers, DSLs, code generators, dev tools.
import watr, { compile, parse, print } from 'watr'
// instant wasm function
const { add } = watr`(func (export "add") (param i32 i32) (result i32)
(i32.add (local.get 0) (local.get 1))
)`
add(2, 3) // 5
// auto-import functions
const { test } = watr`(func (export "test") (call ${console.log} (i32.const 42)))`
test() // logs 42
// interpolate values
const { pi } = watr`(global (export "pi") f64 (f64.const ${Math.PI}))`
// compile to binary
const binary = compile(`(func (export "f") (result f64) (f64.const 1))`)
const module = new WebAssembly.Module(binary)
const { f } = new WebAssembly.Instance(module).exports
// parse / print
parse('(i32.const 42)') // ['i32.const', 42]
print('(module(func(result i32)i32.const 42))') // (module\n (func (result i32)\n ...
npx watr input.wat # → input.wasm
npx watr input.wat -o out.wasm # custom output
npx watr input.wat --print # pretty-print
npx watr input.wat --minify # minify
| Size | Speed | |
|---|---|---|
| watr | 10 KB | 4,460 op/s |
| spec/wast.js | 216 KB | 2,185 op/s |
| wabt | 282 KB | 1,273 op/s |
| binaryen | 1,100 KB | 718 op/s |
| wat-compiler | 7.7 KB (MVP) | 539 op/s |