2.2.5 • Published 8 months ago

watr v2.2.5

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

watr test npm bundle size

Bare minimum wasm text compiler & formatter, light & fast alternative for wat2wasm. Useful for hi-level languages or dynamic (in-browser) compilation.

 Size (gzipped)Performance (op/s)
watr3.8 kb6000
wat-compiler6 kb348
wabt300 kb574

Usage

import wat from 'watr'

// compile text to binary
const buffer = wat(`(func
  (export "double") (param f64) (result f64)
  (f64.mul (local.get 0) (f64.const 2))
)`)

// create instance
const module = new WebAssembly.Module(buffer)
const instance = new WebAssembly.Instance(module)

// use API
const {double} = instance.exports
double(108) // 216

API

Compile

Compiles 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

Print

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

Parse

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

Status

  • wasm core
  • multiple values
  • bulk memory ops (0 index)
  • func/ref types
  • multiple memories

Useful links

Alternatives

1.4.1

9 months ago

1.3.2

9 months ago

1.4.0

9 months ago

1.3.1

9 months ago

1.3.0

9 months ago

2.2.1

8 months ago

2.1.2

8 months ago

2.2.0

8 months ago

2.1.1

8 months ago

2.2.3

8 months ago

2.1.4

8 months ago

2.2.2

8 months ago

2.1.3

8 months ago

2.2.5

8 months ago

2.2.4

8 months ago

2.1.0

8 months ago

2.0.0

8 months ago

1.2.0

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago

0.0.0

2 years ago