watr 3.3.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/LICENSE ADDED
@@ -0,0 +1,26 @@
1
+ MIT License
2
+
3
+ Copyright (c) Dmitry Ivanov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
23
+ ---
24
+
25
+ This work is also offered under the Krishnized License (https://github.com/krishnized/license).
26
+
package/bin/watr.js ADDED
@@ -0,0 +1,112 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * watr CLI - WebAssembly Text Format compiler
4
+ *
5
+ * Usage:
6
+ * watr input.wat # compile to input.wasm
7
+ * watr input.wat -o output.wasm # compile to output.wasm
8
+ * watr input.wat --print # pretty-print WAT
9
+ * watr input.wat --minify # minify WAT
10
+ * watr input.wat --polyfill # polyfill newer features to MVP
11
+ * watr --help # show help
12
+ *
13
+ * @module watr/bin
14
+ */
15
+
16
+ import { readFileSync, writeFileSync } from 'fs'
17
+ import { basename } from 'path'
18
+ import compile from '../src/compile.js'
19
+ import print from '../src/print.js'
20
+ import polyfill from '../src/polyfill.js'
21
+ import parse from '../src/parse.js'
22
+
23
+ const args = process.argv.slice(2)
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)
46
+
47
+ // Help
48
+ if (flags.has('-h') || flags.has('--help') || !files.length) {
49
+ console.log(`
50
+ watr - Light & fast WAT compiler
51
+
52
+ Usage:
53
+ watr <input.wat> [options]
54
+
55
+ Options:
56
+ -o, --output <file> Output file (default: input.wasm)
57
+ -p, --print Pretty-print WAT to stdout
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
62
+ -h, --help Show this help
63
+
64
+ Examples:
65
+ watr add.wat # → add.wasm
66
+ watr add.wat -o lib/add.wasm # → lib/add.wasm
67
+ watr add.wat --print # pretty-print
68
+ watr add.wat --polyfill # polyfill all features
69
+ watr add.wat --polyfill funcref # polyfill specific features
70
+ cat add.wat | watr - # stdin → stdout (binary)
71
+
72
+ ॐ https://github.com/dy/watr
73
+ `)
74
+ process.exit(flags.has('-h') || flags.has('--help') ? 0 : 1)
75
+ }
76
+
77
+ // Input
78
+ const input = files[0]
79
+ const src = input === '-'
80
+ ? readFileSync(0, 'utf8')
81
+ : readFileSync(input, 'utf8')
82
+
83
+ // Print mode
84
+ if (flags.has('-p') || flags.has('--print')) {
85
+ console.log(print(src, { indent: ' ', newline: '\n' }))
86
+ process.exit(0)
87
+ }
88
+
89
+ // Minify mode
90
+ if (flags.has('-m') || flags.has('--minify')) {
91
+ console.log(print(src, { indent: '', newline: '' }))
92
+ process.exit(0)
93
+ }
94
+
95
+ // Compile mode
96
+ let ast = parse(src)
97
+ if (polyfillOpts) ast = polyfill(ast, polyfillOpts)
98
+ const binary = compile(ast)
99
+
100
+ // Output
101
+ const output = outIdx !== -1 && args[outIdx + 1]
102
+ ? args[outIdx + 1]
103
+ : input === '-'
104
+ ? null
105
+ : input.replace(/\.wat$/, '') + '.wasm'
106
+
107
+ if (output) {
108
+ writeFileSync(output, binary)
109
+ console.error(`✓ ${basename(output)} (${binary.length} bytes)`)
110
+ } else {
111
+ process.stdout.write(binary)
112
+ }