watr 1.2.0 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/readme.md +183 -151
- package/watr.js +564 -564
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -1,151 +1,183 @@
|
|
|
1
|
-
# watr [](https://github.com/audio-lab/watr/actions/workflows/test.js.yml)
|
|
2
|
-
|
|
3
|
-
> Light & fast WAT compiler.
|
|
4
|
-
|
|
5
|
-
Provides bare minimum WAT to WASM compilation
|
|
6
|
-
Useful as WASM API layer for hi-level languages
|
|
7
|
-
<!--, eg. [sonl](https://github.com/audio-lab/sonl). -->
|
|
8
|
-
|
|
9
|
-
<!-- See [REPL](https://audio-lab.github.io/watr/repl.html).-->
|
|
10
|
-
|
|
11
|
-
<!--
|
|
12
|
-
| watr | wat-compiler | wabt
|
|
13
|
-
---|---|---|---
|
|
14
|
-
Size (gzipped) | 2.8kb | 6kb | 300kb
|
|
15
|
-
Performance (op/s) | 45000 | 2500 | 3100
|
|
16
|
-
-->
|
|
17
|
-
|
|
18
|
-
| Size (gzipped) | Performance (op/s)
|
|
19
|
-
---|---|---
|
|
20
|
-
watr | 3.8 kb | 1900
|
|
21
|
-
wat-compiler | 6 kb | 135
|
|
22
|
-
wabt | 300 kb | 250
|
|
23
|
-
|
|
24
|
-
## Usage
|
|
25
|
-
|
|
26
|
-
```js
|
|
27
|
-
import wat from 'watr'
|
|
28
|
-
|
|
29
|
-
// compile text to binary
|
|
30
|
-
const buffer = wat(`(func
|
|
31
|
-
(export "double") (param f64) (result f64)
|
|
32
|
-
(f64.mul (local.get 0) (f64.const 2))
|
|
33
|
-
)`)
|
|
34
|
-
|
|
35
|
-
// create instance
|
|
36
|
-
const module = new WebAssembly.Module(buffer)
|
|
37
|
-
const instance = new WebAssembly.Instance(module)
|
|
38
|
-
|
|
39
|
-
// use API
|
|
40
|
-
const {double} = instance.exports
|
|
41
|
-
double(108) // 216
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
##
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
```
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
)
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
-->
|
|
150
|
-
|
|
151
|
-
|
|
1
|
+
# watr [](https://github.com/audio-lab/watr/actions/workflows/test.js.yml)
|
|
2
|
+
|
|
3
|
+
> Light & fast WAT compiler.
|
|
4
|
+
|
|
5
|
+
Provides bare minimum WAT to WASM compilation.<br/>
|
|
6
|
+
Useful as WASM API layer, eg. for hi-level languages or for dynamic (in-browser?) compilation.
|
|
7
|
+
<!--, eg. [sonl](https://github.com/audio-lab/sonl). -->
|
|
8
|
+
|
|
9
|
+
<!-- See [REPL](https://audio-lab.github.io/watr/repl.html).-->
|
|
10
|
+
|
|
11
|
+
<!--
|
|
12
|
+
| watr | wat-compiler | wabt
|
|
13
|
+
---|---|---|---
|
|
14
|
+
Size (gzipped) | 2.8kb | 6kb | 300kb
|
|
15
|
+
Performance (op/s) | 45000 | 2500 | 3100
|
|
16
|
+
-->
|
|
17
|
+
|
|
18
|
+
| Size (gzipped) | Performance (op/s)
|
|
19
|
+
---|---|---
|
|
20
|
+
watr | 3.8 kb | 1900
|
|
21
|
+
[wat-compiler](https://github.com/stagas/wat-compiler) | 6 kb | 135
|
|
22
|
+
[wabt](https://github.com/AssemblyScript/wabt.js) | 300 kb | 250
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
```js
|
|
27
|
+
import wat from 'watr'
|
|
28
|
+
|
|
29
|
+
// compile text to binary
|
|
30
|
+
const buffer = wat(`(func
|
|
31
|
+
(export "double") (param f64) (result f64)
|
|
32
|
+
(f64.mul (local.get 0) (f64.const 2))
|
|
33
|
+
)`)
|
|
34
|
+
|
|
35
|
+
// create instance
|
|
36
|
+
const module = new WebAssembly.Module(buffer)
|
|
37
|
+
const instance = new WebAssembly.Instance(module)
|
|
38
|
+
|
|
39
|
+
// use API
|
|
40
|
+
const {double} = instance.exports
|
|
41
|
+
double(108) // 216
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## API
|
|
45
|
+
|
|
46
|
+
### Parse
|
|
47
|
+
|
|
48
|
+
Parser converts input Wasm text string to syntax tree.
|
|
49
|
+
|
|
50
|
+
```js
|
|
51
|
+
import { parse } from 'watr
|
|
52
|
+
|
|
53
|
+
parse(`(func (export "double") (param f64) (result f64) (f64.mul (local.get 0) (f64.const 2)))`)
|
|
54
|
+
|
|
55
|
+
// [
|
|
56
|
+
// 'func', ['export', '"double"'], ['param', 'f64'], ['result', 'f64'],
|
|
57
|
+
// ['f64.mul', ['local.get', 0], ['f64.const', 2]]
|
|
58
|
+
// ]
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Compile
|
|
62
|
+
|
|
63
|
+
Compiles Wasm tree or text into wasm binary. Lightweight alternative to [wabt/wat2wasm](https://github.com/WebAssembly/wabt).
|
|
64
|
+
|
|
65
|
+
```js
|
|
66
|
+
import { compile } from 'watr'
|
|
67
|
+
|
|
68
|
+
const buffer = compile([
|
|
69
|
+
'func', ['export', '"double"'], ['param', 'f64'], ['result', 'f64'],
|
|
70
|
+
['f64.mul', ['local.get', 0], ['f64.const', 2]]
|
|
71
|
+
])
|
|
72
|
+
const module = new WebAssembly.Module(buffer)
|
|
73
|
+
const instance = new WebAssembly.Instance(module)
|
|
74
|
+
const {double} = instance.exports
|
|
75
|
+
|
|
76
|
+
double(108) // 216
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Print
|
|
80
|
+
|
|
81
|
+
Format input Wasm text string or tree into minified or pretty form.
|
|
82
|
+
|
|
83
|
+
```js
|
|
84
|
+
import { print } from 'watr'
|
|
85
|
+
|
|
86
|
+
const tree = [
|
|
87
|
+
'func', ['export', '"double"'], ['param', 'f64'], ['result', 'f64'],
|
|
88
|
+
['f64.mul', ['local.get', 0], ['f64.const', 2]]
|
|
89
|
+
]
|
|
90
|
+
|
|
91
|
+
// minify (default)
|
|
92
|
+
const str = print(tree, {
|
|
93
|
+
indent: false,
|
|
94
|
+
newline: false,
|
|
95
|
+
pad: false
|
|
96
|
+
})
|
|
97
|
+
// (func (export "double")(param f64)(result f64)(f64.mul (local.get 0)(f64.const 2)))
|
|
98
|
+
|
|
99
|
+
// pretty-print
|
|
100
|
+
const str = print(tree, {
|
|
101
|
+
indent: ' ', // indentation step
|
|
102
|
+
newline: '\n', // new line
|
|
103
|
+
pad: '', // pad each newline with chars
|
|
104
|
+
})
|
|
105
|
+
// (func (export "double")
|
|
106
|
+
// (param f64) (result f64)
|
|
107
|
+
// (f64.mul
|
|
108
|
+
// (local.get 0)
|
|
109
|
+
// (f64.const 2)))
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
<!--
|
|
113
|
+
## Limitations
|
|
114
|
+
|
|
115
|
+
It may miss some edge cases and nice error messages.
|
|
116
|
+
For better REPL/dev experience use [wabt](https://github.com/AssemblyScript/wabt.js).
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
Ambiguous syntax is prohibited in favor of explicit lispy notation. Each instruction must have prefix signature with parenthesized immediates and arguments.
|
|
120
|
+
|
|
121
|
+
```wast
|
|
122
|
+
(func (result i32)
|
|
123
|
+
i32.const 1 ;; ✘ stacked arguments
|
|
124
|
+
drop
|
|
125
|
+
i32.const 0
|
|
126
|
+
i32.load offset=0 align=4 ;; ✘ ungrouped immediates
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
(func (result i32)
|
|
130
|
+
(drop (i32.const 1)) ;; ✔ nested arguments
|
|
131
|
+
(i32.load offset=0 align=4 (i32.const 0)) ;; ✔ grouped immediates
|
|
132
|
+
)
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
```wast
|
|
136
|
+
(local.get 0) ;; ✘ stacked argument
|
|
137
|
+
if (result i32) ;; ✘ inline instruction
|
|
138
|
+
(i32.const 1)
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
(if (result i32) (local.get 0) ;; ✔ explicit signature
|
|
142
|
+
(i32.const 1)
|
|
143
|
+
)
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
```wast
|
|
147
|
+
(f32.const 0x1.fffffep+127) ;; ✘ floating HEX - not supported
|
|
148
|
+
```
|
|
149
|
+
-->
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
## Useful links
|
|
154
|
+
|
|
155
|
+
* [MDN: control flow](https://developer.mozilla.org/en-US/docs/WebAssembly/Reference/Control_flow)
|
|
156
|
+
* [WASM reference manual](https://github.com/sunfishcode/wasm-reference-manual/blob/master/WebAssembly.md#loop)
|
|
157
|
+
* [WASM binary encoding](https://github.com/WebAssembly/design/blob/main/BinaryEncoding.md)
|
|
158
|
+
|
|
159
|
+
<!--
|
|
160
|
+
## Refs
|
|
161
|
+
|
|
162
|
+
* [mdn wasm text format](https://developer.mozilla.org/en-US/docs/WebAssembly/Understanding_the_text_format)
|
|
163
|
+
* [wasm reference manual](https://github.com/sunfishcode/wasm-reference-manual/blob/master/WebAssembly.md)
|
|
164
|
+
* [wabt source search](https://github.com/WebAssembly/wabt/search?p=5&q=then)
|
|
165
|
+
* [wat control flow](https://developer.mozilla.org/en-US/docs/WebAssembly/Reference/Control_flow)
|
|
166
|
+
* [ontouchstart wasm book](https://ontouchstart.pages.dev/chapter_wasm_binary)
|
|
167
|
+
* [wat-compiler](https://github.com/stagas/wat-compiler/)
|
|
168
|
+
* [hackernoon](https://web.archive.org/web/20210215171830/https://hackernoon.com/webassembly-binary-format-explained-part-2-hj1t33yp?source=rss)
|
|
169
|
+
* [webassemblyjs](https://github.com/xtuc/webassemblyjs)
|
|
170
|
+
* [chasm](https://github.com/ColinEberhardt/chasm/blob/master/src/encoding.ts)
|
|
171
|
+
* [WebBS](https://github.com/j-s-n/WebBS)
|
|
172
|
+
* [leb128a](https://github.com/minhducsun2002/leb128/blob/master/src/index.ts)
|
|
173
|
+
* [leb128b](https://github.com/shmishtopher/wasm-LEB128/tree/master/esm)
|
|
174
|
+
|
|
175
|
+
-->
|
|
176
|
+
|
|
177
|
+
## Alternatives
|
|
178
|
+
|
|
179
|
+
* [wabt](https://www.npmjs.com/package/wabt) − port of WABT for the web, de-facto standard.
|
|
180
|
+
* [wat-compiler](https://www.npmjs.com/package/wat-compiler) − compact alternative for WABT, older brother of _watr_.
|
|
181
|
+
* [web49](https://github.com/FastVM/Web49)
|
|
182
|
+
|
|
183
|
+
<p align=center><a href="https://github.com/krsnzd/license/">🕉</a></p>
|