watr 4.1.0 → 4.2.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/bin/watr.js +29 -6
- package/dist/watr.js +829 -17
- package/dist/watr.min.js +5 -5
- package/package.json +1 -1
- package/readme.md +13 -38
- package/src/compile.js +20 -10
- package/src/optimize.js +1184 -0
- package/src/parse.js +2 -2
- package/src/util.js +4 -4
- package/types/src/compile.d.ts.map +1 -1
- package/types/src/optimize.d.ts +88 -0
- package/types/src/optimize.d.ts.map +1 -0
- package/types/src/util.d.ts +3 -3
- package/types/src/util.d.ts.map +1 -1
- package/types/watr.d.ts +6 -3
- package/types/watr.d.ts.map +1 -1
- package/watr.js +12 -6
package/src/parse.js
CHANGED
|
@@ -2,7 +2,7 @@ import { err } from "./util.js"
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Parses a wasm text string and constructs a nested array structure (AST).
|
|
5
|
-
* Each array node has `.
|
|
5
|
+
* Each array node has `.loc` property with source offset for error reporting.
|
|
6
6
|
*
|
|
7
7
|
* @param {string} str - The input string with WAT code to parse.
|
|
8
8
|
* @returns {Array} An array representing the nested syntax tree (AST).
|
|
@@ -13,7 +13,7 @@ export default (str) => {
|
|
|
13
13
|
const commit = () => buf && (level.push(buf), buf = '')
|
|
14
14
|
|
|
15
15
|
const parseLevel = (pos) => {
|
|
16
|
-
level.
|
|
16
|
+
level.loc = pos // store start position for error reporting
|
|
17
17
|
for (let c, root, p; i < str.length;) {
|
|
18
18
|
c = str.charCodeAt(i)
|
|
19
19
|
|
package/src/util.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Throws an error with optional source position.
|
|
3
|
-
* Uses err.src for source and err.
|
|
4
|
-
* If pos provided or err.
|
|
3
|
+
* Uses err.src for source and err.loc for default position.
|
|
4
|
+
* If pos provided or err.loc set, appends "at line:col".
|
|
5
5
|
*
|
|
6
6
|
* @param {string} text - Error message
|
|
7
|
-
* @param {number} [pos] - Byte offset in source (defaults to err.
|
|
7
|
+
* @param {number} [pos] - Byte offset in source (defaults to err.loc)
|
|
8
8
|
* @throws {Error}
|
|
9
9
|
*/
|
|
10
|
-
export const err = (text, pos=err.
|
|
10
|
+
export const err = (text, pos=err.loc) => {
|
|
11
11
|
if (pos != null && err.src) {
|
|
12
12
|
let line = 1, col = 1
|
|
13
13
|
for (let i = 0; i < pos && i < err.src.length; i++) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compile.d.ts","sourceRoot":"","sources":["../../src/compile.js"],"names":[],"mappings":"AA+BA;;;;;GAKG;AACH,uCAHW,MAAM,QAAM,GACV,UAAU,
|
|
1
|
+
{"version":3,"file":"compile.d.ts","sourceRoot":"","sources":["../../src/compile.js"],"names":[],"mappings":"AA+BA;;;;;GAKG;AACH,uCAHW,MAAM,QAAM,GACV,UAAU,CA6LtB"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Optimize AST.
|
|
3
|
+
*
|
|
4
|
+
* @param {Array|string} ast - AST or WAT source
|
|
5
|
+
* @param {boolean|string|Object} [opts=true] - Optimization options
|
|
6
|
+
* @returns {Array} Optimized AST
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* optimize(ast) // all optimizations
|
|
10
|
+
* optimize(ast, 'treeshake') // only treeshake
|
|
11
|
+
* optimize(ast, { fold: true }) // explicit
|
|
12
|
+
*/
|
|
13
|
+
export default function optimize(ast: any[] | string, opts?: boolean | string | any): any[];
|
|
14
|
+
/**
|
|
15
|
+
* Remove unused functions, globals, types, tables.
|
|
16
|
+
* Keeps exports and their transitive dependencies.
|
|
17
|
+
* @param {Array} ast
|
|
18
|
+
* @returns {Array}
|
|
19
|
+
*/
|
|
20
|
+
export function treeshake(ast: any[]): any[];
|
|
21
|
+
/**
|
|
22
|
+
* Fold constant expressions.
|
|
23
|
+
* @param {Array} ast
|
|
24
|
+
* @returns {Array}
|
|
25
|
+
*/
|
|
26
|
+
export function fold(ast: any[]): any[];
|
|
27
|
+
/**
|
|
28
|
+
* Remove dead code after control flow terminators.
|
|
29
|
+
* @param {Array} ast
|
|
30
|
+
* @returns {Array}
|
|
31
|
+
*/
|
|
32
|
+
export function deadcode(ast: any[]): any[];
|
|
33
|
+
/**
|
|
34
|
+
* Reuse locals of the same type to reduce total local count.
|
|
35
|
+
* Basic version: deduplicate unused locals.
|
|
36
|
+
* @param {Array} ast
|
|
37
|
+
* @returns {Array}
|
|
38
|
+
*/
|
|
39
|
+
export function localReuse(ast: any[]): any[];
|
|
40
|
+
/**
|
|
41
|
+
* Remove identity operations.
|
|
42
|
+
* @param {Array} ast
|
|
43
|
+
* @returns {Array}
|
|
44
|
+
*/
|
|
45
|
+
export function identity(ast: any[]): any[];
|
|
46
|
+
/**
|
|
47
|
+
* Strength reduction: replace expensive ops with cheaper equivalents.
|
|
48
|
+
* @param {Array} ast
|
|
49
|
+
* @returns {Array}
|
|
50
|
+
*/
|
|
51
|
+
export function strength(ast: any[]): any[];
|
|
52
|
+
/**
|
|
53
|
+
* Simplify branches with constant conditions.
|
|
54
|
+
* @param {Array} ast
|
|
55
|
+
* @returns {Array}
|
|
56
|
+
*/
|
|
57
|
+
export function branch(ast: any[]): any[];
|
|
58
|
+
/**
|
|
59
|
+
* Propagate constant values through local variables.
|
|
60
|
+
* When a local is set to a constant and not modified before use, replace the get with the constant.
|
|
61
|
+
* @param {Array} ast
|
|
62
|
+
* @returns {Array}
|
|
63
|
+
*/
|
|
64
|
+
export function propagate(ast: any[]): any[];
|
|
65
|
+
/**
|
|
66
|
+
* Inline tiny functions (single expression, no locals, no params or simple params).
|
|
67
|
+
* @param {Array} ast
|
|
68
|
+
* @returns {Array}
|
|
69
|
+
*/
|
|
70
|
+
export function inline(ast: any[]): any[];
|
|
71
|
+
/**
|
|
72
|
+
* Normalize options to { opt: bool } map.
|
|
73
|
+
* @param {boolean|string|Object} opts
|
|
74
|
+
* @returns {Object}
|
|
75
|
+
*/
|
|
76
|
+
export function normalize(opts: boolean | string | any): any;
|
|
77
|
+
export namespace OPTS {
|
|
78
|
+
let treeshake: boolean;
|
|
79
|
+
let fold: boolean;
|
|
80
|
+
let deadcode: boolean;
|
|
81
|
+
let locals: boolean;
|
|
82
|
+
let identity: boolean;
|
|
83
|
+
let strength: boolean;
|
|
84
|
+
let branch: boolean;
|
|
85
|
+
let propagate: boolean;
|
|
86
|
+
let inline: boolean;
|
|
87
|
+
}
|
|
88
|
+
//# sourceMappingURL=optimize.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"optimize.d.ts","sourceRoot":"","sources":["../../src/optimize.js"],"names":[],"mappings":"AAioCA;;;;;;;;;;;GAWG;AACH,sCATW,QAAM,MAAM,SACZ,OAAO,GAAC,MAAM,MAAO,SAwB/B;AAtkCD;;;;;GAKG;AACH,6CA8LC;AAyHD;;;;GAIG;AACH,wCAmCC;AAwQD;;;;GAIG;AACH,4CAuBC;AA+CD;;;;;GAKG;AACH,8CAqDC;AApTD;;;;GAIG;AACH,4CASC;AAID;;;;GAIG;AACH,4CA6DC;AAID;;;;GAIG;AACH,0CA0EC;AAiJD;;;;;GAKG;AACH,6CAuEC;AAID;;;;GAIG;AACH,0CAwFC;AAn+BD;;;;GAIG;AACH,gCAHW,OAAO,GAAC,MAAM,MAAO,OAe/B"}
|
package/types/src/util.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Throws an error with optional source position.
|
|
3
|
-
* Uses err.src for source and err.
|
|
4
|
-
* If pos provided or err.
|
|
3
|
+
* Uses err.src for source and err.loc for default position.
|
|
4
|
+
* If pos provided or err.loc set, appends "at line:col".
|
|
5
5
|
*
|
|
6
6
|
* @param {string} text - Error message
|
|
7
|
-
* @param {number} [pos] - Byte offset in source (defaults to err.
|
|
7
|
+
* @param {number} [pos] - Byte offset in source (defaults to err.loc)
|
|
8
8
|
* @throws {Error}
|
|
9
9
|
*/
|
|
10
10
|
export const err: (text: string, pos?: number) => never;
|
package/types/src/util.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../../src/util.js"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,mBAAoB,MAJT,MAIa,EAAE,MAHf,
|
|
1
|
+
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../../src/util.js"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,mBAAoB,MAJT,MAIa,EAAE,MAHf,MAG0B,WAUpC;AAOM,2CAAkF;AAEzF,8DAA8D;AAC9D,2BAAiD;AAEjD,6DAA6D;AAC7D,2BAAiD;AAe1C,uBAHI,MAAM,GACJ,MAAM,EAAE,CA8BpB;AAUM,4BAHI,MAAM,GACJ,MAAM,CAE6C"}
|
package/types/watr.d.ts
CHANGED
|
@@ -21,17 +21,20 @@ export function watr(strings: string | TemplateStringsArray, ...values: any[]):
|
|
|
21
21
|
*
|
|
22
22
|
* @param {string|TemplateStringsArray} source - WAT source or template strings
|
|
23
23
|
* @param {...any} values - Interpolation values (for template literal)
|
|
24
|
-
* Last value can be options object:
|
|
24
|
+
* Last value can be options object:
|
|
25
|
+
* - polyfill: true | 'funcref sign_ext' | { funcref: true }
|
|
26
|
+
* - optimize: true | 'fold treeshake' | { fold: true }
|
|
25
27
|
* @returns {Uint8Array} WebAssembly binary
|
|
26
28
|
*
|
|
27
29
|
* @example
|
|
28
30
|
* compile('(func (export "f") (result i32) (i32.const 42))')
|
|
29
31
|
* compile`(func (export "f") (result f64) (f64.const ${Math.PI}))`
|
|
30
|
-
* compile(src, { polyfill: true })
|
|
32
|
+
* compile(src, { polyfill: true, optimize: true })
|
|
31
33
|
*/
|
|
32
34
|
export function compile(source: string | TemplateStringsArray, ...values: any[]): Uint8Array;
|
|
33
35
|
import parse from './src/parse.js';
|
|
34
36
|
import print from './src/print.js';
|
|
35
37
|
import _polyfill from './src/polyfill.js';
|
|
36
|
-
|
|
38
|
+
import _optimize from './src/optimize.js';
|
|
39
|
+
export { parse, print, _polyfill as polyfill, _optimize as optimize };
|
|
37
40
|
//# sourceMappingURL=watr.d.ts.map
|
package/types/watr.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"watr.d.ts","sourceRoot":"","sources":["../watr.js"],"names":[],"mappings":";
|
|
1
|
+
{"version":3,"file":"watr.d.ts","sourceRoot":"","sources":["../watr.js"],"names":[],"mappings":";AAiPA;;;;;;;;;;;;;;;GAeG;AACH,8BAbW,MAAM,GAAC,oBAAoB,aACxB,GAAG,EAAA,GACJ,WAAW,CAAC,OAAO,CAgB/B;AA9HD;;;;;;;;;;;;;;GAcG;AACH,gCAZW,MAAM,GAAC,oBAAoB,aACxB,GAAG,EAAA,GAIJ,UAAU,CA+FtB;kBAxOiB,gBAAgB;kBAChB,gBAAgB;sBACZ,mBAAmB;sBACnB,mBAAmB"}
|
package/watr.js
CHANGED
|
@@ -8,6 +8,7 @@ import _compile from './src/compile.js'
|
|
|
8
8
|
import parse from './src/parse.js'
|
|
9
9
|
import print from './src/print.js'
|
|
10
10
|
import _polyfill from './src/polyfill.js'
|
|
11
|
+
import _optimize from './src/optimize.js'
|
|
11
12
|
|
|
12
13
|
/** Private Use Area character as placeholder for interpolation */
|
|
13
14
|
const PUA = '\uE000'
|
|
@@ -138,13 +139,15 @@ function genImports(imports) {
|
|
|
138
139
|
*
|
|
139
140
|
* @param {string|TemplateStringsArray} source - WAT source or template strings
|
|
140
141
|
* @param {...any} values - Interpolation values (for template literal)
|
|
141
|
-
* Last value can be options object:
|
|
142
|
+
* Last value can be options object:
|
|
143
|
+
* - polyfill: true | 'funcref sign_ext' | { funcref: true }
|
|
144
|
+
* - optimize: true | 'fold treeshake' | { fold: true }
|
|
142
145
|
* @returns {Uint8Array} WebAssembly binary
|
|
143
146
|
*
|
|
144
147
|
* @example
|
|
145
148
|
* compile('(func (export "f") (result i32) (i32.const 42))')
|
|
146
149
|
* compile`(func (export "f") (result f64) (f64.const ${Math.PI}))`
|
|
147
|
-
* compile(src, { polyfill: true })
|
|
150
|
+
* compile(src, { polyfill: true, optimize: true })
|
|
148
151
|
*/
|
|
149
152
|
function compile(source, ...values) {
|
|
150
153
|
// Options object as last argument (non-template call)
|
|
@@ -216,8 +219,9 @@ function compile(source, ...values) {
|
|
|
216
219
|
}
|
|
217
220
|
}
|
|
218
221
|
|
|
219
|
-
// Apply
|
|
222
|
+
// Apply transforms
|
|
220
223
|
if (opts.polyfill) ast = _polyfill(ast, opts.polyfill)
|
|
224
|
+
if (opts.optimize) ast = _optimize(ast, opts.optimize)
|
|
221
225
|
|
|
222
226
|
const binary = _compile(ast)
|
|
223
227
|
// Attach imports for watr() to use
|
|
@@ -226,8 +230,10 @@ function compile(source, ...values) {
|
|
|
226
230
|
}
|
|
227
231
|
|
|
228
232
|
// String/AST source with options
|
|
229
|
-
if (opts.polyfill) {
|
|
230
|
-
|
|
233
|
+
if (opts.polyfill || opts.optimize) {
|
|
234
|
+
let ast = typeof source === 'string' ? parse(source) : source
|
|
235
|
+
if (opts.polyfill) ast = _polyfill(ast, opts.polyfill)
|
|
236
|
+
if (opts.optimize) ast = _optimize(ast, opts.optimize)
|
|
231
237
|
return _compile(ast)
|
|
232
238
|
}
|
|
233
239
|
return _compile(source)
|
|
@@ -257,4 +263,4 @@ function watr(strings, ...values) {
|
|
|
257
263
|
}
|
|
258
264
|
|
|
259
265
|
export default watr
|
|
260
|
-
export { watr, compile, parse, print, _polyfill as polyfill }
|
|
266
|
+
export { watr, compile, parse, print, _polyfill as polyfill, _optimize as optimize }
|