jz 0.0.0 → 0.1.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/LICENSE +21 -0
- package/README.md +373 -0
- package/cli.js +163 -0
- package/index.js +247 -0
- package/module/array.js +1322 -0
- package/module/collection.js +793 -0
- package/module/console.js +192 -0
- package/module/core.js +644 -0
- package/module/function.js +181 -0
- package/module/index.js +15 -0
- package/module/json.js +506 -0
- package/module/math.js +390 -0
- package/module/number.js +601 -0
- package/module/object.js +359 -0
- package/module/regex.js +914 -0
- package/module/schema.js +106 -0
- package/module/string.js +985 -0
- package/module/symbol.js +55 -0
- package/module/timer.js +253 -0
- package/module/typedarray.js +713 -0
- package/package.json +56 -5
- package/src/analyze.js +1927 -0
- package/src/autoload.js +175 -0
- package/src/compile.js +1211 -0
- package/src/ctx.js +244 -0
- package/src/emit.js +2105 -0
- package/src/host.js +536 -0
- package/src/ir.js +649 -0
- package/src/jzify.js +418 -0
- package/src/key.js +73 -0
- package/src/narrow.js +928 -0
- package/src/optimize.js +1352 -0
- package/src/plan.js +105 -0
- package/src/prepare.js +1534 -0
- package/src/source.js +76 -0
- package/wasi.js +80 -0
package/src/source.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
export function normalizeSource(code) {
|
|
2
|
+
let source = code.startsWith('#!') ? `//${code.slice(2)}` : code
|
|
3
|
+
// Optional catch binding: `catch {` → `catch(_e) {` (parser gap workaround)
|
|
4
|
+
source = source.replace(/\bcatch\s*\{/g, 'catch(_e){')
|
|
5
|
+
let out = ''
|
|
6
|
+
let parenDepth = 0
|
|
7
|
+
let bracketDepth = 0
|
|
8
|
+
let quote = ''
|
|
9
|
+
let lineComment = false
|
|
10
|
+
let blockComment = false
|
|
11
|
+
|
|
12
|
+
for (let i = 0; i < source.length; i++) {
|
|
13
|
+
const ch = source[i]
|
|
14
|
+
const next = source[i + 1]
|
|
15
|
+
|
|
16
|
+
if (lineComment) {
|
|
17
|
+
out += ch
|
|
18
|
+
if (ch === '\n') lineComment = false
|
|
19
|
+
continue
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (blockComment) {
|
|
23
|
+
out += ch
|
|
24
|
+
if (ch === '*' && next === '/') { out += next; i++; blockComment = false }
|
|
25
|
+
continue
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (quote) {
|
|
29
|
+
out += ch
|
|
30
|
+
if (ch === '\\') { if (next != null) { out += next; i++ } }
|
|
31
|
+
else if (ch === quote) quote = ''
|
|
32
|
+
continue
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (ch === '/' && next === '/') { out += ch + next; i++; lineComment = true; continue }
|
|
36
|
+
if (ch === '/' && next === '*') { out += ch + next; i++; blockComment = true; continue }
|
|
37
|
+
if (ch === '"' || ch === "'" || ch === '`') { out += ch; quote = ch; continue }
|
|
38
|
+
if (ch === '(') parenDepth++
|
|
39
|
+
else if (ch === ')' && parenDepth > 0) parenDepth--
|
|
40
|
+
else if (ch === '[') bracketDepth++
|
|
41
|
+
else if (ch === ']' && bracketDepth > 0) bracketDepth--
|
|
42
|
+
|
|
43
|
+
out += ch
|
|
44
|
+
if (ch === ';' && parenDepth === 0 && bracketDepth === 0) {
|
|
45
|
+
let j = i + 1
|
|
46
|
+
let sawNewline = false
|
|
47
|
+
while (j < source.length) {
|
|
48
|
+
const c = source[j]
|
|
49
|
+
const n = source[j + 1]
|
|
50
|
+
if (c === ' ' || c === '\t' || c === '\r' || c === '\n') {
|
|
51
|
+
if (c === '\n') sawNewline = true
|
|
52
|
+
j++
|
|
53
|
+
continue
|
|
54
|
+
}
|
|
55
|
+
if (c === '/' && n === '/') {
|
|
56
|
+
j += 2
|
|
57
|
+
while (j < source.length && source[j] !== '\n') j++
|
|
58
|
+
continue
|
|
59
|
+
}
|
|
60
|
+
if (c === '/' && n === '*') {
|
|
61
|
+
j += 2
|
|
62
|
+
while (j < source.length && !(source[j] === '*' && source[j + 1] === '/')) {
|
|
63
|
+
if (source[j] === '\n') sawNewline = true
|
|
64
|
+
j++
|
|
65
|
+
}
|
|
66
|
+
if (j < source.length) j += 2
|
|
67
|
+
continue
|
|
68
|
+
}
|
|
69
|
+
break
|
|
70
|
+
}
|
|
71
|
+
if (sawNewline && source[j] === '(') out += ';'
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return out
|
|
76
|
+
}
|
package/wasi.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WASI Preview 1 polyfill for jz modules.
|
|
3
|
+
*
|
|
4
|
+
* Provides wasi_snapshot_preview1 imports for environments without native WASI.
|
|
5
|
+
* The compiled .wasm uses standard WASI — runs natively on wasmtime/wasmer/deno.
|
|
6
|
+
* This polyfill is only needed for browsers and plain Node.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* import { instantiate } from 'jz/wasi'
|
|
10
|
+
* const inst = instantiate(wasm)
|
|
11
|
+
* inst.exports.f()
|
|
12
|
+
*
|
|
13
|
+
* @module wasi
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Create WASI import object for WebAssembly instantiation.
|
|
18
|
+
* @param {object} [opts]
|
|
19
|
+
* @param {function} [opts.write] - Custom write: (fd, text) => void
|
|
20
|
+
*/
|
|
21
|
+
export function wasi(opts = {}) {
|
|
22
|
+
let mem = null
|
|
23
|
+
const fallbackWrite = (fd, text) => {
|
|
24
|
+
const stream = fd === 1 ? globalThis.process?.stdout : globalThis.process?.stderr
|
|
25
|
+
if (stream && typeof stream.write === 'function') {
|
|
26
|
+
try { stream.write(text); return }
|
|
27
|
+
catch {}
|
|
28
|
+
}
|
|
29
|
+
const msg = text.replace(/\n$/, '')
|
|
30
|
+
;(fd === 1 ? console.log : console.warn)(msg)
|
|
31
|
+
}
|
|
32
|
+
const write = opts.write || fallbackWrite
|
|
33
|
+
|
|
34
|
+
return {
|
|
35
|
+
wasi_snapshot_preview1: {
|
|
36
|
+
fd_write(fd, iovs, iovs_len, nwritten) {
|
|
37
|
+
const dv = new DataView(mem.buffer)
|
|
38
|
+
let written = 0
|
|
39
|
+
for (let i = 0; i < iovs_len; i++) {
|
|
40
|
+
const ptr = dv.getUint32(iovs + i * 8, true)
|
|
41
|
+
const len = dv.getUint32(iovs + i * 8 + 4, true)
|
|
42
|
+
write(fd, new TextDecoder().decode(new Uint8Array(mem.buffer, ptr, len)))
|
|
43
|
+
written += len
|
|
44
|
+
}
|
|
45
|
+
dv.setUint32(nwritten, written, true)
|
|
46
|
+
return 0
|
|
47
|
+
},
|
|
48
|
+
clock_time_get(clock_id, precision, result_ptr) {
|
|
49
|
+
const dv = new DataView(mem.buffer)
|
|
50
|
+
const now = clock_id === 0
|
|
51
|
+
? BigInt(Math.round(Date.now() * 1e6)) // realtime: ms → ns
|
|
52
|
+
: BigInt(Math.round(performance.now() * 1e6)) // monotonic: ms → ns
|
|
53
|
+
dv.setBigInt64(result_ptr, now, true)
|
|
54
|
+
return 0
|
|
55
|
+
},
|
|
56
|
+
proc_exit() {},
|
|
57
|
+
environ_sizes_get(count_ptr, size_ptr) {
|
|
58
|
+
const dv = new DataView(mem.buffer)
|
|
59
|
+
dv.setUint32(count_ptr, 0, true)
|
|
60
|
+
dv.setUint32(size_ptr, 0, true)
|
|
61
|
+
return 0
|
|
62
|
+
},
|
|
63
|
+
environ_get() { return 0 },
|
|
64
|
+
},
|
|
65
|
+
_setMemory(m) { mem = m },
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Compile and instantiate a jz WASI module.
|
|
71
|
+
* @param {BufferSource} wasm
|
|
72
|
+
* @param {object} [opts] - Options passed to wasi()
|
|
73
|
+
* @returns {WebAssembly.Instance}
|
|
74
|
+
*/
|
|
75
|
+
export function instantiate(wasm, opts = {}) {
|
|
76
|
+
const imports = wasi(opts)
|
|
77
|
+
const inst = new WebAssembly.Instance(new WebAssembly.Module(wasm), imports)
|
|
78
|
+
imports._setMemory(inst.exports.memory)
|
|
79
|
+
return inst
|
|
80
|
+
}
|