watr npm bundle size npm test

Light & fast WAT compiler

docs  ·  demo

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
const { add, log } = watr`
(func (export "add") (param i32 i32) (result i32) (i32.add (local.get 0) (local.get 1)))
(func (export "log") (param f64) (call ${console.log} (local.get 0)))
`
add(2, 3) // 5
log(42) // logs 42

CLI

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 → MVP

Metrics

Used by