quickjs-emscripten-sync 1.6.0 → 1.8.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.
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { Intrinsics } from 'quickjs-emscripten';
1
2
  import { QuickJSContext } from 'quickjs-emscripten';
2
3
  import { QuickJSDeferredPromise } from 'quickjs-emscripten';
3
4
  import { QuickJSHandle } from 'quickjs-emscripten';
@@ -28,22 +29,38 @@ export declare class Arena {
28
29
  */
29
30
  evalCode<T = any>(code: string): T;
30
31
  /**
31
- * Evaluate ES module code in the VM. The module can import/export, but the return value
32
- * depends on whether the module exports are accessible (implementation varies by quickjs-emscripten version).
33
- * Use this for side effects or when you don't need access to exports.
32
+ * Evaluate ES module code in the VM and get the module's exports.
33
+ *
34
+ * Requires quickjs-emscripten >= 0.29.0 for export access.
34
35
  *
35
36
  * @param code - The ES module code to evaluate
36
37
  * @param filename - Optional filename for debugging purposes (default: "module.js")
37
- * @returns Undefined in most cases (module side effects are applied)
38
+ * @returns The module's exports object, or a Promise resolving to exports if using top-level await
38
39
  *
39
40
  * @example
40
41
  * ```js
41
- * // Execute module code with side effects
42
- * arena.expose({ data: { count: 0 } });
43
- * arena.evalModule('import { data } from "globals"; data.count = 42;'); // undefined, but data.count is now 42
42
+ * // Simple module with exports
43
+ * const exports = arena.evalModule(`
44
+ * export const value = 42;
45
+ * export function greet(name) {
46
+ * return "Hello, " + name;
47
+ * }
48
+ * `);
49
+ * console.log(exports.value); // 42
50
+ * console.log(exports.greet("World")); // "Hello, World"
51
+ *
52
+ * // Module with default export
53
+ * const mod = arena.evalModule('export default function(x) { return x * 2; }');
54
+ * console.log(mod.default(21)); // 42
55
+ *
56
+ * // Module with top-level await
57
+ * const promise = arena.evalModule('export const data = await Promise.resolve(123);');
58
+ * arena.executePendingJobs();
59
+ * const exports = await promise;
60
+ * console.log(exports.data); // 123
44
61
  * ```
45
62
  */
46
- evalModule(code: string, filename?: string): void;
63
+ evalModule<T = any>(code: string, filename?: string): T | Promise<T>;
47
64
  /**
48
65
  * Almost same as `vm.executePendingJobs()`, but it converts and re-throws error objects when an error is thrown during evaluation.
49
66
  */
@@ -214,7 +231,7 @@ export declare function consumeAll<T extends QuickJSHandle[], K>(handles: T, cb:
214
231
  */
215
232
  export declare const defaultRegisteredObjects: [any, string][];
216
233
 
217
- export declare function eq(ctx: QuickJSContext, a: QuickJSHandle, b: QuickJSHandle): boolean;
234
+ export { Intrinsics }
218
235
 
219
236
  export declare function isES2015Class(cls: any): cls is new (...args: any[]) => any;
220
237
 
@@ -85,8 +85,8 @@ const I = [
85
85
  ...Object.getOwnPropertyNames(Symbol).filter((n) => typeof Symbol[n] == "symbol").map((n) => [Symbol[n], `Symbol.${n}`])
86
86
  ];
87
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", {
88
+ const t = n.unwrapResult(n.evalCode(e)), r = (o, ...i) => n.unwrapResult(n.callFunction(t, o != null ? o : n.undefined, ...i)), s = () => t.dispose();
89
+ return r.dispose = s, r[Symbol.dispose] = s, r.alive = !0, Object.defineProperty(r, "alive", {
90
90
  get: () => t.alive
91
91
  }), r;
92
92
  }
@@ -99,21 +99,18 @@ function y(n, e, t, ...r) {
99
99
  }
100
100
  }
101
101
  function G(n, e, t) {
102
- return n.dump(y(n, "Object.is", void 0, e, t));
103
- }
104
- function J(n, e, t) {
105
102
  return n.dump(y(n, "(a, b) => a instanceof b", void 0, e, t));
106
103
  }
107
- function W(n, e) {
104
+ function J(n, e) {
108
105
  return n.dump(
109
106
  y(n, 'a => typeof a === "object" && a !== null || typeof a === "function"', void 0, e)
110
107
  );
111
108
  }
112
- function z(n, e) {
109
+ function W(n, e) {
113
110
  const t = JSON.stringify(e);
114
111
  return t ? y(n, "JSON.parse", void 0, n.newString(t)) : n.undefined;
115
112
  }
116
- function be(n, e) {
113
+ function _e(n, e) {
117
114
  try {
118
115
  return e(n);
119
116
  } finally {
@@ -121,7 +118,7 @@ function be(n, e) {
121
118
  t.alive && t.dispose();
122
119
  }
123
120
  }
124
- function $([n, e], t) {
121
+ function z([n, e], t) {
125
122
  try {
126
123
  return t(n);
127
124
  } finally {
@@ -136,20 +133,20 @@ function g(n, e) {
136
133
  r && t.dispose();
137
134
  }
138
135
  }
139
- function L(n) {
136
+ function $(n) {
140
137
  return "handle" in n;
141
138
  }
142
- function q(n) {
143
- return L(n) ? n.handle : n;
139
+ function L(n) {
140
+ return $(n) ? n.handle : n;
144
141
  }
145
- function Q(n, e, t, r) {
142
+ function V(n, e, t, r) {
146
143
  var o;
147
144
  let s;
148
145
  for (const i of r)
149
146
  if (s = i(e, n), s) break;
150
147
  return s ? (o = t(e, s)) != null ? o : s : void 0;
151
148
  }
152
- function V(n, e) {
149
+ function Q(n, e) {
153
150
  return typeof n != "symbol" ? void 0 : y(
154
151
  e,
155
152
  "d => Symbol(d)",
@@ -157,11 +154,11 @@ function V(n, e) {
157
154
  n.description ? e.newString(n.description) : e.undefined
158
155
  );
159
156
  }
160
- function X(n, e) {
157
+ function q(n, e) {
161
158
  return n instanceof Date ? y(e, "d => new Date(d)", void 0, e.newNumber(n.getTime())) : void 0;
162
159
  }
163
- const Y = [V, X];
164
- function Z(n) {
160
+ const X = [Q, q];
161
+ function Y(n) {
165
162
  return typeof n == "function" && /^class\s/.test(Function.prototype.toString.call(n));
166
163
  }
167
164
  function v(n) {
@@ -185,10 +182,10 @@ function E(n, e) {
185
182
  };
186
183
  return r(n), t;
187
184
  }
188
- function ve(n, e) {
185
+ function be(n, e) {
189
186
  return E(n, e ? (t, r) => r.size < e : void 0).size;
190
187
  }
191
- function K() {
188
+ function Z() {
192
189
  let n = () => {
193
190
  }, e = () => {
194
191
  };
@@ -212,12 +209,12 @@ function M(n, e, t, r) {
212
209
  }, i = Object.getOwnPropertyDescriptors(e);
213
210
  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();
214
211
  }
215
- function x(n, e, t, r, s, o) {
212
+ function K(n, e, t, r, s, o) {
216
213
  var u;
217
214
  if (typeof e != "function") return;
218
215
  const i = n.newFunction(e.name, function(...p) {
219
216
  const l = r(this), f = p.map((d) => r(d));
220
- if (Z(e) && v(l)) {
217
+ if (Y(e) && v(l)) {
221
218
  const d = new e(...f);
222
219
  return Object.entries(d).forEach(([m, h]) => {
223
220
  n.setProp(this, m, t(h));
@@ -242,18 +239,18 @@ function x(n, e, t, r, s, o) {
242
239
  ), a = (u = s(e, i)) != null ? u : i;
243
240
  return M(n, e, i, t), a;
244
241
  }
245
- function ee(n, e, t) {
242
+ function x(n, e, t) {
246
243
  var o;
247
- const r = z(n, e);
244
+ const r = W(n, e);
248
245
  return (o = t(e, r)) != null ? o : r;
249
246
  }
250
- function te(n, e, t, r) {
247
+ function ee(n, e, t, r) {
251
248
  var u;
252
249
  if (typeof e != "object" || e === null) return;
253
250
  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
251
  return a && y(n, "Object.setPrototypeOf", void 0, o, a).dispose(), M(n, e, s, t), o;
255
252
  }
256
- function ne(n, e) {
253
+ function te(n, e) {
257
254
  switch (typeof e) {
258
255
  case "undefined":
259
256
  return n.undefined;
@@ -267,7 +264,7 @@ function ne(n, e) {
267
264
  return e === null ? n.null : void 0;
268
265
  }
269
266
  }
270
- function re(n, e, t, r) {
267
+ function ne(n, e, t, r) {
271
268
  var o;
272
269
  if (!(e instanceof Promise)) return;
273
270
  const s = n.newPromise();
@@ -280,7 +277,7 @@ function R(n, e) {
280
277
  var l, f, d, m, h;
281
278
  const { ctx: t, unmarshal: r, isMarshalable: s, find: o, pre: i } = e;
282
279
  {
283
- const _ = ne(t, n);
280
+ const _ = te(t, n);
284
281
  if (_)
285
282
  return _;
286
283
  }
@@ -293,28 +290,28 @@ function R(n, e) {
293
290
  return t.undefined;
294
291
  const u = (_, b) => i(_, b, a);
295
292
  if (a === "json")
296
- return ee(t, n, u);
293
+ return x(t, n, u);
297
294
  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;
295
+ return (h = (m = (d = (f = V(t, n, u, [...X, ...(l = e.custom) != null ? l : []])) != null ? f : ne(t, n, p, u)) != null ? d : K(t, n, p, r, u, e.preApply)) != null ? m : ee(t, n, p, u)) != null ? h : t.undefined;
299
296
  }
300
- function se(n, e, t, r) {
297
+ function re(n, e, t, r) {
301
298
  var o;
302
299
  let s;
303
300
  for (const i of r)
304
301
  if (s = i(e, n), s) break;
305
302
  return s ? (o = t(s, e)) != null ? o : s : void 0;
306
303
  }
307
- function oe(n, e) {
304
+ function se(n, e) {
308
305
  if (e.typeof(n) !== "symbol") return;
309
306
  const t = e.getString(e.getProp(n, "description"));
310
307
  return Symbol(t);
311
308
  }
312
- function ie(n, e) {
309
+ function oe(n, e) {
313
310
  if (!e.dump(y(e, "a => a instanceof Date", void 0, n))) return;
314
311
  const t = e.getNumber(y(e, "a => a.getTime()", void 0, n));
315
312
  return new Date(t);
316
313
  }
317
- const ue = [oe, ie];
314
+ const ie = [se, oe];
318
315
  function k(n, e, t, r) {
319
316
  n.newFunction("", (s, o) => {
320
317
  const [i] = r(s);
@@ -350,7 +347,7 @@ function k(n, e, t, r) {
350
347
  ).dispose();
351
348
  });
352
349
  }
353
- function ae(n, e, t, r, s) {
350
+ function ue(n, e, t, r, s) {
354
351
  var a;
355
352
  if (n.typeof(e) !== "function") return;
356
353
  const o = function(...u) {
@@ -370,7 +367,7 @@ function ae(n, e, t, r, s) {
370
367
  }, i = (a = s(o, e)) != null ? a : o;
371
368
  return k(n, e, o, r), i;
372
369
  }
373
- function pe(n, e, t, r) {
370
+ function ae(n, e, t, r) {
374
371
  var a;
375
372
  if (n.typeof(e) !== "object" || // null check
376
373
  n.unwrapResult(n.evalCode("o => o === null")).consume((u) => n.dump(n.unwrapResult(n.callFunction(u, n.undefined, e)))))
@@ -390,20 +387,20 @@ function pe(n, e, t, r) {
390
387
  });
391
388
  return typeof i == "object" && Object.setPrototypeOf(o, i), k(n, e, s, t), o;
392
389
  }
393
- function fe(n, e) {
390
+ function pe(n, e) {
394
391
  const t = n.typeof(e);
395
392
  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];
396
393
  }
397
- function ce(n, e, t, r) {
394
+ function fe(n, e, t, r) {
398
395
  var p;
399
- if (!le(n, e)) return;
400
- const s = K(), [o, i] = t(s.resolve), [a, u] = t(s.reject);
396
+ if (!ce(n, e)) return;
397
+ const s = Z(), [o, i] = t(s.resolve), [a, u] = t(s.reject);
401
398
  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;
402
399
  }
403
- function le(n, e) {
404
- return e.owner ? n.unwrapResult(n.evalCode("Promise")).consume((t) => e.owner ? J(n, e, t) : !1) : !1;
400
+ function ce(n, e) {
401
+ return e.owner ? n.unwrapResult(n.evalCode("Promise")).consume((t) => e.owner ? G(n, e, t) : !1) : !1;
405
402
  }
406
- function de(n, e) {
403
+ function le(n, e) {
407
404
  const [t] = C(n, e);
408
405
  return t;
409
406
  }
@@ -411,7 +408,7 @@ function C(n, e) {
411
408
  var u, p, l, f;
412
409
  const { ctx: t, marshal: r, find: s, pre: o } = e;
413
410
  {
414
- const [d, m] = fe(t, n);
411
+ const [d, m] = pe(t, n);
415
412
  if (m) return [d, !1];
416
413
  }
417
414
  {
@@ -420,7 +417,7 @@ function C(n, e) {
420
417
  return [d, !0];
421
418
  }
422
419
  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];
420
+ return [(f = (l = (p = re(t, n, o, [...ie, ...(u = e.custom) != null ? u : []])) != null ? p : fe(t, n, r, o)) != null ? l : ue(t, n, r, i, o)) != null ? f : ae(t, n, i, o), !1];
424
421
  }
425
422
  class O {
426
423
  constructor(e) {
@@ -582,10 +579,10 @@ class O {
582
579
  );
583
580
  }
584
581
  }
585
- function me(n, e, t, r, s, o, i) {
582
+ function de(n, e, t, r, s, o, i) {
586
583
  if (!v(e) || e instanceof Promise || e instanceof Date || i && !i(e))
587
584
  return;
588
- if (ye(e, t)) return e;
585
+ if (he(e, t)) return e;
589
586
  const a = new Proxy(e, {
590
587
  get(u, p) {
591
588
  return p === t ? u : Reflect.get(u, p);
@@ -616,8 +613,8 @@ function me(n, e, t, r, s, o, i) {
616
613
  });
617
614
  return a;
618
615
  }
619
- function he(n, e, t, r, s, o, i) {
620
- if (!W(n, e) || i && !i(e, n))
616
+ function me(n, e, t, r, s, o, i) {
617
+ if (!J(n, e) || i && !i(e, n))
621
618
  return [void 0, !1];
622
619
  if (F(n, e, r)) return [e, !1];
623
620
  const a = (l) => {
@@ -693,7 +690,7 @@ function w(n, e) {
693
690
  function j(n, e, t) {
694
691
  return F(n, e, t) ? [n.getProp(e, t), !0] : [e, !1];
695
692
  }
696
- function ye(n, e) {
693
+ function he(n, e) {
697
694
  return v(n) && !!n[e];
698
695
  }
699
696
  function F(n, e, t) {
@@ -708,7 +705,7 @@ function F(n, e, t) {
708
705
  )
709
706
  );
710
707
  }
711
- class we {
708
+ class ve {
712
709
  /** Constructs a new Arena instance. It requires a quickjs-emscripten context initialized with `quickjs.newContext()`. */
713
710
  constructor(e, t) {
714
711
  c(this, "context");
@@ -733,7 +730,7 @@ class we {
733
730
  c(this, "_marshalPre", (e, t, r) => {
734
731
  var s;
735
732
  if (r !== "json")
736
- return (s = this._register(e, q(t), this._map)) == null ? void 0 : s[1];
733
+ return (s = this._register(e, L(t), this._map)) == null ? void 0 : s[1];
737
734
  });
738
735
  c(this, "_marshalPreApply", (e, t, r) => {
739
736
  const s = v(t) ? this._unwrap(t) : void 0;
@@ -774,7 +771,7 @@ class we {
774
771
  if (typeof t != "undefined")
775
772
  return t;
776
773
  const [r] = this._wrapHandle(e);
777
- return de(r != null ? r : e, {
774
+ return le(r != null ? r : e, {
778
775
  ctx: this.context,
779
776
  marshal: this._marshal,
780
777
  find: this._unmarshalFind,
@@ -811,24 +808,40 @@ class we {
811
808
  return this._unwrapResultAndUnmarshal(t);
812
809
  }
813
810
  /**
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.
811
+ * Evaluate ES module code in the VM and get the module's exports.
812
+ *
813
+ * Requires quickjs-emscripten >= 0.29.0 for export access.
817
814
  *
818
815
  * @param code - The ES module code to evaluate
819
816
  * @param filename - Optional filename for debugging purposes (default: "module.js")
820
- * @returns Undefined in most cases (module side effects are applied)
817
+ * @returns The module's exports object, or a Promise resolving to exports if using top-level await
821
818
  *
822
819
  * @example
823
820
  * ```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
821
+ * // Simple module with exports
822
+ * const exports = arena.evalModule(`
823
+ * export const value = 42;
824
+ * export function greet(name) {
825
+ * return "Hello, " + name;
826
+ * }
827
+ * `);
828
+ * console.log(exports.value); // 42
829
+ * console.log(exports.greet("World")); // "Hello, World"
830
+ *
831
+ * // Module with default export
832
+ * const mod = arena.evalModule('export default function(x) { return x * 2; }');
833
+ * console.log(mod.default(21)); // 42
834
+ *
835
+ * // Module with top-level await
836
+ * const promise = arena.evalModule('export const data = await Promise.resolve(123);');
837
+ * arena.executePendingJobs();
838
+ * const exports = await promise;
839
+ * console.log(exports.data); // 123
827
840
  * ```
828
841
  */
829
842
  evalModule(e, t = "module.js") {
830
843
  const r = this.context.evalCode(e, t, { type: "module" });
831
- this._unwrapResultAndUnmarshal(r);
844
+ return this._unwrapResultAndUnmarshal(r);
832
845
  }
833
846
  /**
834
847
  * Almost same as `vm.executePendingJobs()`, but it converts and re-throws error objects when an error is thrown during evaluation.
@@ -932,7 +945,7 @@ class we {
932
945
  */
933
946
  expose(e) {
934
947
  for (const [t, r] of Object.entries(e))
935
- $(this._marshal(r), (s) => {
948
+ z(this._marshal(r), (s) => {
936
949
  this.context.setProp(this.context.global, t, s);
937
950
  });
938
951
  }
@@ -956,7 +969,7 @@ class we {
956
969
  register(e, t) {
957
970
  if (this._registeredMap.has(e)) return;
958
971
  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));
972
+ this.context.sameValue(r, this.context.undefined) || (typeof t == "string" && this._registeredMapDispose.add(e), this._registeredMap.set(e, r));
960
973
  }
961
974
  /**
962
975
  * Execute `register` methods for each pair.
@@ -1010,7 +1023,7 @@ class we {
1010
1023
  }
1011
1024
  _wrap(e) {
1012
1025
  var t;
1013
- return me(
1026
+ return de(
1014
1027
  this.context,
1015
1028
  e,
1016
1029
  this._symbol,
@@ -1025,7 +1038,7 @@ class we {
1025
1038
  }
1026
1039
  _wrapHandle(e) {
1027
1040
  var t;
1028
- return he(
1041
+ return me(
1029
1042
  this.context,
1030
1043
  e,
1031
1044
  this._symbol,
@@ -1040,18 +1053,17 @@ class we {
1040
1053
  }
1041
1054
  }
1042
1055
  export {
1043
- we as Arena,
1056
+ ve as Arena,
1044
1057
  O as VMMap,
1045
1058
  y as call,
1046
- ve as complexity,
1047
- be as consumeAll,
1059
+ be as complexity,
1060
+ _e as consumeAll,
1048
1061
  I as defaultRegisteredObjects,
1049
- G as eq,
1050
- Z as isES2015Class,
1051
- W as isHandleObject,
1062
+ Y as isES2015Class,
1063
+ J as isHandleObject,
1052
1064
  v as isObject,
1053
- z as json,
1065
+ W as json,
1054
1066
  R as marshal,
1055
- de as unmarshal,
1067
+ le as unmarshal,
1056
1068
  E as walkObject
1057
1069
  };
@@ -1,23 +1,23 @@
1
- (function(m,v){typeof exports=="object"&&typeof module!="undefined"?v(exports):typeof define=="function"&&define.amd?define(["exports"],v):(m=typeof globalThis!="undefined"?globalThis:m||self,v(m.QuickjsEmscriptenSync={}))})(this,(function(m){"use strict";var ve=Object.defineProperty;var we=(m,v,j)=>v in m?ve(m,v,{enumerable:!0,configurable:!0,writable:!0,value:j}):m[v]=j;var c=(m,v,j)=>we(m,typeof v!="symbol"?v+"":v,j);const v=n=>{const e=new j(n);return new Proxy(n,{get(t,r,s){return r in e?e[r]:Reflect.get(t,r,s)}})};class j{constructor(e){c(this,"context");c(this,"fn");c(this,"fnGenerator");c(this,"fnCounter",Number.MIN_SAFE_INTEGER);c(this,"fnMap",new Map);c(this,"newFunction",(e,t)=>{this.fnCounter++;const r=this.fnCounter;return this.fnMap.set(r,t),this.context.unwrapResult(this.context.callFunction(this.fnGenerator,this.context.undefined,this.context.newString(e),this.context.newNumber(t.length),this.context.newNumber(r),this.fn))});this.context=e;const t=this.fnMap;this.fn=this.context.newFunction("",function(r,...s){const o=e.getNumber(r),i=t.get(o);if(!i)throw new Error("function is not registered");return i.call(this,...s)}),this.fnGenerator=e.unwrapResult(e.evalCode(`((name, length, id, f) => {
1
+ (function(m,v){typeof exports=="object"&&typeof module!="undefined"?v(exports):typeof define=="function"&&define.amd?define(["exports"],v):(m=typeof globalThis!="undefined"?globalThis:m||self,v(m.QuickjsEmscriptenSync={}))})(this,(function(m){"use strict";var be=Object.defineProperty;var ve=(m,v,j)=>v in m?be(m,v,{enumerable:!0,configurable:!0,writable:!0,value:j}):m[v]=j;var c=(m,v,j)=>ve(m,typeof v!="symbol"?v+"":v,j);const v=n=>{const e=new j(n);return new Proxy(n,{get(t,r,s){return r in e?e[r]:Reflect.get(t,r,s)}})};class j{constructor(e){c(this,"context");c(this,"fn");c(this,"fnGenerator");c(this,"fnCounter",Number.MIN_SAFE_INTEGER);c(this,"fnMap",new Map);c(this,"newFunction",(e,t)=>{this.fnCounter++;const r=this.fnCounter;return this.fnMap.set(r,t),this.context.unwrapResult(this.context.callFunction(this.fnGenerator,this.context.undefined,this.context.newString(e),this.context.newNumber(t.length),this.context.newNumber(r),this.fn))});this.context=e;const t=this.fnMap;this.fn=this.context.newFunction("",function(r,...s){const o=e.getNumber(r),i=t.get(o);if(!i)throw new Error("function is not registered");return i.call(this,...s)}),this.fnGenerator=e.unwrapResult(e.evalCode(`((name, length, id, f) => {
2
2
  const fn = function(...args) {
3
3
  return f.call(this, id, ...args);
4
4
  };
5
5
  fn.name = name;
6
6
  fn.length = length;
7
7
  return fn;
8
- })`))}disposeEx(){this.fnGenerator.dispose(),this.fn.dispose()}}const k=[[Symbol,"Symbol"],[Symbol.prototype,"Symbol.prototype"],[Object,"Object"],[Object.prototype,"Object.prototype"],[Function,"Function"],[Function.prototype,"Function.prototype"],[Boolean,"Boolean"],[Boolean.prototype,"Boolean.prototype"],[Array,"Array"],[Array.prototype,"Array.prototype"],[Error,"Error"],[Error.prototype,"Error.prototype"],[EvalError,"EvalError"],[EvalError.prototype,"EvalError.prototype"],[RangeError,"RangeError"],[RangeError.prototype,"RangeError.prototype"],[ReferenceError,"ReferenceError"],[ReferenceError.prototype,"ReferenceError.prototype"],[SyntaxError,"SyntaxError"],[SyntaxError.prototype,"SyntaxError.prototype"],[TypeError,"TypeError"],[TypeError.prototype,"TypeError.prototype"],[URIError,"URIError"],[URIError.prototype,"URIError.prototype"],...Object.getOwnPropertyNames(Symbol).filter(n=>typeof Symbol[n]=="symbol").map(n=>[Symbol[n],`Symbol.${n}`])];function J(n,e){const t=n.unwrapResult(n.evalCode(e)),r=(s,...o)=>n.unwrapResult(n.callFunction(t,s!=null?s:n.undefined,...o));return r.dispose=()=>t.dispose(),r.alive=!0,Object.defineProperty(r,"alive",{get:()=>t.alive}),r}function _(n,e,t,...r){const s=J(n,e);try{return s(t,...r)}finally{s.dispose()}}function C(n,e,t){return n.dump(_(n,"Object.is",void 0,e,t))}function W(n,e,t){return n.dump(_(n,"(a, b) => a instanceof b",void 0,e,t))}function F(n,e){return n.dump(_(n,'a => typeof a === "object" && a !== null || typeof a === "function"',void 0,e))}function H(n,e){const t=JSON.stringify(e);return t?_(n,"JSON.parse",void 0,n.newString(t)):n.undefined}function z(n,e){try{return e(n)}finally{for(const t of n)t.alive&&t.dispose()}}function $([n,e],t){try{return t(n)}finally{e&&n.dispose()}}function S(n,e){try{return e(...n.map(t=>t[0]))}finally{for(const[t,r]of n)r&&t.dispose()}}function q(n){return"handle"in n}function L(n){return q(n)?n.handle:n}function Q(n,e,t,r){var o;let s;for(const i of r)if(s=i(e,n),s)break;return s?(o=t(e,s))!=null?o:s:void 0}function V(n,e){return typeof n!="symbol"?void 0:_(e,"d => Symbol(d)",void 0,n.description?e.newString(n.description):e.undefined)}function X(n,e){return n instanceof Date?_(e,"d => new Date(d)",void 0,e.newNumber(n.getTime())):void 0}const Y=[V,X];function N(n){return typeof n=="function"&&/^class\s/.test(Function.prototype.toString.call(n))}function g(n){return typeof n=="function"||typeof n=="object"&&n!==null}function O(n,e){const t=new Set,r=s=>{if(!(!g(s)||t.has(s)||(e==null?void 0:e(s,t))===!1)){if(t.add(s),Array.isArray(s)){for(const o of s)r(o);return}if(typeof s=="object"){const o=Object.getPrototypeOf(s);o&&o!==Object.prototype&&r(o)}for(const o of Object.values(Object.getOwnPropertyDescriptors(s)))"value"in o&&r(o.value),"get"in o&&r(o.get),"set"in o&&r(o.set)}};return r(n),t}function Z(n,e){return O(n,e?(t,r)=>r.size<e:void 0).size}function K(){let n=()=>{},e=()=>{};return{promise:new Promise((r,s)=>{n=r,e=s}),resolve:n,reject:e}}function A(n,e,t,r){const s=n.newObject(),o=(a,u)=>{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);n.newObject().consume(h=>{Object.entries(u).forEach(([y,b])=>{const w=y==="value"?l:y==="get"?f:y==="set"?d:b?n.true:n.false;w&&n.setProp(h,y,w)}),n.setProp(s,p,h)})},i=Object.getOwnPropertyDescriptors(e);Object.entries(i).forEach(([a,u])=>o(a,u)),Object.getOwnPropertySymbols(i).forEach(a=>o(a,i[a])),_(n,"Object.defineProperties",void 0,t,s).dispose(),s.dispose()}function x(n,e,t,r,s,o){var u;if(typeof e!="function")return;const i=n.newFunction(e.name,function(...p){const l=r(this),f=p.map(d=>r(d));if(N(e)&&g(l)){const d=new e(...f);return Object.entries(d).forEach(([h,y])=>{n.setProp(this,h,t(y))}),this}return t(o?o(e,l,f):e.apply(l,f))}).consume(p=>_(n,`Cls => {
8
+ })`))}disposeEx(){this.fnGenerator.dispose(),this.fn.dispose()}}const k=[[Symbol,"Symbol"],[Symbol.prototype,"Symbol.prototype"],[Object,"Object"],[Object.prototype,"Object.prototype"],[Function,"Function"],[Function.prototype,"Function.prototype"],[Boolean,"Boolean"],[Boolean.prototype,"Boolean.prototype"],[Array,"Array"],[Array.prototype,"Array.prototype"],[Error,"Error"],[Error.prototype,"Error.prototype"],[EvalError,"EvalError"],[EvalError.prototype,"EvalError.prototype"],[RangeError,"RangeError"],[RangeError.prototype,"RangeError.prototype"],[ReferenceError,"ReferenceError"],[ReferenceError.prototype,"ReferenceError.prototype"],[SyntaxError,"SyntaxError"],[SyntaxError.prototype,"SyntaxError.prototype"],[TypeError,"TypeError"],[TypeError.prototype,"TypeError.prototype"],[URIError,"URIError"],[URIError.prototype,"URIError.prototype"],...Object.getOwnPropertyNames(Symbol).filter(n=>typeof Symbol[n]=="symbol").map(n=>[Symbol[n],`Symbol.${n}`])];function G(n,e){const t=n.unwrapResult(n.evalCode(e)),r=(o,...i)=>n.unwrapResult(n.callFunction(t,o!=null?o:n.undefined,...i)),s=()=>t.dispose();return r.dispose=s,r[Symbol.dispose]=s,r.alive=!0,Object.defineProperty(r,"alive",{get:()=>t.alive}),r}function _(n,e,t,...r){const s=G(n,e);try{return s(t,...r)}finally{s.dispose()}}function J(n,e,t){return n.dump(_(n,"(a, b) => a instanceof b",void 0,e,t))}function C(n,e){return n.dump(_(n,'a => typeof a === "object" && a !== null || typeof a === "function"',void 0,e))}function F(n,e){const t=JSON.stringify(e);return t?_(n,"JSON.parse",void 0,n.newString(t)):n.undefined}function W(n,e){try{return e(n)}finally{for(const t of n)t.alive&&t.dispose()}}function z([n,e],t){try{return t(n)}finally{e&&n.dispose()}}function S(n,e){try{return e(...n.map(t=>t[0]))}finally{for(const[t,r]of n)r&&t.dispose()}}function $(n){return"handle"in n}function V(n){return $(n)?n.handle:n}function L(n,e,t,r){var o;let s;for(const i of r)if(s=i(e,n),s)break;return s?(o=t(e,s))!=null?o:s:void 0}function Q(n,e){return typeof n!="symbol"?void 0:_(e,"d => Symbol(d)",void 0,n.description?e.newString(n.description):e.undefined)}function q(n,e){return n instanceof Date?_(e,"d => new Date(d)",void 0,e.newNumber(n.getTime())):void 0}const X=[Q,q];function H(n){return typeof n=="function"&&/^class\s/.test(Function.prototype.toString.call(n))}function g(n){return typeof n=="function"||typeof n=="object"&&n!==null}function O(n,e){const t=new Set,r=s=>{if(!(!g(s)||t.has(s)||(e==null?void 0:e(s,t))===!1)){if(t.add(s),Array.isArray(s)){for(const o of s)r(o);return}if(typeof s=="object"){const o=Object.getPrototypeOf(s);o&&o!==Object.prototype&&r(o)}for(const o of Object.values(Object.getOwnPropertyDescriptors(s)))"value"in o&&r(o.value),"get"in o&&r(o.get),"set"in o&&r(o.set)}};return r(n),t}function Y(n,e){return O(n,e?(t,r)=>r.size<e:void 0).size}function Z(){let n=()=>{},e=()=>{};return{promise:new Promise((r,s)=>{n=r,e=s}),resolve:n,reject:e}}function N(n,e,t,r){const s=n.newObject(),o=(a,u)=>{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);n.newObject().consume(h=>{Object.entries(u).forEach(([y,b])=>{const w=y==="value"?l:y==="get"?f:y==="set"?d:b?n.true:n.false;w&&n.setProp(h,y,w)}),n.setProp(s,p,h)})},i=Object.getOwnPropertyDescriptors(e);Object.entries(i).forEach(([a,u])=>o(a,u)),Object.getOwnPropertySymbols(i).forEach(a=>o(a,i[a])),_(n,"Object.defineProperties",void 0,t,s).dispose(),s.dispose()}function K(n,e,t,r,s,o){var u;if(typeof e!="function")return;const i=n.newFunction(e.name,function(...p){const l=r(this),f=p.map(d=>r(d));if(H(e)&&g(l)){const d=new e(...f);return Object.entries(d).forEach(([h,y])=>{n.setProp(this,h,t(y))}),this}return t(o?o(e,l,f):e.apply(l,f))}).consume(p=>_(n,`Cls => {
9
9
  const fn = function(...args) { return Cls.apply(this, args); };
10
10
  fn.name = Cls.name;
11
11
  fn.length = Cls.length;
12
12
  return fn;
13
- }`,void 0,p)),a=(u=s(e,i))!=null?u:i;return A(n,e,i,t),a}function ee(n,e,t){var o;const r=H(n,e);return(o=t(e,r))!=null?o:r}function te(n,e,t,r){var u;if(typeof e!="object"||e===null)return;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;return a&&_(n,"Object.setPrototypeOf",void 0,o,a).dispose(),A(n,e,s,t),o}function ne(n,e){switch(typeof e){case"undefined":return n.undefined;case"number":return n.newNumber(e);case"string":return n.newString(e);case"boolean":return e?n.true:n.false;case"object":return e===null?n.null:void 0}}function re(n,e,t,r){var o;if(!(e instanceof Promise))return;const s=n.newPromise();return e.then(i=>s.resolve(t(i)),i=>s.reject(t(i))),(o=r(e,s))!=null?o:s.handle}function E(n,e){var l,f,d,h,y;const{ctx:t,unmarshal:r,isMarshalable:s,find:o,pre:i}=e;{const b=ne(t,n);if(b)return b}{const b=o(n);if(b)return b}const a=s==null?void 0:s(n);if(a===!1)return t.undefined;const u=(b,w)=>i(b,w,a);if(a==="json")return ee(t,n,u);const p=b=>E(b,e);return(y=(h=(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?h:te(t,n,p,u))!=null?y:t.undefined}function se(n,e,t,r){var o;let s;for(const i of r)if(s=i(e,n),s)break;return s?(o=t(s,e))!=null?o:s:void 0}function oe(n,e){if(e.typeof(n)!=="symbol")return;const t=e.getString(e.getProp(n,"description"));return Symbol(t)}function ie(n,e){if(!e.dump(_(e,"a => a instanceof Date",void 0,n)))return;const t=e.getNumber(_(e,"a => a.getTime()",void 0,n));return new Date(t)}const ue=[oe,ie];function D(n,e,t,r){n.newFunction("",(s,o)=>{const[i]=r(s);if(typeof i!="string"&&typeof i!="number"&&typeof i!="symbol")return;const a=[["value",!0],["get",!0],["set",!0],["configurable",!1],["enumerable",!1],["writable",!1]].reduce((u,[p,l])=>{const f=n.getProp(o,p),d=n.typeof(f);if(d==="undefined")return u;if(!l&&d==="boolean")return u[p]=n.dump(n.getProp(o,p)),u;const[h,y]=r(f);return y&&f.dispose(),u[p]=h,u},{});Object.defineProperty(t,i,a)}).consume(s=>{_(n,`(o, fn) => {
13
+ }`,void 0,p)),a=(u=s(e,i))!=null?u:i;return N(n,e,i,t),a}function x(n,e,t){var o;const r=F(n,e);return(o=t(e,r))!=null?o:r}function ee(n,e,t,r){var u;if(typeof e!="object"||e===null)return;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;return a&&_(n,"Object.setPrototypeOf",void 0,o,a).dispose(),N(n,e,s,t),o}function te(n,e){switch(typeof e){case"undefined":return n.undefined;case"number":return n.newNumber(e);case"string":return n.newString(e);case"boolean":return e?n.true:n.false;case"object":return e===null?n.null:void 0}}function ne(n,e,t,r){var o;if(!(e instanceof Promise))return;const s=n.newPromise();return e.then(i=>s.resolve(t(i)),i=>s.reject(t(i))),(o=r(e,s))!=null?o:s.handle}function E(n,e){var l,f,d,h,y;const{ctx:t,unmarshal:r,isMarshalable:s,find:o,pre:i}=e;{const b=te(t,n);if(b)return b}{const b=o(n);if(b)return b}const a=s==null?void 0:s(n);if(a===!1)return t.undefined;const u=(b,w)=>i(b,w,a);if(a==="json")return x(t,n,u);const p=b=>E(b,e);return(y=(h=(d=(f=L(t,n,u,[...X,...(l=e.custom)!=null?l:[]]))!=null?f:ne(t,n,p,u))!=null?d:K(t,n,p,r,u,e.preApply))!=null?h:ee(t,n,p,u))!=null?y:t.undefined}function re(n,e,t,r){var o;let s;for(const i of r)if(s=i(e,n),s)break;return s?(o=t(s,e))!=null?o:s:void 0}function se(n,e){if(e.typeof(n)!=="symbol")return;const t=e.getString(e.getProp(n,"description"));return Symbol(t)}function oe(n,e){if(!e.dump(_(e,"a => a instanceof Date",void 0,n)))return;const t=e.getNumber(_(e,"a => a.getTime()",void 0,n));return new Date(t)}const ie=[se,oe];function A(n,e,t,r){n.newFunction("",(s,o)=>{const[i]=r(s);if(typeof i!="string"&&typeof i!="number"&&typeof i!="symbol")return;const a=[["value",!0],["get",!0],["set",!0],["configurable",!1],["enumerable",!1],["writable",!1]].reduce((u,[p,l])=>{const f=n.getProp(o,p),d=n.typeof(f);if(d==="undefined")return u;if(!l&&d==="boolean")return u[p]=n.dump(n.getProp(o,p)),u;const[h,y]=r(f);return y&&f.dispose(),u[p]=h,u},{});Object.defineProperty(t,i,a)}).consume(s=>{_(n,`(o, fn) => {
14
14
  const descs = Object.getOwnPropertyDescriptors(o);
15
15
  Object.entries(descs).forEach(([k, v]) => fn(k, v));
16
16
  Object.getOwnPropertySymbols(descs).forEach(k => fn(k, descs[k]));
17
- }`,void 0,e,s).dispose()})}function ae(n,e,t,r,s){var a;if(n.typeof(e)!=="function")return;const o=function(...u){return S([t(this),...u.map(p=>t(p))],(p,...l)=>{if(new.target){const[y]=r(_(n,"(Cls, ...args) => new Cls(...args)",p,e,...l));return Object.defineProperties(this,Object.getOwnPropertyDescriptors(y)),this}const f=n.unwrapResult(n.callFunction(e,p,...l)),[d,h]=r(f);return h&&f.dispose(),d})},i=(a=s(o,e))!=null?a:o;return D(n,e,o,r),i}function pe(n,e,t,r){var a;if(n.typeof(e)!=="object"||n.unwrapResult(n.evalCode("o => o === null")).consume(u=>n.dump(n.unwrapResult(n.callFunction(u,n.undefined,e)))))return;const s=_(n,"Array.isArray",void 0,e).consume(u=>n.dump(u))?[]:{},o=(a=r(s,e))!=null?a:s,i=_(n,`o => {
17
+ }`,void 0,e,s).dispose()})}function ue(n,e,t,r,s){var a;if(n.typeof(e)!=="function")return;const o=function(...u){return S([t(this),...u.map(p=>t(p))],(p,...l)=>{if(new.target){const[y]=r(_(n,"(Cls, ...args) => new Cls(...args)",p,e,...l));return Object.defineProperties(this,Object.getOwnPropertyDescriptors(y)),this}const f=n.unwrapResult(n.callFunction(e,p,...l)),[d,h]=r(f);return h&&f.dispose(),d})},i=(a=s(o,e))!=null?a:o;return A(n,e,o,r),i}function ae(n,e,t,r){var a;if(n.typeof(e)!=="object"||n.unwrapResult(n.evalCode("o => o === null")).consume(u=>n.dump(n.unwrapResult(n.callFunction(u,n.undefined,e)))))return;const s=_(n,"Array.isArray",void 0,e).consume(u=>n.dump(u))?[]:{},o=(a=r(s,e))!=null?a:s,i=_(n,`o => {
18
18
  const p = Object.getPrototypeOf(o);
19
19
  return !p || p === Object.prototype || p === Array.prototype ? undefined : p;
20
- }`,void 0,e).consume(u=>{if(n.typeof(u)==="undefined")return;const[p]=t(u);return p});return typeof i=="object"&&Object.setPrototypeOf(o,i),D(n,e,s,t),o}function fe(n,e){const t=n.typeof(e);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]}function ce(n,e,t,r){var p;if(!le(n,e))return;const s=K(),[o,i]=t(s.resolve),[a,u]=t(s.reject);return _(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}function le(n,e){return e.owner?n.unwrapResult(n.evalCode("Promise")).consume(t=>e.owner?W(n,e,t):!1):!1}function B(n,e){const[t]=T(n,e);return t}function T(n,e){var u,p,l,f;const{ctx:t,marshal:r,find:s,pre:o}=e;{const[d,h]=fe(t,n);if(h)return[d,!1]}{const d=s(n);if(d)return[d,!0]}const i=d=>T(d,e);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]}class M{constructor(e){c(this,"ctx");c(this,"_map1",new Map);c(this,"_map2",new Map);c(this,"_map3",new Map);c(this,"_map4",new Map);c(this,"_counterMap",new Map);c(this,"_disposables",new Set);c(this,"_mapGet");c(this,"_mapSet");c(this,"_mapDelete");c(this,"_mapClear");c(this,"_counter",Number.MIN_SAFE_INTEGER);this.ctx=e;const t=e.unwrapResult(e.evalCode(`() => {
20
+ }`,void 0,e).consume(u=>{if(n.typeof(u)==="undefined")return;const[p]=t(u);return p});return typeof i=="object"&&Object.setPrototypeOf(o,i),A(n,e,s,t),o}function pe(n,e){const t=n.typeof(e);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]}function fe(n,e,t,r){var p;if(!ce(n,e))return;const s=Z(),[o,i]=t(s.resolve),[a,u]=t(s.reject);return _(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}function ce(n,e){return e.owner?n.unwrapResult(n.evalCode("Promise")).consume(t=>e.owner?J(n,e,t):!1):!1}function D(n,e){const[t]=B(n,e);return t}function B(n,e){var u,p,l,f;const{ctx:t,marshal:r,find:s,pre:o}=e;{const[d,h]=pe(t,n);if(h)return[d,!1]}{const d=s(n);if(d)return[d,!0]}const i=d=>B(d,e);return[(f=(l=(p=re(t,n,o,[...ie,...(u=e.custom)!=null?u:[]]))!=null?p:fe(t,n,r,o))!=null?l:ue(t,n,r,i,o))!=null?f:ae(t,n,i,o),!1]}class M{constructor(e){c(this,"ctx");c(this,"_map1",new Map);c(this,"_map2",new Map);c(this,"_map3",new Map);c(this,"_map4",new Map);c(this,"_counterMap",new Map);c(this,"_disposables",new Set);c(this,"_mapGet");c(this,"_mapSet");c(this,"_mapDelete");c(this,"_mapClear");c(this,"_counter",Number.MIN_SAFE_INTEGER);this.ctx=e;const t=e.unwrapResult(e.evalCode(`() => {
21
21
  const mapSym = new Map();
22
22
  let map = new WeakMap();
23
23
  let map2 = new WeakMap();
@@ -40,7 +40,7 @@
40
40
  map2 = new WeakMap();
41
41
  }
42
42
  };
43
- }`)).consume(r=>this._call(r,void 0));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)}set(e,t,r,s){var a;if(!t.alive||s&&!s.alive)return!1;const o=(a=this.get(e))!=null?a:this.get(r);if(o)return o===t||o===s;const i=this._counter++;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=>{this._call(this._mapSet,void 0,t,u,s!=null?s:this.ctx.undefined)}),!0}merge(e){if(e)for(const t of e)t&&t[1]&&this.set(t[0],t[1],t[2],t[3])}get(e){var s;const t=(s=this._map1.get(e))!=null?s:this._map2.get(e),r=typeof t=="number"?this._map3.get(t):void 0;if(r){if(!r.alive){this.delete(e);return}return r}}getByHandle(e){if(e.alive)return this._counterMap.get(this.ctx.getNumber(this._call(this._mapGet,void 0,e)))}has(e){return!!this.get(e)}hasHandle(e){return typeof this.getByHandle(e)!="undefined"}keys(){return this._map1.keys()}delete(e,t){var i;const r=(i=this._map1.get(e))!=null?i:this._map2.get(e);if(typeof r=="undefined")return;const s=this._map3.get(r),o=this._map4.get(r);this._call(this._mapDelete,void 0,...[s,o].filter(a=>!!(a!=null&&a.alive))),this._map1.delete(e),this._map2.delete(e),this._map3.delete(r),this._map4.delete(r);for(const[a,u]of this._map1)if(u===r){this._map1.delete(a);break}for(const[a,u]of this._map2)if(u===r){this._map2.delete(a);break}for(const[a,u]of this._counterMap)if(u===e){this._counterMap.delete(a);break}t&&(s!=null&&s.alive&&s.dispose(),o!=null&&o.alive&&o.dispose())}deleteByHandle(e,t){const r=this.getByHandle(e);typeof r!="undefined"&&this.delete(r,t)}clear(){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)}dispose(){for(const e of this._disposables.values())e.alive&&e.dispose();for(const e of this._map3.values())e.alive&&e.dispose();for(const e of this._map4.values())e.alive&&e.dispose();this._disposables.clear(),this.clear()}get size(){return this._map1.size}[Symbol.iterator](){const e=this._map1.keys();return{next:()=>{for(;;){const t=e.next();if(t.done)return{value:void 0,done:!0};const r=this._map1.get(t.value);if(typeof r=="undefined")continue;const s=this._map3.get(r),o=this._map4.get(r);if(!s)continue;const i=this._get2(r);return{value:[t.value,s,i,o],done:!1}}}}}_get2(e){for(const[t,r]of this._map2)if(r===e)return t}_call(e,t,...r){return this.ctx.unwrapResult(this.ctx.callFunction(e,typeof t=="undefined"?this.ctx.undefined:t,...r))}}function de(n,e,t,r,s,o,i){if(!g(e)||e instanceof Promise||e instanceof Date||i&&!i(e))return;if(he(e,t))return e;const a=new Proxy(e,{get(u,p){return p===t?u:Reflect.get(u,p)},set(u,p,l,f){var y;const d=P(l,t),h=(y=o==null?void 0:o(f))!=null?y:"host";return h!=="vm"&&!Reflect.set(u,p,d,f)||h==="host"||!n.alive||S([s(f),s(p),s(d)],(b,w,I)=>{const[G,_e]=R(n,b,r);_e?G.consume(be=>n.setProp(be,w,I)):n.setProp(G,w,I)}),!0},deleteProperty(u,p){var f;const l=(f=o==null?void 0:o(a))!=null?f:"host";return S([s(a),s(p)],(d,h)=>{const[y,b]=R(n,d,r);if(l==="vm"||Reflect.deleteProperty(u,p)){if(l==="host"||!n.alive)return!0;b?y.consume(w=>_(n,"(a, b) => delete a[b]",void 0,w,h)):_(n,"(a, b) => delete a[b]",void 0,y,h)}return!0})}});return a}function me(n,e,t,r,s,o,i){if(!F(n,e)||i&&!i(e,n))return[void 0,!1];if(U(n,e,r))return[e,!1];const a=l=>{const f=o==null?void 0:o(s(l));return typeof f=="string"?n.newString(f):n.undefined},u=(l,f,d)=>{const h=s(l);if(!h)return;const y=s(f);if(y==="__proto__")return;const b=s(d);P(h,t)[y]=b},p=(l,f)=>{const d=s(l);if(!d)return;const h=s(f);delete P(d,t)[h]};return n.newFunction("proxyFuncs",(l,...f)=>{switch(n.getNumber(l)){case 1:return a(f[0]);case 2:return u(f[0],f[1],f[2]);case 3:return p(f[0],f[1])}return n.undefined}).consume(l=>[_(n,`(target, sym, proxyFuncs) => {
43
+ }`)).consume(r=>this._call(r,void 0));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)}set(e,t,r,s){var a;if(!t.alive||s&&!s.alive)return!1;const o=(a=this.get(e))!=null?a:this.get(r);if(o)return o===t||o===s;const i=this._counter++;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=>{this._call(this._mapSet,void 0,t,u,s!=null?s:this.ctx.undefined)}),!0}merge(e){if(e)for(const t of e)t&&t[1]&&this.set(t[0],t[1],t[2],t[3])}get(e){var s;const t=(s=this._map1.get(e))!=null?s:this._map2.get(e),r=typeof t=="number"?this._map3.get(t):void 0;if(r){if(!r.alive){this.delete(e);return}return r}}getByHandle(e){if(e.alive)return this._counterMap.get(this.ctx.getNumber(this._call(this._mapGet,void 0,e)))}has(e){return!!this.get(e)}hasHandle(e){return typeof this.getByHandle(e)!="undefined"}keys(){return this._map1.keys()}delete(e,t){var i;const r=(i=this._map1.get(e))!=null?i:this._map2.get(e);if(typeof r=="undefined")return;const s=this._map3.get(r),o=this._map4.get(r);this._call(this._mapDelete,void 0,...[s,o].filter(a=>!!(a!=null&&a.alive))),this._map1.delete(e),this._map2.delete(e),this._map3.delete(r),this._map4.delete(r);for(const[a,u]of this._map1)if(u===r){this._map1.delete(a);break}for(const[a,u]of this._map2)if(u===r){this._map2.delete(a);break}for(const[a,u]of this._counterMap)if(u===e){this._counterMap.delete(a);break}t&&(s!=null&&s.alive&&s.dispose(),o!=null&&o.alive&&o.dispose())}deleteByHandle(e,t){const r=this.getByHandle(e);typeof r!="undefined"&&this.delete(r,t)}clear(){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)}dispose(){for(const e of this._disposables.values())e.alive&&e.dispose();for(const e of this._map3.values())e.alive&&e.dispose();for(const e of this._map4.values())e.alive&&e.dispose();this._disposables.clear(),this.clear()}get size(){return this._map1.size}[Symbol.iterator](){const e=this._map1.keys();return{next:()=>{for(;;){const t=e.next();if(t.done)return{value:void 0,done:!0};const r=this._map1.get(t.value);if(typeof r=="undefined")continue;const s=this._map3.get(r),o=this._map4.get(r);if(!s)continue;const i=this._get2(r);return{value:[t.value,s,i,o],done:!1}}}}}_get2(e){for(const[t,r]of this._map2)if(r===e)return t}_call(e,t,...r){return this.ctx.unwrapResult(this.ctx.callFunction(e,typeof t=="undefined"?this.ctx.undefined:t,...r))}}function le(n,e,t,r,s,o,i){if(!g(e)||e instanceof Promise||e instanceof Date||i&&!i(e))return;if(me(e,t))return e;const a=new Proxy(e,{get(u,p){return p===t?u:Reflect.get(u,p)},set(u,p,l,f){var y;const d=P(l,t),h=(y=o==null?void 0:o(f))!=null?y:"host";return h!=="vm"&&!Reflect.set(u,p,d,f)||h==="host"||!n.alive||S([s(f),s(p),s(d)],(b,w,U)=>{const[I,ye]=R(n,b,r);ye?I.consume(_e=>n.setProp(_e,w,U)):n.setProp(I,w,U)}),!0},deleteProperty(u,p){var f;const l=(f=o==null?void 0:o(a))!=null?f:"host";return S([s(a),s(p)],(d,h)=>{const[y,b]=R(n,d,r);if(l==="vm"||Reflect.deleteProperty(u,p)){if(l==="host"||!n.alive)return!0;b?y.consume(w=>_(n,"(a, b) => delete a[b]",void 0,w,h)):_(n,"(a, b) => delete a[b]",void 0,y,h)}return!0})}});return a}function de(n,e,t,r,s,o,i){if(!C(n,e)||i&&!i(e,n))return[void 0,!1];if(T(n,e,r))return[e,!1];const a=l=>{const f=o==null?void 0:o(s(l));return typeof f=="string"?n.newString(f):n.undefined},u=(l,f,d)=>{const h=s(l);if(!h)return;const y=s(f);if(y==="__proto__")return;const b=s(d);P(h,t)[y]=b},p=(l,f)=>{const d=s(l);if(!d)return;const h=s(f);delete P(d,t)[h]};return n.newFunction("proxyFuncs",(l,...f)=>{switch(n.getNumber(l)){case 1:return a(f[0]);case 2:return u(f[0],f[1],f[2]);case 3:return p(f[0],f[1])}return n.undefined}).consume(l=>[_(n,`(target, sym, proxyFuncs) => {
44
44
  const rec = new Proxy(target, {
45
45
  get(obj, key, receiver) {
46
46
  return key === sym ? obj : Reflect.get(obj, key, receiver)
@@ -68,4 +68,4 @@
68
68
  },
69
69
  });
70
70
  return rec;
71
- }`,void 0,e,r,l),!0])}function P(n,e){var t;return g(n)&&(t=n[e])!=null?t:n}function R(n,e,t){return U(n,e,t)?[n.getProp(e,t),!0]:[e,!1]}function he(n,e){return g(n)&&!!n[e]}function U(n,e,t){return!!n.dump(_(n,'(a, s) => (a instanceof Promise) || (a instanceof Date) || (typeof a === "object" && a !== null || typeof a === "function") && !!a[s]',void 0,e,t))}class ye{constructor(e,t){c(this,"context");c(this,"_map");c(this,"_registeredMap");c(this,"_registeredMapDispose",new Set);c(this,"_sync",new Set);c(this,"_temporalSync",new Set);c(this,"_symbol",Symbol());c(this,"_symbolHandle");c(this,"_options");c(this,"_isMarshalable",e=>{var r,s;const t=(r=this._options)==null?void 0:r.isMarshalable;return(s=typeof t=="function"?t(this._unwrap(e)):t)!=null?s:"json"});c(this,"_marshalFind",e=>{var s,o,i;const t=this._unwrap(e);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});c(this,"_marshalPre",(e,t,r)=>{var s;if(r!=="json")return(s=this._register(e,L(t),this._map))==null?void 0:s[1]});c(this,"_marshalPreApply",(e,t,r)=>{const s=g(t)?this._unwrap(t):void 0;s&&this._temporalSync.add(s);try{return e.apply(t,r)}finally{s&&this._temporalSync.delete(s)}});c(this,"_marshal",e=>{var s,o;const t=this._registeredMap.get(e);if(t)return[t,!1];const r=E((s=this._wrap(e))!=null?s:e,{ctx:this.context,unmarshal:this._unmarshal,isMarshalable:this._isMarshalable,find:this._marshalFind,pre:this._marshalPre,preApply:this._marshalPreApply,custom:(o=this._options)==null?void 0:o.customMarshaller});return[r,!this._map.hasHandle(r)]});c(this,"_preUnmarshal",(e,t)=>{var r;return(r=this._register(e,t,void 0,!0))==null?void 0:r[0]});c(this,"_unmarshalFind",e=>{var t;return(t=this._registeredMap.getByHandle(e))!=null?t:this._map.getByHandle(e)});c(this,"_unmarshal",e=>{var s;const t=this._registeredMap.getByHandle(e);if(typeof t!="undefined")return t;const[r]=this._wrapHandle(e);return B(r!=null?r:e,{ctx:this.context,marshal:this._marshal,find:this._unmarshalFind,pre:this._preUnmarshal,custom:(s=this._options)==null?void 0:s.customUnmarshaller})});c(this,"_syncMode",e=>{const t=this._unwrap(e);return this._sync.has(t)||this._temporalSync.has(t)?"both":void 0});c(this,"_unwrapIfNotSynced",e=>{const t=this._unwrap(e);return t instanceof Promise||!this._sync.has(t)?t:e});var r;t!=null&&t.compat&&!("runtime"in e)&&(e.runtime={hasPendingJob:()=>e.hasPendingJob(),executePendingJobs:s=>e.executePendingJobs(s)}),this.context=t!=null&&t.experimentalContextEx?v(e):e,this._options=t,this._symbolHandle=e.unwrapResult(e.evalCode("Symbol()")),this._map=new M(e),this._registeredMap=new M(e),this.registerAll((r=t==null?void 0:t.registeredObjects)!=null?r:k)}dispose(){var e,t;this._map.dispose(),this._registeredMap.dispose(),this._symbolHandle.dispose(),(t=(e=this.context).disposeEx)==null||t.call(e)}evalCode(e){const t=this.context.evalCode(e);return this._unwrapResultAndUnmarshal(t)}evalModule(e,t="module.js"){const r=this.context.evalCode(e,t,{type:"module"});this._unwrapResultAndUnmarshal(r)}executePendingJobs(e){const t=this.context.runtime.executePendingJobs(e);if("value"in t)return t.value;throw this._unwrapIfNotSynced(t.error.consume(this._unmarshal))}setMemoryLimit(e){this.context.runtime.setMemoryLimit(e)}setMaxStackSize(e){this.context.runtime.setMaxStackSize(e)}getMemoryUsage(){const e=this.context.runtime.computeMemoryUsage();try{return this.context.dump(e)}finally{e.dispose()}}dumpMemoryUsage(){return this.context.runtime.dumpMemoryUsage()}expose(e){for(const[t,r]of Object.entries(e))$(this._marshal(r),s=>{this.context.setProp(this.context.global,t,s)})}sync(e){const t=this._wrap(e);return typeof t=="undefined"?e:(O(t,r=>{const s=this._unwrap(r);this._sync.add(s)}),t)}register(e,t){if(this._registeredMap.has(e))return;const r=typeof t=="string"?this._unwrapResult(this.context.evalCode(t)):t;C(this.context,r,this.context.undefined)||(typeof t=="string"&&this._registeredMapDispose.add(e),this._registeredMap.set(e,r))}registerAll(e){for(const[t,r]of e)this.register(t,r)}unregister(e,t){this._registeredMap.delete(e,this._registeredMapDispose.has(e)||t),this._registeredMapDispose.delete(e)}unregisterAll(e,t){for(const r of e)this.unregister(r,t)}startSync(e){if(!g(e))return;const t=this._unwrap(e);this._sync.add(t)}endSync(e){this._sync.delete(this._unwrap(e))}_unwrapResult(e){if("value"in e)return e.value;throw this._unwrapIfNotSynced(e.error.consume(this._unmarshal))}_unwrapResultAndUnmarshal(e){if(e)return this._unwrapIfNotSynced(this._unwrapResult(e).consume(this._unmarshal))}_register(e,t,r=this._map,s){if(this._registeredMap.has(e)||this._registeredMap.hasHandle(t))return;let o=this._wrap(e);const[i]=this._wrapHandle(t),a=e instanceof Promise;if(!i||!o&&!a)return;a&&(o=e);const u=this._unwrap(e),[p,l]=this._unwrapHandle(t);if(r.set(o,i,u,p))s&&this._sync.add(u);else throw l&&p.dispose(),new Error("already registered");return[o,i]}_wrap(e){var t;return de(this.context,e,this._symbol,this._symbolHandle,this._marshal,this._syncMode,(t=this._options)==null?void 0:t.isWrappable)}_unwrap(e){return P(e,this._symbol)}_wrapHandle(e){var t;return me(this.context,e,this._symbol,this._symbolHandle,this._unmarshal,this._syncMode,(t=this._options)==null?void 0:t.isHandleWrappable)}_unwrapHandle(e){return R(this.context,e,this._symbolHandle)}}m.Arena=ye,m.VMMap=M,m.call=_,m.complexity=Z,m.consumeAll=z,m.defaultRegisteredObjects=k,m.eq=C,m.isES2015Class=N,m.isHandleObject=F,m.isObject=g,m.json=H,m.marshal=E,m.unmarshal=B,m.walkObject=O,Object.defineProperty(m,Symbol.toStringTag,{value:"Module"})}));
71
+ }`,void 0,e,r,l),!0])}function P(n,e){var t;return g(n)&&(t=n[e])!=null?t:n}function R(n,e,t){return T(n,e,t)?[n.getProp(e,t),!0]:[e,!1]}function me(n,e){return g(n)&&!!n[e]}function T(n,e,t){return!!n.dump(_(n,'(a, s) => (a instanceof Promise) || (a instanceof Date) || (typeof a === "object" && a !== null || typeof a === "function") && !!a[s]',void 0,e,t))}class he{constructor(e,t){c(this,"context");c(this,"_map");c(this,"_registeredMap");c(this,"_registeredMapDispose",new Set);c(this,"_sync",new Set);c(this,"_temporalSync",new Set);c(this,"_symbol",Symbol());c(this,"_symbolHandle");c(this,"_options");c(this,"_isMarshalable",e=>{var r,s;const t=(r=this._options)==null?void 0:r.isMarshalable;return(s=typeof t=="function"?t(this._unwrap(e)):t)!=null?s:"json"});c(this,"_marshalFind",e=>{var s,o,i;const t=this._unwrap(e);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});c(this,"_marshalPre",(e,t,r)=>{var s;if(r!=="json")return(s=this._register(e,V(t),this._map))==null?void 0:s[1]});c(this,"_marshalPreApply",(e,t,r)=>{const s=g(t)?this._unwrap(t):void 0;s&&this._temporalSync.add(s);try{return e.apply(t,r)}finally{s&&this._temporalSync.delete(s)}});c(this,"_marshal",e=>{var s,o;const t=this._registeredMap.get(e);if(t)return[t,!1];const r=E((s=this._wrap(e))!=null?s:e,{ctx:this.context,unmarshal:this._unmarshal,isMarshalable:this._isMarshalable,find:this._marshalFind,pre:this._marshalPre,preApply:this._marshalPreApply,custom:(o=this._options)==null?void 0:o.customMarshaller});return[r,!this._map.hasHandle(r)]});c(this,"_preUnmarshal",(e,t)=>{var r;return(r=this._register(e,t,void 0,!0))==null?void 0:r[0]});c(this,"_unmarshalFind",e=>{var t;return(t=this._registeredMap.getByHandle(e))!=null?t:this._map.getByHandle(e)});c(this,"_unmarshal",e=>{var s;const t=this._registeredMap.getByHandle(e);if(typeof t!="undefined")return t;const[r]=this._wrapHandle(e);return D(r!=null?r:e,{ctx:this.context,marshal:this._marshal,find:this._unmarshalFind,pre:this._preUnmarshal,custom:(s=this._options)==null?void 0:s.customUnmarshaller})});c(this,"_syncMode",e=>{const t=this._unwrap(e);return this._sync.has(t)||this._temporalSync.has(t)?"both":void 0});c(this,"_unwrapIfNotSynced",e=>{const t=this._unwrap(e);return t instanceof Promise||!this._sync.has(t)?t:e});var r;t!=null&&t.compat&&!("runtime"in e)&&(e.runtime={hasPendingJob:()=>e.hasPendingJob(),executePendingJobs:s=>e.executePendingJobs(s)}),this.context=t!=null&&t.experimentalContextEx?v(e):e,this._options=t,this._symbolHandle=e.unwrapResult(e.evalCode("Symbol()")),this._map=new M(e),this._registeredMap=new M(e),this.registerAll((r=t==null?void 0:t.registeredObjects)!=null?r:k)}dispose(){var e,t;this._map.dispose(),this._registeredMap.dispose(),this._symbolHandle.dispose(),(t=(e=this.context).disposeEx)==null||t.call(e)}evalCode(e){const t=this.context.evalCode(e);return this._unwrapResultAndUnmarshal(t)}evalModule(e,t="module.js"){const r=this.context.evalCode(e,t,{type:"module"});return this._unwrapResultAndUnmarshal(r)}executePendingJobs(e){const t=this.context.runtime.executePendingJobs(e);if("value"in t)return t.value;throw this._unwrapIfNotSynced(t.error.consume(this._unmarshal))}setMemoryLimit(e){this.context.runtime.setMemoryLimit(e)}setMaxStackSize(e){this.context.runtime.setMaxStackSize(e)}getMemoryUsage(){const e=this.context.runtime.computeMemoryUsage();try{return this.context.dump(e)}finally{e.dispose()}}dumpMemoryUsage(){return this.context.runtime.dumpMemoryUsage()}expose(e){for(const[t,r]of Object.entries(e))z(this._marshal(r),s=>{this.context.setProp(this.context.global,t,s)})}sync(e){const t=this._wrap(e);return typeof t=="undefined"?e:(O(t,r=>{const s=this._unwrap(r);this._sync.add(s)}),t)}register(e,t){if(this._registeredMap.has(e))return;const r=typeof t=="string"?this._unwrapResult(this.context.evalCode(t)):t;this.context.sameValue(r,this.context.undefined)||(typeof t=="string"&&this._registeredMapDispose.add(e),this._registeredMap.set(e,r))}registerAll(e){for(const[t,r]of e)this.register(t,r)}unregister(e,t){this._registeredMap.delete(e,this._registeredMapDispose.has(e)||t),this._registeredMapDispose.delete(e)}unregisterAll(e,t){for(const r of e)this.unregister(r,t)}startSync(e){if(!g(e))return;const t=this._unwrap(e);this._sync.add(t)}endSync(e){this._sync.delete(this._unwrap(e))}_unwrapResult(e){if("value"in e)return e.value;throw this._unwrapIfNotSynced(e.error.consume(this._unmarshal))}_unwrapResultAndUnmarshal(e){if(e)return this._unwrapIfNotSynced(this._unwrapResult(e).consume(this._unmarshal))}_register(e,t,r=this._map,s){if(this._registeredMap.has(e)||this._registeredMap.hasHandle(t))return;let o=this._wrap(e);const[i]=this._wrapHandle(t),a=e instanceof Promise;if(!i||!o&&!a)return;a&&(o=e);const u=this._unwrap(e),[p,l]=this._unwrapHandle(t);if(r.set(o,i,u,p))s&&this._sync.add(u);else throw l&&p.dispose(),new Error("already registered");return[o,i]}_wrap(e){var t;return le(this.context,e,this._symbol,this._symbolHandle,this._marshal,this._syncMode,(t=this._options)==null?void 0:t.isWrappable)}_unwrap(e){return P(e,this._symbol)}_wrapHandle(e){var t;return de(this.context,e,this._symbol,this._symbolHandle,this._unmarshal,this._syncMode,(t=this._options)==null?void 0:t.isHandleWrappable)}_unwrapHandle(e){return R(this.context,e,this._symbolHandle)}}m.Arena=he,m.VMMap=M,m.call=_,m.complexity=Y,m.consumeAll=W,m.defaultRegisteredObjects=k,m.isES2015Class=H,m.isHandleObject=C,m.isObject=g,m.json=F,m.marshal=E,m.unmarshal=D,m.walkObject=O,Object.defineProperty(m,Symbol.toStringTag,{value:"Module"})}));
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "quickjs-emscripten-sync",
3
3
  "author": "rot1024",
4
- "version": "1.6.0",
4
+ "version": "1.8.0",
5
5
  "license": "MIT",
6
6
  "source": "./src/index.ts",
7
7
  "main": "./dist/quickjs-emscripten-sync.umd.js",
@@ -35,7 +35,7 @@
35
35
  "eslint": "^8.57.1",
36
36
  "eslint-config-reearth": "^0.2.1",
37
37
  "prettier": "^2.8.8",
38
- "quickjs-emscripten": "^0.25.0",
38
+ "quickjs-emscripten": "^0.31.0",
39
39
  "typescript": "^5.7.2",
40
40
  "vite": "^6.0.3",
41
41
  "vite-plugin-dts": "^4.3.0",
package/src/index.test.ts CHANGED
@@ -715,6 +715,107 @@ describe("evalModule", () => {
715
715
  arena.dispose();
716
716
  ctx.dispose();
717
717
  });
718
+
719
+ test("module returns exported values (0.29+)", async () => {
720
+ const ctx = (await getQuickJS()).newContext();
721
+ const arena = new Arena(ctx, { isMarshalable: true });
722
+
723
+ const exports = arena.evalModule(`
724
+ export const value = 42;
725
+ export const message = "Hello";
726
+ export const obj = { a: 1, b: 2 };
727
+ `);
728
+
729
+ expect(exports.value).toBe(42);
730
+ expect(exports.message).toBe("Hello");
731
+ expect(exports.obj).toEqual({ a: 1, b: 2 });
732
+
733
+ arena.dispose();
734
+ ctx.dispose();
735
+ });
736
+
737
+ test("module returns exported functions (0.29+)", async () => {
738
+ const ctx = (await getQuickJS()).newContext();
739
+ const arena = new Arena(ctx, { isMarshalable: true });
740
+
741
+ const exports = arena.evalModule(`
742
+ export function greet(name) {
743
+ return "Hello, " + name;
744
+ }
745
+ export function add(a, b) {
746
+ return a + b;
747
+ }
748
+ `);
749
+
750
+ expect(exports.greet("World")).toBe("Hello, World");
751
+ expect(exports.add(2, 3)).toBe(5);
752
+
753
+ arena.dispose();
754
+ ctx.dispose();
755
+ });
756
+
757
+ test("module with default export (0.29+)", async () => {
758
+ const ctx = (await getQuickJS()).newContext();
759
+ const arena = new Arena(ctx, { isMarshalable: true });
760
+
761
+ const exports = arena.evalModule(`
762
+ export default function(x) {
763
+ return x * 2;
764
+ }
765
+ `);
766
+
767
+ expect(exports.default(21)).toBe(42);
768
+
769
+ arena.dispose();
770
+ ctx.dispose();
771
+ });
772
+
773
+ test("module with class export (0.29+)", async () => {
774
+ const ctx = (await getQuickJS()).newContext();
775
+ const arena = new Arena(ctx, { isMarshalable: true });
776
+
777
+ // Export objects and static methods from a class
778
+ const exports = arena.evalModule(`
779
+ export class Counter {
780
+ static create(initial = 0) {
781
+ return { count: initial };
782
+ }
783
+ static increment(obj) {
784
+ return ++obj.count;
785
+ }
786
+ }
787
+ `);
788
+
789
+ // Static methods can be called from the host
790
+ const counter = exports.Counter.create(10);
791
+ expect(counter.count).toBe(10);
792
+ expect(exports.Counter.increment(counter)).toBe(11);
793
+ expect(counter.count).toBe(11);
794
+
795
+ arena.dispose();
796
+ ctx.dispose();
797
+ });
798
+
799
+ test("module with top-level await (0.29+)", async () => {
800
+ const ctx = (await getQuickJS()).newContext();
801
+ const arena = new Arena(ctx, { isMarshalable: true });
802
+
803
+ const exportsPromise = arena.evalModule(`
804
+ export const data = await Promise.resolve(123);
805
+ export const message = "loaded";
806
+ `);
807
+
808
+ expect(exportsPromise).toBeInstanceOf(Promise);
809
+
810
+ arena.executePendingJobs();
811
+
812
+ const exports = await exportsPromise;
813
+ expect(exports.data).toBe(123);
814
+ expect(exports.message).toBe("loaded");
815
+
816
+ arena.dispose();
817
+ ctx.dispose();
818
+ });
718
819
  });
719
820
 
720
821
  describe("memory management", () => {
@@ -911,3 +1012,23 @@ describe("memory management", () => {
911
1012
  ctx.dispose();
912
1013
  });
913
1014
  });
1015
+
1016
+ describe("intrinsics configuration", () => {
1017
+ test("intrinsics can be configured when creating context", async () => {
1018
+ const quickjs = await getQuickJS();
1019
+ const runtime = quickjs.newRuntime();
1020
+
1021
+ // Example: disable eval for sandboxing
1022
+ const ctx = runtime.newContext({ intrinsics: { Eval: false } });
1023
+ const arena = new Arena(ctx, { isMarshalable: true });
1024
+
1025
+ // This test demonstrates that intrinsics are configured at context creation
1026
+ // The actual restrictions would be enforced by quickjs-emscripten
1027
+ expect(arena).toBeDefined();
1028
+ expect(arena.context).toBeDefined();
1029
+
1030
+ arena.dispose();
1031
+ ctx.dispose();
1032
+ runtime.dispose();
1033
+ });
1034
+ });
package/src/index.ts CHANGED
@@ -4,6 +4,7 @@ import type {
4
4
  QuickJSContext,
5
5
  SuccessOrFail,
6
6
  VmCallResult,
7
+ Intrinsics,
7
8
  } from "quickjs-emscripten";
8
9
 
9
10
  import { wrapContext, QuickJSContextEx } from "./contextex";
@@ -12,7 +13,7 @@ import marshal from "./marshal";
12
13
  import unmarshal from "./unmarshal";
13
14
  import { complexity, isES2015Class, isObject, walkObject } from "./util";
14
15
  import VMMap from "./vmmap";
15
- import { call, eq, isHandleObject, json, consumeAll, mayConsume, handleFrom } from "./vmutil";
16
+ import { call, isHandleObject, json, consumeAll, mayConsume, handleFrom } from "./vmutil";
16
17
  import { wrap, wrapHandle, unwrap, unwrapHandle, Wrapped } from "./wrapper";
17
18
 
18
19
  export {
@@ -25,12 +26,13 @@ export {
25
26
  isObject,
26
27
  walkObject,
27
28
  call,
28
- eq,
29
29
  isHandleObject,
30
30
  json,
31
31
  consumeAll,
32
32
  };
33
33
 
34
+ export type { Intrinsics };
35
+
34
36
  export type Options = {
35
37
  /** A callback that returns a boolean value that determines whether an object is marshalled or not. If false, no marshaling will be done and undefined will be passed to the QuickJS VM, otherwise marshaling will be done. By default, all objects will be marshalled. */
36
38
  isMarshalable?: boolean | "json" | ((target: any) => boolean | "json");
@@ -104,24 +106,40 @@ export class Arena {
104
106
  }
105
107
 
106
108
  /**
107
- * Evaluate ES module code in the VM. The module can import/export, but the return value
108
- * depends on whether the module exports are accessible (implementation varies by quickjs-emscripten version).
109
- * Use this for side effects or when you don't need access to exports.
109
+ * Evaluate ES module code in the VM and get the module's exports.
110
+ *
111
+ * Requires quickjs-emscripten >= 0.29.0 for export access.
110
112
  *
111
113
  * @param code - The ES module code to evaluate
112
114
  * @param filename - Optional filename for debugging purposes (default: "module.js")
113
- * @returns Undefined in most cases (module side effects are applied)
115
+ * @returns The module's exports object, or a Promise resolving to exports if using top-level await
114
116
  *
115
117
  * @example
116
118
  * ```js
117
- * // Execute module code with side effects
118
- * arena.expose({ data: { count: 0 } });
119
- * arena.evalModule('import { data } from "globals"; data.count = 42;'); // undefined, but data.count is now 42
119
+ * // Simple module with exports
120
+ * const exports = arena.evalModule(`
121
+ * export const value = 42;
122
+ * export function greet(name) {
123
+ * return "Hello, " + name;
124
+ * }
125
+ * `);
126
+ * console.log(exports.value); // 42
127
+ * console.log(exports.greet("World")); // "Hello, World"
128
+ *
129
+ * // Module with default export
130
+ * const mod = arena.evalModule('export default function(x) { return x * 2; }');
131
+ * console.log(mod.default(21)); // 42
132
+ *
133
+ * // Module with top-level await
134
+ * const promise = arena.evalModule('export const data = await Promise.resolve(123);');
135
+ * arena.executePendingJobs();
136
+ * const exports = await promise;
137
+ * console.log(exports.data); // 123
120
138
  * ```
121
139
  */
122
- evalModule(code: string, filename = "module.js"): void {
140
+ evalModule<T = any>(code: string, filename = "module.js"): T | Promise<T> {
123
141
  const handle = this.context.evalCode(code, filename, { type: "module" });
124
- this._unwrapResultAndUnmarshal(handle);
142
+ return this._unwrapResultAndUnmarshal(handle);
125
143
  }
126
144
 
127
145
  /**
@@ -290,7 +308,7 @@ export class Arena {
290
308
  typeof handleOrCode === "string"
291
309
  ? this._unwrapResult(this.context.evalCode(handleOrCode))
292
310
  : handleOrCode;
293
- if (eq(this.context, handle, this.context.undefined)) return;
311
+ if (this.context.sameValue(handle, this.context.undefined)) return;
294
312
  if (typeof handleOrCode === "string") {
295
313
  this._registeredMapDispose.add(target);
296
314
  }
@@ -1,7 +1,7 @@
1
1
  import { getQuickJS, QuickJSHandle } from "quickjs-emscripten";
2
2
  import { expect, test, vi } from "vitest";
3
3
 
4
- import { json, eq, call } from "../vmutil";
4
+ import { json, call } from "../vmutil";
5
5
 
6
6
  import marshalFunction from "./function";
7
7
 
@@ -9,7 +9,7 @@ test("normal func", async () => {
9
9
  const ctx = (await getQuickJS()).newContext();
10
10
 
11
11
  const marshal = vi.fn(v => json(ctx, v));
12
- const unmarshal = vi.fn(v => (eq(ctx, v, ctx.global) ? undefined : ctx.dump(v)));
12
+ const unmarshal = vi.fn(v => (ctx.sameValue(v, ctx.global) ? undefined : ctx.dump(v)));
13
13
  const preMarshal = vi.fn((_, a) => a);
14
14
  const innerfn = vi.fn((..._args: any[]) => "hoge");
15
15
  const fn = (...args: any[]) => innerfn(...args);
@@ -1,7 +1,6 @@
1
1
  import { getQuickJS } from "quickjs-emscripten";
2
2
  import { expect, test } from "vitest";
3
3
 
4
- import { eq } from "../vmutil";
5
4
 
6
5
  import marshalPrimitive from "./primitive";
7
6
 
@@ -12,12 +11,11 @@ test("works", async () => {
12
11
  expect(marshalPrimitive(ctx, null)).toBe(ctx.null);
13
12
  expect(marshalPrimitive(ctx, false)).toBe(ctx.false);
14
13
  expect(marshalPrimitive(ctx, true)).toBe(ctx.true);
15
- expect(eq(ctx, marshalPrimitive(ctx, 1) ?? ctx.undefined, ctx.newNumber(1))).toBe(true);
16
- expect(eq(ctx, marshalPrimitive(ctx, -100) ?? ctx.undefined, ctx.newNumber(-100))).toBe(true);
17
- expect(eq(ctx, marshalPrimitive(ctx, "hoge") ?? ctx.undefined, ctx.newString("hoge"))).toBe(true);
14
+ expect(ctx.sameValue(marshalPrimitive(ctx, 1) ?? ctx.undefined, ctx.newNumber(1))).toBe(true);
15
+ expect(ctx.sameValue(marshalPrimitive(ctx, -100) ?? ctx.undefined, ctx.newNumber(-100))).toBe(true);
16
+ expect(ctx.sameValue(marshalPrimitive(ctx, "hoge") ?? ctx.undefined, ctx.newString("hoge"))).toBe(true);
18
17
  // expect(
19
- // eq(
20
- // ctx,
18
+ // ctx.sameValue(
21
19
  // marshalPrimitive(ctx, BigInt(1)) ?? ctx.undefined,
22
20
  // ctx.unwrapResult(ctx.evalCode("BigInt(1)"))
23
21
  // )
@@ -5,7 +5,6 @@ import {
5
5
  fn,
6
6
  call,
7
7
  consumeAll,
8
- eq,
9
8
  instanceOf,
10
9
  isHandleObject,
11
10
  json,
@@ -57,8 +56,8 @@ test("eq", async () => {
57
56
  const math2 = ctx.unwrapResult(ctx.evalCode("Math"));
58
57
  const obj = ctx.newObject();
59
58
  expect(math1 === math2).toBe(false);
60
- expect(eq(ctx, math1, math2)).toBe(true);
61
- expect(eq(ctx, math1, obj)).toBe(false);
59
+ expect(ctx.sameValue(math1, math2)).toBe(true);
60
+ expect(ctx.sameValue(math1, obj)).toBe(false);
62
61
 
63
62
  math1.dispose();
64
63
  math2.dispose();
package/src/vmutil.ts CHANGED
@@ -10,10 +10,12 @@ export function fn(
10
10
  code: string,
11
11
  ): ((thisArg: QuickJSHandle | undefined, ...args: QuickJSHandle[]) => QuickJSHandle) & Disposable {
12
12
  const handle = ctx.unwrapResult(ctx.evalCode(code));
13
- const f = (thisArg: QuickJSHandle | undefined, ...args: QuickJSHandle[]): any => {
13
+ const f: any = (thisArg: QuickJSHandle | undefined, ...args: QuickJSHandle[]): any => {
14
14
  return ctx.unwrapResult(ctx.callFunction(handle, thisArg ?? ctx.undefined, ...args));
15
15
  };
16
- f.dispose = () => handle.dispose();
16
+ const disposeFn = () => handle.dispose();
17
+ f.dispose = disposeFn;
18
+ f[Symbol.dispose] = disposeFn;
17
19
  f.alive = true;
18
20
  Object.defineProperty(f, "alive", {
19
21
  get: () => handle.alive,
@@ -35,10 +37,6 @@ export function call(
35
37
  }
36
38
  }
37
39
 
38
- export function eq(ctx: QuickJSContext, a: QuickJSHandle, b: QuickJSHandle): boolean {
39
- return ctx.dump(call(ctx, "Object.is", undefined, a, b));
40
- }
41
-
42
40
  export function instanceOf(ctx: QuickJSContext, a: QuickJSHandle, b: QuickJSHandle): boolean {
43
41
  return ctx.dump(call(ctx, "(a, b) => a instanceof b", undefined, a, b));
44
42
  }
@@ -1,7 +1,7 @@
1
1
  import { QuickJSHandle, getQuickJS } from "quickjs-emscripten";
2
2
  import { expect, test, vi } from "vitest";
3
3
 
4
- import { call, eq, json } from "./vmutil";
4
+ import { call, json } from "./vmutil";
5
5
  import {
6
6
  wrap,
7
7
  unwrap,
@@ -183,7 +183,7 @@ test("wrapHandle, unwrapHandle, isHandleWrapped", async () => {
183
183
  const [handle2, unwrapped2] = unwrapHandle(ctx, wrapped, proxyKeySymbolHandle);
184
184
  expect(unwrapped2).toBe(true);
185
185
  handle2.consume(h => {
186
- expect(eq(ctx, handle, h)).toBe(true);
186
+ expect(ctx.sameValue(handle, h)).toBe(true);
187
187
  });
188
188
 
189
189
  const [wrapped2] = wrapHandle(
@@ -214,7 +214,7 @@ test("wrapHandle without sync", async () => {
214
214
  const proxyKeySymbol = Symbol();
215
215
  const proxyKeySymbolHandle = ctx.unwrapResult(ctx.evalCode(`Symbol()`));
216
216
  const unmarshal = vi.fn((h: QuickJSHandle) =>
217
- wrapped && eq(ctx, h, wrapped) ? target : ctx.dump(h),
217
+ wrapped && ctx.sameValue(h, wrapped) ? target : ctx.dump(h),
218
218
  );
219
219
  const syncMode = vi.fn();
220
220
 
@@ -253,7 +253,7 @@ test("wrapHandle with both sync", async () => {
253
253
  const proxyKeySymbol = Symbol();
254
254
  const proxyKeySymbolHandle = ctx.unwrapResult(ctx.evalCode(`Symbol()`));
255
255
  const unmarshal = vi.fn((h: QuickJSHandle) => {
256
- return wrapped && eq(ctx, h, wrapped) ? target : ctx.dump(h);
256
+ return wrapped && ctx.sameValue(h, wrapped) ? target : ctx.dump(h);
257
257
  });
258
258
  const syncMode = vi.fn((): SyncMode => "both");
259
259
 
@@ -303,7 +303,7 @@ test("wrapHandle with host sync", async () => {
303
303
  const proxyKeySymbol = Symbol();
304
304
  const proxyKeySymbolHandle = ctx.unwrapResult(ctx.evalCode(`Symbol()`));
305
305
  const unmarshal = vi.fn((handle: QuickJSHandle) =>
306
- wrapped && eq(ctx, handle, wrapped) ? target : ctx.dump(handle),
306
+ wrapped && ctx.sameValue(handle, wrapped) ? target : ctx.dump(handle),
307
307
  );
308
308
  const syncMode = vi.fn((): SyncMode => "host");
309
309
 
@@ -352,7 +352,7 @@ test("wrap and wrapHandle", async () => {
352
352
  false,
353
353
  ]);
354
354
  const unmarshal = vi.fn((handle: QuickJSHandle) =>
355
- wrappedHandle && eq(ctx, handle, wrappedHandle) ? wrapped : ctx.dump(handle),
355
+ wrappedHandle && ctx.sameValue(handle, wrappedHandle) ? wrapped : ctx.dump(handle),
356
356
  );
357
357
  const syncMode = vi.fn((): SyncMode => "both");
358
358