@emnapi/runtime 0.38.3 → 0.40.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/emnapi.mjs CHANGED
@@ -33,60 +33,6 @@ class CallbackInfoStack {
33
33
  }
34
34
  }
35
35
 
36
- class Store {
37
- constructor(capacity) {
38
- this._values = [undefined];
39
- this._values.length = capacity;
40
- this._size = 1;
41
- this._freeList = [];
42
- }
43
- add(value) {
44
- let id;
45
- if (this._freeList.length) {
46
- id = this._freeList.shift();
47
- }
48
- else {
49
- id = this._size;
50
- this._size++;
51
- const capacity = this._values.length;
52
- if (id >= capacity) {
53
- this._values.length = Math.ceil(capacity * 1.5);
54
- }
55
- }
56
- value.id = id;
57
- this._values[id] = value;
58
- }
59
- get(id) {
60
- return this._values[id];
61
- }
62
- has(id) {
63
- return this._values[id] !== undefined;
64
- }
65
- remove(id) {
66
- const value = this._values[id];
67
- if (value) {
68
- value.id = 0;
69
- this._values[id] = undefined;
70
- this._freeList.push(Number(id));
71
- }
72
- }
73
- dispose() {
74
- for (let i = 1; i < this._size; ++i) {
75
- const value = this._values[i];
76
- value === null || value === void 0 ? void 0 : value.dispose();
77
- }
78
- this._values = [undefined];
79
- this._size = 1;
80
- this._freeList = [];
81
- }
82
- }
83
-
84
- class EnvStore extends Store {
85
- constructor() {
86
- super(2);
87
- }
88
- }
89
-
90
36
  const supportNewFunction = (function () {
91
37
  let f;
92
38
  try {
@@ -119,22 +65,24 @@ const _global = (function () {
119
65
  }
120
66
  return g;
121
67
  })();
122
- const emptyException = new Error();
123
68
  class TryCatch {
124
69
  constructor() {
125
- this._exception = emptyException;
70
+ this._exception = undefined;
71
+ this._caught = false;
126
72
  }
127
73
  hasCaught() {
128
- return this._exception !== emptyException;
74
+ return this._caught;
129
75
  }
130
76
  exception() {
131
77
  return this._exception;
132
78
  }
133
79
  setError(err) {
80
+ this._caught = true;
134
81
  this._exception = err;
135
82
  }
136
83
  reset() {
137
- this._exception = emptyException;
84
+ this._caught = false;
85
+ this._exception = undefined;
138
86
  }
139
87
  extractException() {
140
88
  const e = this._exception;
@@ -219,77 +167,71 @@ const _Buffer = typeof Buffer === 'function'
219
167
  catch (_) { }
220
168
  return undefined;
221
169
  })();
170
+ const version = "0.40.0";
171
+ const NAPI_VERSION = 9;
172
+ const NAPI_VERSION_EXPERIMENTAL = 2147483647;
173
+ const NODE_API_DEFAULT_MODULE_API_VERSION = 8;
222
174
 
223
175
  class Handle {
224
176
  constructor(id, value) {
225
177
  this.id = id;
226
178
  this.value = value;
227
179
  }
228
- init(id, value) {
229
- this.id = id;
230
- this.value = value;
231
- }
232
180
  data(envObject) {
233
181
  return envObject.getObjectBinding(this.value).data;
234
182
  }
235
- isEmpty() {
236
- return this.id === 0;
237
- }
238
183
  isNumber() {
239
- return !this.isEmpty() && typeof this.value === 'number';
184
+ return typeof this.value === 'number';
240
185
  }
241
186
  isBigInt() {
242
- return !this.isEmpty() && typeof this.value === 'bigint';
187
+ return typeof this.value === 'bigint';
243
188
  }
244
189
  isString() {
245
- return !this.isEmpty() && typeof this.value === 'string';
190
+ return typeof this.value === 'string';
246
191
  }
247
192
  isFunction() {
248
- return !this.isEmpty() && typeof this.value === 'function';
193
+ return typeof this.value === 'function';
249
194
  }
250
195
  isExternal() {
251
- return !this.isEmpty() && (isReferenceType(this.value) && Object.getPrototypeOf(this.value) === null);
196
+ return (isReferenceType(this.value) && Object.getPrototypeOf(this.value) === null);
252
197
  }
253
198
  isObject() {
254
- return !this.isEmpty() && typeof this.value === 'object' && this.value !== null;
199
+ return typeof this.value === 'object' && this.value !== null;
255
200
  }
256
201
  isArray() {
257
- return !this.isEmpty() && Array.isArray(this.value);
202
+ return Array.isArray(this.value);
258
203
  }
259
204
  isArrayBuffer() {
260
- return !this.isEmpty() && (this.value instanceof ArrayBuffer);
205
+ return (this.value instanceof ArrayBuffer);
261
206
  }
262
207
  isTypedArray() {
263
- return !this.isEmpty() && (ArrayBuffer.isView(this.value)) && !(this.value instanceof DataView);
208
+ return (ArrayBuffer.isView(this.value)) && !(this.value instanceof DataView);
264
209
  }
265
210
  isBuffer() {
266
- return !this.isEmpty() && typeof _Buffer === 'function' && _Buffer.isBuffer(this.value);
211
+ return typeof _Buffer === 'function' && _Buffer.isBuffer(this.value);
267
212
  }
268
213
  isDataView() {
269
- return !this.isEmpty() && (this.value instanceof DataView);
214
+ return (this.value instanceof DataView);
270
215
  }
271
216
  isDate() {
272
- return !this.isEmpty() && (this.value instanceof Date);
217
+ return (this.value instanceof Date);
273
218
  }
274
219
  isPromise() {
275
- return !this.isEmpty() && (this.value instanceof Promise);
220
+ return (this.value instanceof Promise);
276
221
  }
277
222
  isBoolean() {
278
- return !this.isEmpty() && typeof this.value === 'boolean';
223
+ return typeof this.value === 'boolean';
279
224
  }
280
225
  isUndefined() {
281
- return !this.isEmpty() && this.value === undefined;
226
+ return this.value === undefined;
282
227
  }
283
228
  isSymbol() {
284
- return !this.isEmpty() && typeof this.value === 'symbol';
229
+ return typeof this.value === 'symbol';
285
230
  }
286
231
  isNull() {
287
- return !this.isEmpty() && this.value === null;
232
+ return this.value === null;
288
233
  }
289
234
  dispose() {
290
- if (this.id === 0)
291
- return;
292
- this.id = 0;
293
235
  this.value = undefined;
294
236
  }
295
237
  }
@@ -321,7 +263,7 @@ class HandleStore {
321
263
  const values = this._values;
322
264
  if (next < values.length) {
323
265
  h = values[next];
324
- h.init(next, value);
266
+ h.value = value;
325
267
  }
326
268
  else {
327
269
  h = new Handle(next, value);
@@ -463,18 +405,6 @@ class ScopeStore {
463
405
  }
464
406
  }
465
407
 
466
- class RefStore extends Store {
467
- constructor() {
468
- super(8);
469
- }
470
- }
471
-
472
- class DeferredStore extends Store {
473
- constructor() {
474
- super(8);
475
- }
476
- }
477
-
478
408
  class RefTracker {
479
409
  constructor() {
480
410
  this._next = null;
@@ -527,14 +457,6 @@ class Finalizer {
527
457
  }
528
458
 
529
459
  class RefBase extends Finalizer {
530
- constructor(envObject, initial_refcount, ownership, finalize_callback, finalize_data, finalize_hint) {
531
- super(envObject, finalize_callback, finalize_data, finalize_hint);
532
- this._next = null;
533
- this._prev = null;
534
- this._refcount = initial_refcount;
535
- this._ownership = ownership;
536
- this.link(!finalize_callback ? envObject.reflist : envObject.finalizing_reflist);
537
- }
538
460
  static finalizeAll(list) {
539
461
  RefTracker.finalizeAll(list);
540
462
  }
@@ -544,6 +466,14 @@ class RefBase extends Finalizer {
544
466
  unlink() {
545
467
  RefTracker.prototype.unlink.call(this);
546
468
  }
469
+ constructor(envObject, initial_refcount, ownership, finalize_callback, finalize_data, finalize_hint) {
470
+ super(envObject, finalize_callback, finalize_data, finalize_hint);
471
+ this._next = null;
472
+ this._prev = null;
473
+ this._refcount = initial_refcount;
474
+ this._ownership = ownership;
475
+ this.link(!finalize_callback ? envObject.reflist : envObject.finalizing_reflist);
476
+ }
547
477
  dispose() {
548
478
  this.unlink();
549
479
  this.envObject.dequeueFinalizer(this);
@@ -595,19 +525,29 @@ class RefBase extends Finalizer {
595
525
  }
596
526
  }
597
527
 
528
+ function throwNodeApiVersionError(moduleName, moduleApiVersion) {
529
+ const errorMessage = `${moduleName} requires Node-API version ${moduleApiVersion}, but this version of Node.js only supports version ${NAPI_VERSION} add-ons.`;
530
+ throw new Error(errorMessage);
531
+ }
532
+ function handleThrow(envObject, value) {
533
+ if (envObject.terminatedOrTerminating()) {
534
+ return;
535
+ }
536
+ throw value;
537
+ }
598
538
  class Env {
599
- constructor(ctx, makeDynCall_vppp, makeDynCall_vp) {
539
+ constructor(ctx, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort) {
600
540
  this.ctx = ctx;
541
+ this.moduleApiVersion = moduleApiVersion;
601
542
  this.makeDynCall_vppp = makeDynCall_vppp;
602
543
  this.makeDynCall_vp = makeDynCall_vp;
544
+ this.abort = abort;
603
545
  this.openHandleScopes = 0;
604
546
  this.instanceData = null;
605
547
  this.tryCatch = new TryCatch();
606
548
  this.refs = 1;
607
549
  this.reflist = new RefTracker();
608
550
  this.finalizing_reflist = new RefTracker();
609
- this.destructing = false;
610
- this.finalizationScheduled = false;
611
551
  this.pendingFinalizers = [];
612
552
  this.lastError = {
613
553
  errorCode: 0,
@@ -617,11 +557,11 @@ class Env {
617
557
  this._bindingMap = new WeakMap();
618
558
  this.id = 0;
619
559
  }
620
- static create(ctx, makeDynCall_vppp, makeDynCall_vp) {
621
- const env = new Env(ctx, makeDynCall_vppp, makeDynCall_vp);
622
- ctx.envStore.add(env);
623
- ctx.addCleanupHook(env, () => { env.unref(); }, 0);
624
- return env;
560
+ canCallIntoJs() {
561
+ return true;
562
+ }
563
+ terminatedOrTerminating() {
564
+ return !this.canCallIntoJs();
625
565
  }
626
566
  ref() {
627
567
  this.refs++;
@@ -640,27 +580,37 @@ class Env {
640
580
  }
641
581
  clearLastError() {
642
582
  const lastError = this.lastError;
643
- lastError.errorCode = 0;
644
- lastError.engineErrorCode = 0;
645
- lastError.engineReserved = 0;
583
+ if (lastError.errorCode !== 0)
584
+ lastError.errorCode = 0;
585
+ if (lastError.engineErrorCode !== 0)
586
+ lastError.engineErrorCode = 0;
587
+ if (lastError.engineReserved !== 0)
588
+ lastError.engineReserved = 0;
646
589
  return 0;
647
590
  }
648
591
  setLastError(error_code, engine_error_code = 0, engine_reserved = 0) {
649
592
  const lastError = this.lastError;
650
- lastError.errorCode = error_code;
651
- lastError.engineErrorCode = engine_error_code;
652
- lastError.engineReserved = engine_reserved;
593
+ if (lastError.errorCode !== error_code)
594
+ lastError.errorCode = error_code;
595
+ if (lastError.engineErrorCode !== engine_error_code)
596
+ lastError.engineErrorCode = engine_error_code;
597
+ if (lastError.engineReserved !== engine_reserved)
598
+ lastError.engineReserved = engine_reserved;
653
599
  return error_code;
654
600
  }
655
601
  getReturnStatus() {
656
602
  return !this.tryCatch.hasCaught() ? 0 : this.setLastError(10);
657
603
  }
658
- callIntoModule(fn) {
604
+ callIntoModule(fn, handleException = handleThrow) {
605
+ const openHandleScopesBefore = this.openHandleScopes;
659
606
  this.clearLastError();
660
607
  const r = fn(this);
608
+ if (openHandleScopesBefore !== this.openHandleScopes) {
609
+ this.abort('open_handle_scopes != open_handle_scopes_before');
610
+ }
661
611
  if (this.tryCatch.hasCaught()) {
662
612
  const err = this.tryCatch.extractException();
663
- throw err;
613
+ handleException(this, err);
664
614
  }
665
615
  return r;
666
616
  }
@@ -679,15 +629,6 @@ class Env {
679
629
  if (this.pendingFinalizers.indexOf(finalizer) === -1) {
680
630
  this.pendingFinalizers.push(finalizer);
681
631
  }
682
- if (!this.finalizationScheduled && !this.destructing) {
683
- this.finalizationScheduled = true;
684
- this.ref();
685
- _setImmediate(() => {
686
- this.finalizationScheduled = false;
687
- this.unref();
688
- this.drainFinalizerQueue();
689
- });
690
- }
691
632
  }
692
633
  dequeueFinalizer(finalizer) {
693
634
  const index = this.pendingFinalizers.indexOf(finalizer);
@@ -695,21 +636,16 @@ class Env {
695
636
  this.pendingFinalizers.splice(index, 1);
696
637
  }
697
638
  }
698
- drainFinalizerQueue() {
699
- while (this.pendingFinalizers.length > 0) {
700
- const refTracker = this.pendingFinalizers.shift();
701
- refTracker.finalize();
702
- }
703
- }
704
- dispose() {
705
- if (this.id === 0)
706
- return;
707
- this.destructing = true;
708
- this.drainFinalizerQueue();
639
+ deleteMe() {
709
640
  RefBase.finalizeAll(this.finalizing_reflist);
710
641
  RefBase.finalizeAll(this.reflist);
711
642
  this.tryCatch.extractException();
712
643
  this.ctx.envStore.remove(this.id);
644
+ }
645
+ dispose() {
646
+ if (this.id === 0)
647
+ return;
648
+ this.deleteMe();
713
649
  this.id = 0;
714
650
  }
715
651
  initObjectBinding(value) {
@@ -737,6 +673,103 @@ class Env {
737
673
  return this.instanceData ? this.instanceData.data() : 0;
738
674
  }
739
675
  }
676
+ class NodeEnv extends Env {
677
+ constructor(ctx, filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort, nodeBinding) {
678
+ super(ctx, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort);
679
+ this.filename = filename;
680
+ this.nodeBinding = nodeBinding;
681
+ this.destructing = false;
682
+ this.finalizationScheduled = false;
683
+ }
684
+ deleteMe() {
685
+ this.destructing = true;
686
+ this.drainFinalizerQueue();
687
+ super.deleteMe();
688
+ }
689
+ canCallIntoJs() {
690
+ return this.ctx.canCallIntoJs();
691
+ }
692
+ triggerFatalException(err) {
693
+ if (this.nodeBinding) {
694
+ this.nodeBinding.napi.fatalException(err);
695
+ }
696
+ else {
697
+ if (typeof process === 'object' && process !== null && typeof process._fatalException === 'function') {
698
+ const handled = process._fatalException(err);
699
+ if (!handled) {
700
+ console.error(err);
701
+ process.exit(1);
702
+ }
703
+ }
704
+ else {
705
+ throw err;
706
+ }
707
+ }
708
+ }
709
+ callbackIntoModule(enforceUncaughtExceptionPolicy, fn) {
710
+ return this.callIntoModule(fn, (envObject, err) => {
711
+ if (envObject.terminatedOrTerminating()) {
712
+ return;
713
+ }
714
+ const hasProcess = typeof process === 'object' && process !== null;
715
+ const hasForceFlag = hasProcess ? Boolean(process.execArgv && (process.execArgv.indexOf('--force-node-api-uncaught-exceptions-policy') !== -1)) : false;
716
+ if (!hasForceFlag && !enforceUncaughtExceptionPolicy) {
717
+ if (hasProcess && typeof process.emitWarning === 'function') {
718
+ 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');
719
+ }
720
+ else {
721
+ throw err;
722
+ }
723
+ }
724
+ envObject.triggerFatalException(err);
725
+ });
726
+ }
727
+ callFinalizer(cb, data, hint) {
728
+ this.callFinalizerInternal(1, cb, data, hint);
729
+ }
730
+ callFinalizerInternal(forceUncaught, cb, data, hint) {
731
+ const f = this.makeDynCall_vppp(cb);
732
+ const env = this.id;
733
+ const scope = this.ctx.openScope(this);
734
+ try {
735
+ this.callbackIntoModule(Boolean(forceUncaught), () => { f(env, data, hint); });
736
+ }
737
+ finally {
738
+ this.ctx.closeScope(this, scope);
739
+ }
740
+ }
741
+ enqueueFinalizer(finalizer) {
742
+ super.enqueueFinalizer(finalizer);
743
+ if (!this.finalizationScheduled && !this.destructing) {
744
+ this.finalizationScheduled = true;
745
+ this.ref();
746
+ _setImmediate(() => {
747
+ this.finalizationScheduled = false;
748
+ this.unref();
749
+ this.drainFinalizerQueue();
750
+ });
751
+ }
752
+ }
753
+ drainFinalizerQueue() {
754
+ while (this.pendingFinalizers.length > 0) {
755
+ const refTracker = this.pendingFinalizers.shift();
756
+ refTracker.finalize();
757
+ }
758
+ }
759
+ }
760
+ function newEnv(ctx, filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort, nodeBinding) {
761
+ moduleApiVersion = typeof moduleApiVersion !== 'number' ? NODE_API_DEFAULT_MODULE_API_VERSION : moduleApiVersion;
762
+ if (moduleApiVersion < NODE_API_DEFAULT_MODULE_API_VERSION) {
763
+ moduleApiVersion = NODE_API_DEFAULT_MODULE_API_VERSION;
764
+ }
765
+ else if (moduleApiVersion > NAPI_VERSION && moduleApiVersion !== NAPI_VERSION_EXPERIMENTAL) {
766
+ throwNodeApiVersionError(filename, moduleApiVersion);
767
+ }
768
+ const env = new NodeEnv(ctx, filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort, nodeBinding);
769
+ ctx.envStore.add(env);
770
+ ctx.addCleanupHook(env, () => { env.unref(); }, 0);
771
+ return env;
772
+ }
740
773
 
741
774
  class EmnapiError extends Error {
742
775
  constructor(message) {
@@ -820,7 +853,12 @@ class Persistent {
820
853
  this._param = param;
821
854
  this._callback = callback;
822
855
  }
823
- catch (_) { }
856
+ catch (err) {
857
+ if (typeof value === 'symbol') ;
858
+ else {
859
+ throw err;
860
+ }
861
+ }
824
862
  }
825
863
  clearWeak() {
826
864
  if (!supportFinalizer || this._ref === undefined)
@@ -881,31 +919,32 @@ function weakCallback(ref) {
881
919
  ref.persistent.reset();
882
920
  ref.envObject.enqueueFinalizer(ref);
883
921
  }
922
+ function canBeHeldWeakly(value) {
923
+ return value.isObject() || value.isFunction() || value.isSymbol();
924
+ }
884
925
  class Reference extends RefBase {
885
- constructor(envObject, initialRefcount, ownership, finalize_callback = 0, finalize_data = 0, finalize_hint = 0) {
886
- super(envObject, initialRefcount >>> 0, ownership, finalize_callback, finalize_data, finalize_hint);
887
- this.id = 0;
888
- }
889
926
  static create(envObject, handle_id, initialRefcount, ownership, finalize_callback = 0, finalize_data = 0, finalize_hint = 0) {
890
927
  const handle = envObject.ctx.handleStore.get(handle_id);
891
928
  const ref = new Reference(envObject, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint);
892
929
  envObject.ctx.refStore.add(ref);
930
+ ref.canBeWeak = canBeHeldWeakly(handle);
893
931
  ref.persistent = new Persistent(handle.value);
894
932
  if (initialRefcount === 0) {
895
933
  ref._setWeak();
896
934
  }
897
935
  return ref;
898
936
  }
937
+ constructor(envObject, initialRefcount, ownership, finalize_callback = 0, finalize_data = 0, finalize_hint = 0) {
938
+ super(envObject, initialRefcount >>> 0, ownership, finalize_callback, finalize_data, finalize_hint);
939
+ this.id = 0;
940
+ }
899
941
  ref() {
900
942
  if (this.persistent.isEmpty()) {
901
943
  return 0;
902
944
  }
903
945
  const count = super.ref();
904
- if (count === 1) {
905
- const obj = this.persistent.deref();
906
- if (obj) {
907
- this.persistent.clearWeak();
908
- }
946
+ if (count === 1 && this.canBeWeak) {
947
+ this.persistent.clearWeak();
909
948
  }
910
949
  return count;
911
950
  }
@@ -916,23 +955,25 @@ class Reference extends RefBase {
916
955
  const oldRefcount = this.refCount();
917
956
  const refcount = super.unref();
918
957
  if (oldRefcount === 1 && refcount === 0) {
919
- const obj = this.persistent.deref();
920
- if (obj) {
921
- this._setWeak();
922
- }
958
+ this._setWeak();
923
959
  }
924
960
  return refcount;
925
961
  }
926
962
  get() {
927
- const obj = this.persistent.deref();
928
- if (obj) {
929
- const handle = this.envObject.ensureHandle(obj);
930
- return handle.id;
963
+ if (this.persistent.isEmpty()) {
964
+ return 0;
931
965
  }
932
- return 0;
966
+ const obj = this.persistent.deref();
967
+ const handle = this.envObject.ensureHandle(obj);
968
+ return handle.id;
933
969
  }
934
970
  _setWeak() {
935
- this.persistent.setWeak(this, weakCallback);
971
+ if (this.canBeWeak) {
972
+ this.persistent.setWeak(this, weakCallback);
973
+ }
974
+ else {
975
+ this.persistent.reset();
976
+ }
936
977
  }
937
978
  finalize() {
938
979
  this.persistent.reset();
@@ -949,16 +990,16 @@ class Reference extends RefBase {
949
990
  }
950
991
 
951
992
  class Deferred {
952
- constructor(ctx, value) {
953
- this.id = 0;
954
- this.ctx = ctx;
955
- this.value = value;
956
- }
957
993
  static create(ctx, value) {
958
994
  const deferred = new Deferred(ctx, value);
959
995
  ctx.deferredStore.add(deferred);
960
996
  return deferred;
961
997
  }
998
+ constructor(ctx, value) {
999
+ this.id = 0;
1000
+ this.ctx = ctx;
1001
+ this.value = value;
1002
+ }
962
1003
  resolve(value) {
963
1004
  this.value.resolve(value);
964
1005
  this.dispose();
@@ -975,6 +1016,54 @@ class Deferred {
975
1016
  }
976
1017
  }
977
1018
 
1019
+ class Store {
1020
+ constructor() {
1021
+ this._values = [undefined];
1022
+ this._values.length = 4;
1023
+ this._size = 1;
1024
+ this._freeList = [];
1025
+ }
1026
+ add(value) {
1027
+ let id;
1028
+ if (this._freeList.length) {
1029
+ id = this._freeList.shift();
1030
+ }
1031
+ else {
1032
+ id = this._size;
1033
+ this._size++;
1034
+ const capacity = this._values.length;
1035
+ if (id >= capacity) {
1036
+ this._values.length = capacity + (capacity >> 1) + 16;
1037
+ }
1038
+ }
1039
+ value.id = id;
1040
+ this._values[id] = value;
1041
+ }
1042
+ get(id) {
1043
+ return this._values[id];
1044
+ }
1045
+ has(id) {
1046
+ return this._values[id] !== undefined;
1047
+ }
1048
+ remove(id) {
1049
+ const value = this._values[id];
1050
+ if (value) {
1051
+ value.id = 0;
1052
+ this._values[id] = undefined;
1053
+ this._freeList.push(Number(id));
1054
+ }
1055
+ }
1056
+ dispose() {
1057
+ for (let i = 1; i < this._size; ++i) {
1058
+ const value = this._values[i];
1059
+ value === null || value === void 0 ? void 0 : value.dispose();
1060
+ }
1061
+ this._values = [undefined];
1062
+ this._size = 1;
1063
+ this._freeList = [];
1064
+ }
1065
+ }
1066
+
978
1067
  class CleanupHookCallback {
979
1068
  constructor(envObject, fn, arg, order) {
980
1069
  this.envObject = envObject;
@@ -1047,10 +1136,12 @@ class NodejsWaitingRequestCounter {
1047
1136
  }
1048
1137
  class Context {
1049
1138
  constructor() {
1050
- this.envStore = new EnvStore();
1139
+ this._isStopping = false;
1140
+ this._canCallIntoJs = true;
1141
+ this.envStore = new Store();
1051
1142
  this.scopeStore = new ScopeStore();
1052
- this.refStore = new RefStore();
1053
- this.deferredStore = new DeferredStore();
1143
+ this.refStore = new Store();
1144
+ this.deferredStore = new Store();
1054
1145
  this.handleStore = new HandleStore();
1055
1146
  this.cbinfoStack = new CallbackInfoStack();
1056
1147
  this.feature = {
@@ -1068,10 +1159,18 @@ class Context {
1068
1159
  if (typeof process === 'object' && process !== null && typeof process.once === 'function') {
1069
1160
  this.refCounter = new NodejsWaitingRequestCounter();
1070
1161
  process.once('beforeExit', () => {
1071
- this.runCleanup();
1162
+ this.destroy();
1072
1163
  });
1073
1164
  }
1074
1165
  }
1166
+ getRuntimeVersions() {
1167
+ return {
1168
+ version,
1169
+ NAPI_VERSION,
1170
+ NAPI_VERSION_EXPERIMENTAL,
1171
+ NODE_API_DEFAULT_MODULE_API_VERSION
1172
+ };
1173
+ }
1075
1174
  createNotSupportWeakRefError(api, message) {
1076
1175
  return new NotSupportWeakRefError(api, message);
1077
1176
  }
@@ -1087,8 +1186,8 @@ class Context {
1087
1186
  createDeferred(value) {
1088
1187
  return Deferred.create(this, value);
1089
1188
  }
1090
- createEnv(makeDynCall_vppp, makeDynCall_vp) {
1091
- return Env.create(this, makeDynCall_vppp, makeDynCall_vp);
1189
+ createEnv(filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort, nodeBinding) {
1190
+ return newEnv(this, filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort, nodeBinding);
1092
1191
  }
1093
1192
  getCurrentScope() {
1094
1193
  return this.scopeStore.currentScope;
@@ -1131,7 +1230,18 @@ class Context {
1131
1230
  var _a;
1132
1231
  (_a = this.refCounter) === null || _a === void 0 ? void 0 : _a.decrease();
1133
1232
  }
1134
- dispose() {
1233
+ setCanCallIntoJs(value) {
1234
+ this._canCallIntoJs = value;
1235
+ }
1236
+ setStopping(value) {
1237
+ this._isStopping = value;
1238
+ }
1239
+ canCallIntoJs() {
1240
+ return this._canCallIntoJs && !this._isStopping;
1241
+ }
1242
+ destroy() {
1243
+ this.setStopping(true);
1244
+ this.setCanCallIntoJs(false);
1135
1245
  this.runCleanup();
1136
1246
  }
1137
1247
  }
@@ -1146,6 +1256,4 @@ function getDefaultContext() {
1146
1256
  return defaultContext;
1147
1257
  }
1148
1258
 
1149
- const version = "0.38.3";
1150
-
1151
- 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 };
1259
+ 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 };