jz 0.1.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/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 CHANGED
@@ -20,10 +20,16 @@
20
20
  */
21
21
  export function wasi(opts = {}) {
22
22
  let mem = null
23
- const write = opts.write || ((fd, text) => {
24
- if (fd === 1) typeof process !== 'undefined' && process.stdout ? process.stdout.write(text) : console.log(text.replace(/\n$/, ''))
25
- else typeof process !== 'undefined' && process.stderr ? process.stderr.write(text) : console.warn(text.replace(/\n$/, ''))
26
- })
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
27
33
 
28
34
  return {
29
35
  wasi_snapshot_preview1: {