watr 4.7.2 → 5.0.0

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/src/template.js CHANGED
@@ -19,6 +19,24 @@ import { resultType } from './const.js'
19
19
  /** Private Use Area character as placeholder for interpolation */
20
20
  const PUA = '\uE000'
21
21
 
22
+ /**
23
+ * Apply a backend transform (`polyfill`/`optimize`), or throw an actionable
24
+ * pointer when this entry doesn't bundle it. The default `watr` build wires a
25
+ * lean backend (parse + compile) and leaves the heavy transforms to their own
26
+ * entries, so `compile(src, { optimize })` here directs you to compose instead.
27
+ *
28
+ * @param {Function|undefined} fn - transform from the backend
29
+ * @param {string} name - 'polyfill' | 'optimize'
30
+ * @param {Array} ast
31
+ * @param {any} opt - the option value
32
+ * @returns {Array} transformed AST
33
+ */
34
+ function applyTransform(fn, name, ast, opt) {
35
+ if (typeof fn !== 'function')
36
+ throw Error(`watr: '${name}' is not bundled in this entry \u2014 import it from 'watr/${name}' and compose: compile(${name}(src))`)
37
+ return fn(ast, opt)
38
+ }
39
+
22
40
  /**
23
41
  * Infer type of an expression AST node.
24
42
  * Used for auto-import parameter type inference.
@@ -209,9 +227,9 @@ export function compile(backend, source, values) {
209
227
  }
210
228
  }
211
229
 
212
- // Apply transforms
213
- if (opts.polyfill) ast = polyfill(ast, opts.polyfill)
214
- if (opts.optimize) ast = optimize(ast, opts.optimize)
230
+ // Apply transforms (heavy passes live in separate entries — see applyTransform)
231
+ if (opts.polyfill) ast = applyTransform(polyfill, 'polyfill', ast, opts.polyfill)
232
+ if (opts.optimize) ast = applyTransform(optimize, 'optimize', ast, opts.optimize)
215
233
 
216
234
  const binary = emit(ast)
217
235
  // Attach imports for watr() to use
@@ -222,8 +240,8 @@ export function compile(backend, source, values) {
222
240
  // String/AST source with options
223
241
  if (opts.polyfill || opts.optimize) {
224
242
  let ast = typeof source === 'string' ? parse(source) : source
225
- if (opts.polyfill) ast = polyfill(ast, opts.polyfill)
226
- if (opts.optimize) ast = optimize(ast, opts.optimize)
243
+ if (opts.polyfill) ast = applyTransform(polyfill, 'polyfill', ast, opts.polyfill)
244
+ if (opts.optimize) ast = applyTransform(optimize, 'optimize', ast, opts.optimize)
227
245
  return emit(ast)
228
246
  }
229
247
  return emit(source)
@@ -1 +1 @@
1
- {"version":3,"file":"template.d.ts","sourceRoot":"","sources":["../../src/template.js"],"names":[],"mappings":"AA4HA;;;;;;;;;;GAUG;AACH,8CAPW,MAAM,WAAO,oBAAoB,UACjC,GAAG,EAAE,GAIH,UAAU,CAgGtB;AAED;;;;;;;GAOG;AACH,2CAJW,MAAM,WAAO,oBAAoB,UACjC,GAAG,EAAE,GACH,WAAW,CAAC,OAAO,CAO/B"}
1
+ {"version":3,"file":"template.d.ts","sourceRoot":"","sources":["../../src/template.js"],"names":[],"mappings":"AA8IA;;;;;;;;;;GAUG;AACH,8CAPW,MAAM,WAAO,oBAAoB,UACjC,GAAG,EAAE,GAIH,UAAU,CAgGtB;AAED;;;;;;;GAOG;AACH,2CAJW,MAAM,WAAO,oBAAoB,UACjC,GAAG,EAAE,GACH,WAAW,CAAC,OAAO,CAO/B"}
package/types/watr.d.ts CHANGED
@@ -17,18 +17,17 @@ export function watr(source: string | TemplateStringsArray, ...values: any[]): W
17
17
  *
18
18
  * @param {string|TemplateStringsArray} source - WAT source or template strings
19
19
  * @param {...any} values - Interpolation values (for template literal).
20
- * Last value can be an options object: { polyfill, optimize }.
21
20
  * @returns {Uint8Array} WebAssembly binary
22
21
  *
23
22
  * @example
24
23
  * compile('(func (export "f") (result i32) (i32.const 42))')
25
24
  * compile`(func (export "f") (result f64) (f64.const ${Math.PI}))`
26
- * compile(src, { polyfill: true, optimize: true })
25
+ * // transforms ship as separate entries — compose them:
26
+ * // import optimize from 'watr/optimize'
27
+ * // compile(optimize(src))
27
28
  */
28
29
  export function compile(source: string | TemplateStringsArray, ...values: any[]): Uint8Array;
29
30
  import parse from './src/parse.js';
30
31
  import print from './src/print.js';
31
- import _polyfill from './src/polyfill.js';
32
- import _optimize from './src/optimize.js';
33
- export { parse, print, _polyfill as polyfill, _optimize as optimize };
32
+ export { parse, print };
34
33
  //# sourceMappingURL=watr.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"watr.d.ts","sourceRoot":"","sources":["../watr.js"],"names":[],"mappings":";AAiCA;;;;;;;;;;;GAWG;AACH,6BATW,MAAM,GAAC,oBAAoB,aACxB,GAAG,EAAA,GACJ,WAAW,CAAC,OAAO,CAS/B;AA/BD;;;;;;;;;;;;GAYG;AACH,gCAVW,MAAM,GAAC,oBAAoB,aACxB,GAAG,EAAA,GAEJ,UAAU,CAStB;kBAxBiB,gBAAgB;kBAChB,gBAAgB;sBACZ,mBAAmB;sBACnB,mBAAmB"}
1
+ {"version":3,"file":"watr.d.ts","sourceRoot":"","sources":["../watr.js"],"names":[],"mappings":";AAoCA;;;;;;;;;;;GAWG;AACH,6BATW,MAAM,GAAC,oBAAoB,aACxB,GAAG,EAAA,GACJ,WAAW,CAAC,OAAO,CAS/B;AAhCD;;;;;;;;;;;;;GAaG;AACH,gCAXW,MAAM,GAAC,oBAAoB,aACxB,GAAG,EAAA,GACJ,UAAU,CAWtB;kBA3BiB,gBAAgB;kBAChB,gBAAgB"}
package/watr.js CHANGED
@@ -7,25 +7,28 @@
7
7
  import _compile from './src/compile.js'
8
8
  import parse from './src/parse.js'
9
9
  import print from './src/print.js'
10
- import _polyfill from './src/polyfill.js'
11
- import _optimize from './src/optimize.js'
12
10
  import { compile as _tcompile, watr as _twatr } from './src/template.js'
13
11
 
14
- /** JS-source backend primitives for the tagged-template layer. */
15
- const backend = { parse, compile: _compile, optimize: _optimize, polyfill: _polyfill }
12
+ /** JS-source backend primitives for the tagged-template layer.
13
+ * `polyfill` and `optimize` are intentionally NOT wired here they're heavy
14
+ * (the optimizer alone is ~5× the core encoder) and ship as separate entries,
15
+ * `watr/polyfill` and `watr/optimize`, that you compose explicitly:
16
+ * `compile(optimize(src))`. This keeps the default bundle minimal. */
17
+ const backend = { parse, compile: _compile }
16
18
 
17
19
  /**
18
20
  * Compile WAT to binary. Supports both string and template literal.
19
21
  *
20
22
  * @param {string|TemplateStringsArray} source - WAT source or template strings
21
23
  * @param {...any} values - Interpolation values (for template literal).
22
- * Last value can be an options object: { polyfill, optimize }.
23
24
  * @returns {Uint8Array} WebAssembly binary
24
25
  *
25
26
  * @example
26
27
  * compile('(func (export "f") (result i32) (i32.const 42))')
27
28
  * compile`(func (export "f") (result f64) (f64.const ${Math.PI}))`
28
- * compile(src, { polyfill: true, optimize: true })
29
+ * // transforms ship as separate entries — compose them:
30
+ * // import optimize from 'watr/optimize'
31
+ * // compile(optimize(src))
29
32
  */
30
33
  function compile(source, ...values) {
31
34
  return _tcompile(backend, source, values)
@@ -48,4 +51,4 @@ function watr(source, ...values) {
48
51
  }
49
52
 
50
53
  export default watr
51
- export { watr, compile, parse, print, _polyfill as polyfill, _optimize as optimize }
54
+ export { watr, compile, parse, print }