Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/dy/watr

Light & fast wasm text compiler/formatter
https://github.com/dy/watr

minify minify-wat pretty-print wabt wasm wasm-text wasm2wat wast wat wat2wasm webassembly

Last synced: 27 days ago
JSON representation

Light & fast wasm text compiler/formatter

Awesome Lists containing this project

README

        

# watr [![test](https://github.com/audio-lab/watr/actions/workflows/test.js.yml/badge.svg)](https://github.com/audio-lab/watr/actions/workflows/test.js.yml) [![npm bundle size](https://img.shields.io/bundlephobia/minzip/watr/latest?color=brightgreen&label=gzip)](https://bundlephobia.com/package/watr) [![npm](https://img.shields.io/npm/v/watr?color=red)](https://npmjs.org/watr)

Bare minimum wasm text compiler/formatter. A light & fast alternative to [wat2wasm](https://github.com/AssemblyScript/wabt.js).

Useful for hi-level languages or dynamic (in-browser) compilation.

## Usage

### Compile

Compile wasm text or syntax tree into wasm binary.

```js
import compile from 'watr' // or `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
```

### Print

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)))
```

### 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]]
// ]
```

## Status

* [x] core
* [x] [multi-value](https://github.com/WebAssembly/spec/blob/master/proposals/multi-value/Overview.md)
* [x] [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)
* [x] [extended const](https://github.com/WebAssembly/extended-const/blob/main/proposals/extended-const/Overview.md)
* [ ] [multiple memories](https://github.com/WebAssembly/multi-memory/blob/master/proposals/multi-memory/Overview.md)
* [ ] [func refs](https://github.com/WebAssembly/function-references/blob/main/proposals/function-references/Overview.md)
* [ ] [gc](https://github.com/WebAssembly/gc)

## Alternatives

  | Size (gzipped) | Performance (op/s)
---|---|---
watr | 5 kb | 6000
[wat-compiler](https://github.com/stagas/wat-compiler) | 6 kb | 348
[wabt](https://github.com/AssemblyScript/wabt.js) | 300 kb | 574

## Useful links

* [watlings](https://github.com/EmNudge/watlings) – learn Wasm text by examples.
* [MDN: control flow](https://developer.mozilla.org/en-US/docs/WebAssembly/Reference/Control_flow)
* [WASM reference manual](https://github.com/sunfishcode/wasm-reference-manual/blob/master/WebAssembly.md#loop)
* [WASM binary encoding](https://github.com/WebAssembly/design/blob/main/BinaryEncoding.md)

🕉