watr 2.1.0 → 2.1.2
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 +6 -4
- package/src/compile.js +21 -16
- package/src/parse.js +6 -0
- package/src/print.js +9 -0
- package/watr.js +6 -0
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
# watr [](https://github.com/audio-lab/watr/actions/workflows/test.js.yml) [](https://bundlephobia.com/package/watr)
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
Light & fast alternative for [wat2wasm](https://github.com/AssemblyScript/wabt.js), useful for hi-level languages or dynamic (in-browser) compilation.<br>
|
|
3
|
+
Bare minimum wasm text compiler & formatter, light & fast alternative for [wat2wasm](https://github.com/AssemblyScript/wabt.js).<br/>
|
|
4
|
+
Useful for hi-level languages or dynamic (in-browser) compilation.<br>
|
|
6
5
|
|
|
7
6
|
<!-- See [REPL](https://audio-lab.github.io/watr/repl.html).-->
|
|
8
7
|
|
|
9
8
|
| Size (gzipped) | Performance (op/s)
|
|
10
9
|
---|---|---
|
|
11
|
-
watr | 3.8 kb |
|
|
10
|
+
watr | 3.8 kb | 6250
|
|
12
11
|
[wat-compiler](https://github.com/stagas/wat-compiler) | 6 kb | 348
|
|
13
12
|
[wabt](https://github.com/AssemblyScript/wabt.js) | 300 kb | 574
|
|
14
13
|
|
|
@@ -101,12 +100,15 @@ parse(`(func (export "double") (param f64) (result f64) (f64.mul (local.get 0) (
|
|
|
101
100
|
|
|
102
101
|
No floating HEX support, eg. `(f32.const 0x1.fffffep+127)`.
|
|
103
102
|
|
|
103
|
+
<!--
|
|
104
104
|
## Projects using watr
|
|
105
105
|
|
|
106
106
|
* [auro](https://github.com/audio-lab/auro) – audio processing language
|
|
107
|
+
-->
|
|
107
108
|
|
|
108
109
|
## Useful links
|
|
109
110
|
|
|
111
|
+
* [watlings](https://github.com/EmNudge/watlings) – learn Wasm text by examples.
|
|
110
112
|
* [MDN: control flow](https://developer.mozilla.org/en-US/docs/WebAssembly/Reference/Control_flow)
|
|
111
113
|
* [WASM reference manual](https://github.com/sunfishcode/wasm-reference-manual/blob/master/WebAssembly.md#loop)
|
|
112
114
|
* [WASM binary encoding](https://github.com/WebAssembly/design/blob/main/BinaryEncoding.md)
|
package/src/compile.js
CHANGED
|
@@ -4,7 +4,12 @@ import parse from './parse.js'
|
|
|
4
4
|
|
|
5
5
|
const OP_END = 0xb, OP_I32_CONST = 0x41, OP_I64_CONST = 0x42, OP_F32_CONST = 0x43, OP_F64_CONST = 0x44
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Converts a WebAssembly Text Format (WAT) tree to a WebAssembly binary format (Wasm).
|
|
9
|
+
*
|
|
10
|
+
* @param {string|Array} nodes - The WAT tree or string to be compiled to Wasm binary.
|
|
11
|
+
* @returns {Uint8Array} The compiled Wasm binary data.
|
|
12
|
+
*/
|
|
8
13
|
export default (nodes) => {
|
|
9
14
|
if (typeof nodes === 'string') nodes = parse(nodes);
|
|
10
15
|
|
|
@@ -161,32 +166,32 @@ const build = {
|
|
|
161
166
|
immed = [0]
|
|
162
167
|
}
|
|
163
168
|
|
|
164
|
-
// (
|
|
165
|
-
else if (opCode
|
|
169
|
+
// (block ...), (loop ...), (if ...)
|
|
170
|
+
else if (opCode === 2 || opCode === 3 || opCode === 4) {
|
|
166
171
|
blocks.push(opCode)
|
|
167
172
|
|
|
168
173
|
// (block $x) (loop $y)
|
|
169
174
|
if (opCode < 4 && args[0]?.[0] === '$') (blocks[args.shift()] = blocks.length)
|
|
170
175
|
|
|
171
176
|
// get type
|
|
172
|
-
// (result i32)
|
|
173
|
-
if (args[0]?.[0] === 'result') {
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
177
|
+
// (result i32) - doesn't require registering type
|
|
178
|
+
if (args[0]?.[0] === 'result' && args[0].length < 3) {
|
|
179
|
+
let [, type] = args.shift()
|
|
180
|
+
immed = [TYPE[type]]
|
|
181
|
+
}
|
|
182
|
+
// (result i32 i32)
|
|
183
|
+
else if (args[0]?.[0] === 'result' || args[0]?.[0] === 'param') {
|
|
184
|
+
let [typeId] = consumeType(args, ctx)
|
|
185
|
+
immed = [typeId]
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
immed = [TYPE.void]
|
|
183
189
|
}
|
|
184
|
-
else immed = [TYPE.void]
|
|
185
190
|
|
|
186
191
|
if (group) {
|
|
192
|
+
// (block xxx) -> block xxx end
|
|
187
193
|
nodes.unshift('end')
|
|
188
194
|
|
|
189
|
-
// (block xxx) -> block xxx end
|
|
190
195
|
if (opCode < 4) while (args.length) nodes.unshift(args.pop())
|
|
191
196
|
|
|
192
197
|
// (if cond a) -> cond if a end
|
package/src/parse.js
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
const OPAREN = 40, CPAREN = 41, OBRACK = 91, CBRACK = 93, SPACE = 32, DQUOTE = 34, PERIOD = 46,
|
|
2
2
|
_0 = 48, _9 = 57, SEMIC = 59, NEWLINE = 32, PLUS = 43, MINUS = 45, COLON = 58
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Parses a wasm text string and constructs a nested array structure (AST).
|
|
6
|
+
*
|
|
7
|
+
* @param {string} str - The input string with WAT code to parse.
|
|
8
|
+
* @returns {Array} An array representing the nested syntax tree (AST).
|
|
9
|
+
*/
|
|
4
10
|
export default (str) => {
|
|
5
11
|
let i = 0, level = [], buf = ''
|
|
6
12
|
|
package/src/print.js
CHANGED
|
@@ -2,6 +2,15 @@ import parse from './parse.js';
|
|
|
2
2
|
|
|
3
3
|
let indent = '', newline = '\n'
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Formats a tree or a WAT string.
|
|
7
|
+
*
|
|
8
|
+
* @param {string | Array} tree - The code to print. If a string is provided, it will be parsed first.
|
|
9
|
+
* @param {Object} [options] - Printing options.
|
|
10
|
+
* @param {string} [options.indent=' '] - The string used for one level of indentation.
|
|
11
|
+
* @param {string} [options.newline='\n'] - The string used for line breaks.
|
|
12
|
+
* @returns {string} The formatted WAT string.
|
|
13
|
+
*/
|
|
5
14
|
export default function print(tree, options = {}) {
|
|
6
15
|
if (typeof tree === 'string') tree = parse(tree);
|
|
7
16
|
|