watr
v4.3.0
Published
Light & fast WAT compiler – WebAssembly Text to binary, parse, print, transform
Maintainers
Readme
watr

Light & fast WAT compiler
- feature & spec-complete, zero deps
- compile · polyfill · optimize · parse · prettify · minify
- instant wasm, JS interop, CLI, clear errors
Usage
import watr, { compile, parse, print } from 'watr'
// compile to binary
const binary = compile('(func (export "f") (result f64) (f64.const 1))', {
polyfill: false, // transform newer features to MVP
optimize: true // fold constants, treeshake, eliminate dead code ...
})
const module = new WebAssembly.Module(binary)
const { f } = new WebAssembly.Instance(module).exports
// parse
parse('(i32.const 42)') // ['i32.const', 42]
// print
print('(module(func(result i32)i32.const 42))') // (module\n (func (result i32)\n ...
// 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
// instant wasm: interpolate, auto-import ...
const { test } = watr`(func (export "test") (call ${console.log} (i32.const 42)))`
test() // logs 42CLI
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
npx watr input.wat --optimize # fold, treeshake, deadcode
npx watr input.wat --polyfill # newer features → MVPMetrics
- 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
Used by
- jz – minimal static JS subset
