2
2
3
3
> Light & fast WAT compiler.
4
4
5
- Provides bare minimum WAT to WASM compilation without unnecessary syntax complexities (see [ limitations ] ( #limitations ) ) .<br />
5
+ Provides bare minimum WAT to WASM compilation.<br />
6
6
Useful as WASM API layer, eg. for hi-level languages or for dynamic (in-browser?) compilation.
7
7
<!-- , eg. [sonl](https://github.com/audio-lab/sonl). -->
8
8
@@ -41,9 +41,26 @@ const {double} = instance.exports
41
41
double (108 ) // 216
42
42
```
43
43
44
- ## Compiler
44
+ ## API
45
45
46
- WAT tree can be compiled directly, bypassing text parsing:
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).
47
64
48
65
```js
49
66
import { compile } from ' watr'
@@ -59,7 +76,42 @@ const {double} = instance.exports
59
76
double(108) // 216
60
77
```
61
78
79
+ ### Print (in progress)
80
+
81
+ Format input Wasm text string or tree into pretty or minified form (depending on config).
62
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
+ // pretty-print
92
+ const str = print(tree, {
93
+ indent: ' ' , // indentation characters
94
+ newline: ' \n' , // new line charactes
95
+ pad: 1, // pad start of each line
96
+ comments: true // keep comments
97
+ })
98
+ // (func (export "double")
99
+ // (param f64) (result f64)
100
+ // (f64.mul
101
+ // (local.get 0)
102
+ // (f64.const 2)))
103
+
104
+ // minify
105
+ const str = print(tree, {
106
+ indent: false,
107
+ newline: false,
108
+ pad: 0,
109
+ comments: false
110
+ })
111
+ // (func (export "double")(param f64)(result f64)(f64.mul (local.get 0)(f64.const 2)))
112
+ ```
113
+
114
+ <!--
63
115
## Limitations
64
116
65
117
Ambiguous syntax is prohibited in favor of explicit lispy notation. Each instruction must have prefix signature with parenthesized immediates and arguments.
92
144
```wast
93
145
(f32.const 0x1.fffffep+127) ;; ✘ floating HEX - not supported
94
146
```
95
-
96
- ``` wast
97
- (global.set $pc (;(i32.const <new-pc>);)) ;; ✘ fallthrough arguments
98
- (global.set $pc (i32.const 0)) ;; ✔ explicit arguments
99
- ```
147
+ -->
100
148
101
149
Inline style is supported for compatibility, but discrouraged.
102
150
It may also miss some edge cases and nice error messages.
@@ -110,19 +158,6 @@ For better REPL/dev experience use [wabt](https://github.com/AssemblyScript/wabt
110
158
* [WASM binary encoding](https://github.com/WebAssembly/design/blob/main/BinaryEncoding.md)
111
159
112
160
<!--
113
- Main goal is to get very fluent with wasm text.
114
-
115
- Experiments:
116
-
117
- * [x] global read/write use in function
118
- * [x] scopes: refer, goto
119
- * [x] stack: understanding named and full references
120
- * [x] memory: reading/writing global memory
121
- * [x] memory: creating arrays on the go
122
- * [x] memory: passing pointer to a function
123
- * [x] benchmark array setting agains js loop
124
- → it's faster almost twice
125
-
126
161
## Refs
127
162
128
163
* [mdn wasm text format](https://developer.mozilla.org/en-US/docs/WebAssembly/Understanding_the_text_format)
@@ -142,7 +177,7 @@ Experiments:
142
177
143
178
## Alternatives
144
179
145
- * [ wabt] ( https://www.npmjs.com/package/wabt ) − port of WABT for the web, industry standard.
180
+ * [wabt](https://www.npmjs.com/package/wabt) − port of WABT for the web, de-facto standard.
146
181
* [wat-compiler](https://www.npmjs.com/package/wat-compiler) − compact alternative for WABT, older brother of _watr_.
147
182
* [web49](https://github.com/FastVM/Web49)
148
183
0 commit comments