@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.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;
@@ -168,19 +116,6 @@ const supportBigInt = typeof BigInt !== 'undefined';
168
116
  function isReferenceType(v) {
169
117
  return (typeof v === 'object' && v !== null) || typeof v === 'function';
170
118
  }
171
- const _setImmediate = typeof setImmediate === 'function'
172
- ? setImmediate
173
- : function (f) {
174
- if (typeof f !== 'function')
175
- return;
176
- let channel = new MessageChannel();
177
- channel.port1.onmessage = function () {
178
- channel.port1.onmessage = null;
179
- channel = undefined;
180
- f();
181
- };
182
- channel.port2.postMessage(null);
183
- };
184
119
  const _require = (function () {
185
120
  let nativeRequire;
186
121
  if (typeof __webpack_public_path__ !== 'undefined') {
@@ -195,86 +130,108 @@ const _require = (function () {
195
130
  }
196
131
  return nativeRequire;
197
132
  })();
198
- const Buffer = (function () {
199
- if (typeof _global.Buffer === 'function')
200
- return _global.Buffer;
201
- try {
202
- return _require('buffer').Buffer;
203
- }
204
- catch (_) { }
205
- return undefined;
206
- })();
133
+ const _MessageChannel = typeof MessageChannel === 'function'
134
+ ? MessageChannel
135
+ : (function () {
136
+ try {
137
+ return _require('worker_threads').MessageChannel;
138
+ }
139
+ catch (_) { }
140
+ return undefined;
141
+ })();
142
+ const _setImmediate = typeof setImmediate === 'function'
143
+ ? setImmediate
144
+ : function (callback) {
145
+ if (typeof callback !== 'function') {
146
+ throw new TypeError('The "callback" argument must be of type function');
147
+ }
148
+ if (_MessageChannel) {
149
+ let channel = new _MessageChannel();
150
+ channel.port1.onmessage = function () {
151
+ channel.port1.onmessage = null;
152
+ channel = undefined;
153
+ callback();
154
+ };
155
+ channel.port2.postMessage(null);
156
+ }
157
+ else {
158
+ setTimeout(callback, 0);
159
+ }
160
+ };
161
+ const _Buffer = typeof Buffer === 'function'
162
+ ? Buffer
163
+ : (function () {
164
+ try {
165
+ return _require('buffer').Buffer;
166
+ }
167
+ catch (_) { }
168
+ return undefined;
169
+ })();
170
+ const version = "0.39.0";
171
+ const NAPI_VERSION = 9;
172
+ const NAPI_VERSION_EXPERIMENTAL = 2147483647;
173
+ const NODE_API_DEFAULT_MODULE_API_VERSION = 8;
207
174
 
208
175
  class Handle {
209
176
  constructor(id, value) {
210
177
  this.id = id;
211
178
  this.value = value;
212
179
  }
213
- init(id, value) {
214
- this.id = id;
215
- this.value = value;
216
- }
217
180
  data(envObject) {
218
181
  return envObject.getObjectBinding(this.value).data;
219
182
  }
220
- isEmpty() {
221
- return this.id === 0;
222
- }
223
183
  isNumber() {
224
- return !this.isEmpty() && typeof this.value === 'number';
184
+ return typeof this.value === 'number';
225
185
  }
226
186
  isBigInt() {
227
- return !this.isEmpty() && typeof this.value === 'bigint';
187
+ return typeof this.value === 'bigint';
228
188
  }
229
189
  isString() {
230
- return !this.isEmpty() && typeof this.value === 'string';
190
+ return typeof this.value === 'string';
231
191
  }
232
192
  isFunction() {
233
- return !this.isEmpty() && typeof this.value === 'function';
193
+ return typeof this.value === 'function';
234
194
  }
235
195
  isExternal() {
236
- return !this.isEmpty() && (isReferenceType(this.value) && Object.getPrototypeOf(this.value) === null);
196
+ return (isReferenceType(this.value) && Object.getPrototypeOf(this.value) === null);
237
197
  }
238
198
  isObject() {
239
- return !this.isEmpty() && typeof this.value === 'object' && this.value !== null;
199
+ return typeof this.value === 'object' && this.value !== null;
240
200
  }
241
201
  isArray() {
242
- return !this.isEmpty() && Array.isArray(this.value);
202
+ return Array.isArray(this.value);
243
203
  }
244
204
  isArrayBuffer() {
245
- return !this.isEmpty() && (this.value instanceof ArrayBuffer);
205
+ return (this.value instanceof ArrayBuffer);
246
206
  }
247
207
  isTypedArray() {
248
- return !this.isEmpty() && (ArrayBuffer.isView(this.value)) && !(this.value instanceof DataView);
208
+ return (ArrayBuffer.isView(this.value)) && !(this.value instanceof DataView);
249
209
  }
250
210
  isBuffer() {
251
- return !this.isEmpty() && typeof Buffer === 'function' && Buffer.isBuffer(this.value);
211
+ return typeof _Buffer === 'function' && _Buffer.isBuffer(this.value);
252
212
  }
253
213
  isDataView() {
254
- return !this.isEmpty() && (this.value instanceof DataView);
214
+ return (this.value instanceof DataView);
255
215
  }
256
216
  isDate() {
257
- return !this.isEmpty() && (this.value instanceof Date);
217
+ return (this.value instanceof Date);
258
218
  }
259
219
  isPromise() {
260
- return !this.isEmpty() && (this.value instanceof Promise);
220
+ return (this.value instanceof Promise);
261
221
  }
262
222
  isBoolean() {
263
- return !this.isEmpty() && typeof this.value === 'boolean';
223
+ return typeof this.value === 'boolean';
264
224
  }
265
225
  isUndefined() {
266
- return !this.isEmpty() && this.value === undefined;
226
+ return this.value === undefined;
267
227
  }
268
228
  isSymbol() {
269
- return !this.isEmpty() && typeof this.value === 'symbol';
229
+ return typeof this.value === 'symbol';
270
230
  }
271
231
  isNull() {
272
- return !this.isEmpty() && this.value === null;
232
+ return this.value === null;
273
233
  }
274
234
  dispose() {
275
- if (this.id === 0)
276
- return;
277
- this.id = 0;
278
235
  this.value = undefined;
279
236
  }
280
237
  }
@@ -306,7 +263,7 @@ class HandleStore {
306
263
  const values = this._values;
307
264
  if (next < values.length) {
308
265
  h = values[next];
309
- h.init(next, value);
266
+ h.value = value;
310
267
  }
311
268
  else {
312
269
  h = new Handle(next, value);
@@ -448,18 +405,6 @@ class ScopeStore {
448
405
  }
449
406
  }
450
407
 
451
- class RefStore extends Store {
452
- constructor() {
453
- super(8);
454
- }
455
- }
456
-
457
- class DeferredStore extends Store {
458
- constructor() {
459
- super(8);
460
- }
461
- }
462
-
463
408
  class RefTracker {
464
409
  constructor() {
465
410
  this._next = null;
@@ -580,19 +525,29 @@ class RefBase extends Finalizer {
580
525
  }
581
526
  }
582
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
+ }
583
538
  class Env {
584
- constructor(ctx, makeDynCall_vppp, makeDynCall_vp) {
539
+ constructor(ctx, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort) {
585
540
  this.ctx = ctx;
541
+ this.moduleApiVersion = moduleApiVersion;
586
542
  this.makeDynCall_vppp = makeDynCall_vppp;
587
543
  this.makeDynCall_vp = makeDynCall_vp;
544
+ this.abort = abort;
588
545
  this.openHandleScopes = 0;
589
546
  this.instanceData = null;
590
547
  this.tryCatch = new TryCatch();
591
548
  this.refs = 1;
592
549
  this.reflist = new RefTracker();
593
550
  this.finalizing_reflist = new RefTracker();
594
- this.destructing = false;
595
- this.finalizationScheduled = false;
596
551
  this.pendingFinalizers = [];
597
552
  this.lastError = {
598
553
  errorCode: 0,
@@ -602,11 +557,11 @@ class Env {
602
557
  this._bindingMap = new WeakMap();
603
558
  this.id = 0;
604
559
  }
605
- static create(ctx, makeDynCall_vppp, makeDynCall_vp) {
606
- const env = new Env(ctx, makeDynCall_vppp, makeDynCall_vp);
607
- ctx.envStore.add(env);
608
- ctx.addCleanupHook(env, () => { env.unref(); }, 0);
609
- return env;
560
+ canCallIntoJs() {
561
+ return true;
562
+ }
563
+ terminatedOrTerminating() {
564
+ return !this.canCallIntoJs();
610
565
  }
611
566
  ref() {
612
567
  this.refs++;
@@ -625,27 +580,37 @@ class Env {
625
580
  }
626
581
  clearLastError() {
627
582
  const lastError = this.lastError;
628
- lastError.errorCode = 0;
629
- lastError.engineErrorCode = 0;
630
- 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;
631
589
  return 0;
632
590
  }
633
591
  setLastError(error_code, engine_error_code = 0, engine_reserved = 0) {
634
592
  const lastError = this.lastError;
635
- lastError.errorCode = error_code;
636
- lastError.engineErrorCode = engine_error_code;
637
- 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;
638
599
  return error_code;
639
600
  }
640
601
  getReturnStatus() {
641
602
  return !this.tryCatch.hasCaught() ? 0 : this.setLastError(10);
642
603
  }
643
- callIntoModule(fn) {
604
+ callIntoModule(fn, handleException = handleThrow) {
605
+ const openHandleScopesBefore = this.openHandleScopes;
644
606
  this.clearLastError();
645
607
  const r = fn(this);
608
+ if (openHandleScopesBefore !== this.openHandleScopes) {
609
+ this.abort('open_handle_scopes != open_handle_scopes_before');
610
+ }
646
611
  if (this.tryCatch.hasCaught()) {
647
612
  const err = this.tryCatch.extractException();
648
- throw err;
613
+ handleException(this, err);
649
614
  }
650
615
  return r;
651
616
  }
@@ -664,15 +629,6 @@ class Env {
664
629
  if (this.pendingFinalizers.indexOf(finalizer) === -1) {
665
630
  this.pendingFinalizers.push(finalizer);
666
631
  }
667
- if (!this.finalizationScheduled && !this.destructing) {
668
- this.finalizationScheduled = true;
669
- this.ref();
670
- _setImmediate(() => {
671
- this.finalizationScheduled = false;
672
- this.unref();
673
- this.drainFinalizerQueue();
674
- });
675
- }
676
632
  }
677
633
  dequeueFinalizer(finalizer) {
678
634
  const index = this.pendingFinalizers.indexOf(finalizer);
@@ -680,21 +636,16 @@ class Env {
680
636
  this.pendingFinalizers.splice(index, 1);
681
637
  }
682
638
  }
683
- drainFinalizerQueue() {
684
- while (this.pendingFinalizers.length > 0) {
685
- const refTracker = this.pendingFinalizers.shift();
686
- refTracker.finalize();
687
- }
688
- }
689
- dispose() {
690
- if (this.id === 0)
691
- return;
692
- this.destructing = true;
693
- this.drainFinalizerQueue();
639
+ deleteMe() {
694
640
  RefBase.finalizeAll(this.finalizing_reflist);
695
641
  RefBase.finalizeAll(this.reflist);
696
642
  this.tryCatch.extractException();
697
643
  this.ctx.envStore.remove(this.id);
644
+ }
645
+ dispose() {
646
+ if (this.id === 0)
647
+ return;
648
+ this.deleteMe();
698
649
  this.id = 0;
699
650
  }
700
651
  initObjectBinding(value) {
@@ -722,6 +673,103 @@ class Env {
722
673
  return this.instanceData ? this.instanceData.data() : 0;
723
674
  }
724
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
+ }
725
773
 
726
774
  class EmnapiError extends Error {
727
775
  constructor(message) {
@@ -805,7 +853,12 @@ class Persistent {
805
853
  this._param = param;
806
854
  this._callback = callback;
807
855
  }
808
- catch (_) { }
856
+ catch (err) {
857
+ if (typeof value === 'symbol') ;
858
+ else {
859
+ throw err;
860
+ }
861
+ }
809
862
  }
810
863
  clearWeak() {
811
864
  if (!supportFinalizer || this._ref === undefined)
@@ -866,6 +919,9 @@ function weakCallback(ref) {
866
919
  ref.persistent.reset();
867
920
  ref.envObject.enqueueFinalizer(ref);
868
921
  }
922
+ function canBeHeldWeakly(value) {
923
+ return value.isObject() || value.isFunction() || value.isSymbol();
924
+ }
869
925
  class Reference extends RefBase {
870
926
  constructor(envObject, initialRefcount, ownership, finalize_callback = 0, finalize_data = 0, finalize_hint = 0) {
871
927
  super(envObject, initialRefcount >>> 0, ownership, finalize_callback, finalize_data, finalize_hint);
@@ -875,6 +931,7 @@ class Reference extends RefBase {
875
931
  const handle = envObject.ctx.handleStore.get(handle_id);
876
932
  const ref = new Reference(envObject, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint);
877
933
  envObject.ctx.refStore.add(ref);
934
+ ref.canBeWeak = canBeHeldWeakly(handle);
878
935
  ref.persistent = new Persistent(handle.value);
879
936
  if (initialRefcount === 0) {
880
937
  ref._setWeak();
@@ -886,11 +943,8 @@ class Reference extends RefBase {
886
943
  return 0;
887
944
  }
888
945
  const count = super.ref();
889
- if (count === 1) {
890
- const obj = this.persistent.deref();
891
- if (obj) {
892
- this.persistent.clearWeak();
893
- }
946
+ if (count === 1 && this.canBeWeak) {
947
+ this.persistent.clearWeak();
894
948
  }
895
949
  return count;
896
950
  }
@@ -901,23 +955,25 @@ class Reference extends RefBase {
901
955
  const oldRefcount = this.refCount();
902
956
  const refcount = super.unref();
903
957
  if (oldRefcount === 1 && refcount === 0) {
904
- const obj = this.persistent.deref();
905
- if (obj) {
906
- this._setWeak();
907
- }
958
+ this._setWeak();
908
959
  }
909
960
  return refcount;
910
961
  }
911
962
  get() {
912
- const obj = this.persistent.deref();
913
- if (obj) {
914
- const handle = this.envObject.ensureHandle(obj);
915
- return handle.id;
963
+ if (this.persistent.isEmpty()) {
964
+ return 0;
916
965
  }
917
- return 0;
966
+ const obj = this.persistent.deref();
967
+ const handle = this.envObject.ensureHandle(obj);
968
+ return handle.id;
918
969
  }
919
970
  _setWeak() {
920
- this.persistent.setWeak(this, weakCallback);
971
+ if (this.canBeWeak) {
972
+ this.persistent.setWeak(this, weakCallback);
973
+ }
974
+ else {
975
+ this.persistent.reset();
976
+ }
921
977
  }
922
978
  finalize() {
923
979
  this.persistent.reset();
@@ -960,6 +1016,54 @@ class Deferred {
960
1016
  }
961
1017
  }
962
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
+
963
1067
  class CleanupHookCallback {
964
1068
  constructor(envObject, fn, arg, order) {
965
1069
  this.envObject = envObject;
@@ -1012,7 +1116,7 @@ class CleanupQueue {
1012
1116
  }
1013
1117
  class NodejsWaitingRequestCounter {
1014
1118
  constructor() {
1015
- this.refHandle = new MessageChannel().port1;
1119
+ this.refHandle = new _MessageChannel().port1;
1016
1120
  this.count = 0;
1017
1121
  }
1018
1122
  increase() {
@@ -1032,10 +1136,12 @@ class NodejsWaitingRequestCounter {
1032
1136
  }
1033
1137
  class Context {
1034
1138
  constructor() {
1035
- this.envStore = new EnvStore();
1139
+ this._isStopping = false;
1140
+ this._canCallIntoJs = true;
1141
+ this.envStore = new Store();
1036
1142
  this.scopeStore = new ScopeStore();
1037
- this.refStore = new RefStore();
1038
- this.deferredStore = new DeferredStore();
1143
+ this.refStore = new Store();
1144
+ this.deferredStore = new Store();
1039
1145
  this.handleStore = new HandleStore();
1040
1146
  this.cbinfoStack = new CallbackInfoStack();
1041
1147
  this.feature = {
@@ -1046,16 +1152,25 @@ class Context {
1046
1152
  supportNewFunction,
1047
1153
  canSetFunctionName,
1048
1154
  setImmediate: _setImmediate,
1049
- Buffer
1155
+ Buffer: _Buffer,
1156
+ MessageChannel: _MessageChannel
1050
1157
  };
1051
1158
  this.cleanupQueue = new CleanupQueue();
1052
1159
  if (typeof process === 'object' && process !== null && typeof process.once === 'function') {
1053
1160
  this.refCounter = new NodejsWaitingRequestCounter();
1054
1161
  process.once('beforeExit', () => {
1055
- this.runCleanup();
1162
+ this.destroy();
1056
1163
  });
1057
1164
  }
1058
1165
  }
1166
+ getRuntimeVersions() {
1167
+ return {
1168
+ version,
1169
+ NAPI_VERSION,
1170
+ NAPI_VERSION_EXPERIMENTAL,
1171
+ NODE_API_DEFAULT_MODULE_API_VERSION
1172
+ };
1173
+ }
1059
1174
  createNotSupportWeakRefError(api, message) {
1060
1175
  return new NotSupportWeakRefError(api, message);
1061
1176
  }
@@ -1071,8 +1186,8 @@ class Context {
1071
1186
  createDeferred(value) {
1072
1187
  return Deferred.create(this, value);
1073
1188
  }
1074
- createEnv(makeDynCall_vppp, makeDynCall_vp) {
1075
- 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);
1076
1191
  }
1077
1192
  getCurrentScope() {
1078
1193
  return this.scopeStore.currentScope;
@@ -1115,7 +1230,18 @@ class Context {
1115
1230
  var _a;
1116
1231
  (_a = this.refCounter) === null || _a === void 0 ? void 0 : _a.decrease();
1117
1232
  }
1118
- 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);
1119
1245
  this.runCleanup();
1120
1246
  }
1121
1247
  }
@@ -1130,6 +1256,4 @@ function getDefaultContext() {
1130
1256
  return defaultContext;
1131
1257
  }
1132
1258
 
1133
- const version = "0.38.2";
1134
-
1135
- 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 };