jz 0.1.1 → 0.2.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/wasi.js CHANGED
@@ -17,6 +17,7 @@
17
17
  * Create WASI import object for WebAssembly instantiation.
18
18
  * @param {object} [opts]
19
19
  * @param {function} [opts.write] - Custom write: (fd, text) => void
20
+ * @param {function} [opts.read] - Custom read: (fd, buf: Uint8Array) => bytesRead
20
21
  */
21
22
  export function wasi(opts = {}) {
22
23
  let mem = null
@@ -33,6 +34,18 @@ export function wasi(opts = {}) {
33
34
 
34
35
  return {
35
36
  wasi_snapshot_preview1: {
37
+ fd_read(fd, iovs, iovs_len, nread) {
38
+ const dv = new DataView(mem.buffer)
39
+ let total = 0
40
+ for (let i = 0; i < iovs_len; i++) {
41
+ const ptr = dv.getUint32(iovs + i * 8, true)
42
+ const len = dv.getUint32(iovs + i * 8 + 4, true)
43
+ const buf = new Uint8Array(mem.buffer, ptr, len)
44
+ total += opts.read ? (opts.read(fd, buf) || 0) : 0
45
+ }
46
+ dv.setUint32(nread, total, true)
47
+ return 0
48
+ },
36
49
  fd_write(fd, iovs, iovs_len, nwritten) {
37
50
  const dv = new DataView(mem.buffer)
38
51
  let written = 0
package/src/key.js DELETED
@@ -1,73 +0,0 @@
1
- /** Static property-key evaluation for computed member names. */
2
-
3
- const NO_VALUE = Symbol('no-static-property-key')
4
-
5
- export function staticPropertyKey(node) {
6
- const value = staticValue(node)
7
- return value === NO_VALUE ? null : String(value)
8
- }
9
-
10
- function staticValue(node) {
11
- if (node === undefined) return undefined
12
- if (node === null || typeof node === 'number' || typeof node === 'string' || typeof node === 'boolean') return node
13
- if (!Array.isArray(node)) return NO_VALUE
14
-
15
- const [op, ...args] = node
16
- if (op == null) return args.length ? args[0] : undefined
17
- if (op === 'str') return args[0]
18
- if (op === '[]' && args.length === 1) return staticValue(args[0])
19
- if (op === '()' && args[0] === 'String' && args.length === 2) {
20
- const value = staticValue(args[1])
21
- return value === NO_VALUE ? NO_VALUE : String(value)
22
- }
23
- if (op === '()' && args[0] === 'Number' && args.length === 2) {
24
- const value = staticValue(args[1])
25
- return value === NO_VALUE ? NO_VALUE : Number(value)
26
- }
27
- if (op === '?:' || op === '?') {
28
- const cond = staticValue(args[0])
29
- return cond === NO_VALUE ? NO_VALUE : staticValue(cond ? args[1] : args[2])
30
- }
31
- if (op === '&&' || op === '||') {
32
- const left = staticValue(args[0])
33
- if (left === NO_VALUE) return NO_VALUE
34
- return op === '&&' ? (left ? staticValue(args[1]) : left) : (left ? left : staticValue(args[1]))
35
- }
36
- if (op === '??') {
37
- const left = staticValue(args[0])
38
- return left === NO_VALUE ? NO_VALUE : left == null ? staticValue(args[1]) : left
39
- }
40
-
41
- if (args.length === 1) {
42
- const value = staticValue(args[0])
43
- if (value === NO_VALUE) return NO_VALUE
44
- if (op === 'u+') return +value
45
- if (op === 'u-') return -value
46
- if (op === '!') return !value
47
- if (op === '~') return ~value
48
- return NO_VALUE
49
- }
50
-
51
- if (args.length === 2) {
52
- const left = staticValue(args[0])
53
- const right = staticValue(args[1])
54
- if (left === NO_VALUE || right === NO_VALUE) return NO_VALUE
55
- switch (op) {
56
- case '+': return typeof left === 'string' || typeof right === 'string' ? String(left) + String(right) : Number(left) + Number(right)
57
- case '-': return Number(left) - Number(right)
58
- case '*': return Number(left) * Number(right)
59
- case '/': return Number(left) / Number(right)
60
- case '%': return Number(left) % Number(right)
61
- case '**': return Number(left) ** Number(right)
62
- case '&': return Number(left) & Number(right)
63
- case '|': return Number(left) | Number(right)
64
- case '^': return Number(left) ^ Number(right)
65
- case '<<': return Number(left) << Number(right)
66
- case '>>': return Number(left) >> Number(right)
67
- case '>>>': return Number(left) >>> Number(right)
68
- default: return NO_VALUE
69
- }
70
- }
71
-
72
- return NO_VALUE
73
- }
package/src/source.js DELETED
@@ -1,76 +0,0 @@
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
- }