@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.
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;
@@ -212,19 +157,6 @@
212
157
  function isReferenceType(v) {
213
158
  return (typeof v === 'object' && v !== null) || typeof v === 'function';
214
159
  }
215
- var _setImmediate = typeof setImmediate === 'function'
216
- ? setImmediate
217
- : function (f) {
218
- if (typeof f !== 'function')
219
- return;
220
- var channel = new MessageChannel();
221
- channel.port1.onmessage = function () {
222
- channel.port1.onmessage = null;
223
- channel = undefined;
224
- f();
225
- };
226
- channel.port2.postMessage(null);
227
- };
228
160
  var _require = /*#__PURE__*/ (function () {
229
161
  var nativeRequire;
230
162
  if (typeof __webpack_public_path__ !== 'undefined') {
@@ -239,86 +171,108 @@
239
171
  }
240
172
  return nativeRequire;
241
173
  })();
242
- var Buffer = /*#__PURE__*/ (function () {
243
- if (typeof _global.Buffer === 'function')
244
- return _global.Buffer;
245
- try {
246
- return _require('buffer').Buffer;
247
- }
248
- catch (_) { }
249
- return undefined;
250
- })();
174
+ var _MessageChannel = typeof MessageChannel === 'function'
175
+ ? MessageChannel
176
+ : /*#__PURE__*/ (function () {
177
+ try {
178
+ return _require('worker_threads').MessageChannel;
179
+ }
180
+ catch (_) { }
181
+ return undefined;
182
+ })();
183
+ var _setImmediate = typeof setImmediate === 'function'
184
+ ? setImmediate
185
+ : function (callback) {
186
+ if (typeof callback !== 'function') {
187
+ throw new TypeError('The "callback" argument must be of type function');
188
+ }
189
+ if (_MessageChannel) {
190
+ var channel_1 = new _MessageChannel();
191
+ channel_1.port1.onmessage = function () {
192
+ channel_1.port1.onmessage = null;
193
+ channel_1 = undefined;
194
+ callback();
195
+ };
196
+ channel_1.port2.postMessage(null);
197
+ }
198
+ else {
199
+ setTimeout(callback, 0);
200
+ }
201
+ };
202
+ var _Buffer = typeof Buffer === 'function'
203
+ ? Buffer
204
+ : /*#__PURE__*/ (function () {
205
+ try {
206
+ return _require('buffer').Buffer;
207
+ }
208
+ catch (_) { }
209
+ return undefined;
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 */;
251
215
 
252
216
  var Handle = /*#__PURE__*/ (function () {
253
217
  function Handle(id, value) {
254
218
  this.id = id;
255
219
  this.value = value;
256
220
  }
257
- Handle.prototype.init = function (id, value) {
258
- this.id = id;
259
- this.value = value;
260
- };
261
221
  Handle.prototype.data = function (envObject) {
262
222
  return envObject.getObjectBinding(this.value).data;
263
223
  };
264
- Handle.prototype.isEmpty = function () {
265
- return this.id === 0;
266
- };
267
224
  Handle.prototype.isNumber = function () {
268
- return !this.isEmpty() && typeof this.value === 'number';
225
+ return typeof this.value === 'number';
269
226
  };
270
227
  Handle.prototype.isBigInt = function () {
271
- return !this.isEmpty() && typeof this.value === 'bigint';
228
+ return typeof this.value === 'bigint';
272
229
  };
273
230
  Handle.prototype.isString = function () {
274
- return !this.isEmpty() && typeof this.value === 'string';
231
+ return typeof this.value === 'string';
275
232
  };
276
233
  Handle.prototype.isFunction = function () {
277
- return !this.isEmpty() && typeof this.value === 'function';
234
+ return typeof this.value === 'function';
278
235
  };
279
236
  Handle.prototype.isExternal = function () {
280
- return !this.isEmpty() && (isReferenceType(this.value) && Object.getPrototypeOf(this.value) === null);
237
+ return (isReferenceType(this.value) && Object.getPrototypeOf(this.value) === null);
281
238
  };
282
239
  Handle.prototype.isObject = function () {
283
- return !this.isEmpty() && typeof this.value === 'object' && this.value !== null;
240
+ return typeof this.value === 'object' && this.value !== null;
284
241
  };
285
242
  Handle.prototype.isArray = function () {
286
- return !this.isEmpty() && Array.isArray(this.value);
243
+ return Array.isArray(this.value);
287
244
  };
288
245
  Handle.prototype.isArrayBuffer = function () {
289
- return !this.isEmpty() && (this.value instanceof ArrayBuffer);
246
+ return (this.value instanceof ArrayBuffer);
290
247
  };
291
248
  Handle.prototype.isTypedArray = function () {
292
- return !this.isEmpty() && (ArrayBuffer.isView(this.value)) && !(this.value instanceof DataView);
249
+ return (ArrayBuffer.isView(this.value)) && !(this.value instanceof DataView);
293
250
  };
294
251
  Handle.prototype.isBuffer = function () {
295
- return !this.isEmpty() && typeof Buffer === 'function' && Buffer.isBuffer(this.value);
252
+ return typeof _Buffer === 'function' && _Buffer.isBuffer(this.value);
296
253
  };
297
254
  Handle.prototype.isDataView = function () {
298
- return !this.isEmpty() && (this.value instanceof DataView);
255
+ return (this.value instanceof DataView);
299
256
  };
300
257
  Handle.prototype.isDate = function () {
301
- return !this.isEmpty() && (this.value instanceof Date);
258
+ return (this.value instanceof Date);
302
259
  };
303
260
  Handle.prototype.isPromise = function () {
304
- return !this.isEmpty() && (this.value instanceof Promise);
261
+ return (this.value instanceof Promise);
305
262
  };
306
263
  Handle.prototype.isBoolean = function () {
307
- return !this.isEmpty() && typeof this.value === 'boolean';
264
+ return typeof this.value === 'boolean';
308
265
  };
309
266
  Handle.prototype.isUndefined = function () {
310
- return !this.isEmpty() && this.value === undefined;
267
+ return this.value === undefined;
311
268
  };
312
269
  Handle.prototype.isSymbol = function () {
313
- return !this.isEmpty() && typeof this.value === 'symbol';
270
+ return typeof this.value === 'symbol';
314
271
  };
315
272
  Handle.prototype.isNull = function () {
316
- return !this.isEmpty() && this.value === null;
273
+ return this.value === null;
317
274
  };
318
275
  Handle.prototype.dispose = function () {
319
- if (this.id === 0)
320
- return;
321
- this.id = 0;
322
276
  this.value = undefined;
323
277
  };
324
278
  return Handle;
@@ -353,7 +307,7 @@
353
307
  var values = this._values;
354
308
  if (next < values.length) {
355
309
  h = values[next];
356
- h.init(next, value);
310
+ h.value = value;
357
311
  }
358
312
  else {
359
313
  h = new Handle(next, value);
@@ -499,22 +453,6 @@
499
453
  return ScopeStore;
500
454
  }());
501
455
 
502
- var RefStore = /*#__PURE__*/ (function (_super) {
503
- __extends(RefStore, _super);
504
- function RefStore() {
505
- return _super.call(this, 8) || this;
506
- }
507
- return RefStore;
508
- }(Store));
509
-
510
- var DeferredStore = /*#__PURE__*/ (function (_super) {
511
- __extends(DeferredStore, _super);
512
- function DeferredStore() {
513
- return _super.call(this, 8) || this;
514
- }
515
- return DeferredStore;
516
- }(Store));
517
-
518
456
  var RefTracker = /*#__PURE__*/ (function () {
519
457
  function RefTracker() {
520
458
  this._next = null;
@@ -646,19 +584,29 @@
646
584
  return RefBase;
647
585
  }(Finalizer));
648
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
+ }
649
597
  var Env = /*#__PURE__*/ (function () {
650
- function Env(ctx, makeDynCall_vppp, makeDynCall_vp) {
598
+ function Env(ctx, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort) {
651
599
  this.ctx = ctx;
600
+ this.moduleApiVersion = moduleApiVersion;
652
601
  this.makeDynCall_vppp = makeDynCall_vppp;
653
602
  this.makeDynCall_vp = makeDynCall_vp;
603
+ this.abort = abort;
654
604
  this.openHandleScopes = 0;
655
605
  this.instanceData = null;
656
606
  this.tryCatch = new TryCatch();
657
607
  this.refs = 1;
658
608
  this.reflist = new RefTracker();
659
609
  this.finalizing_reflist = new RefTracker();
660
- this.destructing = false;
661
- this.finalizationScheduled = false;
662
610
  this.pendingFinalizers = [];
663
611
  this.lastError = {
664
612
  errorCode: 0 /* napi_status.napi_ok */,
@@ -668,11 +616,12 @@
668
616
  this._bindingMap = new WeakMap();
669
617
  this.id = 0;
670
618
  }
671
- Env.create = function (ctx, makeDynCall_vppp, makeDynCall_vp) {
672
- var env = new Env(ctx, makeDynCall_vppp, makeDynCall_vp);
673
- ctx.envStore.add(env);
674
- ctx.addCleanupHook(env, function () { env.unref(); }, 0);
675
- return env;
619
+ /** @virtual */
620
+ Env.prototype.canCallIntoJs = function () {
621
+ return true;
622
+ };
623
+ Env.prototype.terminatedOrTerminating = function () {
624
+ return !this.canCallIntoJs();
676
625
  };
677
626
  Env.prototype.ref = function () {
678
627
  this.refs++;
@@ -691,32 +640,44 @@
691
640
  };
692
641
  Env.prototype.clearLastError = function () {
693
642
  var lastError = this.lastError;
694
- lastError.errorCode = 0 /* napi_status.napi_ok */;
695
- lastError.engineErrorCode = 0;
696
- 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;
697
649
  return 0 /* napi_status.napi_ok */;
698
650
  };
699
651
  Env.prototype.setLastError = function (error_code, engine_error_code, engine_reserved) {
700
652
  if (engine_error_code === void 0) { engine_error_code = 0; }
701
653
  if (engine_reserved === void 0) { engine_reserved = 0; }
702
654
  var lastError = this.lastError;
703
- lastError.errorCode = error_code;
704
- lastError.engineErrorCode = engine_error_code;
705
- 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;
706
661
  return error_code;
707
662
  };
708
663
  Env.prototype.getReturnStatus = function () {
709
664
  return !this.tryCatch.hasCaught() ? 0 /* napi_status.napi_ok */ : this.setLastError(10 /* napi_status.napi_pending_exception */);
710
665
  };
711
- Env.prototype.callIntoModule = function (fn) {
666
+ Env.prototype.callIntoModule = function (fn, handleException) {
667
+ if (handleException === void 0) { handleException = handleThrow; }
668
+ var openHandleScopesBefore = this.openHandleScopes;
712
669
  this.clearLastError();
713
670
  var r = fn(this);
671
+ if (openHandleScopesBefore !== this.openHandleScopes) {
672
+ this.abort('open_handle_scopes != open_handle_scopes_before');
673
+ }
714
674
  if (this.tryCatch.hasCaught()) {
715
675
  var err = this.tryCatch.extractException();
716
- throw err;
676
+ handleException(this, err);
717
677
  }
718
678
  return r;
719
679
  };
680
+ /** @virtual */
720
681
  Env.prototype.callFinalizer = function (cb, data, hint) {
721
682
  var f = this.makeDynCall_vppp(cb);
722
683
  var env = this.id;
@@ -728,42 +689,30 @@
728
689
  this.ctx.closeScope(this, scope);
729
690
  }
730
691
  };
692
+ /** @virtual */
731
693
  Env.prototype.enqueueFinalizer = function (finalizer) {
732
- var _this = this;
733
694
  if (this.pendingFinalizers.indexOf(finalizer) === -1) {
734
695
  this.pendingFinalizers.push(finalizer);
735
696
  }
736
- if (!this.finalizationScheduled && !this.destructing) {
737
- this.finalizationScheduled = true;
738
- this.ref();
739
- _setImmediate(function () {
740
- _this.finalizationScheduled = false;
741
- _this.unref();
742
- _this.drainFinalizerQueue();
743
- });
744
- }
745
697
  };
698
+ /** @virtual */
746
699
  Env.prototype.dequeueFinalizer = function (finalizer) {
747
700
  var index = this.pendingFinalizers.indexOf(finalizer);
748
701
  if (index !== -1) {
749
702
  this.pendingFinalizers.splice(index, 1);
750
703
  }
751
704
  };
752
- Env.prototype.drainFinalizerQueue = function () {
753
- while (this.pendingFinalizers.length > 0) {
754
- var refTracker = this.pendingFinalizers.shift();
755
- refTracker.finalize();
756
- }
757
- };
758
- Env.prototype.dispose = function () {
759
- if (this.id === 0)
760
- return;
761
- this.destructing = true;
762
- this.drainFinalizerQueue();
705
+ /** @virtual */
706
+ Env.prototype.deleteMe = function () {
763
707
  RefBase.finalizeAll(this.finalizing_reflist);
764
708
  RefBase.finalizeAll(this.reflist);
765
709
  this.tryCatch.extractException();
766
710
  this.ctx.envStore.remove(this.id);
711
+ };
712
+ Env.prototype.dispose = function () {
713
+ if (this.id === 0)
714
+ return;
715
+ this.deleteMe();
767
716
  this.id = 0;
768
717
  };
769
718
  Env.prototype.initObjectBinding = function (value) {
@@ -792,6 +741,108 @@
792
741
  };
793
742
  return Env;
794
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
+ }
795
846
 
796
847
  var EmnapiError = /*#__PURE__*/ (function (_super) {
797
848
  __extends(EmnapiError, _super);
@@ -880,19 +931,19 @@
880
931
  return;
881
932
  var value = this._ref.deref();
882
933
  try {
883
- // try {
884
934
  Persistent._registry.register(value, this, this);
885
935
  var weakRef = new WeakRef(value);
886
936
  this._ref.dispose();
887
937
  this._ref = weakRef;
888
- // } catch (_) {
889
- // Persistent._registry.register(this._ref, this, this)
890
- // this._ref = new WeakRef<any>(this._ref)
891
- // }
892
938
  this._param = param;
893
939
  this._callback = callback;
894
940
  }
895
- catch (_) { }
941
+ catch (err) {
942
+ if (typeof value === 'symbol') ;
943
+ else {
944
+ throw err;
945
+ }
946
+ }
896
947
  };
897
948
  Persistent.prototype.clearWeak = function () {
898
949
  if (!supportFinalizer || this._ref === undefined)
@@ -954,6 +1005,9 @@
954
1005
  ref.persistent.reset();
955
1006
  ref.envObject.enqueueFinalizer(ref);
956
1007
  }
1008
+ function canBeHeldWeakly(value) {
1009
+ return value.isObject() || value.isFunction() || value.isSymbol();
1010
+ }
957
1011
  var Reference = /*#__PURE__*/ (function (_super) {
958
1012
  __extends(Reference, _super);
959
1013
  function Reference(envObject, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint) {
@@ -971,6 +1025,7 @@
971
1025
  var handle = envObject.ctx.handleStore.get(handle_id);
972
1026
  var ref = new Reference(envObject, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint);
973
1027
  envObject.ctx.refStore.add(ref);
1028
+ ref.canBeWeak = canBeHeldWeakly(handle);
974
1029
  ref.persistent = new Persistent(handle.value);
975
1030
  if (initialRefcount === 0) {
976
1031
  ref._setWeak();
@@ -982,11 +1037,8 @@
982
1037
  return 0;
983
1038
  }
984
1039
  var count = _super.prototype.ref.call(this);
985
- if (count === 1) {
986
- var obj = this.persistent.deref();
987
- if (obj) {
988
- this.persistent.clearWeak();
989
- }
1040
+ if (count === 1 && this.canBeWeak) {
1041
+ this.persistent.clearWeak();
990
1042
  }
991
1043
  return count;
992
1044
  };
@@ -997,23 +1049,25 @@
997
1049
  var oldRefcount = this.refCount();
998
1050
  var refcount = _super.prototype.unref.call(this);
999
1051
  if (oldRefcount === 1 && refcount === 0) {
1000
- var obj = this.persistent.deref();
1001
- if (obj) {
1002
- this._setWeak();
1003
- }
1052
+ this._setWeak();
1004
1053
  }
1005
1054
  return refcount;
1006
1055
  };
1007
1056
  Reference.prototype.get = function () {
1008
- var obj = this.persistent.deref();
1009
- if (obj) {
1010
- var handle = this.envObject.ensureHandle(obj);
1011
- return handle.id;
1057
+ if (this.persistent.isEmpty()) {
1058
+ return 0;
1012
1059
  }
1013
- return 0;
1060
+ var obj = this.persistent.deref();
1061
+ var handle = this.envObject.ensureHandle(obj);
1062
+ return handle.id;
1014
1063
  };
1015
1064
  Reference.prototype._setWeak = function () {
1016
- this.persistent.setWeak(this, weakCallback);
1065
+ if (this.canBeWeak) {
1066
+ this.persistent.setWeak(this, weakCallback);
1067
+ }
1068
+ else {
1069
+ this.persistent.reset();
1070
+ }
1017
1071
  };
1018
1072
  Reference.prototype.finalize = function () {
1019
1073
  this.persistent.reset();
@@ -1058,6 +1112,55 @@
1058
1112
  return Deferred;
1059
1113
  }());
1060
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
+
1061
1164
  var CleanupHookCallback = /*#__PURE__*/ (function () {
1062
1165
  function CleanupHookCallback(envObject, fn, arg, order) {
1063
1166
  this.envObject = envObject;
@@ -1112,7 +1215,7 @@
1112
1215
  }());
1113
1216
  var NodejsWaitingRequestCounter = /*#__PURE__*/ (function () {
1114
1217
  function NodejsWaitingRequestCounter() {
1115
- this.refHandle = new MessageChannel().port1;
1218
+ this.refHandle = new _MessageChannel().port1;
1116
1219
  this.count = 0;
1117
1220
  }
1118
1221
  NodejsWaitingRequestCounter.prototype.increase = function () {
@@ -1134,10 +1237,12 @@
1134
1237
  var Context = /*#__PURE__*/ (function () {
1135
1238
  function Context() {
1136
1239
  var _this = this;
1137
- this.envStore = new EnvStore();
1240
+ this._isStopping = false;
1241
+ this._canCallIntoJs = true;
1242
+ this.envStore = new Store();
1138
1243
  this.scopeStore = new ScopeStore();
1139
- this.refStore = new RefStore();
1140
- this.deferredStore = new DeferredStore();
1244
+ this.refStore = new Store();
1245
+ this.deferredStore = new Store();
1141
1246
  this.handleStore = new HandleStore();
1142
1247
  this.cbinfoStack = new CallbackInfoStack();
1143
1248
  this.feature = {
@@ -1148,16 +1253,26 @@
1148
1253
  supportNewFunction: supportNewFunction,
1149
1254
  canSetFunctionName: canSetFunctionName,
1150
1255
  setImmediate: _setImmediate,
1151
- Buffer: Buffer
1256
+ Buffer: _Buffer,
1257
+ MessageChannel: _MessageChannel
1152
1258
  };
1153
1259
  this.cleanupQueue = new CleanupQueue();
1154
1260
  if (typeof process === 'object' && process !== null && typeof process.once === 'function') {
1155
1261
  this.refCounter = new NodejsWaitingRequestCounter();
1156
1262
  process.once('beforeExit', function () {
1157
- _this.runCleanup();
1263
+ _this.destroy();
1158
1264
  });
1159
1265
  }
1160
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
+ };
1161
1276
  Context.prototype.createNotSupportWeakRefError = function (api, message) {
1162
1277
  return new NotSupportWeakRefError(api, message);
1163
1278
  };
@@ -1176,8 +1291,8 @@
1176
1291
  Context.prototype.createDeferred = function (value) {
1177
1292
  return Deferred.create(this, value);
1178
1293
  };
1179
- Context.prototype.createEnv = function (makeDynCall_vppp, makeDynCall_vp) {
1180
- 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);
1181
1296
  };
1182
1297
  Context.prototype.getCurrentScope = function () {
1183
1298
  return this.scopeStore.currentScope;
@@ -1220,7 +1335,18 @@
1220
1335
  var _a;
1221
1336
  (_a = this.refCounter) === null || _a === void 0 ? void 0 : _a.decrease();
1222
1337
  };
1223
- 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);
1224
1350
  this.runCleanup();
1225
1351
  };
1226
1352
  return Context;
@@ -1236,27 +1362,26 @@
1236
1362
  return defaultContext;
1237
1363
  }
1238
1364
 
1239
- var version = "0.38.2";
1240
-
1241
1365
  exports.CallbackInfo = CallbackInfo;
1242
1366
  exports.CallbackInfoStack = CallbackInfoStack;
1243
1367
  exports.ConstHandle = ConstHandle;
1244
1368
  exports.Context = Context;
1245
1369
  exports.Deferred = Deferred;
1246
- exports.DeferredStore = DeferredStore;
1247
1370
  exports.EmnapiError = EmnapiError;
1248
1371
  exports.Env = Env;
1249
- exports.EnvStore = EnvStore;
1250
1372
  exports.Finalizer = Finalizer;
1251
1373
  exports.Handle = Handle;
1252
1374
  exports.HandleScope = HandleScope;
1253
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;
1254
1380
  exports.NotSupportBigIntError = NotSupportBigIntError;
1255
1381
  exports.NotSupportBufferError = NotSupportBufferError;
1256
1382
  exports.NotSupportWeakRefError = NotSupportWeakRefError;
1257
1383
  exports.Persistent = Persistent;
1258
1384
  exports.RefBase = RefBase;
1259
- exports.RefStore = RefStore;
1260
1385
  exports.RefTracker = RefTracker;
1261
1386
  exports.Reference = Reference;
1262
1387
  exports.ScopeStore = ScopeStore;