@emnapi/runtime 0.38.3 → 0.39.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/emnapi.js CHANGED
@@ -40,93 +40,36 @@
40
40
  return CallbackInfoStack;
41
41
  }());
42
42
 
43
- /******************************************************************************
44
- Copyright (c) Microsoft Corporation.
45
-
46
- Permission to use, copy, modify, and/or distribute this software for any
47
- purpose with or without fee is hereby granted.
48
-
49
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
50
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
51
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
52
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
53
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
54
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
55
- PERFORMANCE OF THIS SOFTWARE.
56
- ***************************************************************************** */
57
- /* global Reflect, Promise */
58
-
59
- var extendStatics = function(d, b) {
60
- extendStatics = Object.setPrototypeOf ||
61
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
62
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
63
- return extendStatics(d, b);
64
- };
65
-
66
- function __extends(d, b) {
67
- if (typeof b !== "function" && b !== null)
68
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
69
- extendStatics(d, b);
70
- function __() { this.constructor = d; }
71
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
72
- }
43
+ /******************************************************************************
44
+ Copyright (c) Microsoft Corporation.
73
45
 
74
- var Store = /*#__PURE__*/ (function () {
75
- function Store(capacity) {
76
- this._values = [undefined];
77
- this._values.length = capacity;
78
- this._size = 1;
79
- this._freeList = [];
80
- }
81
- Store.prototype.add = function (value) {
82
- var id;
83
- if (this._freeList.length) {
84
- id = this._freeList.shift();
85
- }
86
- else {
87
- id = this._size;
88
- this._size++;
89
- var capacity = this._values.length;
90
- if (id >= capacity) {
91
- this._values.length = Math.ceil(capacity * 1.5);
92
- }
93
- }
94
- value.id = id;
95
- this._values[id] = value;
96
- };
97
- Store.prototype.get = function (id) {
98
- return this._values[id];
99
- };
100
- Store.prototype.has = function (id) {
101
- return this._values[id] !== undefined;
102
- };
103
- Store.prototype.remove = function (id) {
104
- var value = this._values[id];
105
- if (value) {
106
- value.id = 0;
107
- this._values[id] = undefined;
108
- this._freeList.push(Number(id));
109
- }
110
- };
111
- Store.prototype.dispose = function () {
112
- for (var i = 1; i < this._size; ++i) {
113
- var value = this._values[i];
114
- value === null || value === void 0 ? void 0 : value.dispose();
115
- }
116
- this._values = [undefined];
117
- this._size = 1;
118
- this._freeList = [];
119
- };
120
- return Store;
121
- }());
46
+ Permission to use, copy, modify, and/or distribute this software for any
47
+ purpose with or without fee is hereby granted.
122
48
 
123
- var EnvStore = /*#__PURE__*/ (function (_super) {
124
- __extends(EnvStore, _super);
125
- function EnvStore() {
126
- return _super.call(this, 2) || this;
127
- }
128
- return EnvStore;
129
- }(Store));
49
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
50
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
51
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
52
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
53
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
54
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
55
+ PERFORMANCE OF THIS SOFTWARE.
56
+ ***************************************************************************** */
57
+ /* global Reflect, Promise */
58
+
59
+ var extendStatics = function(d, b) {
60
+ extendStatics = Object.setPrototypeOf ||
61
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
62
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
63
+ return extendStatics(d, b);
64
+ };
65
+
66
+ function __extends(d, b) {
67
+ if (typeof b !== "function" && b !== null)
68
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
69
+ extendStatics(d, b);
70
+ function __() { this.constructor = d; }
71
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
72
+ }
130
73
 
131
74
  var supportNewFunction = /*#__PURE__*/ (function () {
132
75
  var f;
@@ -160,22 +103,24 @@
160
103
  }
161
104
  return g;
162
105
  })();
163
- var emptyException = new Error();
164
106
  var TryCatch = /*#__PURE__*/ (function () {
165
107
  function TryCatch() {
166
- this._exception = emptyException;
108
+ this._exception = undefined;
109
+ this._caught = false;
167
110
  }
168
111
  TryCatch.prototype.hasCaught = function () {
169
- return this._exception !== emptyException;
112
+ return this._caught;
170
113
  };
171
114
  TryCatch.prototype.exception = function () {
172
115
  return this._exception;
173
116
  };
174
117
  TryCatch.prototype.setError = function (err) {
118
+ this._caught = true;
175
119
  this._exception = err;
176
120
  };
177
121
  TryCatch.prototype.reset = function () {
178
- this._exception = emptyException;
122
+ this._caught = false;
123
+ this._exception = undefined;
179
124
  };
180
125
  TryCatch.prototype.extractException = function () {
181
126
  var e = this._exception;
@@ -263,77 +208,71 @@
263
208
  catch (_) { }
264
209
  return undefined;
265
210
  })();
211
+ var version = "0.39.0";
212
+ var NAPI_VERSION = 9 /* Version.NAPI_VERSION */;
213
+ var NAPI_VERSION_EXPERIMENTAL = 2147483647 /* Version.NAPI_VERSION_EXPERIMENTAL */;
214
+ var NODE_API_DEFAULT_MODULE_API_VERSION = 8 /* Version.NODE_API_DEFAULT_MODULE_API_VERSION */;
266
215
 
267
216
  var Handle = /*#__PURE__*/ (function () {
268
217
  function Handle(id, value) {
269
218
  this.id = id;
270
219
  this.value = value;
271
220
  }
272
- Handle.prototype.init = function (id, value) {
273
- this.id = id;
274
- this.value = value;
275
- };
276
221
  Handle.prototype.data = function (envObject) {
277
222
  return envObject.getObjectBinding(this.value).data;
278
223
  };
279
- Handle.prototype.isEmpty = function () {
280
- return this.id === 0;
281
- };
282
224
  Handle.prototype.isNumber = function () {
283
- return !this.isEmpty() && typeof this.value === 'number';
225
+ return typeof this.value === 'number';
284
226
  };
285
227
  Handle.prototype.isBigInt = function () {
286
- return !this.isEmpty() && typeof this.value === 'bigint';
228
+ return typeof this.value === 'bigint';
287
229
  };
288
230
  Handle.prototype.isString = function () {
289
- return !this.isEmpty() && typeof this.value === 'string';
231
+ return typeof this.value === 'string';
290
232
  };
291
233
  Handle.prototype.isFunction = function () {
292
- return !this.isEmpty() && typeof this.value === 'function';
234
+ return typeof this.value === 'function';
293
235
  };
294
236
  Handle.prototype.isExternal = function () {
295
- return !this.isEmpty() && (isReferenceType(this.value) && Object.getPrototypeOf(this.value) === null);
237
+ return (isReferenceType(this.value) && Object.getPrototypeOf(this.value) === null);
296
238
  };
297
239
  Handle.prototype.isObject = function () {
298
- return !this.isEmpty() && typeof this.value === 'object' && this.value !== null;
240
+ return typeof this.value === 'object' && this.value !== null;
299
241
  };
300
242
  Handle.prototype.isArray = function () {
301
- return !this.isEmpty() && Array.isArray(this.value);
243
+ return Array.isArray(this.value);
302
244
  };
303
245
  Handle.prototype.isArrayBuffer = function () {
304
- return !this.isEmpty() && (this.value instanceof ArrayBuffer);
246
+ return (this.value instanceof ArrayBuffer);
305
247
  };
306
248
  Handle.prototype.isTypedArray = function () {
307
- return !this.isEmpty() && (ArrayBuffer.isView(this.value)) && !(this.value instanceof DataView);
249
+ return (ArrayBuffer.isView(this.value)) && !(this.value instanceof DataView);
308
250
  };
309
251
  Handle.prototype.isBuffer = function () {
310
- return !this.isEmpty() && typeof _Buffer === 'function' && _Buffer.isBuffer(this.value);
252
+ return typeof _Buffer === 'function' && _Buffer.isBuffer(this.value);
311
253
  };
312
254
  Handle.prototype.isDataView = function () {
313
- return !this.isEmpty() && (this.value instanceof DataView);
255
+ return (this.value instanceof DataView);
314
256
  };
315
257
  Handle.prototype.isDate = function () {
316
- return !this.isEmpty() && (this.value instanceof Date);
258
+ return (this.value instanceof Date);
317
259
  };
318
260
  Handle.prototype.isPromise = function () {
319
- return !this.isEmpty() && (this.value instanceof Promise);
261
+ return (this.value instanceof Promise);
320
262
  };
321
263
  Handle.prototype.isBoolean = function () {
322
- return !this.isEmpty() && typeof this.value === 'boolean';
264
+ return typeof this.value === 'boolean';
323
265
  };
324
266
  Handle.prototype.isUndefined = function () {
325
- return !this.isEmpty() && this.value === undefined;
267
+ return this.value === undefined;
326
268
  };
327
269
  Handle.prototype.isSymbol = function () {
328
- return !this.isEmpty() && typeof this.value === 'symbol';
270
+ return typeof this.value === 'symbol';
329
271
  };
330
272
  Handle.prototype.isNull = function () {
331
- return !this.isEmpty() && this.value === null;
273
+ return this.value === null;
332
274
  };
333
275
  Handle.prototype.dispose = function () {
334
- if (this.id === 0)
335
- return;
336
- this.id = 0;
337
276
  this.value = undefined;
338
277
  };
339
278
  return Handle;
@@ -368,7 +307,7 @@
368
307
  var values = this._values;
369
308
  if (next < values.length) {
370
309
  h = values[next];
371
- h.init(next, value);
310
+ h.value = value;
372
311
  }
373
312
  else {
374
313
  h = new Handle(next, value);
@@ -514,22 +453,6 @@
514
453
  return ScopeStore;
515
454
  }());
516
455
 
517
- var RefStore = /*#__PURE__*/ (function (_super) {
518
- __extends(RefStore, _super);
519
- function RefStore() {
520
- return _super.call(this, 8) || this;
521
- }
522
- return RefStore;
523
- }(Store));
524
-
525
- var DeferredStore = /*#__PURE__*/ (function (_super) {
526
- __extends(DeferredStore, _super);
527
- function DeferredStore() {
528
- return _super.call(this, 8) || this;
529
- }
530
- return DeferredStore;
531
- }(Store));
532
-
533
456
  var RefTracker = /*#__PURE__*/ (function () {
534
457
  function RefTracker() {
535
458
  this._next = null;
@@ -661,19 +584,29 @@
661
584
  return RefBase;
662
585
  }(Finalizer));
663
586
 
587
+ function throwNodeApiVersionError(moduleName, moduleApiVersion) {
588
+ var errorMessage = "".concat(moduleName, " requires Node-API version ").concat(moduleApiVersion, ", but this version of Node.js only supports version ").concat(NAPI_VERSION, " add-ons.");
589
+ throw new Error(errorMessage);
590
+ }
591
+ function handleThrow(envObject, value) {
592
+ if (envObject.terminatedOrTerminating()) {
593
+ return;
594
+ }
595
+ throw value;
596
+ }
664
597
  var Env = /*#__PURE__*/ (function () {
665
- function Env(ctx, makeDynCall_vppp, makeDynCall_vp) {
598
+ function Env(ctx, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort) {
666
599
  this.ctx = ctx;
600
+ this.moduleApiVersion = moduleApiVersion;
667
601
  this.makeDynCall_vppp = makeDynCall_vppp;
668
602
  this.makeDynCall_vp = makeDynCall_vp;
603
+ this.abort = abort;
669
604
  this.openHandleScopes = 0;
670
605
  this.instanceData = null;
671
606
  this.tryCatch = new TryCatch();
672
607
  this.refs = 1;
673
608
  this.reflist = new RefTracker();
674
609
  this.finalizing_reflist = new RefTracker();
675
- this.destructing = false;
676
- this.finalizationScheduled = false;
677
610
  this.pendingFinalizers = [];
678
611
  this.lastError = {
679
612
  errorCode: 0 /* napi_status.napi_ok */,
@@ -683,11 +616,12 @@
683
616
  this._bindingMap = new WeakMap();
684
617
  this.id = 0;
685
618
  }
686
- Env.create = function (ctx, makeDynCall_vppp, makeDynCall_vp) {
687
- var env = new Env(ctx, makeDynCall_vppp, makeDynCall_vp);
688
- ctx.envStore.add(env);
689
- ctx.addCleanupHook(env, function () { env.unref(); }, 0);
690
- return env;
619
+ /** @virtual */
620
+ Env.prototype.canCallIntoJs = function () {
621
+ return true;
622
+ };
623
+ Env.prototype.terminatedOrTerminating = function () {
624
+ return !this.canCallIntoJs();
691
625
  };
692
626
  Env.prototype.ref = function () {
693
627
  this.refs++;
@@ -706,32 +640,44 @@
706
640
  };
707
641
  Env.prototype.clearLastError = function () {
708
642
  var lastError = this.lastError;
709
- lastError.errorCode = 0 /* napi_status.napi_ok */;
710
- lastError.engineErrorCode = 0;
711
- lastError.engineReserved = 0;
643
+ if (lastError.errorCode !== 0 /* napi_status.napi_ok */)
644
+ lastError.errorCode = 0 /* napi_status.napi_ok */;
645
+ if (lastError.engineErrorCode !== 0)
646
+ lastError.engineErrorCode = 0;
647
+ if (lastError.engineReserved !== 0)
648
+ lastError.engineReserved = 0;
712
649
  return 0 /* napi_status.napi_ok */;
713
650
  };
714
651
  Env.prototype.setLastError = function (error_code, engine_error_code, engine_reserved) {
715
652
  if (engine_error_code === void 0) { engine_error_code = 0; }
716
653
  if (engine_reserved === void 0) { engine_reserved = 0; }
717
654
  var lastError = this.lastError;
718
- lastError.errorCode = error_code;
719
- lastError.engineErrorCode = engine_error_code;
720
- lastError.engineReserved = engine_reserved;
655
+ if (lastError.errorCode !== error_code)
656
+ lastError.errorCode = error_code;
657
+ if (lastError.engineErrorCode !== engine_error_code)
658
+ lastError.engineErrorCode = engine_error_code;
659
+ if (lastError.engineReserved !== engine_reserved)
660
+ lastError.engineReserved = engine_reserved;
721
661
  return error_code;
722
662
  };
723
663
  Env.prototype.getReturnStatus = function () {
724
664
  return !this.tryCatch.hasCaught() ? 0 /* napi_status.napi_ok */ : this.setLastError(10 /* napi_status.napi_pending_exception */);
725
665
  };
726
- Env.prototype.callIntoModule = function (fn) {
666
+ Env.prototype.callIntoModule = function (fn, handleException) {
667
+ if (handleException === void 0) { handleException = handleThrow; }
668
+ var openHandleScopesBefore = this.openHandleScopes;
727
669
  this.clearLastError();
728
670
  var r = fn(this);
671
+ if (openHandleScopesBefore !== this.openHandleScopes) {
672
+ this.abort('open_handle_scopes != open_handle_scopes_before');
673
+ }
729
674
  if (this.tryCatch.hasCaught()) {
730
675
  var err = this.tryCatch.extractException();
731
- throw err;
676
+ handleException(this, err);
732
677
  }
733
678
  return r;
734
679
  };
680
+ /** @virtual */
735
681
  Env.prototype.callFinalizer = function (cb, data, hint) {
736
682
  var f = this.makeDynCall_vppp(cb);
737
683
  var env = this.id;
@@ -743,42 +689,30 @@
743
689
  this.ctx.closeScope(this, scope);
744
690
  }
745
691
  };
692
+ /** @virtual */
746
693
  Env.prototype.enqueueFinalizer = function (finalizer) {
747
- var _this = this;
748
694
  if (this.pendingFinalizers.indexOf(finalizer) === -1) {
749
695
  this.pendingFinalizers.push(finalizer);
750
696
  }
751
- if (!this.finalizationScheduled && !this.destructing) {
752
- this.finalizationScheduled = true;
753
- this.ref();
754
- _setImmediate(function () {
755
- _this.finalizationScheduled = false;
756
- _this.unref();
757
- _this.drainFinalizerQueue();
758
- });
759
- }
760
697
  };
698
+ /** @virtual */
761
699
  Env.prototype.dequeueFinalizer = function (finalizer) {
762
700
  var index = this.pendingFinalizers.indexOf(finalizer);
763
701
  if (index !== -1) {
764
702
  this.pendingFinalizers.splice(index, 1);
765
703
  }
766
704
  };
767
- Env.prototype.drainFinalizerQueue = function () {
768
- while (this.pendingFinalizers.length > 0) {
769
- var refTracker = this.pendingFinalizers.shift();
770
- refTracker.finalize();
771
- }
772
- };
773
- Env.prototype.dispose = function () {
774
- if (this.id === 0)
775
- return;
776
- this.destructing = true;
777
- this.drainFinalizerQueue();
705
+ /** @virtual */
706
+ Env.prototype.deleteMe = function () {
778
707
  RefBase.finalizeAll(this.finalizing_reflist);
779
708
  RefBase.finalizeAll(this.reflist);
780
709
  this.tryCatch.extractException();
781
710
  this.ctx.envStore.remove(this.id);
711
+ };
712
+ Env.prototype.dispose = function () {
713
+ if (this.id === 0)
714
+ return;
715
+ this.deleteMe();
782
716
  this.id = 0;
783
717
  };
784
718
  Env.prototype.initObjectBinding = function (value) {
@@ -807,6 +741,108 @@
807
741
  };
808
742
  return Env;
809
743
  }());
744
+ var NodeEnv = /*#__PURE__*/ (function (_super) {
745
+ __extends(NodeEnv, _super);
746
+ function NodeEnv(ctx, filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort, nodeBinding) {
747
+ var _this = _super.call(this, ctx, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort) || this;
748
+ _this.filename = filename;
749
+ _this.nodeBinding = nodeBinding;
750
+ _this.destructing = false;
751
+ _this.finalizationScheduled = false;
752
+ return _this;
753
+ }
754
+ NodeEnv.prototype.deleteMe = function () {
755
+ this.destructing = true;
756
+ this.drainFinalizerQueue();
757
+ _super.prototype.deleteMe.call(this);
758
+ };
759
+ NodeEnv.prototype.canCallIntoJs = function () {
760
+ return this.ctx.canCallIntoJs();
761
+ };
762
+ NodeEnv.prototype.triggerFatalException = function (err) {
763
+ if (this.nodeBinding) {
764
+ this.nodeBinding.napi.fatalException(err);
765
+ }
766
+ else {
767
+ if (typeof process === 'object' && process !== null && typeof process._fatalException === 'function') {
768
+ var handled = process._fatalException(err);
769
+ if (!handled) {
770
+ console.error(err);
771
+ process.exit(1);
772
+ }
773
+ }
774
+ else {
775
+ throw err;
776
+ }
777
+ }
778
+ };
779
+ NodeEnv.prototype.callbackIntoModule = function (enforceUncaughtExceptionPolicy, fn) {
780
+ return this.callIntoModule(fn, function (envObject, err) {
781
+ if (envObject.terminatedOrTerminating()) {
782
+ return;
783
+ }
784
+ var hasProcess = typeof process === 'object' && process !== null;
785
+ var hasForceFlag = hasProcess ? Boolean(process.execArgv && (process.execArgv.indexOf('--force-node-api-uncaught-exceptions-policy') !== -1)) : false;
786
+ if (!hasForceFlag && !enforceUncaughtExceptionPolicy) {
787
+ if (hasProcess && typeof process.emitWarning === 'function') {
788
+ 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');
789
+ }
790
+ else {
791
+ throw err;
792
+ }
793
+ }
794
+ envObject.triggerFatalException(err);
795
+ });
796
+ };
797
+ NodeEnv.prototype.callFinalizer = function (cb, data, hint) {
798
+ this.callFinalizerInternal(1, cb, data, hint);
799
+ };
800
+ NodeEnv.prototype.callFinalizerInternal = function (forceUncaught, cb, data, hint) {
801
+ var f = this.makeDynCall_vppp(cb);
802
+ var env = this.id;
803
+ var scope = this.ctx.openScope(this);
804
+ try {
805
+ this.callbackIntoModule(Boolean(forceUncaught), function () { f(env, data, hint); });
806
+ }
807
+ finally {
808
+ this.ctx.closeScope(this, scope);
809
+ }
810
+ };
811
+ NodeEnv.prototype.enqueueFinalizer = function (finalizer) {
812
+ var _this = this;
813
+ _super.prototype.enqueueFinalizer.call(this, finalizer);
814
+ if (!this.finalizationScheduled && !this.destructing) {
815
+ this.finalizationScheduled = true;
816
+ this.ref();
817
+ _setImmediate(function () {
818
+ _this.finalizationScheduled = false;
819
+ _this.unref();
820
+ _this.drainFinalizerQueue();
821
+ });
822
+ }
823
+ };
824
+ NodeEnv.prototype.drainFinalizerQueue = function () {
825
+ while (this.pendingFinalizers.length > 0) {
826
+ var refTracker = this.pendingFinalizers.shift();
827
+ refTracker.finalize();
828
+ }
829
+ };
830
+ return NodeEnv;
831
+ }(Env));
832
+ function newEnv(ctx, filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort, nodeBinding) {
833
+ moduleApiVersion = typeof moduleApiVersion !== 'number' ? NODE_API_DEFAULT_MODULE_API_VERSION : moduleApiVersion;
834
+ // Validate module_api_version.
835
+ if (moduleApiVersion < NODE_API_DEFAULT_MODULE_API_VERSION) {
836
+ moduleApiVersion = NODE_API_DEFAULT_MODULE_API_VERSION;
837
+ }
838
+ else if (moduleApiVersion > NAPI_VERSION && moduleApiVersion !== NAPI_VERSION_EXPERIMENTAL) {
839
+ throwNodeApiVersionError(filename, moduleApiVersion);
840
+ }
841
+ var env = new NodeEnv(ctx, filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort, nodeBinding);
842
+ ctx.envStore.add(env);
843
+ ctx.addCleanupHook(env, function () { env.unref(); }, 0);
844
+ return env;
845
+ }
810
846
 
811
847
  var EmnapiError = /*#__PURE__*/ (function (_super) {
812
848
  __extends(EmnapiError, _super);
@@ -895,19 +931,19 @@
895
931
  return;
896
932
  var value = this._ref.deref();
897
933
  try {
898
- // try {
899
934
  Persistent._registry.register(value, this, this);
900
935
  var weakRef = new WeakRef(value);
901
936
  this._ref.dispose();
902
937
  this._ref = weakRef;
903
- // } catch (_) {
904
- // Persistent._registry.register(this._ref, this, this)
905
- // this._ref = new WeakRef<any>(this._ref)
906
- // }
907
938
  this._param = param;
908
939
  this._callback = callback;
909
940
  }
910
- catch (_) { }
941
+ catch (err) {
942
+ if (typeof value === 'symbol') ;
943
+ else {
944
+ throw err;
945
+ }
946
+ }
911
947
  };
912
948
  Persistent.prototype.clearWeak = function () {
913
949
  if (!supportFinalizer || this._ref === undefined)
@@ -969,6 +1005,9 @@
969
1005
  ref.persistent.reset();
970
1006
  ref.envObject.enqueueFinalizer(ref);
971
1007
  }
1008
+ function canBeHeldWeakly(value) {
1009
+ return value.isObject() || value.isFunction() || value.isSymbol();
1010
+ }
972
1011
  var Reference = /*#__PURE__*/ (function (_super) {
973
1012
  __extends(Reference, _super);
974
1013
  function Reference(envObject, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint) {
@@ -986,6 +1025,7 @@
986
1025
  var handle = envObject.ctx.handleStore.get(handle_id);
987
1026
  var ref = new Reference(envObject, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint);
988
1027
  envObject.ctx.refStore.add(ref);
1028
+ ref.canBeWeak = canBeHeldWeakly(handle);
989
1029
  ref.persistent = new Persistent(handle.value);
990
1030
  if (initialRefcount === 0) {
991
1031
  ref._setWeak();
@@ -997,11 +1037,8 @@
997
1037
  return 0;
998
1038
  }
999
1039
  var count = _super.prototype.ref.call(this);
1000
- if (count === 1) {
1001
- var obj = this.persistent.deref();
1002
- if (obj) {
1003
- this.persistent.clearWeak();
1004
- }
1040
+ if (count === 1 && this.canBeWeak) {
1041
+ this.persistent.clearWeak();
1005
1042
  }
1006
1043
  return count;
1007
1044
  };
@@ -1012,23 +1049,25 @@
1012
1049
  var oldRefcount = this.refCount();
1013
1050
  var refcount = _super.prototype.unref.call(this);
1014
1051
  if (oldRefcount === 1 && refcount === 0) {
1015
- var obj = this.persistent.deref();
1016
- if (obj) {
1017
- this._setWeak();
1018
- }
1052
+ this._setWeak();
1019
1053
  }
1020
1054
  return refcount;
1021
1055
  };
1022
1056
  Reference.prototype.get = function () {
1023
- var obj = this.persistent.deref();
1024
- if (obj) {
1025
- var handle = this.envObject.ensureHandle(obj);
1026
- return handle.id;
1057
+ if (this.persistent.isEmpty()) {
1058
+ return 0;
1027
1059
  }
1028
- return 0;
1060
+ var obj = this.persistent.deref();
1061
+ var handle = this.envObject.ensureHandle(obj);
1062
+ return handle.id;
1029
1063
  };
1030
1064
  Reference.prototype._setWeak = function () {
1031
- this.persistent.setWeak(this, weakCallback);
1065
+ if (this.canBeWeak) {
1066
+ this.persistent.setWeak(this, weakCallback);
1067
+ }
1068
+ else {
1069
+ this.persistent.reset();
1070
+ }
1032
1071
  };
1033
1072
  Reference.prototype.finalize = function () {
1034
1073
  this.persistent.reset();
@@ -1073,6 +1112,55 @@
1073
1112
  return Deferred;
1074
1113
  }());
1075
1114
 
1115
+ var Store = /*#__PURE__*/ (function () {
1116
+ function Store() {
1117
+ this._values = [undefined];
1118
+ this._values.length = 4;
1119
+ this._size = 1;
1120
+ this._freeList = [];
1121
+ }
1122
+ Store.prototype.add = function (value) {
1123
+ var id;
1124
+ if (this._freeList.length) {
1125
+ id = this._freeList.shift();
1126
+ }
1127
+ else {
1128
+ id = this._size;
1129
+ this._size++;
1130
+ var capacity = this._values.length;
1131
+ if (id >= capacity) {
1132
+ this._values.length = capacity + (capacity >> 1) + 16;
1133
+ }
1134
+ }
1135
+ value.id = id;
1136
+ this._values[id] = value;
1137
+ };
1138
+ Store.prototype.get = function (id) {
1139
+ return this._values[id];
1140
+ };
1141
+ Store.prototype.has = function (id) {
1142
+ return this._values[id] !== undefined;
1143
+ };
1144
+ Store.prototype.remove = function (id) {
1145
+ var value = this._values[id];
1146
+ if (value) {
1147
+ value.id = 0;
1148
+ this._values[id] = undefined;
1149
+ this._freeList.push(Number(id));
1150
+ }
1151
+ };
1152
+ Store.prototype.dispose = function () {
1153
+ for (var i = 1; i < this._size; ++i) {
1154
+ var value = this._values[i];
1155
+ value === null || value === void 0 ? void 0 : value.dispose();
1156
+ }
1157
+ this._values = [undefined];
1158
+ this._size = 1;
1159
+ this._freeList = [];
1160
+ };
1161
+ return Store;
1162
+ }());
1163
+
1076
1164
  var CleanupHookCallback = /*#__PURE__*/ (function () {
1077
1165
  function CleanupHookCallback(envObject, fn, arg, order) {
1078
1166
  this.envObject = envObject;
@@ -1149,10 +1237,12 @@
1149
1237
  var Context = /*#__PURE__*/ (function () {
1150
1238
  function Context() {
1151
1239
  var _this = this;
1152
- this.envStore = new EnvStore();
1240
+ this._isStopping = false;
1241
+ this._canCallIntoJs = true;
1242
+ this.envStore = new Store();
1153
1243
  this.scopeStore = new ScopeStore();
1154
- this.refStore = new RefStore();
1155
- this.deferredStore = new DeferredStore();
1244
+ this.refStore = new Store();
1245
+ this.deferredStore = new Store();
1156
1246
  this.handleStore = new HandleStore();
1157
1247
  this.cbinfoStack = new CallbackInfoStack();
1158
1248
  this.feature = {
@@ -1170,10 +1260,19 @@
1170
1260
  if (typeof process === 'object' && process !== null && typeof process.once === 'function') {
1171
1261
  this.refCounter = new NodejsWaitingRequestCounter();
1172
1262
  process.once('beforeExit', function () {
1173
- _this.runCleanup();
1263
+ _this.destroy();
1174
1264
  });
1175
1265
  }
1176
1266
  }
1267
+ // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
1268
+ Context.prototype.getRuntimeVersions = function () {
1269
+ return {
1270
+ version: version,
1271
+ NAPI_VERSION: NAPI_VERSION,
1272
+ NAPI_VERSION_EXPERIMENTAL: NAPI_VERSION_EXPERIMENTAL,
1273
+ NODE_API_DEFAULT_MODULE_API_VERSION: NODE_API_DEFAULT_MODULE_API_VERSION
1274
+ };
1275
+ };
1177
1276
  Context.prototype.createNotSupportWeakRefError = function (api, message) {
1178
1277
  return new NotSupportWeakRefError(api, message);
1179
1278
  };
@@ -1192,8 +1291,8 @@
1192
1291
  Context.prototype.createDeferred = function (value) {
1193
1292
  return Deferred.create(this, value);
1194
1293
  };
1195
- Context.prototype.createEnv = function (makeDynCall_vppp, makeDynCall_vp) {
1196
- return Env.create(this, makeDynCall_vppp, makeDynCall_vp);
1294
+ Context.prototype.createEnv = function (filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort, nodeBinding) {
1295
+ return newEnv(this, filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort, nodeBinding);
1197
1296
  };
1198
1297
  Context.prototype.getCurrentScope = function () {
1199
1298
  return this.scopeStore.currentScope;
@@ -1236,7 +1335,18 @@
1236
1335
  var _a;
1237
1336
  (_a = this.refCounter) === null || _a === void 0 ? void 0 : _a.decrease();
1238
1337
  };
1239
- Context.prototype.dispose = function () {
1338
+ Context.prototype.setCanCallIntoJs = function (value) {
1339
+ this._canCallIntoJs = value;
1340
+ };
1341
+ Context.prototype.setStopping = function (value) {
1342
+ this._isStopping = value;
1343
+ };
1344
+ Context.prototype.canCallIntoJs = function () {
1345
+ return this._canCallIntoJs && !this._isStopping;
1346
+ };
1347
+ Context.prototype.destroy = function () {
1348
+ this.setStopping(true);
1349
+ this.setCanCallIntoJs(false);
1240
1350
  this.runCleanup();
1241
1351
  };
1242
1352
  return Context;
@@ -1252,27 +1362,26 @@
1252
1362
  return defaultContext;
1253
1363
  }
1254
1364
 
1255
- var version = "0.38.3";
1256
-
1257
1365
  exports.CallbackInfo = CallbackInfo;
1258
1366
  exports.CallbackInfoStack = CallbackInfoStack;
1259
1367
  exports.ConstHandle = ConstHandle;
1260
1368
  exports.Context = Context;
1261
1369
  exports.Deferred = Deferred;
1262
- exports.DeferredStore = DeferredStore;
1263
1370
  exports.EmnapiError = EmnapiError;
1264
1371
  exports.Env = Env;
1265
- exports.EnvStore = EnvStore;
1266
1372
  exports.Finalizer = Finalizer;
1267
1373
  exports.Handle = Handle;
1268
1374
  exports.HandleScope = HandleScope;
1269
1375
  exports.HandleStore = HandleStore;
1376
+ exports.NAPI_VERSION = NAPI_VERSION;
1377
+ exports.NAPI_VERSION_EXPERIMENTAL = NAPI_VERSION_EXPERIMENTAL;
1378
+ exports.NODE_API_DEFAULT_MODULE_API_VERSION = NODE_API_DEFAULT_MODULE_API_VERSION;
1379
+ exports.NodeEnv = NodeEnv;
1270
1380
  exports.NotSupportBigIntError = NotSupportBigIntError;
1271
1381
  exports.NotSupportBufferError = NotSupportBufferError;
1272
1382
  exports.NotSupportWeakRefError = NotSupportWeakRefError;
1273
1383
  exports.Persistent = Persistent;
1274
1384
  exports.RefBase = RefBase;
1275
- exports.RefStore = RefStore;
1276
1385
  exports.RefTracker = RefTracker;
1277
1386
  exports.Reference = Reference;
1278
1387
  exports.ScopeStore = ScopeStore;