@emnapi/runtime 0.38.3 → 0.40.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.
@@ -37,63 +37,6 @@ var CallbackInfoStack = /*#__PURE__*/ (function () {
37
37
  return CallbackInfoStack;
38
38
  }());
39
39
 
40
- var Store = /*#__PURE__*/ (function () {
41
- function Store(capacity) {
42
- this._values = [undefined];
43
- this._values.length = capacity;
44
- this._size = 1;
45
- this._freeList = [];
46
- }
47
- Store.prototype.add = function (value) {
48
- var id;
49
- if (this._freeList.length) {
50
- id = this._freeList.shift();
51
- }
52
- else {
53
- id = this._size;
54
- this._size++;
55
- var capacity = this._values.length;
56
- if (id >= capacity) {
57
- this._values.length = Math.ceil(capacity * 1.5);
58
- }
59
- }
60
- value.id = id;
61
- this._values[id] = value;
62
- };
63
- Store.prototype.get = function (id) {
64
- return this._values[id];
65
- };
66
- Store.prototype.has = function (id) {
67
- return this._values[id] !== undefined;
68
- };
69
- Store.prototype.remove = function (id) {
70
- var value = this._values[id];
71
- if (value) {
72
- value.id = 0;
73
- this._values[id] = undefined;
74
- this._freeList.push(Number(id));
75
- }
76
- };
77
- Store.prototype.dispose = function () {
78
- for (var i = 1; i < this._size; ++i) {
79
- var value = this._values[i];
80
- value === null || value === void 0 ? void 0 : value.dispose();
81
- }
82
- this._values = [undefined];
83
- this._size = 1;
84
- this._freeList = [];
85
- };
86
- return Store;
87
- }());
88
-
89
- var EnvStore = /*#__PURE__*/ (function (_super) {
90
- __extends(EnvStore, _super);
91
- function EnvStore() {
92
- return _super.call(this, 2) || this;
93
- }
94
- return EnvStore;
95
- }(Store));
96
-
97
40
  var supportNewFunction = /*#__PURE__*/ (function () {
98
41
  var f;
99
42
  try {
@@ -126,22 +69,24 @@ var _global = /*#__PURE__*/ (function () {
126
69
  }
127
70
  return g;
128
71
  })();
129
- var emptyException = new Error();
130
72
  var TryCatch = /*#__PURE__*/ (function () {
131
73
  function TryCatch() {
132
- this._exception = emptyException;
74
+ this._exception = undefined;
75
+ this._caught = false;
133
76
  }
134
77
  TryCatch.prototype.hasCaught = function () {
135
- return this._exception !== emptyException;
78
+ return this._caught;
136
79
  };
137
80
  TryCatch.prototype.exception = function () {
138
81
  return this._exception;
139
82
  };
140
83
  TryCatch.prototype.setError = function (err) {
84
+ this._caught = true;
141
85
  this._exception = err;
142
86
  };
143
87
  TryCatch.prototype.reset = function () {
144
- this._exception = emptyException;
88
+ this._caught = false;
89
+ this._exception = undefined;
145
90
  };
146
91
  TryCatch.prototype.extractException = function () {
147
92
  var e = this._exception;
@@ -229,77 +174,71 @@ var _Buffer = typeof Buffer === 'function'
229
174
  catch (_) { }
230
175
  return undefined;
231
176
  })();
177
+ var version = "0.40.0";
178
+ var NAPI_VERSION = 9 /* Version.NAPI_VERSION */;
179
+ var NAPI_VERSION_EXPERIMENTAL = 2147483647 /* Version.NAPI_VERSION_EXPERIMENTAL */;
180
+ var NODE_API_DEFAULT_MODULE_API_VERSION = 8 /* Version.NODE_API_DEFAULT_MODULE_API_VERSION */;
232
181
 
233
182
  var Handle = /*#__PURE__*/ (function () {
234
183
  function Handle(id, value) {
235
184
  this.id = id;
236
185
  this.value = value;
237
186
  }
238
- Handle.prototype.init = function (id, value) {
239
- this.id = id;
240
- this.value = value;
241
- };
242
187
  Handle.prototype.data = function (envObject) {
243
188
  return envObject.getObjectBinding(this.value).data;
244
189
  };
245
- Handle.prototype.isEmpty = function () {
246
- return this.id === 0;
247
- };
248
190
  Handle.prototype.isNumber = function () {
249
- return !this.isEmpty() && typeof this.value === 'number';
191
+ return typeof this.value === 'number';
250
192
  };
251
193
  Handle.prototype.isBigInt = function () {
252
- return !this.isEmpty() && typeof this.value === 'bigint';
194
+ return typeof this.value === 'bigint';
253
195
  };
254
196
  Handle.prototype.isString = function () {
255
- return !this.isEmpty() && typeof this.value === 'string';
197
+ return typeof this.value === 'string';
256
198
  };
257
199
  Handle.prototype.isFunction = function () {
258
- return !this.isEmpty() && typeof this.value === 'function';
200
+ return typeof this.value === 'function';
259
201
  };
260
202
  Handle.prototype.isExternal = function () {
261
- return !this.isEmpty() && (isReferenceType(this.value) && Object.getPrototypeOf(this.value) === null);
203
+ return (isReferenceType(this.value) && Object.getPrototypeOf(this.value) === null);
262
204
  };
263
205
  Handle.prototype.isObject = function () {
264
- return !this.isEmpty() && typeof this.value === 'object' && this.value !== null;
206
+ return typeof this.value === 'object' && this.value !== null;
265
207
  };
266
208
  Handle.prototype.isArray = function () {
267
- return !this.isEmpty() && Array.isArray(this.value);
209
+ return Array.isArray(this.value);
268
210
  };
269
211
  Handle.prototype.isArrayBuffer = function () {
270
- return !this.isEmpty() && (this.value instanceof ArrayBuffer);
212
+ return (this.value instanceof ArrayBuffer);
271
213
  };
272
214
  Handle.prototype.isTypedArray = function () {
273
- return !this.isEmpty() && (ArrayBuffer.isView(this.value)) && !(this.value instanceof DataView);
215
+ return (ArrayBuffer.isView(this.value)) && !(this.value instanceof DataView);
274
216
  };
275
217
  Handle.prototype.isBuffer = function () {
276
- return !this.isEmpty() && typeof _Buffer === 'function' && _Buffer.isBuffer(this.value);
218
+ return typeof _Buffer === 'function' && _Buffer.isBuffer(this.value);
277
219
  };
278
220
  Handle.prototype.isDataView = function () {
279
- return !this.isEmpty() && (this.value instanceof DataView);
221
+ return (this.value instanceof DataView);
280
222
  };
281
223
  Handle.prototype.isDate = function () {
282
- return !this.isEmpty() && (this.value instanceof Date);
224
+ return (this.value instanceof Date);
283
225
  };
284
226
  Handle.prototype.isPromise = function () {
285
- return !this.isEmpty() && (this.value instanceof Promise);
227
+ return (this.value instanceof Promise);
286
228
  };
287
229
  Handle.prototype.isBoolean = function () {
288
- return !this.isEmpty() && typeof this.value === 'boolean';
230
+ return typeof this.value === 'boolean';
289
231
  };
290
232
  Handle.prototype.isUndefined = function () {
291
- return !this.isEmpty() && this.value === undefined;
233
+ return this.value === undefined;
292
234
  };
293
235
  Handle.prototype.isSymbol = function () {
294
- return !this.isEmpty() && typeof this.value === 'symbol';
236
+ return typeof this.value === 'symbol';
295
237
  };
296
238
  Handle.prototype.isNull = function () {
297
- return !this.isEmpty() && this.value === null;
239
+ return this.value === null;
298
240
  };
299
241
  Handle.prototype.dispose = function () {
300
- if (this.id === 0)
301
- return;
302
- this.id = 0;
303
242
  this.value = undefined;
304
243
  };
305
244
  return Handle;
@@ -334,7 +273,7 @@ var HandleStore = /*#__PURE__*/ (function () {
334
273
  var values = this._values;
335
274
  if (next < values.length) {
336
275
  h = values[next];
337
- h.init(next, value);
276
+ h.value = value;
338
277
  }
339
278
  else {
340
279
  h = new Handle(next, value);
@@ -480,22 +419,6 @@ var ScopeStore = /*#__PURE__*/ (function () {
480
419
  return ScopeStore;
481
420
  }());
482
421
 
483
- var RefStore = /*#__PURE__*/ (function (_super) {
484
- __extends(RefStore, _super);
485
- function RefStore() {
486
- return _super.call(this, 8) || this;
487
- }
488
- return RefStore;
489
- }(Store));
490
-
491
- var DeferredStore = /*#__PURE__*/ (function (_super) {
492
- __extends(DeferredStore, _super);
493
- function DeferredStore() {
494
- return _super.call(this, 8) || this;
495
- }
496
- return DeferredStore;
497
- }(Store));
498
-
499
422
  var RefTracker = /*#__PURE__*/ (function () {
500
423
  function RefTracker() {
501
424
  this._next = null;
@@ -627,19 +550,29 @@ var RefBase = /*#__PURE__*/ (function (_super) {
627
550
  return RefBase;
628
551
  }(Finalizer));
629
552
 
553
+ function throwNodeApiVersionError(moduleName, moduleApiVersion) {
554
+ var errorMessage = "".concat(moduleName, " requires Node-API version ").concat(moduleApiVersion, ", but this version of Node.js only supports version ").concat(NAPI_VERSION, " add-ons.");
555
+ throw new Error(errorMessage);
556
+ }
557
+ function handleThrow(envObject, value) {
558
+ if (envObject.terminatedOrTerminating()) {
559
+ return;
560
+ }
561
+ throw value;
562
+ }
630
563
  var Env = /*#__PURE__*/ (function () {
631
- function Env(ctx, makeDynCall_vppp, makeDynCall_vp) {
564
+ function Env(ctx, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort) {
632
565
  this.ctx = ctx;
566
+ this.moduleApiVersion = moduleApiVersion;
633
567
  this.makeDynCall_vppp = makeDynCall_vppp;
634
568
  this.makeDynCall_vp = makeDynCall_vp;
569
+ this.abort = abort;
635
570
  this.openHandleScopes = 0;
636
571
  this.instanceData = null;
637
572
  this.tryCatch = new TryCatch();
638
573
  this.refs = 1;
639
574
  this.reflist = new RefTracker();
640
575
  this.finalizing_reflist = new RefTracker();
641
- this.destructing = false;
642
- this.finalizationScheduled = false;
643
576
  this.pendingFinalizers = [];
644
577
  this.lastError = {
645
578
  errorCode: 0 /* napi_status.napi_ok */,
@@ -649,11 +582,12 @@ var Env = /*#__PURE__*/ (function () {
649
582
  this._bindingMap = new WeakMap();
650
583
  this.id = 0;
651
584
  }
652
- Env.create = function (ctx, makeDynCall_vppp, makeDynCall_vp) {
653
- var env = new Env(ctx, makeDynCall_vppp, makeDynCall_vp);
654
- ctx.envStore.add(env);
655
- ctx.addCleanupHook(env, function () { env.unref(); }, 0);
656
- return env;
585
+ /** @virtual */
586
+ Env.prototype.canCallIntoJs = function () {
587
+ return true;
588
+ };
589
+ Env.prototype.terminatedOrTerminating = function () {
590
+ return !this.canCallIntoJs();
657
591
  };
658
592
  Env.prototype.ref = function () {
659
593
  this.refs++;
@@ -672,32 +606,44 @@ var Env = /*#__PURE__*/ (function () {
672
606
  };
673
607
  Env.prototype.clearLastError = function () {
674
608
  var lastError = this.lastError;
675
- lastError.errorCode = 0 /* napi_status.napi_ok */;
676
- lastError.engineErrorCode = 0;
677
- lastError.engineReserved = 0;
609
+ if (lastError.errorCode !== 0 /* napi_status.napi_ok */)
610
+ lastError.errorCode = 0 /* napi_status.napi_ok */;
611
+ if (lastError.engineErrorCode !== 0)
612
+ lastError.engineErrorCode = 0;
613
+ if (lastError.engineReserved !== 0)
614
+ lastError.engineReserved = 0;
678
615
  return 0 /* napi_status.napi_ok */;
679
616
  };
680
617
  Env.prototype.setLastError = function (error_code, engine_error_code, engine_reserved) {
681
618
  if (engine_error_code === void 0) { engine_error_code = 0; }
682
619
  if (engine_reserved === void 0) { engine_reserved = 0; }
683
620
  var lastError = this.lastError;
684
- lastError.errorCode = error_code;
685
- lastError.engineErrorCode = engine_error_code;
686
- lastError.engineReserved = engine_reserved;
621
+ if (lastError.errorCode !== error_code)
622
+ lastError.errorCode = error_code;
623
+ if (lastError.engineErrorCode !== engine_error_code)
624
+ lastError.engineErrorCode = engine_error_code;
625
+ if (lastError.engineReserved !== engine_reserved)
626
+ lastError.engineReserved = engine_reserved;
687
627
  return error_code;
688
628
  };
689
629
  Env.prototype.getReturnStatus = function () {
690
630
  return !this.tryCatch.hasCaught() ? 0 /* napi_status.napi_ok */ : this.setLastError(10 /* napi_status.napi_pending_exception */);
691
631
  };
692
- Env.prototype.callIntoModule = function (fn) {
632
+ Env.prototype.callIntoModule = function (fn, handleException) {
633
+ if (handleException === void 0) { handleException = handleThrow; }
634
+ var openHandleScopesBefore = this.openHandleScopes;
693
635
  this.clearLastError();
694
636
  var r = fn(this);
637
+ if (openHandleScopesBefore !== this.openHandleScopes) {
638
+ this.abort('open_handle_scopes != open_handle_scopes_before');
639
+ }
695
640
  if (this.tryCatch.hasCaught()) {
696
641
  var err = this.tryCatch.extractException();
697
- throw err;
642
+ handleException(this, err);
698
643
  }
699
644
  return r;
700
645
  };
646
+ /** @virtual */
701
647
  Env.prototype.callFinalizer = function (cb, data, hint) {
702
648
  var f = this.makeDynCall_vppp(cb);
703
649
  var env = this.id;
@@ -709,42 +655,30 @@ var Env = /*#__PURE__*/ (function () {
709
655
  this.ctx.closeScope(this, scope);
710
656
  }
711
657
  };
658
+ /** @virtual */
712
659
  Env.prototype.enqueueFinalizer = function (finalizer) {
713
- var _this = this;
714
660
  if (this.pendingFinalizers.indexOf(finalizer) === -1) {
715
661
  this.pendingFinalizers.push(finalizer);
716
662
  }
717
- if (!this.finalizationScheduled && !this.destructing) {
718
- this.finalizationScheduled = true;
719
- this.ref();
720
- _setImmediate(function () {
721
- _this.finalizationScheduled = false;
722
- _this.unref();
723
- _this.drainFinalizerQueue();
724
- });
725
- }
726
663
  };
664
+ /** @virtual */
727
665
  Env.prototype.dequeueFinalizer = function (finalizer) {
728
666
  var index = this.pendingFinalizers.indexOf(finalizer);
729
667
  if (index !== -1) {
730
668
  this.pendingFinalizers.splice(index, 1);
731
669
  }
732
670
  };
733
- Env.prototype.drainFinalizerQueue = function () {
734
- while (this.pendingFinalizers.length > 0) {
735
- var refTracker = this.pendingFinalizers.shift();
736
- refTracker.finalize();
737
- }
738
- };
739
- Env.prototype.dispose = function () {
740
- if (this.id === 0)
741
- return;
742
- this.destructing = true;
743
- this.drainFinalizerQueue();
671
+ /** @virtual */
672
+ Env.prototype.deleteMe = function () {
744
673
  RefBase.finalizeAll(this.finalizing_reflist);
745
674
  RefBase.finalizeAll(this.reflist);
746
675
  this.tryCatch.extractException();
747
676
  this.ctx.envStore.remove(this.id);
677
+ };
678
+ Env.prototype.dispose = function () {
679
+ if (this.id === 0)
680
+ return;
681
+ this.deleteMe();
748
682
  this.id = 0;
749
683
  };
750
684
  Env.prototype.initObjectBinding = function (value) {
@@ -773,6 +707,108 @@ var Env = /*#__PURE__*/ (function () {
773
707
  };
774
708
  return Env;
775
709
  }());
710
+ var NodeEnv = /*#__PURE__*/ (function (_super) {
711
+ __extends(NodeEnv, _super);
712
+ function NodeEnv(ctx, filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort, nodeBinding) {
713
+ var _this = _super.call(this, ctx, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort) || this;
714
+ _this.filename = filename;
715
+ _this.nodeBinding = nodeBinding;
716
+ _this.destructing = false;
717
+ _this.finalizationScheduled = false;
718
+ return _this;
719
+ }
720
+ NodeEnv.prototype.deleteMe = function () {
721
+ this.destructing = true;
722
+ this.drainFinalizerQueue();
723
+ _super.prototype.deleteMe.call(this);
724
+ };
725
+ NodeEnv.prototype.canCallIntoJs = function () {
726
+ return this.ctx.canCallIntoJs();
727
+ };
728
+ NodeEnv.prototype.triggerFatalException = function (err) {
729
+ if (this.nodeBinding) {
730
+ this.nodeBinding.napi.fatalException(err);
731
+ }
732
+ else {
733
+ if (typeof process === 'object' && process !== null && typeof process._fatalException === 'function') {
734
+ var handled = process._fatalException(err);
735
+ if (!handled) {
736
+ console.error(err);
737
+ process.exit(1);
738
+ }
739
+ }
740
+ else {
741
+ throw err;
742
+ }
743
+ }
744
+ };
745
+ NodeEnv.prototype.callbackIntoModule = function (enforceUncaughtExceptionPolicy, fn) {
746
+ return this.callIntoModule(fn, function (envObject, err) {
747
+ if (envObject.terminatedOrTerminating()) {
748
+ return;
749
+ }
750
+ var hasProcess = typeof process === 'object' && process !== null;
751
+ var hasForceFlag = hasProcess ? Boolean(process.execArgv && (process.execArgv.indexOf('--force-node-api-uncaught-exceptions-policy') !== -1)) : false;
752
+ if (!hasForceFlag && !enforceUncaughtExceptionPolicy) {
753
+ if (hasProcess && typeof process.emitWarning === 'function') {
754
+ process.emitWarning('Uncaught N-API callback exception detected, please run node with option --force-node-api-uncaught-exceptions-policy=true to handle those exceptions properly.', 'DeprecationWarning', 'DEP0168');
755
+ }
756
+ else {
757
+ throw err;
758
+ }
759
+ }
760
+ envObject.triggerFatalException(err);
761
+ });
762
+ };
763
+ NodeEnv.prototype.callFinalizer = function (cb, data, hint) {
764
+ this.callFinalizerInternal(1, cb, data, hint);
765
+ };
766
+ NodeEnv.prototype.callFinalizerInternal = function (forceUncaught, cb, data, hint) {
767
+ var f = this.makeDynCall_vppp(cb);
768
+ var env = this.id;
769
+ var scope = this.ctx.openScope(this);
770
+ try {
771
+ this.callbackIntoModule(Boolean(forceUncaught), function () { f(env, data, hint); });
772
+ }
773
+ finally {
774
+ this.ctx.closeScope(this, scope);
775
+ }
776
+ };
777
+ NodeEnv.prototype.enqueueFinalizer = function (finalizer) {
778
+ var _this = this;
779
+ _super.prototype.enqueueFinalizer.call(this, finalizer);
780
+ if (!this.finalizationScheduled && !this.destructing) {
781
+ this.finalizationScheduled = true;
782
+ this.ref();
783
+ _setImmediate(function () {
784
+ _this.finalizationScheduled = false;
785
+ _this.unref();
786
+ _this.drainFinalizerQueue();
787
+ });
788
+ }
789
+ };
790
+ NodeEnv.prototype.drainFinalizerQueue = function () {
791
+ while (this.pendingFinalizers.length > 0) {
792
+ var refTracker = this.pendingFinalizers.shift();
793
+ refTracker.finalize();
794
+ }
795
+ };
796
+ return NodeEnv;
797
+ }(Env));
798
+ function newEnv(ctx, filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort, nodeBinding) {
799
+ moduleApiVersion = typeof moduleApiVersion !== 'number' ? NODE_API_DEFAULT_MODULE_API_VERSION : moduleApiVersion;
800
+ // Validate module_api_version.
801
+ if (moduleApiVersion < NODE_API_DEFAULT_MODULE_API_VERSION) {
802
+ moduleApiVersion = NODE_API_DEFAULT_MODULE_API_VERSION;
803
+ }
804
+ else if (moduleApiVersion > NAPI_VERSION && moduleApiVersion !== NAPI_VERSION_EXPERIMENTAL) {
805
+ throwNodeApiVersionError(filename, moduleApiVersion);
806
+ }
807
+ var env = new NodeEnv(ctx, filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort, nodeBinding);
808
+ ctx.envStore.add(env);
809
+ ctx.addCleanupHook(env, function () { env.unref(); }, 0);
810
+ return env;
811
+ }
776
812
 
777
813
  var EmnapiError = /*#__PURE__*/ (function (_super) {
778
814
  __extends(EmnapiError, _super);
@@ -861,19 +897,19 @@ var Persistent = /*#__PURE__*/ (function () {
861
897
  return;
862
898
  var value = this._ref.deref();
863
899
  try {
864
- // try {
865
900
  Persistent._registry.register(value, this, this);
866
901
  var weakRef = new WeakRef(value);
867
902
  this._ref.dispose();
868
903
  this._ref = weakRef;
869
- // } catch (_) {
870
- // Persistent._registry.register(this._ref, this, this)
871
- // this._ref = new WeakRef<any>(this._ref)
872
- // }
873
904
  this._param = param;
874
905
  this._callback = callback;
875
906
  }
876
- catch (_) { }
907
+ catch (err) {
908
+ if (typeof value === 'symbol') ;
909
+ else {
910
+ throw err;
911
+ }
912
+ }
877
913
  };
878
914
  Persistent.prototype.clearWeak = function () {
879
915
  if (!supportFinalizer || this._ref === undefined)
@@ -935,6 +971,9 @@ function weakCallback(ref) {
935
971
  ref.persistent.reset();
936
972
  ref.envObject.enqueueFinalizer(ref);
937
973
  }
974
+ function canBeHeldWeakly(value) {
975
+ return value.isObject() || value.isFunction() || value.isSymbol();
976
+ }
938
977
  var Reference = /*#__PURE__*/ (function (_super) {
939
978
  __extends(Reference, _super);
940
979
  function Reference(envObject, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint) {
@@ -952,6 +991,7 @@ var Reference = /*#__PURE__*/ (function (_super) {
952
991
  var handle = envObject.ctx.handleStore.get(handle_id);
953
992
  var ref = new Reference(envObject, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint);
954
993
  envObject.ctx.refStore.add(ref);
994
+ ref.canBeWeak = canBeHeldWeakly(handle);
955
995
  ref.persistent = new Persistent(handle.value);
956
996
  if (initialRefcount === 0) {
957
997
  ref._setWeak();
@@ -963,11 +1003,8 @@ var Reference = /*#__PURE__*/ (function (_super) {
963
1003
  return 0;
964
1004
  }
965
1005
  var count = _super.prototype.ref.call(this);
966
- if (count === 1) {
967
- var obj = this.persistent.deref();
968
- if (obj) {
969
- this.persistent.clearWeak();
970
- }
1006
+ if (count === 1 && this.canBeWeak) {
1007
+ this.persistent.clearWeak();
971
1008
  }
972
1009
  return count;
973
1010
  };
@@ -978,23 +1015,25 @@ var Reference = /*#__PURE__*/ (function (_super) {
978
1015
  var oldRefcount = this.refCount();
979
1016
  var refcount = _super.prototype.unref.call(this);
980
1017
  if (oldRefcount === 1 && refcount === 0) {
981
- var obj = this.persistent.deref();
982
- if (obj) {
983
- this._setWeak();
984
- }
1018
+ this._setWeak();
985
1019
  }
986
1020
  return refcount;
987
1021
  };
988
1022
  Reference.prototype.get = function () {
989
- var obj = this.persistent.deref();
990
- if (obj) {
991
- var handle = this.envObject.ensureHandle(obj);
992
- return handle.id;
1023
+ if (this.persistent.isEmpty()) {
1024
+ return 0;
993
1025
  }
994
- return 0;
1026
+ var obj = this.persistent.deref();
1027
+ var handle = this.envObject.ensureHandle(obj);
1028
+ return handle.id;
995
1029
  };
996
1030
  Reference.prototype._setWeak = function () {
997
- this.persistent.setWeak(this, weakCallback);
1031
+ if (this.canBeWeak) {
1032
+ this.persistent.setWeak(this, weakCallback);
1033
+ }
1034
+ else {
1035
+ this.persistent.reset();
1036
+ }
998
1037
  };
999
1038
  Reference.prototype.finalize = function () {
1000
1039
  this.persistent.reset();
@@ -1039,6 +1078,55 @@ var Deferred = /*#__PURE__*/ (function () {
1039
1078
  return Deferred;
1040
1079
  }());
1041
1080
 
1081
+ var Store = /*#__PURE__*/ (function () {
1082
+ function Store() {
1083
+ this._values = [undefined];
1084
+ this._values.length = 4;
1085
+ this._size = 1;
1086
+ this._freeList = [];
1087
+ }
1088
+ Store.prototype.add = function (value) {
1089
+ var id;
1090
+ if (this._freeList.length) {
1091
+ id = this._freeList.shift();
1092
+ }
1093
+ else {
1094
+ id = this._size;
1095
+ this._size++;
1096
+ var capacity = this._values.length;
1097
+ if (id >= capacity) {
1098
+ this._values.length = capacity + (capacity >> 1) + 16;
1099
+ }
1100
+ }
1101
+ value.id = id;
1102
+ this._values[id] = value;
1103
+ };
1104
+ Store.prototype.get = function (id) {
1105
+ return this._values[id];
1106
+ };
1107
+ Store.prototype.has = function (id) {
1108
+ return this._values[id] !== undefined;
1109
+ };
1110
+ Store.prototype.remove = function (id) {
1111
+ var value = this._values[id];
1112
+ if (value) {
1113
+ value.id = 0;
1114
+ this._values[id] = undefined;
1115
+ this._freeList.push(Number(id));
1116
+ }
1117
+ };
1118
+ Store.prototype.dispose = function () {
1119
+ for (var i = 1; i < this._size; ++i) {
1120
+ var value = this._values[i];
1121
+ value === null || value === void 0 ? void 0 : value.dispose();
1122
+ }
1123
+ this._values = [undefined];
1124
+ this._size = 1;
1125
+ this._freeList = [];
1126
+ };
1127
+ return Store;
1128
+ }());
1129
+
1042
1130
  var CleanupHookCallback = /*#__PURE__*/ (function () {
1043
1131
  function CleanupHookCallback(envObject, fn, arg, order) {
1044
1132
  this.envObject = envObject;
@@ -1115,10 +1203,12 @@ var NodejsWaitingRequestCounter = /*#__PURE__*/ (function () {
1115
1203
  var Context = /*#__PURE__*/ (function () {
1116
1204
  function Context() {
1117
1205
  var _this = this;
1118
- this.envStore = new EnvStore();
1206
+ this._isStopping = false;
1207
+ this._canCallIntoJs = true;
1208
+ this.envStore = new Store();
1119
1209
  this.scopeStore = new ScopeStore();
1120
- this.refStore = new RefStore();
1121
- this.deferredStore = new DeferredStore();
1210
+ this.refStore = new Store();
1211
+ this.deferredStore = new Store();
1122
1212
  this.handleStore = new HandleStore();
1123
1213
  this.cbinfoStack = new CallbackInfoStack();
1124
1214
  this.feature = {
@@ -1136,10 +1226,19 @@ var Context = /*#__PURE__*/ (function () {
1136
1226
  if (typeof process === 'object' && process !== null && typeof process.once === 'function') {
1137
1227
  this.refCounter = new NodejsWaitingRequestCounter();
1138
1228
  process.once('beforeExit', function () {
1139
- _this.runCleanup();
1229
+ _this.destroy();
1140
1230
  });
1141
1231
  }
1142
1232
  }
1233
+ // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
1234
+ Context.prototype.getRuntimeVersions = function () {
1235
+ return {
1236
+ version: version,
1237
+ NAPI_VERSION: NAPI_VERSION,
1238
+ NAPI_VERSION_EXPERIMENTAL: NAPI_VERSION_EXPERIMENTAL,
1239
+ NODE_API_DEFAULT_MODULE_API_VERSION: NODE_API_DEFAULT_MODULE_API_VERSION
1240
+ };
1241
+ };
1143
1242
  Context.prototype.createNotSupportWeakRefError = function (api, message) {
1144
1243
  return new NotSupportWeakRefError(api, message);
1145
1244
  };
@@ -1158,8 +1257,8 @@ var Context = /*#__PURE__*/ (function () {
1158
1257
  Context.prototype.createDeferred = function (value) {
1159
1258
  return Deferred.create(this, value);
1160
1259
  };
1161
- Context.prototype.createEnv = function (makeDynCall_vppp, makeDynCall_vp) {
1162
- return Env.create(this, makeDynCall_vppp, makeDynCall_vp);
1260
+ Context.prototype.createEnv = function (filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort, nodeBinding) {
1261
+ return newEnv(this, filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort, nodeBinding);
1163
1262
  };
1164
1263
  Context.prototype.getCurrentScope = function () {
1165
1264
  return this.scopeStore.currentScope;
@@ -1202,7 +1301,18 @@ var Context = /*#__PURE__*/ (function () {
1202
1301
  var _a;
1203
1302
  (_a = this.refCounter) === null || _a === void 0 ? void 0 : _a.decrease();
1204
1303
  };
1205
- Context.prototype.dispose = function () {
1304
+ Context.prototype.setCanCallIntoJs = function (value) {
1305
+ this._canCallIntoJs = value;
1306
+ };
1307
+ Context.prototype.setStopping = function (value) {
1308
+ this._isStopping = value;
1309
+ };
1310
+ Context.prototype.canCallIntoJs = function () {
1311
+ return this._canCallIntoJs && !this._isStopping;
1312
+ };
1313
+ Context.prototype.destroy = function () {
1314
+ this.setStopping(true);
1315
+ this.setCanCallIntoJs(false);
1206
1316
  this.runCleanup();
1207
1317
  };
1208
1318
  return Context;
@@ -1218,6 +1328,4 @@ function getDefaultContext() {
1218
1328
  return defaultContext;
1219
1329
  }
1220
1330
 
1221
- var version = "0.38.3";
1222
-
1223
- export { CallbackInfo, CallbackInfoStack, ConstHandle, Context, Deferred, DeferredStore, EmnapiError, Env, EnvStore, Finalizer, Handle, HandleScope, HandleStore, NotSupportBigIntError, NotSupportBufferError, NotSupportWeakRefError, Persistent, RefBase, RefStore, RefTracker, Reference, ScopeStore, Store, TryCatch, createContext, getDefaultContext, isReferenceType, version };
1331
+ export { CallbackInfo, CallbackInfoStack, ConstHandle, Context, Deferred, EmnapiError, Env, Finalizer, Handle, HandleScope, HandleStore, NAPI_VERSION, NAPI_VERSION_EXPERIMENTAL, NODE_API_DEFAULT_MODULE_API_VERSION, NodeEnv, NotSupportBigIntError, NotSupportBufferError, NotSupportWeakRefError, Persistent, RefBase, RefTracker, Reference, ScopeStore, Store, TryCatch, createContext, getDefaultContext, isReferenceType, version };