UNPKG

watr

Version:

Light & fast WAT compiler – WebAssembly Text to binary, parse, print, transform

72 lines (48 loc) 2.65 kB
# <img src="./watr.svg" height="16"> watr [![npm](https://img.shields.io/npm/v/watr?color=white)](https://npmjs.org/watr) [![test](https://github.com/dy/watr/actions/workflows/test.js.yml/badge.svg)](https://github.com/dy/watr/actions/workflows/test.js.yml) _Light & fast WAT compiler_ * [feature](https://webassembly.org/features/?categories=browsers%2Cstandalones%2Ctools) & [spec](https://webassembly.github.io/spec/core/text/index.html)-complete, zero deps * [compile](./docs.md#compilesource-options) · [polyfill](./docs.md#polyfillast-options) · [optimize](./docs.md#optimizeast-options) · [parse](./docs.md#parsesource-options) · [prettify](./docs.md#printtree-options) · [minify](./docs.md#printtree-options) * instant wasm, JS interop, CLI, clear errors **[docs](./docs.md)**  **·**  **[demo](https://dy.github.io/watr/play/)** <!-- _Use for_: backends, compilers, DSLs, codegen, dev tools --> ## Usage ```js 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 42 ``` ## CLI ```sh 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, inline, coalesce, … npx watr input.wat --polyfill # newer features → MVP ``` ## Metrics * **watr** — ~84 KB minified (~24 KB gzipped), 4,460 op/s * [spec/wast.js](https://github.com/WebAssembly/spec) — 216 KB, 2,185 op/s * [wabt](https://github.com/WebAssembly/wabt) — 282 KB, 1,273 op/s * [binaryen](https://github.com/WebAssembly/binaryen) — 1,100 KB, 718 op/s * [wat-compiler](https://github.com/stagas/wat-compiler) — ~152 KB (+ wabt dep), 539 op/s ## Used by * [jz](https://github.com/dy/jz) – minimal static JS subset <p align="center"><a href="https://krishnized.github.io/license">ॐ</a></p>