watr 3.2.1 → 4.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/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,83 @@
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 --help # show help
11
+ *
12
+ * @module watr/bin
13
+ */
14
+
15
+ import { readFileSync, writeFileSync } from 'fs'
16
+ import { basename } from 'path'
17
+ import compile from '../src/compile.js'
18
+ import print from '../src/print.js'
19
+
20
+ const args = process.argv.slice(2)
21
+ const flags = new Set(args.filter(a => a.startsWith('-')))
22
+ const files = args.filter(a => !a.startsWith('-'))
23
+
24
+ // Help
25
+ if (flags.has('-h') || flags.has('--help') || !files.length) {
26
+ console.log(`
27
+ watr - Light & fast WAT compiler
28
+
29
+ Usage:
30
+ watr <input.wat> [options]
31
+
32
+ Options:
33
+ -o, --output <file> Output file (default: input.wasm)
34
+ -p, --print Pretty-print WAT to stdout
35
+ -m, --minify Minify WAT to stdout
36
+ -h, --help Show this help
37
+
38
+ Examples:
39
+ watr add.wat # → add.wasm
40
+ watr add.wat -o lib/add.wasm # → lib/add.wasm
41
+ watr add.wat --print # pretty-print
42
+ cat add.wat | watr - # stdin → stdout (binary)
43
+
44
+ ॐ https://github.com/dy/watr
45
+ `)
46
+ process.exit(flags.has('-h') || flags.has('--help') ? 0 : 1)
47
+ }
48
+
49
+ // Input
50
+ const input = files[0]
51
+ const src = input === '-'
52
+ ? readFileSync(0, 'utf8')
53
+ : readFileSync(input, 'utf8')
54
+
55
+ // Print mode
56
+ if (flags.has('-p') || flags.has('--print')) {
57
+ console.log(print(src, { indent: ' ', newline: '\n' }))
58
+ process.exit(0)
59
+ }
60
+
61
+ // Minify mode
62
+ if (flags.has('-m') || flags.has('--minify')) {
63
+ console.log(print(src, { indent: '', newline: '' }))
64
+ process.exit(0)
65
+ }
66
+
67
+ // Compile mode
68
+ const binary = compile(src)
69
+
70
+ // Output
71
+ const outIdx = args.findIndex(a => a === '-o' || a === '--output')
72
+ const output = outIdx !== -1 && args[outIdx + 1]
73
+ ? args[outIdx + 1]
74
+ : input === '-'
75
+ ? null
76
+ : input.replace(/\.wat$/, '') + '.wasm'
77
+
78
+ if (output) {
79
+ writeFileSync(output, binary)
80
+ console.error(`✓ ${basename(output)} (${binary.length} bytes)`)
81
+ } else {
82
+ process.stdout.write(binary)
83
+ }