quickjs-emscripten-sync 1.5.1 → 1.6.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.
@@ -1,4 +1,59 @@
1
- const F = [
1
+ var D = Object.defineProperty;
2
+ var A = (n, e, t) => e in n ? D(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t;
3
+ var c = (n, e, t) => A(n, typeof e != "symbol" ? e + "" : e, t);
4
+ const B = (n) => {
5
+ const e = new U(n);
6
+ return new Proxy(n, {
7
+ get(t, r, s) {
8
+ return r in e ? e[r] : Reflect.get(t, r, s);
9
+ }
10
+ });
11
+ };
12
+ class U {
13
+ constructor(e) {
14
+ c(this, "context");
15
+ c(this, "fn");
16
+ c(this, "fnGenerator");
17
+ c(this, "fnCounter", Number.MIN_SAFE_INTEGER);
18
+ c(this, "fnMap", /* @__PURE__ */ new Map());
19
+ /** Similar to the original newFunction, but no matter how many new functions are generated, newFunction is called only once. */
20
+ c(this, "newFunction", (e, t) => {
21
+ this.fnCounter++;
22
+ const r = this.fnCounter;
23
+ return this.fnMap.set(r, t), this.context.unwrapResult(
24
+ this.context.callFunction(
25
+ this.fnGenerator,
26
+ this.context.undefined,
27
+ this.context.newString(e),
28
+ this.context.newNumber(t.length),
29
+ this.context.newNumber(r),
30
+ this.fn
31
+ )
32
+ );
33
+ });
34
+ this.context = e;
35
+ const t = this.fnMap;
36
+ this.fn = this.context.newFunction("", function(r, ...s) {
37
+ const o = e.getNumber(r), i = t.get(o);
38
+ if (!i) throw new Error("function is not registered");
39
+ return i.call(this, ...s);
40
+ }), this.fnGenerator = e.unwrapResult(
41
+ e.evalCode(`((name, length, id, f) => {
42
+ const fn = function(...args) {
43
+ return f.call(this, id, ...args);
44
+ };
45
+ fn.name = name;
46
+ fn.length = length;
47
+ return fn;
48
+ })`)
49
+ );
50
+ }
51
+ disposeEx() {
52
+ this.fnGenerator.dispose(), this.fn.dispose();
53
+ }
54
+ }
55
+ const I = [
56
+ // basic objects
2
57
  [Symbol, "Symbol"],
3
58
  [Symbol.prototype, "Symbol.prototype"],
4
59
  [Object, "Object"],
@@ -9,6 +64,9 @@ const F = [
9
64
  [Boolean.prototype, "Boolean.prototype"],
10
65
  [Array, "Array"],
11
66
  [Array.prototype, "Array.prototype"],
67
+ // [BigInt, "BigInt"],
68
+ // [BigInt.prototype, "BigInt.prototype"],
69
+ // errors
12
70
  [Error, "Error"],
13
71
  [Error.prototype, "Error.prototype"],
14
72
  [EvalError, "EvalError"],
@@ -23,269 +81,264 @@ const F = [
23
81
  [TypeError.prototype, "TypeError.prototype"],
24
82
  [URIError, "URIError"],
25
83
  [URIError.prototype, "URIError.prototype"],
26
- ...Object.getOwnPropertyNames(Symbol).filter((t) => typeof Symbol[t] == "symbol").map((t) => [Symbol[t], `Symbol.${t}`])
84
+ // built-in symbols
85
+ ...Object.getOwnPropertyNames(Symbol).filter((n) => typeof Symbol[n] == "symbol").map((n) => [Symbol[n], `Symbol.${n}`])
27
86
  ];
28
- function A(t, e) {
29
- const r = t.unwrapResult(t.evalCode(e)), o = (n, ...s) => t.unwrapResult(t.callFunction(r, n != null ? n : t.undefined, ...s));
30
- return o.dispose = () => r.dispose(), o.alive = !0, Object.defineProperty(o, "alive", {
31
- get: () => r.alive
32
- }), o;
33
- }
34
- function d(t, e, r, ...o) {
35
- const n = A(t, e);
87
+ function T(n, e) {
88
+ const t = n.unwrapResult(n.evalCode(e)), r = (s, ...o) => n.unwrapResult(n.callFunction(t, s != null ? s : n.undefined, ...o));
89
+ return r.dispose = () => t.dispose(), r.alive = !0, Object.defineProperty(r, "alive", {
90
+ get: () => t.alive
91
+ }), r;
92
+ }
93
+ function y(n, e, t, ...r) {
94
+ const s = T(n, e);
36
95
  try {
37
- return n(r, ...o);
96
+ return s(t, ...r);
38
97
  } finally {
39
- n.dispose();
98
+ s.dispose();
40
99
  }
41
100
  }
42
- function N(t, e, r) {
43
- return t.dump(d(t, "Object.is", void 0, e, r));
101
+ function G(n, e, t) {
102
+ return n.dump(y(n, "Object.is", void 0, e, t));
44
103
  }
45
- function B(t, e, r) {
46
- return t.dump(d(t, "(a, b) => a instanceof b", void 0, e, r));
104
+ function J(n, e, t) {
105
+ return n.dump(y(n, "(a, b) => a instanceof b", void 0, e, t));
47
106
  }
48
- function J(t, e) {
49
- return t.dump(
50
- d(t, 'a => typeof a === "object" && a !== null || typeof a === "function"', void 0, e)
107
+ function W(n, e) {
108
+ return n.dump(
109
+ y(n, 'a => typeof a === "object" && a !== null || typeof a === "function"', void 0, e)
51
110
  );
52
111
  }
53
- function I(t, e) {
54
- const r = JSON.stringify(e);
55
- return r ? d(t, "JSON.parse", void 0, t.newString(r)) : t.undefined;
112
+ function z(n, e) {
113
+ const t = JSON.stringify(e);
114
+ return t ? y(n, "JSON.parse", void 0, n.newString(t)) : n.undefined;
56
115
  }
57
- function T(t, e) {
116
+ function be(n, e) {
58
117
  try {
59
- return e(t);
118
+ return e(n);
60
119
  } finally {
61
- for (const r of t)
62
- r.alive && r.dispose();
120
+ for (const t of n)
121
+ t.alive && t.dispose();
63
122
  }
64
123
  }
65
- function U([t, e], r) {
124
+ function $([n, e], t) {
66
125
  try {
67
- return r(t);
126
+ return t(n);
68
127
  } finally {
69
- e && t.dispose();
128
+ e && n.dispose();
70
129
  }
71
130
  }
72
- function w(t, e) {
131
+ function g(n, e) {
73
132
  try {
74
- return e(...t.map((r) => r[0]));
133
+ return e(...n.map((t) => t[0]));
75
134
  } finally {
76
- for (const [r, o] of t)
77
- o && r.dispose();
135
+ for (const [t, r] of n)
136
+ r && t.dispose();
78
137
  }
79
138
  }
80
- function W(t) {
81
- return "handle" in t;
139
+ function L(n) {
140
+ return "handle" in n;
82
141
  }
83
- function z(t) {
84
- return W(t) ? t.handle : t;
142
+ function q(n) {
143
+ return L(n) ? n.handle : n;
85
144
  }
86
- function $(t, e, r, o) {
87
- var s;
88
- let n;
89
- for (const i of o)
90
- if (n = i(e, t), n)
91
- break;
92
- return n ? (s = r(e, n)) != null ? s : n : void 0;
145
+ function Q(n, e, t, r) {
146
+ var o;
147
+ let s;
148
+ for (const i of r)
149
+ if (s = i(e, n), s) break;
150
+ return s ? (o = t(e, s)) != null ? o : s : void 0;
93
151
  }
94
- function G(t, e) {
95
- return typeof t != "symbol" ? void 0 : d(
152
+ function V(n, e) {
153
+ return typeof n != "symbol" ? void 0 : y(
96
154
  e,
97
155
  "d => Symbol(d)",
98
156
  void 0,
99
- t.description ? e.newString(t.description) : e.undefined
157
+ n.description ? e.newString(n.description) : e.undefined
100
158
  );
101
159
  }
102
- function q(t, e) {
103
- return t instanceof Date ? d(e, "d => new Date(d)", void 0, e.newNumber(t.getTime())) : void 0;
160
+ function X(n, e) {
161
+ return n instanceof Date ? y(e, "d => new Date(d)", void 0, e.newNumber(n.getTime())) : void 0;
104
162
  }
105
- const Q = [G, q];
106
- function V(t) {
107
- return typeof t == "function" && /^class\s/.test(Function.prototype.toString.call(t));
163
+ const Y = [V, X];
164
+ function Z(n) {
165
+ return typeof n == "function" && /^class\s/.test(Function.prototype.toString.call(n));
108
166
  }
109
- function v(t) {
110
- return typeof t == "function" || typeof t == "object" && t !== null;
167
+ function v(n) {
168
+ return typeof n == "function" || typeof n == "object" && n !== null;
111
169
  }
112
- function S(t, e) {
113
- const r = /* @__PURE__ */ new Set(), o = (n) => {
114
- if (!(!v(n) || r.has(n) || (e == null ? void 0 : e(n, r)) === !1)) {
115
- if (r.add(n), Array.isArray(n)) {
116
- for (const s of n)
117
- o(s);
170
+ function E(n, e) {
171
+ const t = /* @__PURE__ */ new Set(), r = (s) => {
172
+ if (!(!v(s) || t.has(s) || (e == null ? void 0 : e(s, t)) === !1)) {
173
+ if (t.add(s), Array.isArray(s)) {
174
+ for (const o of s)
175
+ r(o);
118
176
  return;
119
177
  }
120
- if (typeof n == "object") {
121
- const s = Object.getPrototypeOf(n);
122
- s && s !== Object.prototype && o(s);
178
+ if (typeof s == "object") {
179
+ const o = Object.getPrototypeOf(s);
180
+ o && o !== Object.prototype && r(o);
123
181
  }
124
- for (const s of Object.values(Object.getOwnPropertyDescriptors(n)))
125
- "value" in s && o(s.value), "get" in s && o(s.get), "set" in s && o(s.set);
182
+ for (const o of Object.values(Object.getOwnPropertyDescriptors(s)))
183
+ "value" in o && r(o.value), "get" in o && r(o.get), "set" in o && r(o.set);
126
184
  }
127
185
  };
128
- return o(t), r;
186
+ return r(n), t;
129
187
  }
130
- function de(t, e) {
131
- return S(t, e ? (r, o) => o.size < e : void 0).size;
188
+ function ve(n, e) {
189
+ return E(n, e ? (t, r) => r.size < e : void 0).size;
132
190
  }
133
- function L() {
134
- let t = () => {
191
+ function K() {
192
+ let n = () => {
135
193
  }, e = () => {
136
194
  };
137
195
  return {
138
- promise: new Promise((o, n) => {
139
- t = o, e = n;
196
+ promise: new Promise((r, s) => {
197
+ n = r, e = s;
140
198
  }),
141
- resolve: t,
199
+ resolve: n,
142
200
  reject: e
143
201
  };
144
202
  }
145
- function E(t, e, r, o) {
146
- const n = t.newObject(), s = (u, a) => {
147
- const p = o(u), l = typeof a.value == "undefined" ? void 0 : o(a.value), f = typeof a.get == "undefined" ? void 0 : o(a.get), c = typeof a.set == "undefined" ? void 0 : o(a.set);
148
- t.newObject().consume((h) => {
149
- Object.entries(a).forEach(([m, y]) => {
150
- const _ = m === "value" ? l : m === "get" ? f : m === "set" ? c : y ? t.true : t.false;
151
- _ && t.setProp(h, m, _);
152
- }), t.setProp(n, p, h);
203
+ function M(n, e, t, r) {
204
+ const s = n.newObject(), o = (a, u) => {
205
+ const p = r(a), l = typeof u.value == "undefined" ? void 0 : r(u.value), f = typeof u.get == "undefined" ? void 0 : r(u.get), d = typeof u.set == "undefined" ? void 0 : r(u.set);
206
+ n.newObject().consume((m) => {
207
+ Object.entries(u).forEach(([h, _]) => {
208
+ const b = h === "value" ? l : h === "get" ? f : h === "set" ? d : _ ? n.true : n.false;
209
+ b && n.setProp(m, h, b);
210
+ }), n.setProp(s, p, m);
153
211
  });
154
212
  }, i = Object.getOwnPropertyDescriptors(e);
155
- Object.entries(i).forEach(([u, a]) => s(u, a)), Object.getOwnPropertySymbols(i).forEach((u) => s(u, i[u])), d(t, "Object.defineProperties", void 0, r, n).dispose(), n.dispose();
213
+ Object.entries(i).forEach(([a, u]) => o(a, u)), Object.getOwnPropertySymbols(i).forEach((a) => o(a, i[a])), y(n, "Object.defineProperties", void 0, t, s).dispose(), s.dispose();
156
214
  }
157
- function X(t, e, r, o, n, s) {
158
- var a;
159
- if (typeof e != "function")
160
- return;
161
- const i = t.newFunction(e.name, function(...p) {
162
- const l = o(this), f = p.map((c) => o(c));
163
- if (V(e) && v(l)) {
164
- const c = new e(...f);
165
- return Object.entries(c).forEach(([h, m]) => {
166
- t.setProp(this, h, r(m));
215
+ function x(n, e, t, r, s, o) {
216
+ var u;
217
+ if (typeof e != "function") return;
218
+ const i = n.newFunction(e.name, function(...p) {
219
+ const l = r(this), f = p.map((d) => r(d));
220
+ if (Z(e) && v(l)) {
221
+ const d = new e(...f);
222
+ return Object.entries(d).forEach(([m, h]) => {
223
+ n.setProp(this, m, t(h));
167
224
  }), this;
168
225
  }
169
- return r(s ? s(e, l, f) : e.apply(l, f));
226
+ return t(o ? o(e, l, f) : e.apply(l, f));
170
227
  }).consume(
171
- (p) => d(
172
- t,
173
- `Cls => {
228
+ (p) => (
229
+ // fucntions created by vm.newFunction are not callable as a class constrcutor
230
+ y(
231
+ n,
232
+ `Cls => {
174
233
  const fn = function(...args) { return Cls.apply(this, args); };
175
234
  fn.name = Cls.name;
176
235
  fn.length = Cls.length;
177
236
  return fn;
178
237
  }`,
179
- void 0,
180
- p
238
+ void 0,
239
+ p
240
+ )
181
241
  )
182
- ), u = (a = n(e, i)) != null ? a : i;
183
- return E(t, e, i, r), u;
242
+ ), a = (u = s(e, i)) != null ? u : i;
243
+ return M(n, e, i, t), a;
184
244
  }
185
- function Y(t, e, r) {
186
- var s;
187
- const o = I(t, e);
188
- return (s = r(e, o)) != null ? s : o;
245
+ function ee(n, e, t) {
246
+ var o;
247
+ const r = z(n, e);
248
+ return (o = t(e, r)) != null ? o : r;
189
249
  }
190
- function Z(t, e, r, o) {
191
- var a;
192
- if (typeof e != "object" || e === null)
193
- return;
194
- const n = Array.isArray(e) ? t.newArray() : t.newObject(), s = (a = o(e, n)) != null ? a : n, i = Object.getPrototypeOf(e), u = i && i !== Object.prototype && i !== Array.prototype ? r(i) : void 0;
195
- return u && d(t, "Object.setPrototypeOf", void 0, s, u).dispose(), E(t, e, n, r), s;
250
+ function te(n, e, t, r) {
251
+ var u;
252
+ if (typeof e != "object" || e === null) return;
253
+ const s = Array.isArray(e) ? n.newArray() : n.newObject(), o = (u = r(e, s)) != null ? u : s, i = Object.getPrototypeOf(e), a = i && i !== Object.prototype && i !== Array.prototype ? t(i) : void 0;
254
+ return a && y(n, "Object.setPrototypeOf", void 0, o, a).dispose(), M(n, e, s, t), o;
196
255
  }
197
- function K(t, e) {
256
+ function ne(n, e) {
198
257
  switch (typeof e) {
199
258
  case "undefined":
200
- return t.undefined;
259
+ return n.undefined;
201
260
  case "number":
202
- return t.newNumber(e);
261
+ return n.newNumber(e);
203
262
  case "string":
204
- return t.newString(e);
263
+ return n.newString(e);
205
264
  case "boolean":
206
- return e ? t.true : t.false;
265
+ return e ? n.true : n.false;
207
266
  case "object":
208
- return e === null ? t.null : void 0;
267
+ return e === null ? n.null : void 0;
209
268
  }
210
269
  }
211
- function x(t, e, r, o) {
212
- var s;
213
- if (!(e instanceof Promise))
214
- return;
215
- const n = t.newPromise();
270
+ function re(n, e, t, r) {
271
+ var o;
272
+ if (!(e instanceof Promise)) return;
273
+ const s = n.newPromise();
216
274
  return e.then(
217
- (i) => n.resolve(r(i)),
218
- (i) => n.reject(r(i))
219
- ), (s = o(e, n)) != null ? s : n.handle;
275
+ (i) => s.resolve(t(i)),
276
+ (i) => s.reject(t(i))
277
+ ), (o = r(e, s)) != null ? o : s.handle;
220
278
  }
221
- function R(t, e) {
222
- var l, f, c, h, m;
223
- const { ctx: r, unmarshal: o, isMarshalable: n, find: s, pre: i } = e;
279
+ function R(n, e) {
280
+ var l, f, d, m, h;
281
+ const { ctx: t, unmarshal: r, isMarshalable: s, find: o, pre: i } = e;
224
282
  {
225
- const y = K(r, t);
226
- if (y)
227
- return y;
283
+ const _ = ne(t, n);
284
+ if (_)
285
+ return _;
228
286
  }
229
287
  {
230
- const y = s(t);
231
- if (y)
232
- return y;
233
- }
234
- const u = n == null ? void 0 : n(t);
235
- if (u === !1)
236
- return r.undefined;
237
- const a = (y, _) => i(y, _, u);
238
- if (u === "json")
239
- return Y(r, t, a);
240
- const p = (y) => R(y, e);
241
- return (m = (h = (c = (f = $(r, t, a, [...Q, ...(l = e.custom) != null ? l : []])) != null ? f : x(r, t, p, a)) != null ? c : X(r, t, p, o, a, e.preApply)) != null ? h : Z(r, t, p, a)) != null ? m : r.undefined;
242
- }
243
- function ee(t, e, r, o) {
244
- var s;
245
- let n;
246
- for (const i of o)
247
- if (n = i(e, t), n)
248
- break;
249
- return n ? (s = r(n, e)) != null ? s : n : void 0;
250
- }
251
- function te(t, e) {
252
- if (e.typeof(t) !== "symbol")
253
- return;
254
- const r = e.getString(e.getProp(t, "description"));
255
- return Symbol(r);
288
+ const _ = o(n);
289
+ if (_) return _;
290
+ }
291
+ const a = s == null ? void 0 : s(n);
292
+ if (a === !1)
293
+ return t.undefined;
294
+ const u = (_, b) => i(_, b, a);
295
+ if (a === "json")
296
+ return ee(t, n, u);
297
+ const p = (_) => R(_, e);
298
+ return (h = (m = (d = (f = Q(t, n, u, [...Y, ...(l = e.custom) != null ? l : []])) != null ? f : re(t, n, p, u)) != null ? d : x(t, n, p, r, u, e.preApply)) != null ? m : te(t, n, p, u)) != null ? h : t.undefined;
256
299
  }
257
- function re(t, e) {
258
- if (!e.dump(d(e, "a => a instanceof Date", void 0, t)))
259
- return;
260
- const r = e.getNumber(d(e, "a => a.getTime()", void 0, t));
261
- return new Date(r);
300
+ function se(n, e, t, r) {
301
+ var o;
302
+ let s;
303
+ for (const i of r)
304
+ if (s = i(e, n), s) break;
305
+ return s ? (o = t(s, e)) != null ? o : s : void 0;
306
+ }
307
+ function oe(n, e) {
308
+ if (e.typeof(n) !== "symbol") return;
309
+ const t = e.getString(e.getProp(n, "description"));
310
+ return Symbol(t);
311
+ }
312
+ function ie(n, e) {
313
+ if (!e.dump(y(e, "a => a instanceof Date", void 0, n))) return;
314
+ const t = e.getNumber(y(e, "a => a.getTime()", void 0, n));
315
+ return new Date(t);
262
316
  }
263
- const ne = [te, re];
264
- function k(t, e, r, o) {
265
- t.newFunction("", (n, s) => {
266
- const [i] = o(n);
317
+ const ue = [oe, ie];
318
+ function k(n, e, t, r) {
319
+ n.newFunction("", (s, o) => {
320
+ const [i] = r(s);
267
321
  if (typeof i != "string" && typeof i != "number" && typeof i != "symbol")
268
322
  return;
269
- const u = [
323
+ const a = [
270
324
  ["value", !0],
271
325
  ["get", !0],
272
326
  ["set", !0],
273
327
  ["configurable", !1],
274
328
  ["enumerable", !1],
275
329
  ["writable", !1]
276
- ].reduce((a, [p, l]) => {
277
- const f = t.getProp(s, p), c = t.typeof(f);
278
- if (c === "undefined")
279
- return a;
280
- if (!l && c === "boolean")
281
- return a[p] = t.dump(t.getProp(s, p)), a;
282
- const [h, m] = o(f);
283
- return m && f.dispose(), a[p] = h, a;
330
+ ].reduce((u, [p, l]) => {
331
+ const f = n.getProp(o, p), d = n.typeof(f);
332
+ if (d === "undefined") return u;
333
+ if (!l && d === "boolean")
334
+ return u[p] = n.dump(n.getProp(o, p)), u;
335
+ const [m, h] = r(f);
336
+ return h && f.dispose(), u[p] = m, u;
284
337
  }, {});
285
- Object.defineProperty(r, i, u);
286
- }).consume((n) => {
287
- d(
288
- t,
338
+ Object.defineProperty(t, i, a);
339
+ }).consume((s) => {
340
+ y(
341
+ n,
289
342
  `(o, fn) => {
290
343
  const descs = Object.getOwnPropertyDescriptors(o);
291
344
  Object.entries(descs).forEach(([k, v]) => fn(k, v));
@@ -293,89 +346,98 @@ function k(t, e, r, o) {
293
346
  }`,
294
347
  void 0,
295
348
  e,
296
- n
349
+ s
297
350
  ).dispose();
298
351
  });
299
352
  }
300
- function se(t, e, r, o, n) {
301
- var u;
302
- if (t.typeof(e) !== "function")
303
- return;
304
- const s = function(...a) {
305
- return w(
306
- [r(this), ...a.map((p) => r(p))],
353
+ function ae(n, e, t, r, s) {
354
+ var a;
355
+ if (n.typeof(e) !== "function") return;
356
+ const o = function(...u) {
357
+ return g(
358
+ [t(this), ...u.map((p) => t(p))],
307
359
  (p, ...l) => {
308
360
  if (new.target) {
309
- const [m] = o(
310
- d(t, "(Cls, ...args) => new Cls(...args)", p, e, ...l)
361
+ const [h] = r(
362
+ y(n, "(Cls, ...args) => new Cls(...args)", p, e, ...l)
311
363
  );
312
- return Object.defineProperties(this, Object.getOwnPropertyDescriptors(m)), this;
364
+ return Object.defineProperties(this, Object.getOwnPropertyDescriptors(h)), this;
313
365
  }
314
- const f = t.unwrapResult(t.callFunction(e, p, ...l)), [c, h] = o(f);
315
- return h && f.dispose(), c;
366
+ const f = n.unwrapResult(n.callFunction(e, p, ...l)), [d, m] = r(f);
367
+ return m && f.dispose(), d;
316
368
  }
317
369
  );
318
- }, i = (u = n(s, e)) != null ? u : s;
319
- return k(t, e, s, o), i;
370
+ }, i = (a = s(o, e)) != null ? a : o;
371
+ return k(n, e, o, r), i;
320
372
  }
321
- function oe(t, e, r, o) {
322
- var u;
323
- if (t.typeof(e) !== "object" || t.unwrapResult(t.evalCode("o => o === null")).consume((a) => t.dump(t.unwrapResult(t.callFunction(a, t.undefined, e)))))
373
+ function pe(n, e, t, r) {
374
+ var a;
375
+ if (n.typeof(e) !== "object" || // null check
376
+ n.unwrapResult(n.evalCode("o => o === null")).consume((u) => n.dump(n.unwrapResult(n.callFunction(u, n.undefined, e)))))
324
377
  return;
325
- const n = d(t, "Array.isArray", void 0, e).consume((a) => t.dump(a)) ? [] : {}, s = (u = o(n, e)) != null ? u : n, i = d(
326
- t,
378
+ const s = y(n, "Array.isArray", void 0, e).consume((u) => n.dump(u)) ? [] : {}, o = (a = r(s, e)) != null ? a : s, i = y(
379
+ n,
327
380
  `o => {
328
381
  const p = Object.getPrototypeOf(o);
329
382
  return !p || p === Object.prototype || p === Array.prototype ? undefined : p;
330
383
  }`,
331
384
  void 0,
332
385
  e
333
- ).consume((a) => {
334
- if (t.typeof(a) === "undefined")
335
- return;
336
- const [p] = r(a);
386
+ ).consume((u) => {
387
+ if (n.typeof(u) === "undefined") return;
388
+ const [p] = t(u);
337
389
  return p;
338
390
  });
339
- return typeof i == "object" && Object.setPrototypeOf(s, i), k(t, e, n, r), s;
391
+ return typeof i == "object" && Object.setPrototypeOf(o, i), k(n, e, s, t), o;
340
392
  }
341
- function ie(t, e) {
342
- const r = t.typeof(e);
343
- return r === "undefined" || r === "number" || r === "string" || r === "boolean" ? [t.dump(e), !0] : r === "object" && t.unwrapResult(t.evalCode("a => a === null")).consume((n) => t.dump(t.unwrapResult(t.callFunction(n, t.undefined, e)))) ? [null, !0] : [void 0, !1];
393
+ function fe(n, e) {
394
+ const t = n.typeof(e);
395
+ return t === "undefined" || t === "number" || t === "string" || t === "boolean" ? [n.dump(e), !0] : t === "object" && n.unwrapResult(n.evalCode("a => a === null")).consume((s) => n.dump(n.unwrapResult(n.callFunction(s, n.undefined, e)))) ? [null, !0] : [void 0, !1];
344
396
  }
345
- function ue(t, e, r, o) {
397
+ function ce(n, e, t, r) {
346
398
  var p;
347
- if (!ae(t, e))
348
- return;
349
- const n = L(), [s, i] = r(n.resolve), [u, a] = r(n.reject);
350
- return d(t, "(p, res, rej) => { p.then(res, rej); }", void 0, e, s, u), i && s.dispose(), a && u.dispose(), (p = o(n.promise, e)) != null ? p : n.promise;
399
+ if (!le(n, e)) return;
400
+ const s = K(), [o, i] = t(s.resolve), [a, u] = t(s.reject);
401
+ return y(n, "(p, res, rej) => { p.then(res, rej); }", void 0, e, o, a), i && o.dispose(), u && a.dispose(), (p = r(s.promise, e)) != null ? p : s.promise;
351
402
  }
352
- function ae(t, e) {
353
- return e.owner ? t.unwrapResult(t.evalCode("Promise")).consume((r) => e.owner ? B(t, e, r) : !1) : !1;
403
+ function le(n, e) {
404
+ return e.owner ? n.unwrapResult(n.evalCode("Promise")).consume((t) => e.owner ? J(n, e, t) : !1) : !1;
354
405
  }
355
- function pe(t, e) {
356
- const [r] = M(t, e);
357
- return r;
406
+ function de(n, e) {
407
+ const [t] = C(n, e);
408
+ return t;
358
409
  }
359
- function M(t, e) {
360
- var a, p, l, f;
361
- const { ctx: r, marshal: o, find: n, pre: s } = e;
410
+ function C(n, e) {
411
+ var u, p, l, f;
412
+ const { ctx: t, marshal: r, find: s, pre: o } = e;
362
413
  {
363
- const [c, h] = ie(r, t);
364
- if (h)
365
- return [c, !1];
414
+ const [d, m] = fe(t, n);
415
+ if (m) return [d, !1];
366
416
  }
367
417
  {
368
- const c = n(t);
369
- if (c)
370
- return [c, !0];
418
+ const d = s(n);
419
+ if (d)
420
+ return [d, !0];
371
421
  }
372
- const i = (c) => M(c, e);
373
- return [(f = (l = (p = ee(r, t, s, [...ne, ...(a = e.custom) != null ? a : []])) != null ? p : ue(r, t, o, s)) != null ? l : se(r, t, o, i, s)) != null ? f : oe(r, t, i, s), !1];
422
+ const i = (d) => C(d, e);
423
+ return [(f = (l = (p = se(t, n, o, [...ue, ...(u = e.custom) != null ? u : []])) != null ? p : ce(t, n, r, o)) != null ? l : ae(t, n, r, i, o)) != null ? f : pe(t, n, i, o), !1];
374
424
  }
375
425
  class O {
376
426
  constructor(e) {
377
- this._map1 = /* @__PURE__ */ new Map(), this._map2 = /* @__PURE__ */ new Map(), this._map3 = /* @__PURE__ */ new Map(), this._map4 = /* @__PURE__ */ new Map(), this._counterMap = /* @__PURE__ */ new Map(), this._disposables = /* @__PURE__ */ new Set(), this._counter = 0, this.ctx = e;
378
- const r = e.unwrapResult(
427
+ c(this, "ctx");
428
+ c(this, "_map1", /* @__PURE__ */ new Map());
429
+ c(this, "_map2", /* @__PURE__ */ new Map());
430
+ c(this, "_map3", /* @__PURE__ */ new Map());
431
+ c(this, "_map4", /* @__PURE__ */ new Map());
432
+ c(this, "_counterMap", /* @__PURE__ */ new Map());
433
+ c(this, "_disposables", /* @__PURE__ */ new Set());
434
+ c(this, "_mapGet");
435
+ c(this, "_mapSet");
436
+ c(this, "_mapDelete");
437
+ c(this, "_mapClear");
438
+ c(this, "_counter", Number.MIN_SAFE_INTEGER);
439
+ this.ctx = e;
440
+ const t = e.unwrapResult(
379
441
  e.evalCode(`() => {
380
442
  const mapSym = new Map();
381
443
  let map = new WeakMap();
@@ -400,39 +462,38 @@ class O {
400
462
  }
401
463
  };
402
464
  }`)
403
- ).consume((o) => this._call(o, void 0));
404
- this._mapGet = e.getProp(r, "get"), this._mapSet = e.getProp(r, "set"), this._mapDelete = e.getProp(r, "delete"), this._mapClear = e.getProp(r, "clear"), r.dispose(), this._disposables.add(this._mapGet), this._disposables.add(this._mapSet), this._disposables.add(this._mapDelete), this._disposables.add(this._mapClear);
405
- }
406
- set(e, r, o, n) {
407
- var u;
408
- if (!r.alive || n && !n.alive)
409
- return !1;
410
- const s = (u = this.get(e)) != null ? u : this.get(o);
411
- if (s)
412
- return s === r || s === n;
465
+ ).consume((r) => this._call(r, void 0));
466
+ this._mapGet = e.getProp(t, "get"), this._mapSet = e.getProp(t, "set"), this._mapDelete = e.getProp(t, "delete"), this._mapClear = e.getProp(t, "clear"), t.dispose(), this._disposables.add(this._mapGet), this._disposables.add(this._mapSet), this._disposables.add(this._mapDelete), this._disposables.add(this._mapClear);
467
+ }
468
+ set(e, t, r, s) {
469
+ var a;
470
+ if (!t.alive || s && !s.alive) return !1;
471
+ const o = (a = this.get(e)) != null ? a : this.get(r);
472
+ if (o)
473
+ return o === t || o === s;
413
474
  const i = this._counter++;
414
- return this._map1.set(e, i), this._map3.set(i, r), this._counterMap.set(i, e), o && (this._map2.set(o, i), n && this._map4.set(i, n)), this.ctx.newNumber(i).consume((a) => {
415
- this._call(this._mapSet, void 0, r, a, n != null ? n : this.ctx.undefined);
475
+ return this._map1.set(e, i), this._map3.set(i, t), this._counterMap.set(i, e), r && (this._map2.set(r, i), s && this._map4.set(i, s)), this.ctx.newNumber(i).consume((u) => {
476
+ this._call(this._mapSet, void 0, t, u, s != null ? s : this.ctx.undefined);
416
477
  }), !0;
417
478
  }
418
479
  merge(e) {
419
- if (!!e)
420
- for (const r of e)
421
- !r || r[1] && this.set(r[0], r[1], r[2], r[3]);
480
+ if (e)
481
+ for (const t of e)
482
+ t && t[1] && this.set(t[0], t[1], t[2], t[3]);
422
483
  }
423
484
  get(e) {
424
- var n;
425
- const r = (n = this._map1.get(e)) != null ? n : this._map2.get(e), o = typeof r == "number" ? this._map3.get(r) : void 0;
426
- if (!!o) {
427
- if (!o.alive) {
485
+ var s;
486
+ const t = (s = this._map1.get(e)) != null ? s : this._map2.get(e), r = typeof t == "number" ? this._map3.get(t) : void 0;
487
+ if (r) {
488
+ if (!r.alive) {
428
489
  this.delete(e);
429
490
  return;
430
491
  }
431
- return o;
492
+ return r;
432
493
  }
433
494
  }
434
495
  getByHandle(e) {
435
- if (!!e.alive)
496
+ if (e.alive)
436
497
  return this._counterMap.get(this.ctx.getNumber(this._call(this._mapGet, void 0, e)));
437
498
  }
438
499
  has(e) {
@@ -444,37 +505,36 @@ class O {
444
505
  keys() {
445
506
  return this._map1.keys();
446
507
  }
447
- delete(e, r) {
508
+ delete(e, t) {
448
509
  var i;
449
- const o = (i = this._map1.get(e)) != null ? i : this._map2.get(e);
450
- if (typeof o == "undefined")
451
- return;
452
- const n = this._map3.get(o), s = this._map4.get(o);
510
+ const r = (i = this._map1.get(e)) != null ? i : this._map2.get(e);
511
+ if (typeof r == "undefined") return;
512
+ const s = this._map3.get(r), o = this._map4.get(r);
453
513
  this._call(
454
514
  this._mapDelete,
455
515
  void 0,
456
- ...[n, s].filter((u) => !!(u != null && u.alive))
457
- ), this._map1.delete(e), this._map2.delete(e), this._map3.delete(o), this._map4.delete(o);
458
- for (const [u, a] of this._map1)
459
- if (a === o) {
460
- this._map1.delete(u);
516
+ ...[s, o].filter((a) => !!(a != null && a.alive))
517
+ ), this._map1.delete(e), this._map2.delete(e), this._map3.delete(r), this._map4.delete(r);
518
+ for (const [a, u] of this._map1)
519
+ if (u === r) {
520
+ this._map1.delete(a);
461
521
  break;
462
522
  }
463
- for (const [u, a] of this._map2)
464
- if (a === o) {
465
- this._map2.delete(u);
523
+ for (const [a, u] of this._map2)
524
+ if (u === r) {
525
+ this._map2.delete(a);
466
526
  break;
467
527
  }
468
- for (const [u, a] of this._counterMap)
469
- if (a === e) {
470
- this._counterMap.delete(u);
528
+ for (const [a, u] of this._counterMap)
529
+ if (u === e) {
530
+ this._counterMap.delete(a);
471
531
  break;
472
532
  }
473
- r && (n != null && n.alive && n.dispose(), s != null && s.alive && s.dispose());
533
+ t && (s != null && s.alive && s.dispose(), o != null && o.alive && o.dispose());
474
534
  }
475
- deleteByHandle(e, r) {
476
- const o = this.getByHandle(e);
477
- typeof o != "undefined" && this.delete(o, r);
535
+ deleteByHandle(e, t) {
536
+ const r = this.getByHandle(e);
537
+ typeof r != "undefined" && this.delete(r, t);
478
538
  }
479
539
  clear() {
480
540
  this._counter = 0, this._map1.clear(), this._map2.clear(), this._map3.clear(), this._map4.clear(), this._counterMap.clear(), this._mapClear.alive && this._call(this._mapClear, void 0);
@@ -496,101 +556,100 @@ class O {
496
556
  return {
497
557
  next: () => {
498
558
  for (; ; ) {
499
- const r = e.next();
500
- if (r.done)
501
- return { value: void 0, done: !0 };
502
- const o = this._map1.get(r.value);
503
- if (typeof o == "undefined")
504
- continue;
505
- const n = this._map3.get(o), s = this._map4.get(o);
506
- if (!n)
507
- continue;
508
- const i = this._get2(o);
509
- return { value: [r.value, n, i, s], done: !1 };
559
+ const t = e.next();
560
+ if (t.done) return { value: void 0, done: !0 };
561
+ const r = this._map1.get(t.value);
562
+ if (typeof r == "undefined") continue;
563
+ const s = this._map3.get(r), o = this._map4.get(r);
564
+ if (!s) continue;
565
+ const i = this._get2(r);
566
+ return { value: [t.value, s, i, o], done: !1 };
510
567
  }
511
568
  }
512
569
  };
513
570
  }
514
571
  _get2(e) {
515
- for (const [r, o] of this._map2)
516
- if (o === e)
517
- return r;
572
+ for (const [t, r] of this._map2)
573
+ if (r === e) return t;
518
574
  }
519
- _call(e, r, ...o) {
575
+ _call(e, t, ...r) {
520
576
  return this.ctx.unwrapResult(
521
577
  this.ctx.callFunction(
522
578
  e,
523
- typeof r == "undefined" ? this.ctx.undefined : r,
524
- ...o
579
+ typeof t == "undefined" ? this.ctx.undefined : t,
580
+ ...r
525
581
  )
526
582
  );
527
583
  }
528
584
  }
529
- function fe(t, e, r, o, n, s, i) {
585
+ function me(n, e, t, r, s, o, i) {
530
586
  if (!v(e) || e instanceof Promise || e instanceof Date || i && !i(e))
531
587
  return;
532
- if (ce(e, r))
533
- return e;
534
- const u = new Proxy(e, {
535
- get(a, p) {
536
- return p === r ? a : Reflect.get(a, p);
588
+ if (ye(e, t)) return e;
589
+ const a = new Proxy(e, {
590
+ get(u, p) {
591
+ return p === t ? u : Reflect.get(u, p);
537
592
  },
538
- set(a, p, l, f) {
539
- var m;
540
- const c = b(l, r), h = (m = s == null ? void 0 : s(f)) != null ? m : "host";
541
- return h !== "vm" && !Reflect.set(a, p, c, f) || h === "host" || !t.alive || w(
542
- [n(f), n(p), n(c)],
543
- (y, _, j) => {
544
- const [P, C] = g(t, y, o);
545
- C ? P.consume((D) => t.setProp(D, _, j)) : t.setProp(P, _, j);
593
+ set(u, p, l, f) {
594
+ var h;
595
+ const d = w(l, t), m = (h = o == null ? void 0 : o(f)) != null ? h : "host";
596
+ return m !== "vm" && !Reflect.set(u, p, d, f) || m === "host" || !n.alive || g(
597
+ [s(f), s(p), s(d)],
598
+ (_, b, P) => {
599
+ const [S, H] = j(n, _, r);
600
+ H ? S.consume((N) => n.setProp(N, b, P)) : n.setProp(S, b, P);
546
601
  }
547
602
  ), !0;
548
603
  },
549
- deleteProperty(a, p) {
604
+ deleteProperty(u, p) {
550
605
  var f;
551
- const l = (f = s == null ? void 0 : s(u)) != null ? f : "host";
552
- return w([n(u), n(p)], (c, h) => {
553
- const [m, y] = g(t, c, o);
554
- if (l === "vm" || Reflect.deleteProperty(a, p)) {
555
- if (l === "host" || !t.alive)
556
- return !0;
557
- y ? m.consume((_) => d(t, "(a, b) => delete a[b]", void 0, _, h)) : d(t, "(a, b) => delete a[b]", void 0, m, h);
606
+ const l = (f = o == null ? void 0 : o(a)) != null ? f : "host";
607
+ return g([s(a), s(p)], (d, m) => {
608
+ const [h, _] = j(n, d, r);
609
+ if (l === "vm" || Reflect.deleteProperty(u, p)) {
610
+ if (l === "host" || !n.alive) return !0;
611
+ _ ? h.consume((b) => y(n, "(a, b) => delete a[b]", void 0, b, m)) : y(n, "(a, b) => delete a[b]", void 0, h, m);
558
612
  }
559
613
  return !0;
560
614
  });
561
615
  }
562
616
  });
563
- return u;
564
- }
565
- function le(t, e, r, o, n, s, i) {
566
- return !J(t, e) || i && !i(e, t) ? [void 0, !1] : H(t, e, o) ? [e, !1] : T(
567
- [
568
- t.newFunction("getSyncMode", (u) => {
569
- const a = s == null ? void 0 : s(n(u));
570
- return typeof a == "string" ? t.newString(a) : t.undefined;
571
- }),
572
- t.newFunction("setter", (u, a, p) => {
573
- const l = n(u);
574
- if (!l)
575
- return;
576
- const f = n(a);
577
- if (f === "__proto__")
578
- return;
579
- const c = n(p);
580
- b(l, r)[f] = c;
581
- }),
582
- t.newFunction("deleter", (u, a) => {
583
- const p = n(u);
584
- if (!p)
585
- return;
586
- const l = n(a);
587
- delete b(p, r)[l];
588
- })
589
- ],
590
- (u) => [
591
- d(
592
- t,
593
- `(target, sym, getSyncMode, setter, deleter) => {
617
+ return a;
618
+ }
619
+ function he(n, e, t, r, s, o, i) {
620
+ if (!W(n, e) || i && !i(e, n))
621
+ return [void 0, !1];
622
+ if (F(n, e, r)) return [e, !1];
623
+ const a = (l) => {
624
+ const f = o == null ? void 0 : o(s(l));
625
+ return typeof f == "string" ? n.newString(f) : n.undefined;
626
+ }, u = (l, f, d) => {
627
+ const m = s(l);
628
+ if (!m) return;
629
+ const h = s(f);
630
+ if (h === "__proto__") return;
631
+ const _ = s(d);
632
+ w(m, t)[h] = _;
633
+ }, p = (l, f) => {
634
+ const d = s(l);
635
+ if (!d) return;
636
+ const m = s(f);
637
+ delete w(d, t)[m];
638
+ };
639
+ return n.newFunction("proxyFuncs", (l, ...f) => {
640
+ switch (n.getNumber(l)) {
641
+ case 1:
642
+ return a(f[0]);
643
+ case 2:
644
+ return u(f[0], f[1], f[2]);
645
+ case 3:
646
+ return p(f[0], f[1]);
647
+ }
648
+ return n.undefined;
649
+ }).consume((l) => [
650
+ y(
651
+ n,
652
+ `(target, sym, proxyFuncs) => {
594
653
  const rec = new Proxy(target, {
595
654
  get(obj, key, receiver) {
596
655
  return key === sym ? obj : Reflect.get(obj, key, receiver)
@@ -599,19 +658,19 @@ function le(t, e, r, o, n, s, i) {
599
658
  const v = typeof value === "object" && value !== null || typeof value === "function"
600
659
  ? value[sym] ?? value
601
660
  : value;
602
- const sync = getSyncMode(receiver) ?? "vm";
661
+ const sync = proxyFuncs(1, receiver) ?? "vm";
603
662
  if (sync === "host" || Reflect.set(obj, key, v, receiver)) {
604
663
  if (sync !== "vm") {
605
- setter(receiver, key, v);
664
+ proxyFuncs(2, receiver, key, v);
606
665
  }
607
666
  }
608
667
  return true;
609
668
  },
610
669
  deleteProperty(obj, key) {
611
- const sync = getSyncMode(rec) ?? "vm";
670
+ const sync = proxyFuncs(1, rec) ?? "vm";
612
671
  if (sync === "host" || Reflect.deleteProperty(obj, key)) {
613
672
  if (sync !== "vm") {
614
- deleter(rec, key);
673
+ proxyFuncs(3, rec, key);
615
674
  }
616
675
  }
617
676
  return true;
@@ -619,152 +678,310 @@ function le(t, e, r, o, n, s, i) {
619
678
  });
620
679
  return rec;
621
680
  }`,
622
- void 0,
623
- e,
624
- o,
625
- ...u
626
- ),
627
- !0
628
- ]
629
- );
681
+ void 0,
682
+ e,
683
+ r,
684
+ l
685
+ ),
686
+ !0
687
+ ]);
630
688
  }
631
- function b(t, e) {
632
- var r;
633
- return v(t) && (r = t[e]) != null ? r : t;
689
+ function w(n, e) {
690
+ var t;
691
+ return v(n) && (t = n[e]) != null ? t : n;
634
692
  }
635
- function g(t, e, r) {
636
- return H(t, e, r) ? [t.getProp(e, r), !0] : [e, !1];
693
+ function j(n, e, t) {
694
+ return F(n, e, t) ? [n.getProp(e, t), !0] : [e, !1];
637
695
  }
638
- function ce(t, e) {
639
- return v(t) && !!t[e];
696
+ function ye(n, e) {
697
+ return v(n) && !!n[e];
640
698
  }
641
- function H(t, e, r) {
642
- return !!t.dump(
643
- d(
644
- t,
699
+ function F(n, e, t) {
700
+ return !!n.dump(
701
+ y(
702
+ n,
703
+ // promise and date cannot be wrapped
645
704
  '(a, s) => (a instanceof Promise) || (a instanceof Date) || (typeof a === "object" && a !== null || typeof a === "function") && !!a[s]',
646
705
  void 0,
647
706
  e,
648
- r
707
+ t
649
708
  )
650
709
  );
651
710
  }
652
- class he {
653
- constructor(e, r) {
654
- var o;
655
- this._registeredMapDispose = /* @__PURE__ */ new Set(), this._sync = /* @__PURE__ */ new Set(), this._temporalSync = /* @__PURE__ */ new Set(), this._symbol = Symbol(), this._isMarshalable = (n) => {
656
- var i, u;
657
- const s = (i = this._options) == null ? void 0 : i.isMarshalable;
658
- return (u = typeof s == "function" ? s(this._unwrap(n)) : s) != null ? u : "json";
659
- }, this._marshalFind = (n) => {
660
- var u, a, p;
661
- const s = this._unwrap(n);
662
- return (p = (a = (u = this._registeredMap.get(n)) != null ? u : s !== n ? this._registeredMap.get(s) : void 0) != null ? a : this._map.get(n)) != null ? p : s !== n ? this._map.get(s) : void 0;
663
- }, this._marshalPre = (n, s, i) => {
664
- var u;
665
- if (i !== "json")
666
- return (u = this._register(n, z(s), this._map)) == null ? void 0 : u[1];
667
- }, this._marshalPreApply = (n, s, i) => {
668
- const u = v(s) ? this._unwrap(s) : void 0;
669
- u && this._temporalSync.add(u);
711
+ class we {
712
+ /** Constructs a new Arena instance. It requires a quickjs-emscripten context initialized with `quickjs.newContext()`. */
713
+ constructor(e, t) {
714
+ c(this, "context");
715
+ c(this, "_map");
716
+ c(this, "_registeredMap");
717
+ c(this, "_registeredMapDispose", /* @__PURE__ */ new Set());
718
+ c(this, "_sync", /* @__PURE__ */ new Set());
719
+ c(this, "_temporalSync", /* @__PURE__ */ new Set());
720
+ c(this, "_symbol", Symbol());
721
+ c(this, "_symbolHandle");
722
+ c(this, "_options");
723
+ c(this, "_isMarshalable", (e) => {
724
+ var r, s;
725
+ const t = (r = this._options) == null ? void 0 : r.isMarshalable;
726
+ return (s = typeof t == "function" ? t(this._unwrap(e)) : t) != null ? s : "json";
727
+ });
728
+ c(this, "_marshalFind", (e) => {
729
+ var s, o, i;
730
+ const t = this._unwrap(e);
731
+ return (i = (o = (s = this._registeredMap.get(e)) != null ? s : t !== e ? this._registeredMap.get(t) : void 0) != null ? o : this._map.get(e)) != null ? i : t !== e ? this._map.get(t) : void 0;
732
+ });
733
+ c(this, "_marshalPre", (e, t, r) => {
734
+ var s;
735
+ if (r !== "json")
736
+ return (s = this._register(e, q(t), this._map)) == null ? void 0 : s[1];
737
+ });
738
+ c(this, "_marshalPreApply", (e, t, r) => {
739
+ const s = v(t) ? this._unwrap(t) : void 0;
740
+ s && this._temporalSync.add(s);
670
741
  try {
671
- return n.apply(s, i);
742
+ return e.apply(t, r);
672
743
  } finally {
673
- u && this._temporalSync.delete(u);
744
+ s && this._temporalSync.delete(s);
674
745
  }
675
- }, this._marshal = (n) => {
676
- var u, a;
677
- const s = this._registeredMap.get(n);
678
- if (s)
679
- return [s, !1];
680
- const i = R((u = this._wrap(n)) != null ? u : n, {
746
+ });
747
+ c(this, "_marshal", (e) => {
748
+ var s, o;
749
+ const t = this._registeredMap.get(e);
750
+ if (t)
751
+ return [t, !1];
752
+ const r = R((s = this._wrap(e)) != null ? s : e, {
681
753
  ctx: this.context,
682
754
  unmarshal: this._unmarshal,
683
755
  isMarshalable: this._isMarshalable,
684
756
  find: this._marshalFind,
685
757
  pre: this._marshalPre,
686
758
  preApply: this._marshalPreApply,
687
- custom: (a = this._options) == null ? void 0 : a.customMarshaller
759
+ custom: (o = this._options) == null ? void 0 : o.customMarshaller
688
760
  });
689
- return [i, !this._map.hasHandle(i)];
690
- }, this._preUnmarshal = (n, s) => {
691
- var i;
692
- return (i = this._register(n, s, void 0, !0)) == null ? void 0 : i[0];
693
- }, this._unmarshalFind = (n) => {
761
+ return [r, !this._map.hasHandle(r)];
762
+ });
763
+ c(this, "_preUnmarshal", (e, t) => {
764
+ var r;
765
+ return (r = this._register(e, t, void 0, !0)) == null ? void 0 : r[0];
766
+ });
767
+ c(this, "_unmarshalFind", (e) => {
768
+ var t;
769
+ return (t = this._registeredMap.getByHandle(e)) != null ? t : this._map.getByHandle(e);
770
+ });
771
+ c(this, "_unmarshal", (e) => {
694
772
  var s;
695
- return (s = this._registeredMap.getByHandle(n)) != null ? s : this._map.getByHandle(n);
696
- }, this._unmarshal = (n) => {
697
- var u;
698
- const s = this._registeredMap.getByHandle(n);
699
- if (typeof s != "undefined")
700
- return s;
701
- const [i] = this._wrapHandle(n);
702
- return pe(i != null ? i : n, {
773
+ const t = this._registeredMap.getByHandle(e);
774
+ if (typeof t != "undefined")
775
+ return t;
776
+ const [r] = this._wrapHandle(e);
777
+ return de(r != null ? r : e, {
703
778
  ctx: this.context,
704
779
  marshal: this._marshal,
705
780
  find: this._unmarshalFind,
706
781
  pre: this._preUnmarshal,
707
- custom: (u = this._options) == null ? void 0 : u.customUnmarshaller
782
+ custom: (s = this._options) == null ? void 0 : s.customUnmarshaller
708
783
  });
709
- }, this._syncMode = (n) => {
710
- const s = this._unwrap(n);
711
- return this._sync.has(s) || this._temporalSync.has(s) ? "both" : void 0;
712
- }, this._unwrapIfNotSynced = (n) => {
713
- const s = this._unwrap(n);
714
- return s instanceof Promise || !this._sync.has(s) ? s : n;
715
- }, (r == null ? void 0 : r.compat) && !("runtime" in e) && (e.runtime = {
784
+ });
785
+ c(this, "_syncMode", (e) => {
786
+ const t = this._unwrap(e);
787
+ return this._sync.has(t) || this._temporalSync.has(t) ? "both" : void 0;
788
+ });
789
+ c(this, "_unwrapIfNotSynced", (e) => {
790
+ const t = this._unwrap(e);
791
+ return t instanceof Promise || !this._sync.has(t) ? t : e;
792
+ });
793
+ var r;
794
+ t != null && t.compat && !("runtime" in e) && (e.runtime = {
716
795
  hasPendingJob: () => e.hasPendingJob(),
717
- executePendingJobs: (n) => e.executePendingJobs(n)
718
- }), this.context = e, this._options = r, this._symbolHandle = e.unwrapResult(e.evalCode("Symbol()")), this._map = new O(e), this._registeredMap = new O(e), this.registerAll((o = r == null ? void 0 : r.registeredObjects) != null ? o : F);
796
+ executePendingJobs: (s) => e.executePendingJobs(s)
797
+ }), this.context = t != null && t.experimentalContextEx ? B(e) : e, this._options = t, this._symbolHandle = e.unwrapResult(e.evalCode("Symbol()")), this._map = new O(e), this._registeredMap = new O(e), this.registerAll((r = t == null ? void 0 : t.registeredObjects) != null ? r : I);
719
798
  }
799
+ /**
800
+ * Dispose of the arena and managed handles. This method won't dispose the VM itself, so the VM has to be disposed of manually.
801
+ */
720
802
  dispose() {
721
- this._map.dispose(), this._registeredMap.dispose(), this._symbolHandle.dispose();
803
+ var e, t;
804
+ this._map.dispose(), this._registeredMap.dispose(), this._symbolHandle.dispose(), (t = (e = this.context).disposeEx) == null || t.call(e);
722
805
  }
806
+ /**
807
+ * Evaluate JS code in the VM and get the result as an object on the host side. It also converts and re-throws error objects when an error is thrown during evaluation.
808
+ */
723
809
  evalCode(e) {
724
- const r = this.context.evalCode(e);
725
- return this._unwrapResultAndUnmarshal(r);
810
+ const t = this.context.evalCode(e);
811
+ return this._unwrapResultAndUnmarshal(t);
726
812
  }
813
+ /**
814
+ * Evaluate ES module code in the VM. The module can import/export, but the return value
815
+ * depends on whether the module exports are accessible (implementation varies by quickjs-emscripten version).
816
+ * Use this for side effects or when you don't need access to exports.
817
+ *
818
+ * @param code - The ES module code to evaluate
819
+ * @param filename - Optional filename for debugging purposes (default: "module.js")
820
+ * @returns Undefined in most cases (module side effects are applied)
821
+ *
822
+ * @example
823
+ * ```js
824
+ * // Execute module code with side effects
825
+ * arena.expose({ data: { count: 0 } });
826
+ * arena.evalModule('import { data } from "globals"; data.count = 42;'); // undefined, but data.count is now 42
827
+ * ```
828
+ */
829
+ evalModule(e, t = "module.js") {
830
+ const r = this.context.evalCode(e, t, { type: "module" });
831
+ this._unwrapResultAndUnmarshal(r);
832
+ }
833
+ /**
834
+ * Almost same as `vm.executePendingJobs()`, but it converts and re-throws error objects when an error is thrown during evaluation.
835
+ */
727
836
  executePendingJobs(e) {
728
- const r = this.context.runtime.executePendingJobs(e);
729
- if ("value" in r)
730
- return r.value;
731
- throw this._unwrapIfNotSynced(r.error.consume(this._unmarshal));
837
+ const t = this.context.runtime.executePendingJobs(e);
838
+ if ("value" in t)
839
+ return t.value;
840
+ throw this._unwrapIfNotSynced(t.error.consume(this._unmarshal));
841
+ }
842
+ /**
843
+ * Set the max memory this runtime can allocate.
844
+ * To remove the limit, set to `-1`.
845
+ *
846
+ * This is useful for preventing runaway memory usage in untrusted code.
847
+ *
848
+ * @param limitBytes - Maximum memory in bytes, or -1 to remove limit
849
+ *
850
+ * @example
851
+ * ```js
852
+ * // Limit sandbox to 10MB
853
+ * arena.setMemoryLimit(10 * 1024 * 1024);
854
+ *
855
+ * try {
856
+ * arena.evalCode(`const huge = new Array(1000000000);`);
857
+ * } catch (e) {
858
+ * console.log("Memory limit exceeded");
859
+ * }
860
+ * ```
861
+ */
862
+ setMemoryLimit(e) {
863
+ this.context.runtime.setMemoryLimit(e);
864
+ }
865
+ /**
866
+ * Set the max stack size for this runtime, in bytes.
867
+ * To remove the limit, set to `0`.
868
+ *
869
+ * This is useful for preventing stack overflow from deeply nested calls or recursion.
870
+ *
871
+ * @param stackSize - Maximum stack size in bytes, or 0 to remove limit
872
+ *
873
+ * @example
874
+ * ```js
875
+ * // Limit stack to 512KB
876
+ * arena.setMaxStackSize(512 * 1024);
877
+ *
878
+ * try {
879
+ * arena.evalCode(`function recurse() { recurse(); } recurse();`);
880
+ * } catch (e) {
881
+ * console.log("Stack overflow prevented");
882
+ * }
883
+ * ```
884
+ */
885
+ setMaxStackSize(e) {
886
+ this.context.runtime.setMaxStackSize(e);
887
+ }
888
+ /**
889
+ * Get detailed memory usage statistics for this runtime.
890
+ *
891
+ * @returns An object containing detailed memory allocation information
892
+ *
893
+ * @example
894
+ * ```js
895
+ * const stats = arena.getMemoryUsage();
896
+ * console.log(`Memory used: ${stats.memory_used_size} bytes`);
897
+ * console.log(`Object count: ${stats.obj_count}`);
898
+ * console.log(`Memory limit: ${stats.malloc_limit}`);
899
+ * ```
900
+ */
901
+ getMemoryUsage() {
902
+ const e = this.context.runtime.computeMemoryUsage();
903
+ try {
904
+ return this.context.dump(e);
905
+ } finally {
906
+ e.dispose();
907
+ }
908
+ }
909
+ /**
910
+ * Get a human-readable description of memory usage in this runtime.
911
+ *
912
+ * @returns A formatted string showing memory statistics
913
+ *
914
+ * @example
915
+ * ```js
916
+ * console.log(arena.dumpMemoryUsage());
917
+ * // Output:
918
+ * // QuickJS memory usage:
919
+ * // malloc_limit: 4294967295
920
+ * // memory_used_size: 67078
921
+ * // ...
922
+ * ```
923
+ */
924
+ dumpMemoryUsage() {
925
+ return this.context.runtime.dumpMemoryUsage();
732
926
  }
927
+ /**
928
+ * Expose objects as global objects in the VM.
929
+ *
930
+ * By default, exposed objects are not synchronized between the host and the VM.
931
+ * If you want to sync an objects, first wrap the object with sync method, and then expose the wrapped object.
932
+ */
733
933
  expose(e) {
734
- for (const [r, o] of Object.entries(e))
735
- U(this._marshal(o), (n) => {
736
- this.context.setProp(this.context.global, r, n);
934
+ for (const [t, r] of Object.entries(e))
935
+ $(this._marshal(r), (s) => {
936
+ this.context.setProp(this.context.global, t, s);
737
937
  });
738
938
  }
939
+ /**
940
+ * Enables sync for the object between the host and the VM and returns objects wrapped with proxies.
941
+ *
942
+ * The return value is necessary in order to reflect changes to the object from the host to the VM. Please note that setting a value in the field or deleting a field in the original object will not synchronize it.
943
+ */
739
944
  sync(e) {
740
- const r = this._wrap(e);
741
- return typeof r == "undefined" ? e : (S(r, (o) => {
742
- const n = this._unwrap(o);
743
- this._sync.add(n);
744
- }), r);
745
- }
746
- register(e, r) {
747
- if (this._registeredMap.has(e))
748
- return;
749
- const o = typeof r == "string" ? this._unwrapResult(this.context.evalCode(r)) : r;
750
- N(this.context, o, this.context.undefined) || (typeof r == "string" && this._registeredMapDispose.add(e), this._registeredMap.set(e, o));
945
+ const t = this._wrap(e);
946
+ return typeof t == "undefined" ? e : (E(t, (r) => {
947
+ const s = this._unwrap(r);
948
+ this._sync.add(s);
949
+ }), t);
950
+ }
951
+ /**
952
+ * Register a pair of objects that will be considered the same between the host and the QuickJS VM.
953
+ *
954
+ * Instead of a string, you can also pass a QuickJSHandle directly. In that case, however, when you have to dispose them manually when destroying the VM.
955
+ */
956
+ register(e, t) {
957
+ if (this._registeredMap.has(e)) return;
958
+ const r = typeof t == "string" ? this._unwrapResult(this.context.evalCode(t)) : t;
959
+ G(this.context, r, this.context.undefined) || (typeof t == "string" && this._registeredMapDispose.add(e), this._registeredMap.set(e, r));
751
960
  }
961
+ /**
962
+ * Execute `register` methods for each pair.
963
+ */
752
964
  registerAll(e) {
753
- for (const [r, o] of e)
754
- this.register(r, o);
965
+ for (const [t, r] of e)
966
+ this.register(t, r);
755
967
  }
756
- unregister(e, r) {
757
- this._registeredMap.delete(e, this._registeredMapDispose.has(e) || r), this._registeredMapDispose.delete(e);
968
+ /**
969
+ * Unregister a pair of objects that were registered with `registeredObjects` option and `register` method.
970
+ */
971
+ unregister(e, t) {
972
+ this._registeredMap.delete(e, this._registeredMapDispose.has(e) || t), this._registeredMapDispose.delete(e);
758
973
  }
759
- unregisterAll(e, r) {
760
- for (const o of e)
761
- this.unregister(o, r);
974
+ /**
975
+ * Execute `unregister` methods for each target.
976
+ */
977
+ unregisterAll(e, t) {
978
+ for (const r of e)
979
+ this.unregister(r, t);
762
980
  }
763
981
  startSync(e) {
764
- if (!v(e))
765
- return;
766
- const r = this._unwrap(e);
767
- this._sync.add(r);
982
+ if (!v(e)) return;
983
+ const t = this._unwrap(e);
984
+ this._sync.add(t);
768
985
  }
769
986
  endSync(e) {
770
987
  this._sync.delete(this._unwrap(e));
@@ -775,68 +992,66 @@ class he {
775
992
  throw this._unwrapIfNotSynced(e.error.consume(this._unmarshal));
776
993
  }
777
994
  _unwrapResultAndUnmarshal(e) {
778
- if (!!e)
995
+ if (e)
779
996
  return this._unwrapIfNotSynced(this._unwrapResult(e).consume(this._unmarshal));
780
997
  }
781
- _register(e, r, o = this._map, n) {
782
- if (this._registeredMap.has(e) || this._registeredMap.hasHandle(r))
783
- return;
784
- let s = this._wrap(e);
785
- const [i] = this._wrapHandle(r), u = e instanceof Promise;
786
- if (!i || !s && !u)
998
+ _register(e, t, r = this._map, s) {
999
+ if (this._registeredMap.has(e) || this._registeredMap.hasHandle(t))
787
1000
  return;
788
- u && (s = e);
789
- const a = this._unwrap(e), [p, l] = this._unwrapHandle(r);
790
- if (o.set(s, i, a, p))
791
- n && this._sync.add(a);
792
- else
793
- throw l && p.dispose(), new Error("already registered");
794
- return [s, i];
1001
+ let o = this._wrap(e);
1002
+ const [i] = this._wrapHandle(t), a = e instanceof Promise;
1003
+ if (!i || !o && !a) return;
1004
+ a && (o = e);
1005
+ const u = this._unwrap(e), [p, l] = this._unwrapHandle(t);
1006
+ if (r.set(o, i, u, p))
1007
+ s && this._sync.add(u);
1008
+ else throw l && p.dispose(), new Error("already registered");
1009
+ return [o, i];
795
1010
  }
796
1011
  _wrap(e) {
797
- var r;
798
- return fe(
1012
+ var t;
1013
+ return me(
799
1014
  this.context,
800
1015
  e,
801
1016
  this._symbol,
802
1017
  this._symbolHandle,
803
1018
  this._marshal,
804
1019
  this._syncMode,
805
- (r = this._options) == null ? void 0 : r.isWrappable
1020
+ (t = this._options) == null ? void 0 : t.isWrappable
806
1021
  );
807
1022
  }
808
1023
  _unwrap(e) {
809
- return b(e, this._symbol);
1024
+ return w(e, this._symbol);
810
1025
  }
811
1026
  _wrapHandle(e) {
812
- var r;
813
- return le(
1027
+ var t;
1028
+ return he(
814
1029
  this.context,
815
1030
  e,
816
1031
  this._symbol,
817
1032
  this._symbolHandle,
818
1033
  this._unmarshal,
819
1034
  this._syncMode,
820
- (r = this._options) == null ? void 0 : r.isHandleWrappable
1035
+ (t = this._options) == null ? void 0 : t.isHandleWrappable
821
1036
  );
822
1037
  }
823
1038
  _unwrapHandle(e) {
824
- return g(this.context, e, this._symbolHandle);
1039
+ return j(this.context, e, this._symbolHandle);
825
1040
  }
826
1041
  }
827
1042
  export {
828
- he as Arena,
1043
+ we as Arena,
829
1044
  O as VMMap,
830
- d as call,
831
- de as complexity,
832
- T as consumeAll,
833
- F as defaultRegisteredObjects,
834
- N as eq,
835
- V as isES2015Class,
836
- J as isHandleObject,
1045
+ y as call,
1046
+ ve as complexity,
1047
+ be as consumeAll,
1048
+ I as defaultRegisteredObjects,
1049
+ G as eq,
1050
+ Z as isES2015Class,
1051
+ W as isHandleObject,
837
1052
  v as isObject,
838
- I as json,
1053
+ z as json,
839
1054
  R as marshal,
840
- pe as unmarshal,
841
- S as walkObject
1055
+ de as unmarshal,
1056
+ E as walkObject
842
1057
  };