watr 4.0.0 → 4.1.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/bin/watr.js CHANGED
@@ -7,6 +7,7 @@
7
7
  * watr input.wat -o output.wasm # compile to output.wasm
8
8
  * watr input.wat --print # pretty-print WAT
9
9
  * watr input.wat --minify # minify WAT
10
+ * watr input.wat --polyfill # polyfill newer features to MVP
10
11
  * watr --help # show help
11
12
  *
12
13
  * @module watr/bin
@@ -16,10 +17,32 @@ import { readFileSync, writeFileSync } from 'fs'
16
17
  import { basename } from 'path'
17
18
  import compile from '../src/compile.js'
18
19
  import print from '../src/print.js'
20
+ import polyfill from '../src/polyfill.js'
21
+ import parse from '../src/parse.js'
19
22
 
20
23
  const args = process.argv.slice(2)
21
- const flags = new Set(args.filter(a => a.startsWith('-')))
22
- const files = args.filter(a => !a.startsWith('-'))
24
+
25
+ // Parse polyfill option first (to exclude feature arg from files)
26
+ let polyfillOpts = null
27
+ const polyfillIdx = args.findIndex(a => a === '--polyfill')
28
+ let polyfillFeatureArg = null
29
+ if (polyfillIdx !== -1) {
30
+ // Check if next arg is feature list (not a flag or file)
31
+ const next = args[polyfillIdx + 1]
32
+ if (next && !next.startsWith('-') && !next.includes('.') && next !== '-') {
33
+ polyfillOpts = next
34
+ polyfillFeatureArg = next
35
+ } else {
36
+ polyfillOpts = true
37
+ }
38
+ }
39
+
40
+ // Parse -o output arg
41
+ const outIdx = args.findIndex(a => a === '-o' || a === '--output')
42
+ const outArg = outIdx !== -1 ? args[outIdx + 1] : null
43
+
44
+ const flags = new Set(args.filter(a => a.startsWith('-') && a !== '-'))
45
+ const files = args.filter(a => (!a.startsWith('-') || a === '-') && a !== polyfillFeatureArg && a !== outArg)
23
46
 
24
47
  // Help
25
48
  if (flags.has('-h') || flags.has('--help') || !files.length) {
@@ -33,12 +56,17 @@ Options:
33
56
  -o, --output <file> Output file (default: input.wasm)
34
57
  -p, --print Pretty-print WAT to stdout
35
58
  -m, --minify Minify WAT to stdout
59
+ --polyfill [features] Polyfill newer features to MVP (default: all)
60
+ Features: funcref sign_ext nontrapping bulk_memory
61
+ return_call i31ref extended_const multi_value
36
62
  -h, --help Show this help
37
63
 
38
64
  Examples:
39
65
  watr add.wat # → add.wasm
40
66
  watr add.wat -o lib/add.wasm # → lib/add.wasm
41
67
  watr add.wat --print # pretty-print
68
+ watr add.wat --polyfill # polyfill all features
69
+ watr add.wat --polyfill funcref # polyfill specific features
42
70
  cat add.wat | watr - # stdin → stdout (binary)
43
71
 
44
72
  ॐ https://github.com/dy/watr
@@ -65,10 +93,11 @@ if (flags.has('-m') || flags.has('--minify')) {
65
93
  }
66
94
 
67
95
  // Compile mode
68
- const binary = compile(src)
96
+ let ast = parse(src)
97
+ if (polyfillOpts) ast = polyfill(ast, polyfillOpts)
98
+ const binary = compile(ast)
69
99
 
70
100
  // Output
71
- const outIdx = args.findIndex(a => a === '-o' || a === '--output')
72
101
  const output = outIdx !== -1 && args[outIdx + 1]
73
102
  ? args[outIdx + 1]
74
103
  : input === '-'