quickjs-emscripten-sync 1.2.0 → 1.5.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.
Files changed (76) hide show
  1. package/LICENSE +2 -2
  2. package/README.md +71 -44
  3. package/dist/index.d.ts +116 -23
  4. package/dist/quickjs-emscripten-sync.mjs +842 -0
  5. package/dist/quickjs-emscripten-sync.umd.js +64 -2
  6. package/package.json +22 -53
  7. package/src/default.ts +2 -2
  8. package/src/index.test.ts +223 -59
  9. package/src/index.ts +72 -66
  10. package/src/marshal/custom.test.ts +50 -0
  11. package/src/marshal/custom.ts +36 -0
  12. package/src/marshal/function.test.ts +64 -75
  13. package/src/marshal/function.ts +15 -18
  14. package/src/marshal/index.test.ts +122 -77
  15. package/src/marshal/index.ts +19 -15
  16. package/src/marshal/json.test.ts +37 -41
  17. package/src/marshal/json.ts +5 -7
  18. package/src/marshal/object.test.ts +59 -84
  19. package/src/marshal/object.ts +8 -15
  20. package/src/marshal/primitive.test.ts +18 -21
  21. package/src/marshal/primitive.ts +11 -10
  22. package/src/marshal/promise.test.ts +85 -0
  23. package/src/marshal/promise.ts +19 -0
  24. package/src/marshal/properties.test.ts +24 -27
  25. package/src/marshal/properties.ts +15 -17
  26. package/src/unmarshal/custom.test.ts +50 -0
  27. package/src/unmarshal/custom.ts +31 -0
  28. package/src/unmarshal/function.test.ts +63 -86
  29. package/src/unmarshal/function.ts +13 -22
  30. package/src/unmarshal/index.test.ts +114 -109
  31. package/src/unmarshal/index.ts +15 -14
  32. package/src/unmarshal/object.test.ts +42 -46
  33. package/src/unmarshal/object.ts +16 -20
  34. package/src/unmarshal/primitive.test.ts +17 -15
  35. package/src/unmarshal/primitive.ts +11 -18
  36. package/src/unmarshal/promise.test.ts +44 -0
  37. package/src/unmarshal/promise.ts +31 -0
  38. package/src/unmarshal/properties.test.ts +11 -9
  39. package/src/unmarshal/properties.ts +44 -45
  40. package/src/util.test.ts +14 -2
  41. package/src/util.ts +18 -8
  42. package/src/vmmap.test.ts +88 -82
  43. package/src/vmmap.ts +30 -44
  44. package/src/vmutil.test.ts +100 -75
  45. package/src/vmutil.ts +51 -34
  46. package/src/wrapper.test.ts +129 -167
  47. package/src/wrapper.ts +59 -62
  48. package/dist/default.d.ts +0 -4
  49. package/dist/marshal/function.d.ts +0 -2
  50. package/dist/marshal/index.d.ts +0 -11
  51. package/dist/marshal/json.d.ts +0 -2
  52. package/dist/marshal/object.d.ts +0 -2
  53. package/dist/marshal/primitive.d.ts +0 -2
  54. package/dist/marshal/properties.d.ts +0 -2
  55. package/dist/marshal/symbol.d.ts +0 -2
  56. package/dist/quickjs-emscripten-sync.cjs +0 -2
  57. package/dist/quickjs-emscripten-sync.cjs.map +0 -1
  58. package/dist/quickjs-emscripten-sync.modern.js +0 -2
  59. package/dist/quickjs-emscripten-sync.modern.js.map +0 -1
  60. package/dist/quickjs-emscripten-sync.module.js +0 -2
  61. package/dist/quickjs-emscripten-sync.module.js.map +0 -1
  62. package/dist/quickjs-emscripten-sync.umd.js.map +0 -1
  63. package/dist/unmarshal/function.d.ts +0 -2
  64. package/dist/unmarshal/index.d.ts +0 -9
  65. package/dist/unmarshal/object.d.ts +0 -2
  66. package/dist/unmarshal/primitive.d.ts +0 -2
  67. package/dist/unmarshal/properties.d.ts +0 -2
  68. package/dist/unmarshal/symbol.d.ts +0 -2
  69. package/dist/util.d.ts +0 -7
  70. package/dist/vmmap.d.ts +0 -35
  71. package/dist/vmutil.d.ts +0 -11
  72. package/dist/wrapper.d.ts +0 -11
  73. package/src/marshal/symbol.test.ts +0 -22
  74. package/src/marshal/symbol.ts +0 -20
  75. package/src/unmarshal/symbol.test.ts +0 -23
  76. package/src/unmarshal/symbol.ts +0 -12
@@ -0,0 +1,842 @@
1
+ const F = [
2
+ [Symbol, "Symbol"],
3
+ [Symbol.prototype, "Symbol.prototype"],
4
+ [Object, "Object"],
5
+ [Object.prototype, "Object.prototype"],
6
+ [Function, "Function"],
7
+ [Function.prototype, "Function.prototype"],
8
+ [Boolean, "Boolean"],
9
+ [Boolean.prototype, "Boolean.prototype"],
10
+ [Array, "Array"],
11
+ [Array.prototype, "Array.prototype"],
12
+ [Error, "Error"],
13
+ [Error.prototype, "Error.prototype"],
14
+ [EvalError, "EvalError"],
15
+ [EvalError.prototype, "EvalError.prototype"],
16
+ [RangeError, "RangeError"],
17
+ [RangeError.prototype, "RangeError.prototype"],
18
+ [ReferenceError, "ReferenceError"],
19
+ [ReferenceError.prototype, "ReferenceError.prototype"],
20
+ [SyntaxError, "SyntaxError"],
21
+ [SyntaxError.prototype, "SyntaxError.prototype"],
22
+ [TypeError, "TypeError"],
23
+ [TypeError.prototype, "TypeError.prototype"],
24
+ [URIError, "URIError"],
25
+ [URIError.prototype, "URIError.prototype"],
26
+ ...Object.getOwnPropertyNames(Symbol).filter((t) => typeof Symbol[t] == "symbol").map((t) => [Symbol[t], `Symbol.${t}`])
27
+ ];
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);
36
+ try {
37
+ return n(r, ...o);
38
+ } finally {
39
+ n.dispose();
40
+ }
41
+ }
42
+ function N(t, e, r) {
43
+ return t.dump(d(t, "Object.is", void 0, e, r));
44
+ }
45
+ function B(t, e, r) {
46
+ return t.dump(d(t, "(a, b) => a instanceof b", void 0, e, r));
47
+ }
48
+ function J(t, e) {
49
+ return t.dump(
50
+ d(t, 'a => typeof a === "object" && a !== null || typeof a === "function"', void 0, e)
51
+ );
52
+ }
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;
56
+ }
57
+ function T(t, e) {
58
+ try {
59
+ return e(t);
60
+ } finally {
61
+ for (const r of t)
62
+ r.alive && r.dispose();
63
+ }
64
+ }
65
+ function U([t, e], r) {
66
+ try {
67
+ return r(t);
68
+ } finally {
69
+ e && t.dispose();
70
+ }
71
+ }
72
+ function w(t, e) {
73
+ try {
74
+ return e(...t.map((r) => r[0]));
75
+ } finally {
76
+ for (const [r, o] of t)
77
+ o && r.dispose();
78
+ }
79
+ }
80
+ function W(t) {
81
+ return "handle" in t;
82
+ }
83
+ function z(t) {
84
+ return W(t) ? t.handle : t;
85
+ }
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;
93
+ }
94
+ function G(t, e) {
95
+ return typeof t != "symbol" ? void 0 : d(
96
+ e,
97
+ "d => Symbol(d)",
98
+ void 0,
99
+ t.description ? e.newString(t.description) : e.undefined
100
+ );
101
+ }
102
+ function q(t, e) {
103
+ return t instanceof Date ? d(e, "d => new Date(d)", void 0, e.newNumber(t.getTime())) : void 0;
104
+ }
105
+ const Q = [G, q];
106
+ function V(t) {
107
+ return typeof t == "function" && /^class\s/.test(Function.prototype.toString.call(t));
108
+ }
109
+ function v(t) {
110
+ return typeof t == "function" || typeof t == "object" && t !== null;
111
+ }
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);
118
+ return;
119
+ }
120
+ if (typeof n == "object") {
121
+ const s = Object.getPrototypeOf(n);
122
+ s && s !== Object.prototype && o(s);
123
+ }
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);
126
+ }
127
+ };
128
+ return o(t), r;
129
+ }
130
+ function de(t, e) {
131
+ return S(t, e ? (r, o) => o.size < e : void 0).size;
132
+ }
133
+ function L() {
134
+ let t = () => {
135
+ }, e = () => {
136
+ };
137
+ return {
138
+ promise: new Promise((o, n) => {
139
+ t = o, e = n;
140
+ }),
141
+ resolve: t,
142
+ reject: e
143
+ };
144
+ }
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);
153
+ });
154
+ }, 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();
156
+ }
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));
167
+ }), this;
168
+ }
169
+ return r(s ? s(e, l, f) : e.apply(l, f));
170
+ }).consume(
171
+ (p) => d(
172
+ t,
173
+ `Cls => {
174
+ const fn = function(...args) { return Cls.apply(this, args); };
175
+ fn.name = Cls.name;
176
+ fn.length = Cls.length;
177
+ return fn;
178
+ }`,
179
+ void 0,
180
+ p
181
+ )
182
+ ), u = (a = n(e, i)) != null ? a : i;
183
+ return E(t, e, i, r), u;
184
+ }
185
+ function Y(t, e, r) {
186
+ var s;
187
+ const o = I(t, e);
188
+ return (s = r(e, o)) != null ? s : o;
189
+ }
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;
196
+ }
197
+ function K(t, e) {
198
+ switch (typeof e) {
199
+ case "undefined":
200
+ return t.undefined;
201
+ case "number":
202
+ return t.newNumber(e);
203
+ case "string":
204
+ return t.newString(e);
205
+ case "boolean":
206
+ return e ? t.true : t.false;
207
+ case "object":
208
+ return e === null ? t.null : void 0;
209
+ }
210
+ }
211
+ function x(t, e, r, o) {
212
+ var s;
213
+ if (!(e instanceof Promise))
214
+ return;
215
+ const n = t.newPromise();
216
+ return e.then(
217
+ (i) => n.resolve(r(i)),
218
+ (i) => n.reject(r(i))
219
+ ), (s = o(e, n)) != null ? s : n.handle;
220
+ }
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;
224
+ {
225
+ const y = K(r, t);
226
+ if (y)
227
+ return y;
228
+ }
229
+ {
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);
256
+ }
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);
262
+ }
263
+ const ne = [te, re];
264
+ function k(t, e, r, o) {
265
+ t.newFunction("", (n, s) => {
266
+ const [i] = o(n);
267
+ if (typeof i != "string" && typeof i != "number" && typeof i != "symbol")
268
+ return;
269
+ const u = [
270
+ ["value", !0],
271
+ ["get", !0],
272
+ ["set", !0],
273
+ ["configurable", !1],
274
+ ["enumerable", !1],
275
+ ["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;
284
+ }, {});
285
+ Object.defineProperty(r, i, u);
286
+ }).consume((n) => {
287
+ d(
288
+ t,
289
+ `(o, fn) => {
290
+ const descs = Object.getOwnPropertyDescriptors(o);
291
+ Object.entries(descs).forEach(([k, v]) => fn(k, v));
292
+ Object.getOwnPropertySymbols(descs).forEach(k => fn(k, descs[k]));
293
+ }`,
294
+ void 0,
295
+ e,
296
+ n
297
+ ).dispose();
298
+ });
299
+ }
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))],
307
+ (p, ...l) => {
308
+ if (new.target) {
309
+ const [m] = o(
310
+ d(t, "(Cls, ...args) => new Cls(...args)", p, e, ...l)
311
+ );
312
+ return Object.defineProperties(this, Object.getOwnPropertyDescriptors(m)), this;
313
+ }
314
+ const f = t.unwrapResult(t.callFunction(e, p, ...l)), [c, h] = o(f);
315
+ return h && f.dispose(), c;
316
+ }
317
+ );
318
+ }, i = (u = n(s, e)) != null ? u : s;
319
+ return k(t, e, s, o), i;
320
+ }
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)))))
324
+ 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,
327
+ `o => {
328
+ const p = Object.getPrototypeOf(o);
329
+ return !p || p === Object.prototype || p === Array.prototype ? undefined : p;
330
+ }`,
331
+ void 0,
332
+ e
333
+ ).consume((a) => {
334
+ if (t.typeof(a) === "undefined")
335
+ return;
336
+ const [p] = r(a);
337
+ return p;
338
+ });
339
+ return typeof i == "object" && Object.setPrototypeOf(s, i), k(t, e, n, r), s;
340
+ }
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];
344
+ }
345
+ function ue(t, e, r, o) {
346
+ 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;
351
+ }
352
+ function ae(t, e) {
353
+ return e.owner ? t.unwrapResult(t.evalCode("Promise")).consume((r) => e.owner ? B(t, e, r) : !1) : !1;
354
+ }
355
+ function pe(t, e) {
356
+ const [r] = M(t, e);
357
+ return r;
358
+ }
359
+ function M(t, e) {
360
+ var a, p, l, f;
361
+ const { ctx: r, marshal: o, find: n, pre: s } = e;
362
+ {
363
+ const [c, h] = ie(r, t);
364
+ if (h)
365
+ return [c, !1];
366
+ }
367
+ {
368
+ const c = n(t);
369
+ if (c)
370
+ return [c, !0];
371
+ }
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];
374
+ }
375
+ class O {
376
+ 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(
379
+ e.evalCode(`() => {
380
+ const mapSym = new Map();
381
+ let map = new WeakMap();
382
+ let map2 = new WeakMap();
383
+ const isObj = o => typeof o === "object" && o !== null || typeof o === "function";
384
+ return {
385
+ get: key => mapSym.get(key) ?? map.get(key) ?? map2.get(key) ?? -1,
386
+ set: (key, value, key2) => {
387
+ if (typeof key === "symbol") mapSym.set(key, value);
388
+ if (isObj(key)) map.set(key, value);
389
+ if (isObj(key2)) map2.set(key2, value);
390
+ },
391
+ delete: (key, key2) => {
392
+ mapSym.delete(key);
393
+ map.delete(key);
394
+ map2.delete(key2);
395
+ },
396
+ clear: () => {
397
+ mapSym.clear();
398
+ map = new WeakMap();
399
+ map2 = new WeakMap();
400
+ }
401
+ };
402
+ }`)
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;
413
+ 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);
416
+ }), !0;
417
+ }
418
+ 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]);
422
+ }
423
+ 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) {
428
+ this.delete(e);
429
+ return;
430
+ }
431
+ return o;
432
+ }
433
+ }
434
+ getByHandle(e) {
435
+ if (!!e.alive)
436
+ return this._counterMap.get(this.ctx.getNumber(this._call(this._mapGet, void 0, e)));
437
+ }
438
+ has(e) {
439
+ return !!this.get(e);
440
+ }
441
+ hasHandle(e) {
442
+ return typeof this.getByHandle(e) != "undefined";
443
+ }
444
+ keys() {
445
+ return this._map1.keys();
446
+ }
447
+ delete(e, r) {
448
+ 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);
453
+ this._call(
454
+ this._mapDelete,
455
+ 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);
461
+ break;
462
+ }
463
+ for (const [u, a] of this._map2)
464
+ if (a === o) {
465
+ this._map2.delete(u);
466
+ break;
467
+ }
468
+ for (const [u, a] of this._counterMap)
469
+ if (a === e) {
470
+ this._counterMap.delete(u);
471
+ break;
472
+ }
473
+ r && (n != null && n.alive && n.dispose(), s != null && s.alive && s.dispose());
474
+ }
475
+ deleteByHandle(e, r) {
476
+ const o = this.getByHandle(e);
477
+ typeof o != "undefined" && this.delete(o, r);
478
+ }
479
+ clear() {
480
+ 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);
481
+ }
482
+ dispose() {
483
+ for (const e of this._disposables.values())
484
+ e.alive && e.dispose();
485
+ for (const e of this._map3.values())
486
+ e.alive && e.dispose();
487
+ for (const e of this._map4.values())
488
+ e.alive && e.dispose();
489
+ this._disposables.clear(), this.clear();
490
+ }
491
+ get size() {
492
+ return this._map1.size;
493
+ }
494
+ [Symbol.iterator]() {
495
+ const e = this._map1.keys();
496
+ return {
497
+ next: () => {
498
+ 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 };
510
+ }
511
+ }
512
+ };
513
+ }
514
+ _get2(e) {
515
+ for (const [r, o] of this._map2)
516
+ if (o === e)
517
+ return r;
518
+ }
519
+ _call(e, r, ...o) {
520
+ return this.ctx.unwrapResult(
521
+ this.ctx.callFunction(
522
+ e,
523
+ typeof r == "undefined" ? this.ctx.undefined : r,
524
+ ...o
525
+ )
526
+ );
527
+ }
528
+ }
529
+ function fe(t, e, r, o, n, s, i) {
530
+ if (!v(e) || e instanceof Promise || e instanceof Date || i && !i(e))
531
+ 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);
537
+ },
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);
546
+ }
547
+ ), !0;
548
+ },
549
+ deleteProperty(a, p) {
550
+ 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);
558
+ }
559
+ return !0;
560
+ });
561
+ }
562
+ });
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) => {
594
+ const rec = new Proxy(target, {
595
+ get(obj, key, receiver) {
596
+ return key === sym ? obj : Reflect.get(obj, key, receiver)
597
+ },
598
+ set(obj, key, value, receiver) {
599
+ const v = typeof value === "object" && value !== null || typeof value === "function"
600
+ ? value[sym] ?? value
601
+ : value;
602
+ const sync = getSyncMode(receiver) ?? "vm";
603
+ if (sync === "host" || Reflect.set(obj, key, v, receiver)) {
604
+ if (sync !== "vm") {
605
+ setter(receiver, key, v);
606
+ }
607
+ }
608
+ return true;
609
+ },
610
+ deleteProperty(obj, key) {
611
+ const sync = getSyncMode(rec) ?? "vm";
612
+ if (sync === "host" || Reflect.deleteProperty(obj, key)) {
613
+ if (sync !== "vm") {
614
+ deleter(rec, key);
615
+ }
616
+ }
617
+ return true;
618
+ },
619
+ });
620
+ return rec;
621
+ }`,
622
+ void 0,
623
+ e,
624
+ o,
625
+ ...u
626
+ ),
627
+ !0
628
+ ]
629
+ );
630
+ }
631
+ function b(t, e) {
632
+ var r;
633
+ return v(t) && (r = t[e]) != null ? r : t;
634
+ }
635
+ function g(t, e, r) {
636
+ return H(t, e, r) ? [t.getProp(e, r), !0] : [e, !1];
637
+ }
638
+ function ce(t, e) {
639
+ return v(t) && !!t[e];
640
+ }
641
+ function H(t, e, r) {
642
+ return !!t.dump(
643
+ d(
644
+ t,
645
+ '(a, s) => (a instanceof Promise) || (a instanceof Date) || (typeof a === "object" && a !== null || typeof a === "function") && !!a[s]',
646
+ void 0,
647
+ e,
648
+ r
649
+ )
650
+ );
651
+ }
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);
670
+ try {
671
+ return n.apply(s, i);
672
+ } finally {
673
+ u && this._temporalSync.delete(u);
674
+ }
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, {
681
+ ctx: this.context,
682
+ unmarshal: this._unmarshal,
683
+ isMarshalable: this._isMarshalable,
684
+ find: this._marshalFind,
685
+ pre: this._marshalPre,
686
+ preApply: this._marshalPreApply,
687
+ custom: (a = this._options) == null ? void 0 : a.customMarshaller
688
+ });
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) => {
694
+ 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, {
703
+ ctx: this.context,
704
+ marshal: this._marshal,
705
+ find: this._unmarshalFind,
706
+ pre: this._preUnmarshal,
707
+ custom: (u = this._options) == null ? void 0 : u.customUnmarshaller
708
+ });
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 = {
716
+ 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);
719
+ }
720
+ dispose() {
721
+ this._map.dispose(), this._registeredMap.dispose(), this._symbolHandle.dispose();
722
+ }
723
+ evalCode(e) {
724
+ const r = this.context.evalCode(e);
725
+ return this._unwrapResultAndUnmarshal(r);
726
+ }
727
+ 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));
732
+ }
733
+ 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);
737
+ });
738
+ }
739
+ 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));
751
+ }
752
+ registerAll(e) {
753
+ for (const [r, o] of e)
754
+ this.register(r, o);
755
+ }
756
+ unregister(e, r) {
757
+ this._registeredMap.delete(e, this._registeredMapDispose.has(e) || r), this._registeredMapDispose.delete(e);
758
+ }
759
+ unregisterAll(e, r) {
760
+ for (const o of e)
761
+ this.unregister(o, r);
762
+ }
763
+ startSync(e) {
764
+ if (!v(e))
765
+ return;
766
+ const r = this._unwrap(e);
767
+ this._sync.add(r);
768
+ }
769
+ endSync(e) {
770
+ this._sync.delete(this._unwrap(e));
771
+ }
772
+ _unwrapResult(e) {
773
+ if ("value" in e)
774
+ return e.value;
775
+ throw this._unwrapIfNotSynced(e.error.consume(this._unmarshal));
776
+ }
777
+ _unwrapResultAndUnmarshal(e) {
778
+ if (!!e)
779
+ return this._unwrapIfNotSynced(this._unwrapResult(e).consume(this._unmarshal));
780
+ }
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)
787
+ 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];
795
+ }
796
+ _wrap(e) {
797
+ var r;
798
+ return fe(
799
+ this.context,
800
+ e,
801
+ this._symbol,
802
+ this._symbolHandle,
803
+ this._marshal,
804
+ this._syncMode,
805
+ (r = this._options) == null ? void 0 : r.isWrappable
806
+ );
807
+ }
808
+ _unwrap(e) {
809
+ return b(e, this._symbol);
810
+ }
811
+ _wrapHandle(e) {
812
+ var r;
813
+ return le(
814
+ this.context,
815
+ e,
816
+ this._symbol,
817
+ this._symbolHandle,
818
+ this._unmarshal,
819
+ this._syncMode,
820
+ (r = this._options) == null ? void 0 : r.isHandleWrappable
821
+ );
822
+ }
823
+ _unwrapHandle(e) {
824
+ return g(this.context, e, this._symbolHandle);
825
+ }
826
+ }
827
+ export {
828
+ he as Arena,
829
+ 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,
837
+ v as isObject,
838
+ I as json,
839
+ R as marshal,
840
+ pe as unmarshal,
841
+ S as walkObject
842
+ };