-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·192 lines (166 loc) · 6.77 KB
/
cli.js
File metadata and controls
executable file
·192 lines (166 loc) · 6.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env node
/**
* JZ CLI - Command-line interface for JZ compiler
*/
import { readFileSync, writeFileSync } from 'fs'
import { resolve } from 'path'
import { pathToFileURL } from 'url'
import { parse } from 'subscript/feature/jessie'
import jz, { compile } from './index.js'
import jzifyFn from './src/jzify.js'
import { codegen } from './src/codegen.js'
import { resolveModuleGraph } from './src/resolve.js'
import { createRequire } from 'module'
const jzRequire = createRequire(import.meta.url)
const PKG = jzRequire('./package.json')
function showHelp() {
console.log(`
jz v${PKG.version} - min JS → WASM compiler
Usage:
jz <file.js> Compile JS to WASM (auto-jzify)
jz --strict <file.js> Strict mode (no auto-transform)
jz --jzify <file.js> Transform JS → jz (auto-derives output file)
jz -e <expression> Evaluate expression
jz --help Show this help
Examples:
jz program.js # → program.wasm
jz program.js --wat # → program.wat
jz program.js -o out.wasm # custom output name
jz program.js -o - # write to stdout
jz program.js -O3 # aggressive optimization
jz program.js -Os # optimize for size
jz program.js --host wasi # emit WASI Preview 1 imports
jz --strict program.js # strict mode
jz --jzify lib.js # → lib.jz
jz -e "1 + 2"
Options:
--output, -o <file> Output file (.wat, .wasm, or - for stdout)
-O<n>, --optimize <n> Optimization level: 0 off, 1 size-only, 2 default,
3 aggressive. Aliases: -Os/size, -Ob/balanced, -Of/speed.
--host <js|wasi> Runtime-service lowering (default js)
--no-alloc Omit _alloc/_clear allocator exports (standalone wasm)
--names Emit wasm name section for profilers/debuggers
--strict Strict jz mode (no auto-transform), reject dynamic fallbacks
--jzify Transform JS to jz (no compilation)
--eval, -e Evaluate expression or file
--wat Output WAT text instead of binary
--resolve Resolve bare specifiers via Node.js module resolution
--imports <file> JSON file with host import specs (e.g. {"env":{"fn":{"params":2}}})
--version, -v Show version number
`)
}
async function main() {
const args = process.argv.slice(2)
if (args.includes('--version') || args.includes('-v')) {
console.log(PKG.version)
return
}
if (args.length === 0 || args.includes('--help') || args.includes('-h')) {
showHelp()
return
}
try {
const evalIdx = args.indexOf('-e') !== -1 ? args.indexOf('-e') : args.indexOf('--eval')
const jzifyIdx = args.indexOf('--jzify')
if (jzifyIdx !== -1) await handleJzify(args.slice(jzifyIdx + 1))
else if (evalIdx !== -1) await handleEvaluate(args.slice(evalIdx + 1))
else await handleCompile(args)
} catch (error) {
console.error(error)
process.exit(1)
}
}
async function handleEvaluate(args) {
const input = args.join(' ')
let code
if (args.length === 1 && (args[0].endsWith('.js') || args[0].endsWith('.jz')))
code = readFileSync(args[0], 'utf8')
else
code = `export let _ = () => ${input}`
const { exports } = jz(code)
// If there's an exported _ (expression eval), call it
if (exports._) console.log(exports._())
else console.log(exports)
}
async function handleJzify(args) {
let inputFile = null, outputFile = null
for (let i = 0; i < args.length; i++) {
if (args[i] === '--output' || args[i] === '-o') outputFile = args[++i]
else if (!inputFile) inputFile = args[i]
}
if (!inputFile) throw new Error('No input file specified')
if (!outputFile) outputFile = inputFile.replace(/\.js$/, '.jz')
const code = readFileSync(inputFile, 'utf8')
const ast = parse(code)
const transformed = jzifyFn(ast)
const out = codegen(transformed) + '\n'
if (outputFile === '-') {
process.stdout.write(out)
} else {
writeFileSync(outputFile, out)
console.log(`${inputFile} → ${outputFile} (${out.length} chars)`)
}
}
// -O<n>/-Os/-Ob/-Of and --optimize <val> → value accepted by compile()'s `optimize` opt
const OPT_ALIAS = { s: 'size', b: 'balanced', f: 'speed' }
function parseOptimize(v) {
if (v == null) return undefined
if (/^\d+$/.test(v)) return +v
return OPT_ALIAS[v] ?? v
}
async function handleCompile(args) {
let inputFile = null, outputFile = null, wat = false, strict = false, resolveNode = false, importsFile = null
let optimize, host, alloc = true, names = false
for (let i = 0; i < args.length; i++) {
const a = args[i]
if (a === '--output' || a === '-o') outputFile = args[++i]
else if (a === '--wat') wat = true
else if (a === '--strict') strict = true
else if (a === '--resolve') resolveNode = true
else if (a === '--imports') importsFile = args[++i]
else if (a === '--optimize' || a === '-O') optimize = parseOptimize(args[++i])
else if (/^-O.+/.test(a)) optimize = parseOptimize(a.slice(2))
else if (a === '--host') host = args[++i]
else if (a === '--no-alloc') alloc = false
else if (a === '--names') names = true
else if (!inputFile) inputFile = a
}
if (!inputFile) throw new Error('No input file specified')
if (!outputFile) outputFile = inputFile.replace(/\.(js|jz)$/, wat ? '.wat' : '.wasm')
if (outputFile.endsWith('.wat')) wat = true
// Resolve imports — canonicalize every specifier to an absolute path so the
// same physical file always produces one module instance (see src/resolve.js).
const { code: codeRewritten, modules } = resolveModuleGraph(inputFile, { resolveNode })
if (process.env.JZ_DEBUG_MODULES === '1') console.error('modules:', Object.keys(modules))
// .jz = strict (no auto-transform), .js = auto-jzify
// --strict forces strict for any extension
const opts = {
wat,
jzify: !strict && !inputFile.endsWith('.jz'),
strict,
importMetaUrl: pathToFileURL(resolve(inputFile)).href,
...(optimize !== undefined && { optimize }),
...(host && { host }),
...(alloc === false && { alloc: false }),
...(names && { profileNames: true }),
...(Object.keys(modules).length && { modules }),
}
if (importsFile) {
const importsPath = resolve(importsFile)
opts.imports = JSON.parse(readFileSync(importsPath, 'utf8'))
}
const result = compile(codeRewritten, opts)
if (outputFile === '-') {
process.stdout.write(result)
} else if (wat) {
writeFileSync(outputFile, result)
console.log(`${inputFile} → ${outputFile} (${result.length} chars)`)
} else {
writeFileSync(outputFile, result)
console.log(`${inputFile} → ${outputFile} (${result.byteLength} bytes)`)
}
}
main().catch(error => {
console.error(error)
process.exit(1)
})