@emnapi/runtime 2.0.0-alpha.2 → 2.0.0-alpha.4

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/emnapi.d.ts CHANGED
@@ -348,6 +348,7 @@ export declare class CountIdAllocator extends Disposable_2 implements IdAllocato
348
348
 
349
349
  export declare class CountIdReuseAllocator extends CountIdAllocator implements IdAllocator {
350
350
  private _freeList;
351
+ private _freeListHead;
351
352
  aquire(): number;
352
353
  release(id: number): void;
353
354
  dispose(): void;
@@ -386,7 +387,7 @@ export declare abstract class Env extends Disposable_2 {
386
387
  refs: number;
387
388
  reflist: RefTracker;
388
389
  finalizing_reflist: RefTracker;
389
- pendingFinalizers: RefTracker[];
390
+ pendingFinalizers: Set<RefTracker>;
390
391
  moduleApiVersion: number;
391
392
  filename: string;
392
393
  nodeBinding?: any;
@@ -646,9 +647,11 @@ export declare class ObjectTemplate extends Template {
646
647
  Ctor: any;
647
648
  internalFieldCount: number;
648
649
  private _accessors;
650
+ private _instances;
649
651
  constructor(ctx: Isolate, Ctor?: any);
650
652
  setAccessor(name: string | symbol, getterWrap: (property: Ptr, info: Ptr, getter: Ptr) => Ptr, setterWrap: (property: Ptr, value: Ptr, info: Ptr, setter: Ptr) => Ptr, getter: Ptr, setter: Ptr, data: any, attribute: number, getterSideEffectType: number, setterSideEffectType: number): void;
651
653
  setInternalFieldCount(value: number): void;
654
+ private _createAccessorWrapper;
652
655
  applyToInstance(instance: any): void;
653
656
  newInstance(_context: any): any;
654
657
  }
@@ -657,6 +660,7 @@ export declare class Persistent<T> extends Disposable_2 {
657
660
  private _param;
658
661
  private _callback;
659
662
  private _isolate;
663
+ private _slotId;
660
664
  id: number;
661
665
  private static readonly _registry;
662
666
  constructor(isolate: Isolate, ...args: [T] | []);
@@ -677,6 +681,7 @@ export declare class Persistent<T> extends Disposable_2 {
677
681
 
678
682
  export declare class PersistentStore extends ArrayStore<Persistent<any>> {
679
683
  private _recyleList;
684
+ private _recyleListHead;
680
685
  constructor(initialCapacity?: number);
681
686
  markUnused(id: number | bigint): void;
682
687
  recycle(): void;
package/dist/emnapi.js CHANGED
@@ -108,7 +108,7 @@ function detectFeatures(features) {
108
108
  return undefined;
109
109
  })(),
110
110
  setImmediate: typeof setImmediate === 'function'
111
- ? setImmediate
111
+ ? setImmediate.bind(getGlobalThis())
112
112
  : function (callback) {
113
113
  if (typeof callback !== 'function') {
114
114
  throw new TypeError('The "callback" argument must be of type function');
@@ -260,7 +260,7 @@ function getDylinkMetadata(binary) {
260
260
  failIf(offset !== end);
261
261
  return customSection;
262
262
  }
263
- const version = "2.0.0-alpha.2";
263
+ const version = "2.0.0-alpha.4";
264
264
  const NODE_API_SUPPORTED_VERSION_MIN = 1;
265
265
  const NODE_API_SUPPORTED_VERSION_MAX = 10;
266
266
  const NAPI_VERSION_EXPERIMENTAL = 2147483647;
@@ -443,10 +443,16 @@ class CountIdReuseAllocator extends CountIdAllocator {
443
443
  constructor() {
444
444
  super(...arguments);
445
445
  this._freeList = [];
446
+ this._freeListHead = 0;
446
447
  }
447
448
  aquire() {
448
- if (this._freeList.length) {
449
- return this._freeList.shift();
449
+ if (this._freeListHead < this._freeList.length) {
450
+ const id = this._freeList[this._freeListHead++];
451
+ if (this._freeListHead === this._freeList.length) {
452
+ this._freeList.length = 0;
453
+ this._freeListHead = 0;
454
+ }
455
+ return id;
450
456
  }
451
457
  return super.aquire();
452
458
  }
@@ -455,13 +461,17 @@ class CountIdReuseAllocator extends CountIdAllocator {
455
461
  }
456
462
  dispose() {
457
463
  this._freeList.length = 0;
464
+ this._freeListHead = 0;
458
465
  super.dispose();
459
466
  }
460
467
  }
461
468
  class BaseArrayStore extends Disposable {
462
469
  constructor(initialCapacity = 1) {
463
470
  super();
464
- this._values = Array(initialCapacity);
471
+ this._values = [undefined];
472
+ for (let i = 1; i < initialCapacity; i++) {
473
+ this._values.push(undefined);
474
+ }
465
475
  }
466
476
  assign(id, value) {
467
477
  this._values[id] = value;
@@ -487,10 +497,6 @@ class ArrayStore extends BaseArrayStore {
487
497
  }
488
498
  insert(value) {
489
499
  const id = this._allocator.aquire();
490
- while (id >= this._values.length) {
491
- const cap = this._values.length;
492
- this._values.length = cap + (cap >> 1) + 16;
493
- }
494
500
  value.id = id;
495
501
  this._values[id] = value;
496
502
  }
@@ -512,13 +518,18 @@ class PersistentStore extends ArrayStore {
512
518
  constructor(initialCapacity = 4) {
513
519
  super(initialCapacity);
514
520
  this._recyleList = [];
521
+ this._recyleListHead = 0;
515
522
  }
516
523
  markUnused(id) {
517
524
  this._recyleList.push(id);
518
525
  }
519
526
  recycle() {
520
- while (this._recyleList.length > 0) {
521
- const id = this._recyleList.shift();
527
+ while (this._recyleListHead < this._recyleList.length) {
528
+ const id = this._recyleList[this._recyleListHead++];
529
+ if (this._recyleListHead === this._recyleList.length) {
530
+ this._recyleList.length = 0;
531
+ this._recyleListHead = 0;
532
+ }
522
533
  const persistent = this.deref(id);
523
534
  persistent.dispose();
524
535
  }
@@ -542,6 +553,7 @@ class Persistent extends Disposable {
542
553
  this.id = 0;
543
554
  this._isolate = isolate;
544
555
  isolate.insertRef(this);
556
+ this._slotId = 0x80000000 + this.id;
545
557
  this.setSlot(args.length === 0 ? undefined : new StrongRef(args[0]));
546
558
  }
547
559
  dispose() {
@@ -586,7 +598,7 @@ class Persistent extends Disposable {
586
598
  this.setSlot(undefined);
587
599
  }
588
600
  slot() {
589
- return 0x80000000 + this.id;
601
+ return this._slotId;
590
602
  }
591
603
  getSlot() {
592
604
  return this._isolate.getRefSlotValue(this.slot());
@@ -696,7 +708,7 @@ class Env extends Disposable {
696
708
  this.refs = 1;
697
709
  this.reflist = new RefTracker();
698
710
  this.finalizing_reflist = new RefTracker();
699
- this.pendingFinalizers = [];
711
+ this.pendingFinalizers = new Set();
700
712
  this.inGcFinalizer = false;
701
713
  this._bindingMap = new WeakMap();
702
714
  this.ctx = ctx;
@@ -769,15 +781,10 @@ class Env extends Disposable {
769
781
  }
770
782
  }
771
783
  enqueueFinalizer(finalizer) {
772
- if (this.pendingFinalizers.indexOf(finalizer) === -1) {
773
- this.pendingFinalizers.push(finalizer);
774
- }
784
+ this.pendingFinalizers.add(finalizer);
775
785
  }
776
786
  dequeueFinalizer(finalizer) {
777
- const index = this.pendingFinalizers.indexOf(finalizer);
778
- if (index !== -1) {
779
- this.pendingFinalizers.splice(index, 1);
780
- }
787
+ this.pendingFinalizers.delete(finalizer);
781
788
  }
782
789
  deleteMe() {
783
790
  RefTracker.finalizeAll(this.finalizing_reflist);
@@ -902,8 +909,8 @@ class NodeEnv extends Env {
902
909
  }
903
910
  }
904
911
  drainFinalizerQueue() {
905
- while (this.pendingFinalizers.length > 0) {
906
- const refTracker = this.pendingFinalizers.shift();
912
+ for (const refTracker of this.pendingFinalizers) {
913
+ this.pendingFinalizers.delete(refTracker);
907
914
  refTracker.finalize();
908
915
  }
909
916
  }
@@ -1310,9 +1317,6 @@ class HandleScope extends Disposable {
1310
1317
  if (this._escapeCalled)
1311
1318
  return 0;
1312
1319
  this._escapeCalled = true;
1313
- if (handle < this.start || handle >= this.end) {
1314
- return 0;
1315
- }
1316
1320
  const id = this.start;
1317
1321
  this.handleStore.swap(handle, id);
1318
1322
  this.start++;
@@ -1500,10 +1504,11 @@ class ObjectTemplate extends Template {
1500
1504
  super(ctx);
1501
1505
  this.internalFieldCount = 0;
1502
1506
  this._accessors = new Map();
1507
+ this._instances = new WeakSet();
1503
1508
  this.Ctor = Ctor ?? Object;
1504
1509
  }
1505
1510
  setAccessor(name, getterWrap, setterWrap, getter, setter, data, attribute, getterSideEffectType, setterSideEffectType) {
1506
- this._accessors.set(name, {
1511
+ const config = {
1507
1512
  name,
1508
1513
  getterWrap,
1509
1514
  setterWrap,
@@ -1512,62 +1517,72 @@ class ObjectTemplate extends Template {
1512
1517
  data,
1513
1518
  attribute,
1514
1519
  getterSideEffectType,
1515
- setterSideEffectType
1516
- });
1520
+ setterSideEffectType,
1521
+ getterFunction: undefined,
1522
+ setterFunction: undefined
1523
+ };
1524
+ config.getterFunction = getter
1525
+ ? this._createAccessorWrapper('getter', config)
1526
+ : undefined;
1527
+ config.setterFunction = setter
1528
+ ? this._createAccessorWrapper('setter', config)
1529
+ : undefined;
1530
+ this._accessors.set(name, config);
1517
1531
  }
1518
1532
  setInternalFieldCount(value) {
1519
1533
  this.internalFieldCount = value;
1520
1534
  }
1521
- applyToInstance(instance) {
1522
- internalField.set(instance, Array(this.internalFieldCount));
1523
- this._addPropertiesToInstance(instance);
1524
- const createAccessorWrapper = (type, data, cb) => {
1525
- const { ctx } = this;
1526
- function _(value) {
1527
- const scope = ctx.openScope();
1528
- const callbackInfo = scope.callbackInfo;
1529
- let returnValue;
1530
- try {
1531
- callbackInfo.data = data;
1532
- callbackInfo.args = type === 'getter' ? [] : [value];
1533
- callbackInfo.thiz = this;
1534
- callbackInfo.holder = findHolder(this, _) || this;
1535
- callbackInfo.fn = _;
1536
- const ret = cb(value);
1537
- returnValue = ret ? ctx.jsValueFromNapiValue(ret) : undefined;
1538
- }
1539
- catch (err) {
1540
- ctx.throwException(err);
1535
+ _createAccessorWrapper(type, config) {
1536
+ const { ctx } = this;
1537
+ const instances = this._instances;
1538
+ const resolveHolder = (receiver) => {
1539
+ let holder = receiver;
1540
+ while (holder != null && !instances.has(holder)) {
1541
+ holder = Object.getPrototypeOf(holder);
1542
+ }
1543
+ return holder || receiver;
1544
+ };
1545
+ function accessor(value) {
1546
+ const scope = ctx.openScope();
1547
+ const callbackInfo = scope.callbackInfo;
1548
+ let returnValue;
1549
+ try {
1550
+ callbackInfo.data = config.data;
1551
+ callbackInfo.args = type === 'getter' ? [] : [value];
1552
+ callbackInfo.thiz = this;
1553
+ callbackInfo.holder = resolveHolder(this);
1554
+ callbackInfo.fn = accessor;
1555
+ const ret = type === 'getter'
1556
+ ? config.getterWrap(ctx.napiValueFromJsValue(config.name), ctx.getCurrentScope().id, config.getter)
1557
+ : config.setterWrap(ctx.napiValueFromJsValue(config.name), ctx.napiValueFromJsValue(value), ctx.getCurrentScope().id, config.setter);
1558
+ returnValue = ret ? ctx.jsValueFromNapiValue(ret) : undefined;
1559
+ }
1560
+ catch (err) {
1561
+ ctx.throwException(err);
1562
+ }
1563
+ ctx.closeScope(scope);
1564
+ if (ctx.hasPendingException()) {
1565
+ if (TryCatch.top) {
1566
+ TryCatch.top.setError(ctx.getAndClearLastException());
1541
1567
  }
1542
- ctx.closeScope(scope);
1543
- if (ctx.hasPendingException()) {
1544
- if (TryCatch.top) {
1545
- TryCatch.top.setError(ctx.getAndClearLastException());
1546
- }
1547
- else {
1548
- throw ctx.getAndClearLastException();
1549
- }
1568
+ else {
1569
+ throw ctx.getAndClearLastException();
1550
1570
  }
1551
- return returnValue;
1552
1571
  }
1553
- return _;
1554
- };
1572
+ return returnValue;
1573
+ }
1574
+ return accessor;
1575
+ }
1576
+ applyToInstance(instance) {
1577
+ this._instances.add(instance);
1578
+ internalField.set(instance, Array(this.internalFieldCount));
1579
+ this._addPropertiesToInstance(instance);
1555
1580
  this._accessors.forEach((config, name) => {
1556
- const { ctx } = this;
1557
- const { getterWrap, setterWrap, getter, setter, data, attribute } = config;
1558
1581
  Object.defineProperty(instance, name, {
1559
- get: getter
1560
- ? createAccessorWrapper('getter', data, () => {
1561
- return getterWrap(ctx.napiValueFromJsValue(name), ctx.getCurrentScope().id, getter);
1562
- }).bind(instance)
1563
- : undefined,
1564
- set: setter
1565
- ? createAccessorWrapper('setter', data, (value) => {
1566
- return setterWrap(ctx.napiValueFromJsValue(name), ctx.napiValueFromJsValue(value), ctx.getCurrentScope().id, setter);
1567
- }).bind(instance)
1568
- : undefined,
1569
- enumerable: !(attribute & 2),
1570
- configurable: !(attribute & 4)
1582
+ get: config.getterFunction,
1583
+ set: config.setterFunction,
1584
+ enumerable: !(config.attribute & 2),
1585
+ configurable: !(config.attribute & 4)
1571
1586
  });
1572
1587
  });
1573
1588
  }
@@ -1624,7 +1639,7 @@ class FunctionTemplate extends Template {
1624
1639
  return this._cached[0];
1625
1640
  }
1626
1641
  const { ctx, callback, v8FunctionCallback, data, signature, _instanceTemplate } = this;
1627
- function _(...args) {
1642
+ function _() {
1628
1643
  if (signature && signature.receiver) {
1629
1644
  const f = signature.receiver.getFunction();
1630
1645
  if (!(this instanceof f)) {
@@ -1636,7 +1651,7 @@ class FunctionTemplate extends Template {
1636
1651
  let returnValue;
1637
1652
  try {
1638
1653
  callbackInfo.data = data;
1639
- callbackInfo.args = args;
1654
+ callbackInfo.args = arguments;
1640
1655
  callbackInfo.thiz = this;
1641
1656
  callbackInfo.holder = findHolder(this, _) || this;
1642
1657
  callbackInfo.fn = _;
@@ -1954,40 +1969,62 @@ const withScope = (envObject, thiz, args, data, getFunction, wrapper, callback)
1954
1969
  }
1955
1970
  };
1956
1971
  class CleanupHookCallback {
1957
- constructor(envObject, fn, arg, order) {
1972
+ constructor(envObject, fn, arg) {
1958
1973
  this.envObject = envObject;
1959
1974
  this.fn = fn;
1960
1975
  this.arg = arg;
1961
- this.order = order;
1962
1976
  }
1963
1977
  }
1964
1978
  class CleanupQueue {
1965
1979
  constructor() {
1966
1980
  this._cleanupHooks = [];
1967
- this._cleanupHookCounter = 0;
1968
1981
  }
1969
1982
  empty() {
1970
1983
  return this._cleanupHooks.length === 0;
1971
1984
  }
1972
1985
  add(envObject, fn, arg) {
1973
- if (this._cleanupHooks.filter((hook) => (hook.envObject === envObject && hook.fn === fn && hook.arg === arg)).length > 0) {
1986
+ if (this._cleanupHookIndex === undefined) {
1987
+ for (let i = 0; i < this._cleanupHooks.length; ++i) {
1988
+ const hook = this._cleanupHooks[i];
1989
+ if (hook.envObject === envObject && hook.fn === fn && hook.arg === arg) {
1990
+ throw new Error('Can not add same fn and arg twice');
1991
+ }
1992
+ }
1993
+ this._cleanupHooks.push(new CleanupHookCallback(envObject, fn, arg));
1994
+ if (this._cleanupHooks.length > CleanupQueue.INDEX_THRESHOLD) {
1995
+ this._createIndex();
1996
+ }
1997
+ return;
1998
+ }
1999
+ let envHooks = this._cleanupHookIndex.get(envObject);
2000
+ if (envHooks === undefined) {
2001
+ envHooks = new Map();
2002
+ this._cleanupHookIndex.set(envObject, envHooks);
2003
+ }
2004
+ let args = envHooks.get(fn);
2005
+ if (args === undefined) {
2006
+ args = new Set();
2007
+ envHooks.set(fn, args);
2008
+ }
2009
+ else if (args.has(arg)) {
1974
2010
  throw new Error('Can not add same fn and arg twice');
1975
2011
  }
1976
- this._cleanupHooks.push(new CleanupHookCallback(envObject, fn, arg, this._cleanupHookCounter++));
2012
+ args.add(arg);
2013
+ this._cleanupHooks.push(new CleanupHookCallback(envObject, fn, arg));
1977
2014
  }
1978
2015
  remove(envObject, fn, arg) {
1979
2016
  for (let i = 0; i < this._cleanupHooks.length; ++i) {
1980
2017
  const hook = this._cleanupHooks[i];
1981
2018
  if (hook.envObject === envObject && hook.fn === fn && hook.arg === arg) {
1982
2019
  this._cleanupHooks.splice(i, 1);
2020
+ this._deleteIndex(hook);
1983
2021
  return;
1984
2022
  }
1985
2023
  }
1986
2024
  }
1987
2025
  drain() {
1988
2026
  const hooks = this._cleanupHooks.slice();
1989
- hooks.sort((a, b) => (b.order - a.order));
1990
- for (let i = 0; i < hooks.length; ++i) {
2027
+ for (let i = hooks.length - 1; i >= 0; --i) {
1991
2028
  const cb = hooks[i];
1992
2029
  if (typeof cb.fn === 'number') {
1993
2030
  cb.envObject.bridge.makeDynCall_vp(cb.fn)(cb.arg);
@@ -1995,14 +2032,58 @@ class CleanupQueue {
1995
2032
  else {
1996
2033
  cb.fn(cb.arg);
1997
2034
  }
1998
- this._cleanupHooks.splice(this._cleanupHooks.indexOf(cb), 1);
2035
+ const lastIndex = this._cleanupHooks.length - 1;
2036
+ if (this._cleanupHooks[lastIndex] === cb) {
2037
+ this._cleanupHooks.pop();
2038
+ this._deleteIndex(cb);
2039
+ }
2040
+ else {
2041
+ const index = this._cleanupHooks.lastIndexOf(cb);
2042
+ if (index !== -1) {
2043
+ this._cleanupHooks.splice(index, 1);
2044
+ this._deleteIndex(cb);
2045
+ }
2046
+ }
1999
2047
  }
2048
+ if (this._cleanupHooks.length === 0) {
2049
+ this._cleanupHookIndex = undefined;
2050
+ }
2051
+ }
2052
+ _createIndex() {
2053
+ const index = new WeakMap();
2054
+ for (let i = 0; i < this._cleanupHooks.length; ++i) {
2055
+ const hook = this._cleanupHooks[i];
2056
+ let envHooks = index.get(hook.envObject);
2057
+ if (envHooks === undefined) {
2058
+ envHooks = new Map();
2059
+ index.set(hook.envObject, envHooks);
2060
+ }
2061
+ let args = envHooks.get(hook.fn);
2062
+ if (args === undefined) {
2063
+ args = new Set();
2064
+ envHooks.set(hook.fn, args);
2065
+ }
2066
+ args.add(hook.arg);
2067
+ }
2068
+ this._cleanupHookIndex = index;
2069
+ }
2070
+ _deleteIndex(hook) {
2071
+ if (this._cleanupHookIndex === undefined)
2072
+ return;
2073
+ const envHooks = this._cleanupHookIndex.get(hook.envObject);
2074
+ const args = envHooks.get(hook.fn);
2075
+ args.delete(hook.arg);
2076
+ if (args.size === 0)
2077
+ envHooks.delete(hook.fn);
2078
+ if (envHooks.size === 0)
2079
+ this._cleanupHookIndex.delete(hook.envObject);
2000
2080
  }
2001
2081
  dispose() {
2002
2082
  this._cleanupHooks.length = 0;
2003
- this._cleanupHookCounter = 0;
2083
+ this._cleanupHookIndex = undefined;
2004
2084
  }
2005
2085
  }
2086
+ CleanupQueue.INDEX_THRESHOLD = 16;
2006
2087
  class NodejsWaitingRequestCounter {
2007
2088
  constructor(MessageChannel) {
2008
2089
  this.refHandle = new MessageChannel().port1;
@@ -2107,15 +2188,15 @@ class Context {
2107
2188
  };
2108
2189
  let _;
2109
2190
  const staticFunctionWrapper = (withScope, envObject, data, callbackWrapper, callback) => {
2110
- return function (...args) {
2111
- return withScope(envObject, this, args, data, _, callbackWrapper, callback);
2191
+ return function () {
2192
+ return withScope(envObject, this, arguments, data, _, callbackWrapper, callback);
2112
2193
  };
2113
2194
  };
2114
2195
  let functionWrapper;
2115
2196
  if (name && dynamicExecution && this.features.makeDynamicFunction) {
2116
2197
  try {
2117
- functionWrapper = this.features.makeDynamicFunction('withScope', 'envObject', 'data', 'callbackWrapper', 'callback', 'return function ' + name + '(...args){' +
2118
- 'return withScope(envObject,this,args,data,' + name + ',callbackWrapper,callback)' +
2198
+ functionWrapper = this.features.makeDynamicFunction('withScope', 'envObject', 'data', 'callbackWrapper', 'callback', 'return function ' + name + '(){' +
2199
+ 'return withScope(envObject,this,arguments,data,' + name + ',callbackWrapper,callback)' +
2119
2200
  '};');
2120
2201
  }
2121
2202
  catch (_) {