Light & fast WASM compiler. An alternative to wabt/wat2wasm.
Useful for high-level languages or dynamic (in-browser) compilation.
Supports complete standard text syntax and official testsuite.
Compile wasm text or syntax tree into wasm binary.
import { compile } from 'watr'
const buffer = compile(`(func (export "double")
(param f64) (result f64)
(f64.mul (local.get 0) (f64.const 2))
)`)
const module = new WebAssembly.Module(buffer)
const instance = new WebAssembly.Instance(module)
const {double} = instance.exports
double(108) // 216
Parse input wasm text into syntax tree.
import { parse } from 'watr'
parse(`(func (export "double") (param f64) (result f64) (f64.mul (local.get 0) (f64.const 2)))`)
// [
// 'func', ['export', '"double"'], ['param', 'f64'], ['result', 'f64'],
// ['f64.mul', ['local.get', 0], ['f64.const', 2]]
// ]
Format input wasm text or syntax tree into minified or pretty form.
import { print } from 'watr'
const src = `(func (export "double")
(param f64) (result f64)
(f64.mul (local.get 0) (f64.const 2))
)`
// pretty-print (default)
print(src, {
indent: ' ',
newline: '\n',
})
// (func (export "double")
// (param f64) (result f64)
// (f64.mul
// (local.get 0)
// (f64.const 2)))
// minify
print(src, {
indent: false,
newline: false
})
// (func (export "double")(param f64)(result f64)(f64.mul (local.get 0)(f64.const 2)))
- core
- mutable_globals
- extended const
- sat_float_to_int
- sign_extension
- multi-value
- bulk memory ops
- memory64
- multiple memories
- simd
- relaxed_simd
- ref types
- func refs
- gc
- exceptions
- threads
- tail_call
- annotations
- code_metadata
Size (gzipped) | Performance (op/s) | |
---|---|---|
watr | 5 kb | 6000 |
wabt | 300 kb | 574 |
wat-compiler | 6 kb | 348 |