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