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