https://github.com/dy/watr
Light & fast wasm text compiler
https://github.com/dy/watr
minify minify-wat pretty-print wabt wasm wasm-text wasm2wat wast wat wat2wasm webassembly
Last synced: 2 months ago
JSON representation
Light & fast wasm text compiler
- Host: GitHub
- URL: https://github.com/dy/watr
- Owner: dy
- Created: 2022-02-06T18:23:46.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-03-10T01:29:19.000Z (2 months ago)
- Last Synced: 2025-03-10T02:46:37.978Z (2 months ago)
- Topics: minify, minify-wat, pretty-print, wabt, wasm, wasm-text, wasm2wat, wast, wat, wat2wasm, webassembly
- Language: WebAssembly
- Homepage: https://dy.github.io/watr/docs/repl
- Size: 3.75 MB
- Stars: 35
- Watchers: 4
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# watr [](https://github.com/audio-lab/watr/actions/workflows/test.js.yml) [](https://bundlephobia.com/package/watr) [](https://npmjs.org/watr)
Light & fast WAT compiler.
Useful for high-level languages or dynamic (in-browser) compilation.
Supports full [spec text syntax](https://webassembly.github.io/spec/core/text/index.html) and [official testsuite](https://github.com/WebAssembly/testsuite).**[REPL](https://dy.github.io/watr/docs/repl)**
## Usage
### Compile
Compile wasm text or syntax tree into wasm binary.
```js
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.exportsdouble(108) // 216
```### Parse
Parse input wasm text into syntax tree.
```js
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.
```js
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)))
```## Status
* [x] core
* [x] [mutable globals](https://github.com/WebAssembly/mutable-global), [extended const](https://github.com/WebAssembly/extended-const/blob/main/proposals/extended-const/Overview.md), [nontrapping float to int](https://github.com/WebAssembly/nontrapping-float-to-int-conversions), [sign extension](https://github.com/WebAssembly/sign-extension-ops)
* [x] [multi-value](https://github.com/WebAssembly/spec/blob/master/proposals/multi-value/Overview.md), [bulk memory ops](https://github.com/WebAssembly/bulk-memory-operations/blob/master/proposals/bulk-memory-operations/Overview.md)
* [x] [simd](https://github.com/WebAssembly/simd/blob/master/proposals/simd/SIMD.md), [relaxed simd](https://github.com/WebAssembly/relaxed-simd), [fixed-width simd](https://github.com/WebAssembly/simd/blob/master/proposals/simd/SIMD.md)
* [x] [tail_call](https://github.com/WebAssembly/tail-call)
* [x] [ref types](https://github.com/WebAssembly/reference-types/blob/master/proposals/reference-types/Overview.md), [func refs](https://github.com/WebAssembly/function-references/blob/main/proposals/function-references/Overview.md), [gc](https://github.com/WebAssembly/gc)
* [ ] [exceptions](https://github.com/WebAssembly/exception-handling), [annotations](https://github.com/WebAssembly/annotations), [code_metadata](https://github.com/WebAssembly/tool-conventions/blob/main/CodeMetadata.md)
* [ ] [multiple memories](https://github.com/WebAssembly/multi-memory/blob/master/proposals/multi-memory/Overview.md), [memory64](https://github.com/WebAssembly/memory64)
* [ ] [js strings](https://github.com/WebAssembly/js-string-builtins/blob/main/proposals/js-string-builtins/Overview.md)
* [ ] wide arithmetic, threads, custom page size,
* [ ] wasm 3## Alternatives
| Size (gzipped) | Performance
---|---|---
watr | 7.5 kb | 6.0 op/s
[spec/wast.js](https://github.com/WebAssembly/spec/tree/main/interpreter#javascript-library) | 216 kb | 2.2 op/s
[wabt](https://github.com/WebAssembly/wabt) | 282 kb | 1.2 op/s
[wat-compiler](https://github.com/stagas/wat-compiler) | 7.7 kb | 0.7 op/s