@emnapi/runtime 0.38.2 → 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.
@@ -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;
@@ -209,19 +154,6 @@ var emnapi = (function (exports) {
209
154
  function isReferenceType(v) {
210
155
  return (typeof v === 'object' && v !== null) || typeof v === 'function';
211
156
  }
212
- var _setImmediate = typeof setImmediate === 'function'
213
- ? setImmediate
214
- : function (f) {
215
- if (typeof f !== 'function')
216
- return;
217
- var channel = new MessageChannel();
218
- channel.port1.onmessage = function () {
219
- channel.port1.onmessage = null;
220
- channel = undefined;
221
- f();
222
- };
223
- channel.port2.postMessage(null);
224
- };
225
157
  var _require = /*#__PURE__*/ (function () {
226
158
  var nativeRequire;
227
159
  if (typeof __webpack_public_path__ !== 'undefined') {
@@ -236,86 +168,108 @@ var emnapi = (function (exports) {
236
168
  }
237
169
  return nativeRequire;
238
170
  })();
239
- var Buffer = /*#__PURE__*/ (function () {
240
- if (typeof _global.Buffer === 'function')
241
- return _global.Buffer;
242
- try {
243
- return _require('buffer').Buffer;
244
- }
245
- catch (_) { }
246
- return undefined;
247
- })();
171
+ var _MessageChannel = typeof MessageChannel === 'function'
172
+ ? MessageChannel
173
+ : /*#__PURE__*/ (function () {
174
+ try {
175
+ return _require('worker_threads').MessageChannel;
176
+ }
177
+ catch (_) { }
178
+ return undefined;
179
+ })();
180
+ var _setImmediate = typeof setImmediate === 'function'
181
+ ? setImmediate
182
+ : function (callback) {
183
+ if (typeof callback !== 'function') {
184
+ throw new TypeError('The "callback" argument must be of type function');
185
+ }
186
+ if (_MessageChannel) {
187
+ var channel_1 = new _MessageChannel();
188
+ channel_1.port1.onmessage = function () {
189
+ channel_1.port1.onmessage = null;
190
+ channel_1 = undefined;
191
+ callback();
192
+ };
193
+ channel_1.port2.postMessage(null);
194
+ }
195
+ else {
196
+ setTimeout(callback, 0);
197
+ }
198
+ };
199
+ var _Buffer = typeof Buffer === 'function'
200
+ ? Buffer
201
+ : /*#__PURE__*/ (function () {
202
+ try {
203
+ return _require('buffer').Buffer;
204
+ }
205
+ catch (_) { }
206
+ return undefined;
207
+ })();
208
+ var version = "0.39.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 */;
248
212
 
249
213
  var Handle = /*#__PURE__*/ (function () {
250
214
  function Handle(id, value) {
251
215
  this.id = id;
252
216
  this.value = value;
253
217
  }
254
- Handle.prototype.init = function (id, value) {
255
- this.id = id;
256
- this.value = value;
257
- };
258
218
  Handle.prototype.data = function (envObject) {
259
219
  return envObject.getObjectBinding(this.value).data;
260
220
  };
261
- Handle.prototype.isEmpty = function () {
262
- return this.id === 0;
263
- };
264
221
  Handle.prototype.isNumber = function () {
265
- return !this.isEmpty() && typeof this.value === 'number';
222
+ return typeof this.value === 'number';
266
223
  };
267
224
  Handle.prototype.isBigInt = function () {
268
- return !this.isEmpty() && typeof this.value === 'bigint';
225
+ return typeof this.value === 'bigint';
269
226
  };
270
227
  Handle.prototype.isString = function () {
271
- return !this.isEmpty() && typeof this.value === 'string';
228
+ return typeof this.value === 'string';
272
229
  };
273
230
  Handle.prototype.isFunction = function () {
274
- return !this.isEmpty() && typeof this.value === 'function';
231
+ return typeof this.value === 'function';
275
232
  };
276
233
  Handle.prototype.isExternal = function () {
277
- return !this.isEmpty() && (isReferenceType(this.value) && Object.getPrototypeOf(this.value) === null);
234
+ return (isReferenceType(this.value) && Object.getPrototypeOf(this.value) === null);
278
235
  };
279
236
  Handle.prototype.isObject = function () {
280
- return !this.isEmpty() && typeof this.value === 'object' && this.value !== null;
237
+ return typeof this.value === 'object' && this.value !== null;
281
238
  };
282
239
  Handle.prototype.isArray = function () {
283
- return !this.isEmpty() && Array.isArray(this.value);
240
+ return Array.isArray(this.value);
284
241
  };
285
242
  Handle.prototype.isArrayBuffer = function () {
286
- return !this.isEmpty() && (this.value instanceof ArrayBuffer);
243
+ return (this.value instanceof ArrayBuffer);
287
244
  };
288
245
  Handle.prototype.isTypedArray = function () {
289
- return !this.isEmpty() && (ArrayBuffer.isView(this.value)) && !(this.value instanceof DataView);
246
+ return (ArrayBuffer.isView(this.value)) && !(this.value instanceof DataView);
290
247
  };
291
248
  Handle.prototype.isBuffer = function () {
292
- return !this.isEmpty() && typeof Buffer === 'function' && Buffer.isBuffer(this.value);
249
+ return typeof _Buffer === 'function' && _Buffer.isBuffer(this.value);
293
250
  };
294
251
  Handle.prototype.isDataView = function () {
295
- return !this.isEmpty() && (this.value instanceof DataView);
252
+ return (this.value instanceof DataView);
296
253
  };
297
254
  Handle.prototype.isDate = function () {
298
- return !this.isEmpty() && (this.value instanceof Date);
255
+ return (this.value instanceof Date);
299
256
  };
300
257
  Handle.prototype.isPromise = function () {
301
- return !this.isEmpty() && (this.value instanceof Promise);
258
+ return (this.value instanceof Promise);
302
259
  };
303
260
  Handle.prototype.isBoolean = function () {
304
- return !this.isEmpty() && typeof this.value === 'boolean';
261
+ return typeof this.value === 'boolean';
305
262
  };
306
263
  Handle.prototype.isUndefined = function () {
307
- return !this.isEmpty() && this.value === undefined;
264
+ return this.value === undefined;
308
265
  };
309
266
  Handle.prototype.isSymbol = function () {
310
- return !this.isEmpty() && typeof this.value === 'symbol';
267
+ return typeof this.value === 'symbol';
311
268
  };
312
269
  Handle.prototype.isNull = function () {
313
- return !this.isEmpty() && this.value === null;
270
+ return this.value === null;
314
271
  };
315
272
  Handle.prototype.dispose = function () {
316
- if (this.id === 0)
317
- return;
318
- this.id = 0;
319
273
  this.value = undefined;
320
274
  };
321
275
  return Handle;
@@ -350,7 +304,7 @@ var emnapi = (function (exports) {
350
304
  var values = this._values;
351
305
  if (next < values.length) {
352
306
  h = values[next];
353
- h.init(next, value);
307
+ h.value = value;
354
308
  }
355
309
  else {
356
310
  h = new Handle(next, value);
@@ -496,22 +450,6 @@ var emnapi = (function (exports) {
496
450
  return ScopeStore;
497
451
  }());
498
452
 
499
- var RefStore = /*#__PURE__*/ (function (_super) {
500
- __extends(RefStore, _super);
501
- function RefStore() {
502
- return _super.call(this, 8) || this;
503
- }
504
- return RefStore;
505
- }(Store));
506
-
507
- var DeferredStore = /*#__PURE__*/ (function (_super) {
508
- __extends(DeferredStore, _super);
509
- function DeferredStore() {
510
- return _super.call(this, 8) || this;
511
- }
512
- return DeferredStore;
513
- }(Store));
514
-
515
453
  var RefTracker = /*#__PURE__*/ (function () {
516
454
  function RefTracker() {
517
455
  this._next = null;
@@ -643,19 +581,29 @@ var emnapi = (function (exports) {
643
581
  return RefBase;
644
582
  }(Finalizer));
645
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
+ }
646
594
  var Env = /*#__PURE__*/ (function () {
647
- function Env(ctx, makeDynCall_vppp, makeDynCall_vp) {
595
+ function Env(ctx, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort) {
648
596
  this.ctx = ctx;
597
+ this.moduleApiVersion = moduleApiVersion;
649
598
  this.makeDynCall_vppp = makeDynCall_vppp;
650
599
  this.makeDynCall_vp = makeDynCall_vp;
600
+ this.abort = abort;
651
601
  this.openHandleScopes = 0;
652
602
  this.instanceData = null;
653
603
  this.tryCatch = new TryCatch();
654
604
  this.refs = 1;
655
605
  this.reflist = new RefTracker();
656
606
  this.finalizing_reflist = new RefTracker();
657
- this.destructing = false;
658
- this.finalizationScheduled = false;
659
607
  this.pendingFinalizers = [];
660
608
  this.lastError = {
661
609
  errorCode: 0 /* napi_status.napi_ok */,
@@ -665,11 +613,12 @@ var emnapi = (function (exports) {
665
613
  this._bindingMap = new WeakMap();
666
614
  this.id = 0;
667
615
  }
668
- Env.create = function (ctx, makeDynCall_vppp, makeDynCall_vp) {
669
- var env = new Env(ctx, makeDynCall_vppp, makeDynCall_vp);
670
- ctx.envStore.add(env);
671
- ctx.addCleanupHook(env, function () { env.unref(); }, 0);
672
- return env;
616
+ /** @virtual */
617
+ Env.prototype.canCallIntoJs = function () {
618
+ return true;
619
+ };
620
+ Env.prototype.terminatedOrTerminating = function () {
621
+ return !this.canCallIntoJs();
673
622
  };
674
623
  Env.prototype.ref = function () {
675
624
  this.refs++;
@@ -688,32 +637,44 @@ var emnapi = (function (exports) {
688
637
  };
689
638
  Env.prototype.clearLastError = function () {
690
639
  var lastError = this.lastError;
691
- lastError.errorCode = 0 /* napi_status.napi_ok */;
692
- lastError.engineErrorCode = 0;
693
- 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;
694
646
  return 0 /* napi_status.napi_ok */;
695
647
  };
696
648
  Env.prototype.setLastError = function (error_code, engine_error_code, engine_reserved) {
697
649
  if (engine_error_code === void 0) { engine_error_code = 0; }
698
650
  if (engine_reserved === void 0) { engine_reserved = 0; }
699
651
  var lastError = this.lastError;
700
- lastError.errorCode = error_code;
701
- lastError.engineErrorCode = engine_error_code;
702
- 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;
703
658
  return error_code;
704
659
  };
705
660
  Env.prototype.getReturnStatus = function () {
706
661
  return !this.tryCatch.hasCaught() ? 0 /* napi_status.napi_ok */ : this.setLastError(10 /* napi_status.napi_pending_exception */);
707
662
  };
708
- Env.prototype.callIntoModule = function (fn) {
663
+ Env.prototype.callIntoModule = function (fn, handleException) {
664
+ if (handleException === void 0) { handleException = handleThrow; }
665
+ var openHandleScopesBefore = this.openHandleScopes;
709
666
  this.clearLastError();
710
667
  var r = fn(this);
668
+ if (openHandleScopesBefore !== this.openHandleScopes) {
669
+ this.abort('open_handle_scopes != open_handle_scopes_before');
670
+ }
711
671
  if (this.tryCatch.hasCaught()) {
712
672
  var err = this.tryCatch.extractException();
713
- throw err;
673
+ handleException(this, err);
714
674
  }
715
675
  return r;
716
676
  };
677
+ /** @virtual */
717
678
  Env.prototype.callFinalizer = function (cb, data, hint) {
718
679
  var f = this.makeDynCall_vppp(cb);
719
680
  var env = this.id;
@@ -725,42 +686,30 @@ var emnapi = (function (exports) {
725
686
  this.ctx.closeScope(this, scope);
726
687
  }
727
688
  };
689
+ /** @virtual */
728
690
  Env.prototype.enqueueFinalizer = function (finalizer) {
729
- var _this = this;
730
691
  if (this.pendingFinalizers.indexOf(finalizer) === -1) {
731
692
  this.pendingFinalizers.push(finalizer);
732
693
  }
733
- if (!this.finalizationScheduled && !this.destructing) {
734
- this.finalizationScheduled = true;
735
- this.ref();
736
- _setImmediate(function () {
737
- _this.finalizationScheduled = false;
738
- _this.unref();
739
- _this.drainFinalizerQueue();
740
- });
741
- }
742
694
  };
695
+ /** @virtual */
743
696
  Env.prototype.dequeueFinalizer = function (finalizer) {
744
697
  var index = this.pendingFinalizers.indexOf(finalizer);
745
698
  if (index !== -1) {
746
699
  this.pendingFinalizers.splice(index, 1);
747
700
  }
748
701
  };
749
- Env.prototype.drainFinalizerQueue = function () {
750
- while (this.pendingFinalizers.length > 0) {
751
- var refTracker = this.pendingFinalizers.shift();
752
- refTracker.finalize();
753
- }
754
- };
755
- Env.prototype.dispose = function () {
756
- if (this.id === 0)
757
- return;
758
- this.destructing = true;
759
- this.drainFinalizerQueue();
702
+ /** @virtual */
703
+ Env.prototype.deleteMe = function () {
760
704
  RefBase.finalizeAll(this.finalizing_reflist);
761
705
  RefBase.finalizeAll(this.reflist);
762
706
  this.tryCatch.extractException();
763
707
  this.ctx.envStore.remove(this.id);
708
+ };
709
+ Env.prototype.dispose = function () {
710
+ if (this.id === 0)
711
+ return;
712
+ this.deleteMe();
764
713
  this.id = 0;
765
714
  };
766
715
  Env.prototype.initObjectBinding = function (value) {
@@ -789,6 +738,108 @@ var emnapi = (function (exports) {
789
738
  };
790
739
  return Env;
791
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
+ }
792
843
 
793
844
  var EmnapiError = /*#__PURE__*/ (function (_super) {
794
845
  __extends(EmnapiError, _super);
@@ -877,19 +928,19 @@ var emnapi = (function (exports) {
877
928
  return;
878
929
  var value = this._ref.deref();
879
930
  try {
880
- // try {
881
931
  Persistent._registry.register(value, this, this);
882
932
  var weakRef = new WeakRef(value);
883
933
  this._ref.dispose();
884
934
  this._ref = weakRef;
885
- // } catch (_) {
886
- // Persistent._registry.register(this._ref, this, this)
887
- // this._ref = new WeakRef<any>(this._ref)
888
- // }
889
935
  this._param = param;
890
936
  this._callback = callback;
891
937
  }
892
- catch (_) { }
938
+ catch (err) {
939
+ if (typeof value === 'symbol') ;
940
+ else {
941
+ throw err;
942
+ }
943
+ }
893
944
  };
894
945
  Persistent.prototype.clearWeak = function () {
895
946
  if (!supportFinalizer || this._ref === undefined)
@@ -951,6 +1002,9 @@ var emnapi = (function (exports) {
951
1002
  ref.persistent.reset();
952
1003
  ref.envObject.enqueueFinalizer(ref);
953
1004
  }
1005
+ function canBeHeldWeakly(value) {
1006
+ return value.isObject() || value.isFunction() || value.isSymbol();
1007
+ }
954
1008
  var Reference = /*#__PURE__*/ (function (_super) {
955
1009
  __extends(Reference, _super);
956
1010
  function Reference(envObject, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint) {
@@ -968,6 +1022,7 @@ var emnapi = (function (exports) {
968
1022
  var handle = envObject.ctx.handleStore.get(handle_id);
969
1023
  var ref = new Reference(envObject, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint);
970
1024
  envObject.ctx.refStore.add(ref);
1025
+ ref.canBeWeak = canBeHeldWeakly(handle);
971
1026
  ref.persistent = new Persistent(handle.value);
972
1027
  if (initialRefcount === 0) {
973
1028
  ref._setWeak();
@@ -979,11 +1034,8 @@ var emnapi = (function (exports) {
979
1034
  return 0;
980
1035
  }
981
1036
  var count = _super.prototype.ref.call(this);
982
- if (count === 1) {
983
- var obj = this.persistent.deref();
984
- if (obj) {
985
- this.persistent.clearWeak();
986
- }
1037
+ if (count === 1 && this.canBeWeak) {
1038
+ this.persistent.clearWeak();
987
1039
  }
988
1040
  return count;
989
1041
  };
@@ -994,23 +1046,25 @@ var emnapi = (function (exports) {
994
1046
  var oldRefcount = this.refCount();
995
1047
  var refcount = _super.prototype.unref.call(this);
996
1048
  if (oldRefcount === 1 && refcount === 0) {
997
- var obj = this.persistent.deref();
998
- if (obj) {
999
- this._setWeak();
1000
- }
1049
+ this._setWeak();
1001
1050
  }
1002
1051
  return refcount;
1003
1052
  };
1004
1053
  Reference.prototype.get = function () {
1005
- var obj = this.persistent.deref();
1006
- if (obj) {
1007
- var handle = this.envObject.ensureHandle(obj);
1008
- return handle.id;
1054
+ if (this.persistent.isEmpty()) {
1055
+ return 0;
1009
1056
  }
1010
- return 0;
1057
+ var obj = this.persistent.deref();
1058
+ var handle = this.envObject.ensureHandle(obj);
1059
+ return handle.id;
1011
1060
  };
1012
1061
  Reference.prototype._setWeak = function () {
1013
- this.persistent.setWeak(this, weakCallback);
1062
+ if (this.canBeWeak) {
1063
+ this.persistent.setWeak(this, weakCallback);
1064
+ }
1065
+ else {
1066
+ this.persistent.reset();
1067
+ }
1014
1068
  };
1015
1069
  Reference.prototype.finalize = function () {
1016
1070
  this.persistent.reset();
@@ -1055,6 +1109,55 @@ var emnapi = (function (exports) {
1055
1109
  return Deferred;
1056
1110
  }());
1057
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
+
1058
1161
  var CleanupHookCallback = /*#__PURE__*/ (function () {
1059
1162
  function CleanupHookCallback(envObject, fn, arg, order) {
1060
1163
  this.envObject = envObject;
@@ -1109,7 +1212,7 @@ var emnapi = (function (exports) {
1109
1212
  }());
1110
1213
  var NodejsWaitingRequestCounter = /*#__PURE__*/ (function () {
1111
1214
  function NodejsWaitingRequestCounter() {
1112
- this.refHandle = new MessageChannel().port1;
1215
+ this.refHandle = new _MessageChannel().port1;
1113
1216
  this.count = 0;
1114
1217
  }
1115
1218
  NodejsWaitingRequestCounter.prototype.increase = function () {
@@ -1131,10 +1234,12 @@ var emnapi = (function (exports) {
1131
1234
  var Context = /*#__PURE__*/ (function () {
1132
1235
  function Context() {
1133
1236
  var _this = this;
1134
- this.envStore = new EnvStore();
1237
+ this._isStopping = false;
1238
+ this._canCallIntoJs = true;
1239
+ this.envStore = new Store();
1135
1240
  this.scopeStore = new ScopeStore();
1136
- this.refStore = new RefStore();
1137
- this.deferredStore = new DeferredStore();
1241
+ this.refStore = new Store();
1242
+ this.deferredStore = new Store();
1138
1243
  this.handleStore = new HandleStore();
1139
1244
  this.cbinfoStack = new CallbackInfoStack();
1140
1245
  this.feature = {
@@ -1145,16 +1250,26 @@ var emnapi = (function (exports) {
1145
1250
  supportNewFunction: supportNewFunction,
1146
1251
  canSetFunctionName: canSetFunctionName,
1147
1252
  setImmediate: _setImmediate,
1148
- Buffer: Buffer
1253
+ Buffer: _Buffer,
1254
+ MessageChannel: _MessageChannel
1149
1255
  };
1150
1256
  this.cleanupQueue = new CleanupQueue();
1151
1257
  if (typeof process === 'object' && process !== null && typeof process.once === 'function') {
1152
1258
  this.refCounter = new NodejsWaitingRequestCounter();
1153
1259
  process.once('beforeExit', function () {
1154
- _this.runCleanup();
1260
+ _this.destroy();
1155
1261
  });
1156
1262
  }
1157
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
+ };
1158
1273
  Context.prototype.createNotSupportWeakRefError = function (api, message) {
1159
1274
  return new NotSupportWeakRefError(api, message);
1160
1275
  };
@@ -1173,8 +1288,8 @@ var emnapi = (function (exports) {
1173
1288
  Context.prototype.createDeferred = function (value) {
1174
1289
  return Deferred.create(this, value);
1175
1290
  };
1176
- Context.prototype.createEnv = function (makeDynCall_vppp, makeDynCall_vp) {
1177
- 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);
1178
1293
  };
1179
1294
  Context.prototype.getCurrentScope = function () {
1180
1295
  return this.scopeStore.currentScope;
@@ -1217,7 +1332,18 @@ var emnapi = (function (exports) {
1217
1332
  var _a;
1218
1333
  (_a = this.refCounter) === null || _a === void 0 ? void 0 : _a.decrease();
1219
1334
  };
1220
- 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);
1221
1347
  this.runCleanup();
1222
1348
  };
1223
1349
  return Context;
@@ -1233,27 +1359,26 @@ var emnapi = (function (exports) {
1233
1359
  return defaultContext;
1234
1360
  }
1235
1361
 
1236
- var version = "0.38.2";
1237
-
1238
1362
  exports.CallbackInfo = CallbackInfo;
1239
1363
  exports.CallbackInfoStack = CallbackInfoStack;
1240
1364
  exports.ConstHandle = ConstHandle;
1241
1365
  exports.Context = Context;
1242
1366
  exports.Deferred = Deferred;
1243
- exports.DeferredStore = DeferredStore;
1244
1367
  exports.EmnapiError = EmnapiError;
1245
1368
  exports.Env = Env;
1246
- exports.EnvStore = EnvStore;
1247
1369
  exports.Finalizer = Finalizer;
1248
1370
  exports.Handle = Handle;
1249
1371
  exports.HandleScope = HandleScope;
1250
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;
1251
1377
  exports.NotSupportBigIntError = NotSupportBigIntError;
1252
1378
  exports.NotSupportBufferError = NotSupportBufferError;
1253
1379
  exports.NotSupportWeakRefError = NotSupportWeakRefError;
1254
1380
  exports.Persistent = Persistent;
1255
1381
  exports.RefBase = RefBase;
1256
- exports.RefStore = RefStore;
1257
1382
  exports.RefTracker = RefTracker;
1258
1383
  exports.Reference = Reference;
1259
1384
  exports.ScopeStore = ScopeStore;