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 CHANGED
@@ -18,6 +18,7 @@ import { basename } from 'path'
18
18
  import compile from '../src/compile.js'
19
19
  import print from '../src/print.js'
20
20
  import polyfill from '../src/polyfill.js'
21
+ import optimize from '../src/optimize.js'
21
22
  import parse from '../src/parse.js'
22
23
 
23
24
  const args = process.argv.slice(2)
@@ -37,12 +38,26 @@ if (polyfillIdx !== -1) {
37
38
  }
38
39
  }
39
40
 
41
+ // Parse optimize option
42
+ let optimizeOpts = null
43
+ const optimizeIdx = args.findIndex(a => a === '-O' || a === '--optimize')
44
+ let optimizeFeatureArg = null
45
+ if (optimizeIdx !== -1) {
46
+ const next = args[optimizeIdx + 1]
47
+ if (next && !next.startsWith('-') && !next.includes('.') && next !== '-') {
48
+ optimizeOpts = next
49
+ optimizeFeatureArg = next
50
+ } else {
51
+ optimizeOpts = true
52
+ }
53
+ }
54
+
40
55
  // Parse -o output arg
41
56
  const outIdx = args.findIndex(a => a === '-o' || a === '--output')
42
57
  const outArg = outIdx !== -1 ? args[outIdx + 1] : null
43
58
 
44
59
  const flags = new Set(args.filter(a => a.startsWith('-') && a !== '-'))
45
- const files = args.filter(a => (!a.startsWith('-') || a === '-') && a !== polyfillFeatureArg && a !== outArg)
60
+ const files = args.filter(a => (!a.startsWith('-') || a === '-') && a !== polyfillFeatureArg && a !== optimizeFeatureArg && a !== outArg)
46
61
 
47
62
  // Help
48
63
  if (flags.has('-h') || flags.has('--help') || !files.length) {
@@ -56,6 +71,9 @@ Options:
56
71
  -o, --output <file> Output file (default: input.wasm)
57
72
  -p, --print Pretty-print WAT to stdout
58
73
  -m, --minify Minify WAT to stdout
74
+ -O, --optimize [opts] Optimize AST (default: all)
75
+ Options: fold identity strength branch propagate inline
76
+ deadcode locals treeshake
59
77
  --polyfill [features] Polyfill newer features to MVP (default: all)
60
78
  Features: funcref sign_ext nontrapping bulk_memory
61
79
  return_call i31ref extended_const multi_value
@@ -65,11 +83,13 @@ Examples:
65
83
  watr add.wat # → add.wasm
66
84
  watr add.wat -o lib/add.wasm # → lib/add.wasm
67
85
  watr add.wat --print # pretty-print
86
+ watr add.wat -O # optimize all
87
+ watr add.wat -O treeshake # optimize specific
68
88
  watr add.wat --polyfill # polyfill all features
69
89
  watr add.wat --polyfill funcref # polyfill specific features
70
90
  cat add.wat | watr - # stdin → stdout (binary)
71
91
 
72
- https://github.com/dy/watr
92
+
73
93
  `)
74
94
  process.exit(flags.has('-h') || flags.has('--help') ? 0 : 1)
75
95
  }
@@ -80,21 +100,24 @@ const src = input === '-'
80
100
  ? readFileSync(0, 'utf8')
81
101
  : readFileSync(input, 'utf8')
82
102
 
103
+ // Parse and transform
104
+ let ast = parse(src)
105
+ if (polyfillOpts) ast = polyfill(ast, polyfillOpts)
106
+ if (optimizeOpts) ast = optimize(ast, optimizeOpts)
107
+
83
108
  // Print mode
84
109
  if (flags.has('-p') || flags.has('--print')) {
85
- console.log(print(src, { indent: ' ', newline: '\n' }))
110
+ console.log(print(ast, { indent: ' ', newline: '\n' }))
86
111
  process.exit(0)
87
112
  }
88
113
 
89
114
  // Minify mode
90
115
  if (flags.has('-m') || flags.has('--minify')) {
91
- console.log(print(src, { indent: '', newline: '' }))
116
+ console.log(print(ast, { indent: '', newline: '' }))
92
117
  process.exit(0)
93
118
  }
94
119
 
95
120
  // Compile mode
96
- let ast = parse(src)
97
- if (polyfillOpts) ast = polyfill(ast, polyfillOpts)
98
121
  const binary = compile(ast)
99
122
 
100
123
  // Output