quickjs-emscripten-sync 1.3.0 → 1.4.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 (44) hide show
  1. package/README.md +36 -34
  2. package/dist/index.d.ts +17 -15
  3. package/dist/quickjs-emscripten-sync.es.js +146 -135
  4. package/dist/quickjs-emscripten-sync.umd.js +6 -6
  5. package/package.json +3 -3
  6. package/src/index.test.ts +146 -60
  7. package/src/index.ts +40 -26
  8. package/src/marshal/function.test.ts +61 -53
  9. package/src/marshal/function.ts +7 -6
  10. package/src/marshal/index.test.ts +73 -65
  11. package/src/marshal/index.ts +12 -11
  12. package/src/marshal/json.test.ts +30 -30
  13. package/src/marshal/json.ts +4 -3
  14. package/src/marshal/object.test.ts +55 -50
  15. package/src/marshal/object.ts +6 -5
  16. package/src/marshal/primitive.test.ts +21 -17
  17. package/src/marshal/primitive.ts +10 -9
  18. package/src/marshal/promise.test.ts +14 -14
  19. package/src/marshal/promise.ts +3 -3
  20. package/src/marshal/properties.test.ts +17 -15
  21. package/src/marshal/properties.ts +10 -9
  22. package/src/marshal/symbol.test.ts +6 -6
  23. package/src/marshal/symbol.ts +5 -4
  24. package/src/unmarshal/function.test.ts +45 -43
  25. package/src/unmarshal/function.ts +9 -8
  26. package/src/unmarshal/index.test.ts +41 -40
  27. package/src/unmarshal/index.ts +9 -8
  28. package/src/unmarshal/object.test.ts +33 -31
  29. package/src/unmarshal/object.ts +12 -11
  30. package/src/unmarshal/primitive.test.ts +18 -15
  31. package/src/unmarshal/primitive.ts +9 -9
  32. package/src/unmarshal/promise.test.ts +11 -11
  33. package/src/unmarshal/promise.ts +9 -11
  34. package/src/unmarshal/properties.test.ts +6 -6
  35. package/src/unmarshal/properties.ts +47 -44
  36. package/src/unmarshal/symbol.test.ts +6 -6
  37. package/src/unmarshal/symbol.ts +4 -4
  38. package/src/util.test.ts +1 -0
  39. package/src/vmmap.test.ts +72 -88
  40. package/src/vmmap.ts +16 -16
  41. package/src/vmutil.test.ts +71 -73
  42. package/src/vmutil.ts +22 -18
  43. package/src/wrapper.test.ts +111 -88
  44. package/src/wrapper.ts +31 -27
@@ -1,5 +1,5 @@
1
1
  class VMMap {
2
- constructor(vm) {
2
+ constructor(ctx) {
3
3
  this._map1 = /* @__PURE__ */ new Map();
4
4
  this._map2 = /* @__PURE__ */ new Map();
5
5
  this._map3 = /* @__PURE__ */ new Map();
@@ -7,8 +7,8 @@ class VMMap {
7
7
  this._counterMap = /* @__PURE__ */ new Map();
8
8
  this._disposables = /* @__PURE__ */ new Set();
9
9
  this._counter = 0;
10
- this.vm = vm;
11
- const result = vm.unwrapResult(vm.evalCode(`() => {
10
+ this.ctx = ctx;
11
+ const result = ctx.unwrapResult(ctx.evalCode(`() => {
12
12
  const mapSym = new Map();
13
13
  let map = new WeakMap();
14
14
  let map2 = new WeakMap();
@@ -32,10 +32,10 @@ class VMMap {
32
32
  }
33
33
  };
34
34
  }`)).consume((fn2) => this._call(fn2, void 0));
35
- this._mapGet = vm.getProp(result, "get");
36
- this._mapSet = vm.getProp(result, "set");
37
- this._mapDelete = vm.getProp(result, "delete");
38
- this._mapClear = vm.getProp(result, "clear");
35
+ this._mapGet = ctx.getProp(result, "get");
36
+ this._mapSet = ctx.getProp(result, "set");
37
+ this._mapDelete = ctx.getProp(result, "delete");
38
+ this._mapClear = ctx.getProp(result, "clear");
39
39
  result.dispose();
40
40
  this._disposables.add(this._mapGet);
41
41
  this._disposables.add(this._mapSet);
@@ -60,8 +60,8 @@ class VMMap {
60
60
  this._map4.set(counter, handle2);
61
61
  }
62
62
  }
63
- this.vm.newNumber(counter).consume((c) => {
64
- this._call(this._mapSet, void 0, handle, c, handle2 != null ? handle2 : this.vm.undefined);
63
+ this.ctx.newNumber(counter).consume((c) => {
64
+ this._call(this._mapSet, void 0, handle, c, handle2 != null ? handle2 : this.ctx.undefined);
65
65
  });
66
66
  return true;
67
67
  }
@@ -92,7 +92,7 @@ class VMMap {
92
92
  if (!handle.alive) {
93
93
  return;
94
94
  }
95
- return this._counterMap.get(this.vm.getNumber(this._call(this._mapGet, void 0, handle)));
95
+ return this._counterMap.get(this.ctx.getNumber(this._call(this._mapGet, void 0, handle)));
96
96
  }
97
97
  has(key) {
98
98
  return !!this.get(key);
@@ -207,7 +207,7 @@ class VMMap {
207
207
  }
208
208
  }
209
209
  _call(fn2, thisArg, ...args) {
210
- return this.vm.unwrapResult(this.vm.callFunction(fn2, typeof thisArg === "undefined" ? this.vm.undefined : thisArg, ...args));
210
+ return this.ctx.unwrapResult(this.ctx.callFunction(fn2, typeof thisArg === "undefined" ? this.ctx.undefined : thisArg, ...args));
211
211
  }
212
212
  }
213
213
  function isES2015Class(cls) {
@@ -264,10 +264,10 @@ function newDeferred() {
264
264
  reject: rej
265
265
  };
266
266
  }
267
- function fn(vm, code) {
268
- const handle = vm.unwrapResult(vm.evalCode(code));
267
+ function fn(ctx, code) {
268
+ const handle = ctx.unwrapResult(ctx.evalCode(code));
269
269
  const f = (thisArg, ...args) => {
270
- return vm.unwrapResult(vm.callFunction(handle, thisArg != null ? thisArg : vm.undefined, ...args));
270
+ return ctx.unwrapResult(ctx.callFunction(handle, thisArg != null ? thisArg : ctx.undefined, ...args));
271
271
  };
272
272
  f.dispose = () => handle.dispose();
273
273
  f.alive = true;
@@ -276,28 +276,28 @@ function fn(vm, code) {
276
276
  });
277
277
  return f;
278
278
  }
279
- function call(vm, code, thisArg, ...args) {
280
- const f = fn(vm, code);
279
+ function call(ctx, code, thisArg, ...args) {
280
+ const f = fn(ctx, code);
281
281
  try {
282
282
  return f(thisArg, ...args);
283
283
  } finally {
284
284
  f.dispose();
285
285
  }
286
286
  }
287
- function eq(vm, a, b) {
288
- return vm.dump(call(vm, "Object.is", void 0, a, b));
287
+ function eq(ctx, a, b) {
288
+ return ctx.dump(call(ctx, "Object.is", void 0, a, b));
289
289
  }
290
- function instanceOf(vm, a, b) {
291
- return vm.dump(call(vm, "(a, b) => a instanceof b", void 0, a, b));
290
+ function instanceOf(ctx, a, b) {
291
+ return ctx.dump(call(ctx, "(a, b) => a instanceof b", void 0, a, b));
292
292
  }
293
- function isHandleObject(vm, a) {
294
- return vm.dump(call(vm, `a => typeof a === "object" && a !== null || typeof a === "function"`, void 0, a));
293
+ function isHandleObject(ctx, h) {
294
+ return ctx.dump(call(ctx, `a => typeof a === "object" && a !== null || typeof a === "function"`, void 0, h));
295
295
  }
296
- function json(vm, target) {
296
+ function json(ctx, target) {
297
297
  const json2 = JSON.stringify(target);
298
298
  if (!json2)
299
- return vm.undefined;
300
- return call(vm, `JSON.parse`, void 0, vm.newString(json2));
299
+ return ctx.undefined;
300
+ return call(ctx, `JSON.parse`, void 0, ctx.newString(json2));
301
301
  }
302
302
  function consumeAll(handles, cb) {
303
303
  try {
@@ -335,109 +335,109 @@ function isQuickJSDeferredPromise(d) {
335
335
  function handleFrom(d) {
336
336
  return isQuickJSDeferredPromise(d) ? d.handle : d;
337
337
  }
338
- function marshalProperties(vm, target, handle, marshal2) {
339
- const descs = vm.newObject();
338
+ function marshalProperties(ctx, target, handle, marshal2) {
339
+ const descs = ctx.newObject();
340
340
  const cb = (key, desc2) => {
341
341
  const keyHandle = marshal2(key);
342
342
  const valueHandle = typeof desc2.value === "undefined" ? void 0 : marshal2(desc2.value);
343
343
  const getHandle = typeof desc2.get === "undefined" ? void 0 : marshal2(desc2.get);
344
344
  const setHandle = typeof desc2.set === "undefined" ? void 0 : marshal2(desc2.set);
345
- vm.newObject().consume((descObj) => {
345
+ ctx.newObject().consume((descObj) => {
346
346
  Object.entries(desc2).forEach(([k, v]) => {
347
- const v2 = k === "value" ? valueHandle : k === "get" ? getHandle : k === "set" ? setHandle : v ? vm.true : vm.false;
347
+ const v2 = k === "value" ? valueHandle : k === "get" ? getHandle : k === "set" ? setHandle : v ? ctx.true : ctx.false;
348
348
  if (v2) {
349
- vm.setProp(descObj, k, v2);
349
+ ctx.setProp(descObj, k, v2);
350
350
  }
351
351
  });
352
- vm.setProp(descs, keyHandle, descObj);
352
+ ctx.setProp(descs, keyHandle, descObj);
353
353
  });
354
354
  };
355
355
  const desc = Object.getOwnPropertyDescriptors(target);
356
356
  Object.entries(desc).forEach(([k, v]) => cb(k, v));
357
357
  Object.getOwnPropertySymbols(desc).forEach((k) => cb(k, desc[k]));
358
- call(vm, `Object.defineProperties`, void 0, handle, descs).dispose();
358
+ call(ctx, `Object.defineProperties`, void 0, handle, descs).dispose();
359
359
  descs.dispose();
360
360
  }
361
- function marshalFunction(vm, target, marshal2, unmarshal2, preMarshal, preApply) {
361
+ function marshalFunction(ctx, target, marshal2, unmarshal2, preMarshal, preApply) {
362
362
  var _a;
363
363
  if (typeof target !== "function")
364
364
  return;
365
- const raw = vm.newFunction(target.name, function(...argHandles) {
365
+ const raw = ctx.newFunction(target.name, function(...argHandles) {
366
366
  const that = unmarshal2(this);
367
367
  const args = argHandles.map((a) => unmarshal2(a));
368
368
  if (isES2015Class(target) && isObject(that)) {
369
369
  const result = new target(...args);
370
370
  Object.entries(result).forEach(([key, value]) => {
371
- vm.setProp(this, key, marshal2(value));
371
+ ctx.setProp(this, key, marshal2(value));
372
372
  });
373
373
  return this;
374
374
  }
375
375
  return marshal2(preApply ? preApply(target, that, args) : target.apply(that, args));
376
- }).consume((handle2) => call(vm, `Cls => {
376
+ }).consume((handle2) => call(ctx, `Cls => {
377
377
  const fn = function(...args) { return Cls.apply(this, args); };
378
378
  fn.name = Cls.name;
379
379
  fn.length = Cls.length;
380
380
  return fn;
381
381
  }`, void 0, handle2));
382
382
  const handle = (_a = preMarshal(target, raw)) != null ? _a : raw;
383
- marshalProperties(vm, target, raw, marshal2);
383
+ marshalProperties(ctx, target, raw, marshal2);
384
384
  return handle;
385
385
  }
386
- function marshalObject(vm, target, marshal2, preMarshal) {
386
+ function marshalObject(ctx, target, marshal2, preMarshal) {
387
387
  var _a;
388
388
  if (typeof target !== "object" || target === null)
389
389
  return;
390
- const raw = Array.isArray(target) ? vm.newArray() : vm.newObject();
390
+ const raw = Array.isArray(target) ? ctx.newArray() : ctx.newObject();
391
391
  const handle = (_a = preMarshal(target, raw)) != null ? _a : raw;
392
392
  const prototype = Object.getPrototypeOf(target);
393
393
  const prototypeHandle = prototype && prototype !== Object.prototype && prototype !== Array.prototype ? marshal2(prototype) : void 0;
394
394
  if (prototypeHandle) {
395
- call(vm, "Object.setPrototypeOf", void 0, handle, prototypeHandle).dispose();
395
+ call(ctx, "Object.setPrototypeOf", void 0, handle, prototypeHandle).dispose();
396
396
  }
397
- marshalProperties(vm, target, raw, marshal2);
397
+ marshalProperties(ctx, target, raw, marshal2);
398
398
  return handle;
399
399
  }
400
- function marshalPrimitive(vm, target) {
400
+ function marshalPrimitive(ctx, target) {
401
401
  switch (typeof target) {
402
402
  case "undefined":
403
- return vm.undefined;
403
+ return ctx.undefined;
404
404
  case "number":
405
- return vm.newNumber(target);
405
+ return ctx.newNumber(target);
406
406
  case "string":
407
- return vm.newString(target);
407
+ return ctx.newString(target);
408
408
  case "boolean":
409
- return target ? vm.true : vm.false;
409
+ return target ? ctx.true : ctx.false;
410
410
  case "object":
411
- return target === null ? vm.null : void 0;
411
+ return target === null ? ctx.null : void 0;
412
412
  }
413
413
  return void 0;
414
414
  }
415
- function marshalSymbol(vm, target, preMarshal) {
415
+ function marshalSymbol(ctx, target, preMarshal) {
416
416
  var _a;
417
417
  if (typeof target !== "symbol")
418
418
  return;
419
- const handle = call(vm, "d => Symbol(d)", void 0, target.description ? vm.newString(target.description) : vm.undefined);
419
+ const handle = call(ctx, "d => Symbol(d)", void 0, target.description ? ctx.newString(target.description) : ctx.undefined);
420
420
  return (_a = preMarshal(target, handle)) != null ? _a : handle;
421
421
  }
422
- function marshalJSON(vm, target, preMarshal) {
422
+ function marshalJSON(ctx, target, preMarshal) {
423
423
  var _a;
424
- const raw = json(vm, target);
424
+ const raw = json(ctx, target);
425
425
  const handle = (_a = preMarshal(target, raw)) != null ? _a : raw;
426
426
  return handle;
427
427
  }
428
- function marshalPromise(vm, target, marshal2, preMarshal) {
428
+ function marshalPromise(ctx, target, marshal2, preMarshal) {
429
429
  var _a;
430
430
  if (!(target instanceof Promise))
431
431
  return;
432
- const promise = vm.newPromise();
432
+ const promise = ctx.newPromise();
433
433
  target.then((d) => promise.resolve(marshal2(d)), (d) => promise.reject(marshal2(d)));
434
434
  return (_a = preMarshal(target, promise)) != null ? _a : promise.handle;
435
435
  }
436
436
  function marshal(target, options) {
437
437
  var _a, _b, _c, _d;
438
- const { vm, unmarshal: unmarshal2, isMarshalable, find, pre } = options;
438
+ const { ctx, unmarshal: unmarshal2, isMarshalable, find, pre } = options;
439
439
  {
440
- const primitive = marshalPrimitive(vm, target);
440
+ const primitive = marshalPrimitive(ctx, target);
441
441
  if (primitive) {
442
442
  return primitive;
443
443
  }
@@ -449,17 +449,17 @@ function marshal(target, options) {
449
449
  }
450
450
  const marshalable = isMarshalable == null ? void 0 : isMarshalable(target);
451
451
  if (marshalable === false) {
452
- return vm.undefined;
452
+ return ctx.undefined;
453
453
  }
454
454
  const pre2 = (target2, handle) => pre(target2, handle, marshalable);
455
455
  if (marshalable === "json") {
456
- return marshalJSON(vm, target, pre2);
456
+ return marshalJSON(ctx, target, pre2);
457
457
  }
458
458
  const marshal2 = (t) => marshal(t, options);
459
- return (_d = (_c = (_b = (_a = marshalSymbol(vm, target, pre2)) != null ? _a : marshalPromise(vm, target, marshal2, pre2)) != null ? _b : marshalFunction(vm, target, marshal2, unmarshal2, pre2, options.preApply)) != null ? _c : marshalObject(vm, target, marshal2, pre2)) != null ? _d : vm.undefined;
459
+ return (_d = (_c = (_b = (_a = marshalSymbol(ctx, target, pre2)) != null ? _a : marshalPromise(ctx, target, marshal2, pre2)) != null ? _b : marshalFunction(ctx, target, marshal2, unmarshal2, pre2, options.preApply)) != null ? _c : marshalObject(ctx, target, marshal2, pre2)) != null ? _d : ctx.undefined;
460
460
  }
461
- function unmarshalProperties(vm, handle, target, unmarshal2) {
462
- vm.newFunction("", (key, value) => {
461
+ function unmarshalProperties(ctx, handle, target, unmarshal2) {
462
+ ctx.newFunction("", (key, value) => {
463
463
  const [keyName] = unmarshal2(key);
464
464
  if (typeof keyName !== "string" && typeof keyName !== "number" && typeof keyName !== "symbol")
465
465
  return;
@@ -471,12 +471,12 @@ function unmarshalProperties(vm, handle, target, unmarshal2) {
471
471
  ["enumerable", false],
472
472
  ["writable", false]
473
473
  ].reduce((desc2, [key2, unmarshable]) => {
474
- const h = vm.getProp(value, key2);
475
- const ty = vm.typeof(h);
474
+ const h = ctx.getProp(value, key2);
475
+ const ty = ctx.typeof(h);
476
476
  if (ty === "undefined")
477
477
  return desc2;
478
478
  if (!unmarshable && ty === "boolean") {
479
- desc2[key2] = vm.dump(vm.getProp(value, key2));
479
+ desc2[key2] = ctx.dump(ctx.getProp(value, key2));
480
480
  return desc2;
481
481
  }
482
482
  const [v, alreadyExists] = unmarshal2(h);
@@ -488,25 +488,25 @@ function unmarshalProperties(vm, handle, target, unmarshal2) {
488
488
  }, {});
489
489
  Object.defineProperty(target, keyName, desc);
490
490
  }).consume((fn2) => {
491
- call(vm, `(o, fn) => {
491
+ call(ctx, `(o, fn) => {
492
492
  const descs = Object.getOwnPropertyDescriptors(o);
493
493
  Object.entries(descs).forEach(([k, v]) => fn(k, v));
494
494
  Object.getOwnPropertySymbols(descs).forEach(k => fn(k, descs[k]));
495
495
  }`, void 0, handle, fn2).dispose();
496
496
  });
497
497
  }
498
- function unmarshalFunction(vm, handle, marshal2, unmarshal2, preUnmarshal) {
498
+ function unmarshalFunction(ctx, handle, marshal2, unmarshal2, preUnmarshal) {
499
499
  var _a;
500
- if (vm.typeof(handle) !== "function")
500
+ if (ctx.typeof(handle) !== "function")
501
501
  return;
502
502
  const raw = function(...args) {
503
503
  return mayConsumeAll([marshal2(this), ...args.map((a) => marshal2(a))], (thisHandle, ...argHandles) => {
504
504
  if (new.target) {
505
- const [instance] = unmarshal2(call(vm, `(Cls, ...args) => new Cls(...args)`, thisHandle, handle, ...argHandles));
505
+ const [instance] = unmarshal2(call(ctx, `(Cls, ...args) => new Cls(...args)`, thisHandle, handle, ...argHandles));
506
506
  Object.defineProperties(this, Object.getOwnPropertyDescriptors(instance));
507
507
  return this;
508
508
  }
509
- const resultHandle = vm.unwrapResult(vm.callFunction(handle, thisHandle, ...argHandles));
509
+ const resultHandle = ctx.unwrapResult(ctx.callFunction(handle, thisHandle, ...argHandles));
510
510
  const [result, alreadyExists] = unmarshal2(resultHandle);
511
511
  if (alreadyExists)
512
512
  resultHandle.dispose();
@@ -514,20 +514,20 @@ function unmarshalFunction(vm, handle, marshal2, unmarshal2, preUnmarshal) {
514
514
  });
515
515
  };
516
516
  const func = (_a = preUnmarshal(raw, handle)) != null ? _a : raw;
517
- unmarshalProperties(vm, handle, raw, unmarshal2);
517
+ unmarshalProperties(ctx, handle, raw, unmarshal2);
518
518
  return func;
519
519
  }
520
- function unmarshalObject(vm, handle, unmarshal2, preUnmarshal) {
520
+ function unmarshalObject(ctx, handle, unmarshal2, preUnmarshal) {
521
521
  var _a;
522
- if (vm.typeof(handle) !== "object" || vm.unwrapResult(vm.evalCode("o => o === null")).consume((n) => vm.dump(vm.unwrapResult(vm.callFunction(n, vm.undefined, handle)))))
522
+ if (ctx.typeof(handle) !== "object" || ctx.unwrapResult(ctx.evalCode("o => o === null")).consume((n) => ctx.dump(ctx.unwrapResult(ctx.callFunction(n, ctx.undefined, handle)))))
523
523
  return;
524
- const raw = call(vm, "Array.isArray", void 0, handle).consume((r) => vm.dump(r)) ? [] : {};
524
+ const raw = call(ctx, "Array.isArray", void 0, handle).consume((r) => ctx.dump(r)) ? [] : {};
525
525
  const obj = (_a = preUnmarshal(raw, handle)) != null ? _a : raw;
526
- const prototype = call(vm, `o => {
526
+ const prototype = call(ctx, `o => {
527
527
  const p = Object.getPrototypeOf(o);
528
528
  return !p || p === Object.prototype || p === Array.prototype ? undefined : p;
529
529
  }`, void 0, handle).consume((prototype2) => {
530
- if (vm.typeof(prototype2) === "undefined")
530
+ if (ctx.typeof(prototype2) === "undefined")
531
531
  return;
532
532
  const [proto] = unmarshal2(prototype2);
533
533
  return proto;
@@ -535,49 +535,49 @@ function unmarshalObject(vm, handle, unmarshal2, preUnmarshal) {
535
535
  if (typeof prototype === "object") {
536
536
  Object.setPrototypeOf(obj, prototype);
537
537
  }
538
- unmarshalProperties(vm, handle, raw, unmarshal2);
538
+ unmarshalProperties(ctx, handle, raw, unmarshal2);
539
539
  return obj;
540
540
  }
541
- function unmarshalPrimitive(vm, handle) {
542
- const ty = vm.typeof(handle);
541
+ function unmarshalPrimitive(ctx, handle) {
542
+ const ty = ctx.typeof(handle);
543
543
  if (ty === "undefined" || ty === "number" || ty === "string" || ty === "boolean") {
544
- return [vm.dump(handle), true];
544
+ return [ctx.dump(handle), true];
545
545
  } else if (ty === "object") {
546
- const isNull = vm.unwrapResult(vm.evalCode("a => a === null")).consume((n) => vm.dump(vm.unwrapResult(vm.callFunction(n, vm.undefined, handle))));
546
+ const isNull = ctx.unwrapResult(ctx.evalCode("a => a === null")).consume((n) => ctx.dump(ctx.unwrapResult(ctx.callFunction(n, ctx.undefined, handle))));
547
547
  if (isNull) {
548
548
  return [null, true];
549
549
  }
550
550
  }
551
551
  return [void 0, false];
552
552
  }
553
- function unmarshalPromise(vm, handle, marshal2, preUnmarshal) {
553
+ function unmarshalPromise(ctx, handle, marshal2, preUnmarshal) {
554
554
  var _a;
555
- if (!isPromiseHandle(handle))
555
+ if (!isPromiseHandle(ctx, handle))
556
556
  return;
557
557
  const deferred = newDeferred();
558
558
  const [resHandle, resShouldBeDisposed] = marshal2(deferred.resolve);
559
559
  const [rejHandle, rejShouldBeDisposed] = marshal2(deferred.reject);
560
- call(vm, "(p, res, rej) => { p.then(res, rej); }", void 0, handle, resHandle, rejHandle);
560
+ call(ctx, "(p, res, rej) => { p.then(res, rej); }", void 0, handle, resHandle, rejHandle);
561
561
  if (resShouldBeDisposed)
562
562
  resHandle.dispose();
563
563
  if (rejShouldBeDisposed)
564
564
  rejHandle.dispose();
565
565
  return (_a = preUnmarshal(deferred.promise, handle)) != null ? _a : deferred.promise;
566
566
  }
567
- function isPromiseHandle(handle) {
567
+ function isPromiseHandle(ctx, handle) {
568
568
  if (!handle.owner)
569
569
  return false;
570
- return handle.owner.unwrapResult(handle.owner.evalCode("Promise")).consume((promise) => {
570
+ return ctx.unwrapResult(ctx.evalCode("Promise")).consume((promise) => {
571
571
  if (!handle.owner)
572
572
  return false;
573
- return instanceOf(handle.owner, handle, promise);
573
+ return instanceOf(ctx, handle, promise);
574
574
  });
575
575
  }
576
- function unmarshalSymbol(vm, handle, preUnmarshal) {
576
+ function unmarshalSymbol(ctx, handle, preUnmarshal) {
577
577
  var _a;
578
- if (vm.typeof(handle) !== "symbol")
578
+ if (ctx.typeof(handle) !== "symbol")
579
579
  return;
580
- const desc = vm.getString(vm.getProp(handle, "description"));
580
+ const desc = ctx.getString(ctx.getProp(handle, "description"));
581
581
  const sym = Symbol(desc);
582
582
  return (_a = preUnmarshal(sym, handle)) != null ? _a : sym;
583
583
  }
@@ -587,9 +587,9 @@ function unmarshal(handle, options) {
587
587
  }
588
588
  function unmarshalInner(handle, options) {
589
589
  var _a, _b, _c;
590
- const { vm, marshal: marshal2, find, pre } = options;
590
+ const { ctx, marshal: marshal2, find, pre } = options;
591
591
  {
592
- const [target, ok] = unmarshalPrimitive(vm, handle);
592
+ const [target, ok] = unmarshalPrimitive(ctx, handle);
593
593
  if (ok)
594
594
  return [target, false];
595
595
  }
@@ -600,11 +600,11 @@ function unmarshalInner(handle, options) {
600
600
  }
601
601
  }
602
602
  const unmarshal2 = (h) => unmarshalInner(h, options);
603
- const result = (_c = (_b = (_a = unmarshalSymbol(vm, handle, pre)) != null ? _a : unmarshalPromise(vm, handle, marshal2, pre)) != null ? _b : unmarshalFunction(vm, handle, marshal2, unmarshal2, pre)) != null ? _c : unmarshalObject(vm, handle, unmarshal2, pre);
603
+ const result = (_c = (_b = (_a = unmarshalSymbol(ctx, handle, pre)) != null ? _a : unmarshalPromise(ctx, handle, marshal2, pre)) != null ? _b : unmarshalFunction(ctx, handle, marshal2, unmarshal2, pre)) != null ? _c : unmarshalObject(ctx, handle, unmarshal2, pre);
604
604
  return [result, false];
605
605
  }
606
- function wrap(vm, target, proxyKeySymbol, proxyKeySymbolHandle, marshal2, syncMode) {
607
- if (!isObject(target))
606
+ function wrap(ctx, target, proxyKeySymbol, proxyKeySymbolHandle, marshal2, syncMode) {
607
+ if (!isObject(target) || target instanceof Promise)
608
608
  return void 0;
609
609
  if (isWrapped(target, proxyKeySymbol))
610
610
  return target;
@@ -616,14 +616,14 @@ function wrap(vm, target, proxyKeySymbol, proxyKeySymbolHandle, marshal2, syncMo
616
616
  var _a;
617
617
  const v = unwrap(value, proxyKeySymbol);
618
618
  const sync = (_a = syncMode == null ? void 0 : syncMode(receiver)) != null ? _a : "host";
619
- if (sync !== "vm" && !Reflect.set(obj, key, v, receiver) || sync === "host" || !vm.alive)
619
+ if (sync !== "vm" && !Reflect.set(obj, key, v, receiver) || sync === "host" || !ctx.alive)
620
620
  return true;
621
621
  mayConsumeAll([marshal2(receiver), marshal2(key), marshal2(v)], (receiverHandle, keyHandle, valueHandle) => {
622
- const [handle2, unwrapped] = unwrapHandle(vm, receiverHandle, proxyKeySymbolHandle);
622
+ const [handle2, unwrapped] = unwrapHandle(ctx, receiverHandle, proxyKeySymbolHandle);
623
623
  if (unwrapped) {
624
- handle2.consume((h) => vm.setProp(h, keyHandle, valueHandle));
624
+ handle2.consume((h) => ctx.setProp(h, keyHandle, valueHandle));
625
625
  } else {
626
- vm.setProp(handle2, keyHandle, valueHandle);
626
+ ctx.setProp(handle2, keyHandle, valueHandle);
627
627
  }
628
628
  });
629
629
  return true;
@@ -632,14 +632,14 @@ function wrap(vm, target, proxyKeySymbol, proxyKeySymbolHandle, marshal2, syncMo
632
632
  var _a;
633
633
  const sync = (_a = syncMode == null ? void 0 : syncMode(rec)) != null ? _a : "host";
634
634
  return mayConsumeAll([marshal2(rec), marshal2(key)], (recHandle, keyHandle) => {
635
- const [handle2, unwrapped] = unwrapHandle(vm, recHandle, proxyKeySymbolHandle);
635
+ const [handle2, unwrapped] = unwrapHandle(ctx, recHandle, proxyKeySymbolHandle);
636
636
  if (sync === "vm" || Reflect.deleteProperty(obj, key)) {
637
- if (sync === "host" || !vm.alive)
637
+ if (sync === "host" || !ctx.alive)
638
638
  return true;
639
639
  if (unwrapped) {
640
- handle2.consume((h) => call(vm, `(a, b) => delete a[b]`, void 0, h, keyHandle));
640
+ handle2.consume((h) => call(ctx, `(a, b) => delete a[b]`, void 0, h, keyHandle));
641
641
  } else {
642
- call(vm, `(a, b) => delete a[b]`, void 0, handle2, keyHandle);
642
+ call(ctx, `(a, b) => delete a[b]`, void 0, handle2, keyHandle);
643
643
  }
644
644
  }
645
645
  return true;
@@ -648,19 +648,19 @@ function wrap(vm, target, proxyKeySymbol, proxyKeySymbolHandle, marshal2, syncMo
648
648
  });
649
649
  return rec;
650
650
  }
651
- function wrapHandle(vm, handle, proxyKeySymbol, proxyKeySymbolHandle, unmarshal2, syncMode) {
652
- if (!isHandleObject(vm, handle))
651
+ function wrapHandle(ctx, handle, proxyKeySymbol, proxyKeySymbolHandle, unmarshal2, syncMode) {
652
+ if (!isHandleObject(ctx, handle))
653
653
  return [void 0, false];
654
- if (isHandleWrapped(vm, handle, proxyKeySymbolHandle))
654
+ if (isHandleWrapped(ctx, handle, proxyKeySymbolHandle))
655
655
  return [handle, false];
656
656
  return consumeAll([
657
- vm.newFunction("getSyncMode", (h) => {
657
+ ctx.newFunction("getSyncMode", (h) => {
658
658
  const res = syncMode == null ? void 0 : syncMode(unmarshal2(h));
659
659
  if (typeof res === "string")
660
- return vm.newString(res);
661
- return vm.undefined;
660
+ return ctx.newString(res);
661
+ return ctx.undefined;
662
662
  }),
663
- vm.newFunction("setter", (h, keyHandle, valueHandle) => {
663
+ ctx.newFunction("setter", (h, keyHandle, valueHandle) => {
664
664
  const target = unmarshal2(h);
665
665
  if (!target)
666
666
  return;
@@ -670,7 +670,7 @@ function wrapHandle(vm, handle, proxyKeySymbol, proxyKeySymbolHandle, unmarshal2
670
670
  const value = unmarshal2(valueHandle);
671
671
  unwrap(target, proxyKeySymbol)[key] = value;
672
672
  }),
673
- vm.newFunction("deleter", (h, keyHandle) => {
673
+ ctx.newFunction("deleter", (h, keyHandle) => {
674
674
  const target = unmarshal2(h);
675
675
  if (!target)
676
676
  return;
@@ -678,7 +678,7 @@ function wrapHandle(vm, handle, proxyKeySymbol, proxyKeySymbolHandle, unmarshal2
678
678
  delete unwrap(target, proxyKeySymbol)[key];
679
679
  })
680
680
  ], (args) => [
681
- call(vm, `(target, sym, getSyncMode, setter, deleter) => {
681
+ call(ctx, `(target, sym, getSyncMode, setter, deleter) => {
682
682
  const rec = new Proxy(target, {
683
683
  get(obj, key, receiver) {
684
684
  return key === sym ? obj : Reflect.get(obj, key, receiver)
@@ -714,16 +714,16 @@ function unwrap(obj, key) {
714
714
  var _a;
715
715
  return isObject(obj) ? (_a = obj[key]) != null ? _a : obj : obj;
716
716
  }
717
- function unwrapHandle(vm, handle, key) {
718
- if (!isHandleWrapped(vm, handle, key))
717
+ function unwrapHandle(ctx, handle, key) {
718
+ if (!isHandleWrapped(ctx, handle, key))
719
719
  return [handle, false];
720
- return [vm.getProp(handle, key), true];
720
+ return [ctx.getProp(handle, key), true];
721
721
  }
722
722
  function isWrapped(obj, key) {
723
723
  return isObject(obj) && !!obj[key];
724
724
  }
725
- function isHandleWrapped(vm, handle, key) {
726
- return !!vm.dump(call(vm, `(a, s) => (typeof a === "object" && a !== null || typeof a === "function") && !!a[s]`, void 0, handle, key));
725
+ function isHandleWrapped(ctx, handle, key) {
726
+ return !!ctx.dump(call(ctx, `(a, s) => (a instanceof Promise) || (typeof a === "object" && a !== null || typeof a === "function") && !!a[s]`, void 0, handle, key));
727
727
  }
728
728
  const defaultRegisteredObjects = [
729
729
  [Symbol, "Symbol"],
@@ -753,7 +753,7 @@ const defaultRegisteredObjects = [
753
753
  ...Object.getOwnPropertyNames(Symbol).filter((k) => typeof Symbol[k] === "symbol").map((k) => [Symbol[k], `Symbol.${k}`])
754
754
  ];
755
755
  class Arena {
756
- constructor(vm, options) {
756
+ constructor(ctx, options) {
757
757
  var _a;
758
758
  this._registeredMapDispose = /* @__PURE__ */ new Set();
759
759
  this._sync = /* @__PURE__ */ new Set();
@@ -794,7 +794,7 @@ class Arena {
794
794
  return [registered, false];
795
795
  }
796
796
  const handle = marshal((_a2 = this._wrap(target)) != null ? _a2 : target, {
797
- vm: this.vm,
797
+ ctx: this.context,
798
798
  unmarshal: this._unmarshal,
799
799
  isMarshalable: this._isMarshalable,
800
800
  find: this._marshalFind,
@@ -818,7 +818,7 @@ class Arena {
818
818
  }
819
819
  const [wrappedHandle] = this._wrapHandle(handle);
820
820
  return unmarshal(wrappedHandle != null ? wrappedHandle : handle, {
821
- vm: this.vm,
821
+ ctx: this.context,
822
822
  marshal: this._marshal,
823
823
  find: this._unmarshalFind,
824
824
  pre: this._preUnmarshal
@@ -830,13 +830,19 @@ class Arena {
830
830
  };
831
831
  this._unwrapIfNotSynced = (target) => {
832
832
  const unwrapped = this._unwrap(target);
833
- return this._sync.has(unwrapped) ? target : unwrapped;
833
+ return unwrapped instanceof Promise || !this._sync.has(unwrapped) ? unwrapped : target;
834
834
  };
835
- this.vm = vm;
835
+ if ((options == null ? void 0 : options.compat) && !("runtime" in ctx)) {
836
+ ctx.runtime = {
837
+ hasPendingJob: () => ctx.hasPendingJob(),
838
+ executePendingJobs: (maxJobsToExecute) => ctx.executePendingJobs(maxJobsToExecute)
839
+ };
840
+ }
841
+ this.context = ctx;
836
842
  this._options = options;
837
- this._symbolHandle = vm.unwrapResult(vm.evalCode(`Symbol()`));
838
- this._map = new VMMap(vm);
839
- this._registeredMap = new VMMap(vm);
843
+ this._symbolHandle = ctx.unwrapResult(ctx.evalCode(`Symbol()`));
844
+ this._map = new VMMap(ctx);
845
+ this._registeredMap = new VMMap(ctx);
840
846
  this.registerAll((_a = options == null ? void 0 : options.registeredObjects) != null ? _a : defaultRegisteredObjects);
841
847
  }
842
848
  dispose() {
@@ -845,11 +851,11 @@ class Arena {
845
851
  this._symbolHandle.dispose();
846
852
  }
847
853
  evalCode(code) {
848
- const handle = this.vm.evalCode(code);
854
+ const handle = this.context.evalCode(code);
849
855
  return this._unwrapResultAndUnmarshal(handle);
850
856
  }
851
857
  executePendingJobs(maxJobsToExecute) {
852
- const result = this.vm.executePendingJobs(maxJobsToExecute);
858
+ const result = this.context.runtime.executePendingJobs(maxJobsToExecute);
853
859
  if ("value" in result) {
854
860
  return result.value;
855
861
  }
@@ -858,7 +864,7 @@ class Arena {
858
864
  expose(obj) {
859
865
  for (const [key, value] of Object.entries(obj)) {
860
866
  mayConsume(this._marshal(value), (handle) => {
861
- this.vm.setProp(this.vm.global, key, handle);
867
+ this.context.setProp(this.context.global, key, handle);
862
868
  });
863
869
  }
864
870
  }
@@ -867,15 +873,16 @@ class Arena {
867
873
  if (typeof wrapped === "undefined")
868
874
  return target;
869
875
  walkObject(wrapped, (v) => {
870
- this._sync.add(this._unwrap(v));
876
+ const u = this._unwrap(v);
877
+ this._sync.add(u);
871
878
  });
872
879
  return wrapped;
873
880
  }
874
881
  register(target, handleOrCode) {
875
882
  if (this._registeredMap.has(target))
876
883
  return;
877
- const handle = typeof handleOrCode === "string" ? this._unwrapResult(this.vm.evalCode(handleOrCode)) : handleOrCode;
878
- if (eq(this.vm, handle, this.vm.undefined))
884
+ const handle = typeof handleOrCode === "string" ? this._unwrapResult(this.context.evalCode(handleOrCode)) : handleOrCode;
885
+ if (eq(this.context, handle, this.context.undefined))
879
886
  return;
880
887
  if (typeof handleOrCode === "string") {
881
888
  this._registeredMapDispose.add(target);
@@ -899,7 +906,8 @@ class Arena {
899
906
  startSync(target) {
900
907
  if (!isObject(target))
901
908
  return;
902
- this._sync.add(this._unwrap(target));
909
+ const u = this._unwrap(target);
910
+ this._sync.add(u);
903
911
  }
904
912
  endSync(target) {
905
913
  this._sync.delete(this._unwrap(target));
@@ -919,10 +927,13 @@ class Arena {
919
927
  if (this._registeredMap.has(t) || this._registeredMap.hasHandle(h)) {
920
928
  return;
921
929
  }
922
- const wrappedT = this._wrap(t);
930
+ let wrappedT = this._wrap(t);
923
931
  const [wrappedH] = this._wrapHandle(h);
924
- if (!wrappedT || !wrappedH)
932
+ const isPromise = t instanceof Promise;
933
+ if (!wrappedH || !wrappedT && !isPromise)
925
934
  return;
935
+ if (isPromise)
936
+ wrappedT = t;
926
937
  const unwrappedT = this._unwrap(t);
927
938
  const [unwrappedH, unwrapped] = this._unwrapHandle(h);
928
939
  const res = map.set(wrappedT, wrappedH, unwrappedT, unwrappedH);
@@ -936,16 +947,16 @@ class Arena {
936
947
  return [wrappedT, wrappedH];
937
948
  }
938
949
  _wrap(target) {
939
- return wrap(this.vm, target, this._symbol, this._symbolHandle, this._marshal, this._syncMode);
950
+ return wrap(this.context, target, this._symbol, this._symbolHandle, this._marshal, this._syncMode);
940
951
  }
941
952
  _unwrap(target) {
942
953
  return unwrap(target, this._symbol);
943
954
  }
944
955
  _wrapHandle(handle) {
945
- return wrapHandle(this.vm, handle, this._symbol, this._symbolHandle, this._unmarshal, this._syncMode);
956
+ return wrapHandle(this.context, handle, this._symbol, this._symbolHandle, this._unmarshal, this._syncMode);
946
957
  }
947
958
  _unwrapHandle(target) {
948
- return unwrapHandle(this.vm, target, this._symbolHandle);
959
+ return unwrapHandle(this.context, target, this._symbolHandle);
949
960
  }
950
961
  }
951
962
  export { Arena, VMMap, call, complexity, consumeAll, defaultRegisteredObjects, eq, isES2015Class, isHandleObject, isObject, json, marshal, unmarshal, walkObject };