@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.
@@ -35,60 +35,6 @@ class CallbackInfoStack {
35
35
  }
36
36
  }
37
37
 
38
- class Store {
39
- constructor(capacity) {
40
- this._values = [undefined];
41
- this._values.length = capacity;
42
- this._size = 1;
43
- this._freeList = [];
44
- }
45
- add(value) {
46
- let id;
47
- if (this._freeList.length) {
48
- id = this._freeList.shift();
49
- }
50
- else {
51
- id = this._size;
52
- this._size++;
53
- const capacity = this._values.length;
54
- if (id >= capacity) {
55
- this._values.length = Math.ceil(capacity * 1.5);
56
- }
57
- }
58
- value.id = id;
59
- this._values[id] = value;
60
- }
61
- get(id) {
62
- return this._values[id];
63
- }
64
- has(id) {
65
- return this._values[id] !== undefined;
66
- }
67
- remove(id) {
68
- const value = this._values[id];
69
- if (value) {
70
- value.id = 0;
71
- this._values[id] = undefined;
72
- this._freeList.push(Number(id));
73
- }
74
- }
75
- dispose() {
76
- for (let i = 1; i < this._size; ++i) {
77
- const value = this._values[i];
78
- value === null || value === void 0 ? void 0 : value.dispose();
79
- }
80
- this._values = [undefined];
81
- this._size = 1;
82
- this._freeList = [];
83
- }
84
- }
85
-
86
- class EnvStore extends Store {
87
- constructor() {
88
- super(2);
89
- }
90
- }
91
-
92
38
  const supportNewFunction = (function () {
93
39
  let f;
94
40
  try {
@@ -121,22 +67,24 @@ const _global = (function () {
121
67
  }
122
68
  return g;
123
69
  })();
124
- const emptyException = new Error();
125
70
  class TryCatch {
126
71
  constructor() {
127
- this._exception = emptyException;
72
+ this._exception = undefined;
73
+ this._caught = false;
128
74
  }
129
75
  hasCaught() {
130
- return this._exception !== emptyException;
76
+ return this._caught;
131
77
  }
132
78
  exception() {
133
79
  return this._exception;
134
80
  }
135
81
  setError(err) {
82
+ this._caught = true;
136
83
  this._exception = err;
137
84
  }
138
85
  reset() {
139
- this._exception = emptyException;
86
+ this._caught = false;
87
+ this._exception = undefined;
140
88
  }
141
89
  extractException() {
142
90
  const e = this._exception;
@@ -170,19 +118,6 @@ const supportBigInt = typeof BigInt !== 'undefined';
170
118
  function isReferenceType(v) {
171
119
  return (typeof v === 'object' && v !== null) || typeof v === 'function';
172
120
  }
173
- const _setImmediate = typeof setImmediate === 'function'
174
- ? setImmediate
175
- : function (f) {
176
- if (typeof f !== 'function')
177
- return;
178
- let channel = new MessageChannel();
179
- channel.port1.onmessage = function () {
180
- channel.port1.onmessage = null;
181
- channel = undefined;
182
- f();
183
- };
184
- channel.port2.postMessage(null);
185
- };
186
121
  const _require = (function () {
187
122
  let nativeRequire;
188
123
  if (typeof __webpack_public_path__ !== 'undefined') {
@@ -197,86 +132,108 @@ const _require = (function () {
197
132
  }
198
133
  return nativeRequire;
199
134
  })();
200
- const Buffer = (function () {
201
- if (typeof _global.Buffer === 'function')
202
- return _global.Buffer;
203
- try {
204
- return _require('buffer').Buffer;
205
- }
206
- catch (_) { }
207
- return undefined;
208
- })();
135
+ const _MessageChannel = typeof MessageChannel === 'function'
136
+ ? MessageChannel
137
+ : (function () {
138
+ try {
139
+ return _require('worker_threads').MessageChannel;
140
+ }
141
+ catch (_) { }
142
+ return undefined;
143
+ })();
144
+ const _setImmediate = typeof setImmediate === 'function'
145
+ ? setImmediate
146
+ : function (callback) {
147
+ if (typeof callback !== 'function') {
148
+ throw new TypeError('The "callback" argument must be of type function');
149
+ }
150
+ if (_MessageChannel) {
151
+ let channel = new _MessageChannel();
152
+ channel.port1.onmessage = function () {
153
+ channel.port1.onmessage = null;
154
+ channel = undefined;
155
+ callback();
156
+ };
157
+ channel.port2.postMessage(null);
158
+ }
159
+ else {
160
+ setTimeout(callback, 0);
161
+ }
162
+ };
163
+ const _Buffer = typeof Buffer === 'function'
164
+ ? Buffer
165
+ : (function () {
166
+ try {
167
+ return _require('buffer').Buffer;
168
+ }
169
+ catch (_) { }
170
+ return undefined;
171
+ })();
172
+ const version = "0.39.0";
173
+ const NAPI_VERSION = 9;
174
+ const NAPI_VERSION_EXPERIMENTAL = 2147483647;
175
+ const NODE_API_DEFAULT_MODULE_API_VERSION = 8;
209
176
 
210
177
  class Handle {
211
178
  constructor(id, value) {
212
179
  this.id = id;
213
180
  this.value = value;
214
181
  }
215
- init(id, value) {
216
- this.id = id;
217
- this.value = value;
218
- }
219
182
  data(envObject) {
220
183
  return envObject.getObjectBinding(this.value).data;
221
184
  }
222
- isEmpty() {
223
- return this.id === 0;
224
- }
225
185
  isNumber() {
226
- return !this.isEmpty() && typeof this.value === 'number';
186
+ return typeof this.value === 'number';
227
187
  }
228
188
  isBigInt() {
229
- return !this.isEmpty() && typeof this.value === 'bigint';
189
+ return typeof this.value === 'bigint';
230
190
  }
231
191
  isString() {
232
- return !this.isEmpty() && typeof this.value === 'string';
192
+ return typeof this.value === 'string';
233
193
  }
234
194
  isFunction() {
235
- return !this.isEmpty() && typeof this.value === 'function';
195
+ return typeof this.value === 'function';
236
196
  }
237
197
  isExternal() {
238
- return !this.isEmpty() && (isReferenceType(this.value) && Object.getPrototypeOf(this.value) === null);
198
+ return (isReferenceType(this.value) && Object.getPrototypeOf(this.value) === null);
239
199
  }
240
200
  isObject() {
241
- return !this.isEmpty() && typeof this.value === 'object' && this.value !== null;
201
+ return typeof this.value === 'object' && this.value !== null;
242
202
  }
243
203
  isArray() {
244
- return !this.isEmpty() && Array.isArray(this.value);
204
+ return Array.isArray(this.value);
245
205
  }
246
206
  isArrayBuffer() {
247
- return !this.isEmpty() && (this.value instanceof ArrayBuffer);
207
+ return (this.value instanceof ArrayBuffer);
248
208
  }
249
209
  isTypedArray() {
250
- return !this.isEmpty() && (ArrayBuffer.isView(this.value)) && !(this.value instanceof DataView);
210
+ return (ArrayBuffer.isView(this.value)) && !(this.value instanceof DataView);
251
211
  }
252
212
  isBuffer() {
253
- return !this.isEmpty() && typeof Buffer === 'function' && Buffer.isBuffer(this.value);
213
+ return typeof _Buffer === 'function' && _Buffer.isBuffer(this.value);
254
214
  }
255
215
  isDataView() {
256
- return !this.isEmpty() && (this.value instanceof DataView);
216
+ return (this.value instanceof DataView);
257
217
  }
258
218
  isDate() {
259
- return !this.isEmpty() && (this.value instanceof Date);
219
+ return (this.value instanceof Date);
260
220
  }
261
221
  isPromise() {
262
- return !this.isEmpty() && (this.value instanceof Promise);
222
+ return (this.value instanceof Promise);
263
223
  }
264
224
  isBoolean() {
265
- return !this.isEmpty() && typeof this.value === 'boolean';
225
+ return typeof this.value === 'boolean';
266
226
  }
267
227
  isUndefined() {
268
- return !this.isEmpty() && this.value === undefined;
228
+ return this.value === undefined;
269
229
  }
270
230
  isSymbol() {
271
- return !this.isEmpty() && typeof this.value === 'symbol';
231
+ return typeof this.value === 'symbol';
272
232
  }
273
233
  isNull() {
274
- return !this.isEmpty() && this.value === null;
234
+ return this.value === null;
275
235
  }
276
236
  dispose() {
277
- if (this.id === 0)
278
- return;
279
- this.id = 0;
280
237
  this.value = undefined;
281
238
  }
282
239
  }
@@ -308,7 +265,7 @@ class HandleStore {
308
265
  const values = this._values;
309
266
  if (next < values.length) {
310
267
  h = values[next];
311
- h.init(next, value);
268
+ h.value = value;
312
269
  }
313
270
  else {
314
271
  h = new Handle(next, value);
@@ -450,18 +407,6 @@ class ScopeStore {
450
407
  }
451
408
  }
452
409
 
453
- class RefStore extends Store {
454
- constructor() {
455
- super(8);
456
- }
457
- }
458
-
459
- class DeferredStore extends Store {
460
- constructor() {
461
- super(8);
462
- }
463
- }
464
-
465
410
  class RefTracker {
466
411
  constructor() {
467
412
  this._next = null;
@@ -582,19 +527,29 @@ class RefBase extends Finalizer {
582
527
  }
583
528
  }
584
529
 
530
+ function throwNodeApiVersionError(moduleName, moduleApiVersion) {
531
+ const errorMessage = `${moduleName} requires Node-API version ${moduleApiVersion}, but this version of Node.js only supports version ${NAPI_VERSION} add-ons.`;
532
+ throw new Error(errorMessage);
533
+ }
534
+ function handleThrow(envObject, value) {
535
+ if (envObject.terminatedOrTerminating()) {
536
+ return;
537
+ }
538
+ throw value;
539
+ }
585
540
  class Env {
586
- constructor(ctx, makeDynCall_vppp, makeDynCall_vp) {
541
+ constructor(ctx, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort) {
587
542
  this.ctx = ctx;
543
+ this.moduleApiVersion = moduleApiVersion;
588
544
  this.makeDynCall_vppp = makeDynCall_vppp;
589
545
  this.makeDynCall_vp = makeDynCall_vp;
546
+ this.abort = abort;
590
547
  this.openHandleScopes = 0;
591
548
  this.instanceData = null;
592
549
  this.tryCatch = new TryCatch();
593
550
  this.refs = 1;
594
551
  this.reflist = new RefTracker();
595
552
  this.finalizing_reflist = new RefTracker();
596
- this.destructing = false;
597
- this.finalizationScheduled = false;
598
553
  this.pendingFinalizers = [];
599
554
  this.lastError = {
600
555
  errorCode: 0,
@@ -604,11 +559,11 @@ class Env {
604
559
  this._bindingMap = new WeakMap();
605
560
  this.id = 0;
606
561
  }
607
- static create(ctx, makeDynCall_vppp, makeDynCall_vp) {
608
- const env = new Env(ctx, makeDynCall_vppp, makeDynCall_vp);
609
- ctx.envStore.add(env);
610
- ctx.addCleanupHook(env, () => { env.unref(); }, 0);
611
- return env;
562
+ canCallIntoJs() {
563
+ return true;
564
+ }
565
+ terminatedOrTerminating() {
566
+ return !this.canCallIntoJs();
612
567
  }
613
568
  ref() {
614
569
  this.refs++;
@@ -627,27 +582,37 @@ class Env {
627
582
  }
628
583
  clearLastError() {
629
584
  const lastError = this.lastError;
630
- lastError.errorCode = 0;
631
- lastError.engineErrorCode = 0;
632
- lastError.engineReserved = 0;
585
+ if (lastError.errorCode !== 0)
586
+ lastError.errorCode = 0;
587
+ if (lastError.engineErrorCode !== 0)
588
+ lastError.engineErrorCode = 0;
589
+ if (lastError.engineReserved !== 0)
590
+ lastError.engineReserved = 0;
633
591
  return 0;
634
592
  }
635
593
  setLastError(error_code, engine_error_code = 0, engine_reserved = 0) {
636
594
  const lastError = this.lastError;
637
- lastError.errorCode = error_code;
638
- lastError.engineErrorCode = engine_error_code;
639
- lastError.engineReserved = engine_reserved;
595
+ if (lastError.errorCode !== error_code)
596
+ lastError.errorCode = error_code;
597
+ if (lastError.engineErrorCode !== engine_error_code)
598
+ lastError.engineErrorCode = engine_error_code;
599
+ if (lastError.engineReserved !== engine_reserved)
600
+ lastError.engineReserved = engine_reserved;
640
601
  return error_code;
641
602
  }
642
603
  getReturnStatus() {
643
604
  return !this.tryCatch.hasCaught() ? 0 : this.setLastError(10);
644
605
  }
645
- callIntoModule(fn) {
606
+ callIntoModule(fn, handleException = handleThrow) {
607
+ const openHandleScopesBefore = this.openHandleScopes;
646
608
  this.clearLastError();
647
609
  const r = fn(this);
610
+ if (openHandleScopesBefore !== this.openHandleScopes) {
611
+ this.abort('open_handle_scopes != open_handle_scopes_before');
612
+ }
648
613
  if (this.tryCatch.hasCaught()) {
649
614
  const err = this.tryCatch.extractException();
650
- throw err;
615
+ handleException(this, err);
651
616
  }
652
617
  return r;
653
618
  }
@@ -666,15 +631,6 @@ class Env {
666
631
  if (this.pendingFinalizers.indexOf(finalizer) === -1) {
667
632
  this.pendingFinalizers.push(finalizer);
668
633
  }
669
- if (!this.finalizationScheduled && !this.destructing) {
670
- this.finalizationScheduled = true;
671
- this.ref();
672
- _setImmediate(() => {
673
- this.finalizationScheduled = false;
674
- this.unref();
675
- this.drainFinalizerQueue();
676
- });
677
- }
678
634
  }
679
635
  dequeueFinalizer(finalizer) {
680
636
  const index = this.pendingFinalizers.indexOf(finalizer);
@@ -682,21 +638,16 @@ class Env {
682
638
  this.pendingFinalizers.splice(index, 1);
683
639
  }
684
640
  }
685
- drainFinalizerQueue() {
686
- while (this.pendingFinalizers.length > 0) {
687
- const refTracker = this.pendingFinalizers.shift();
688
- refTracker.finalize();
689
- }
690
- }
691
- dispose() {
692
- if (this.id === 0)
693
- return;
694
- this.destructing = true;
695
- this.drainFinalizerQueue();
641
+ deleteMe() {
696
642
  RefBase.finalizeAll(this.finalizing_reflist);
697
643
  RefBase.finalizeAll(this.reflist);
698
644
  this.tryCatch.extractException();
699
645
  this.ctx.envStore.remove(this.id);
646
+ }
647
+ dispose() {
648
+ if (this.id === 0)
649
+ return;
650
+ this.deleteMe();
700
651
  this.id = 0;
701
652
  }
702
653
  initObjectBinding(value) {
@@ -724,6 +675,103 @@ class Env {
724
675
  return this.instanceData ? this.instanceData.data() : 0;
725
676
  }
726
677
  }
678
+ class NodeEnv extends Env {
679
+ constructor(ctx, filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort, nodeBinding) {
680
+ super(ctx, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort);
681
+ this.filename = filename;
682
+ this.nodeBinding = nodeBinding;
683
+ this.destructing = false;
684
+ this.finalizationScheduled = false;
685
+ }
686
+ deleteMe() {
687
+ this.destructing = true;
688
+ this.drainFinalizerQueue();
689
+ super.deleteMe();
690
+ }
691
+ canCallIntoJs() {
692
+ return this.ctx.canCallIntoJs();
693
+ }
694
+ triggerFatalException(err) {
695
+ if (this.nodeBinding) {
696
+ this.nodeBinding.napi.fatalException(err);
697
+ }
698
+ else {
699
+ if (typeof process === 'object' && process !== null && typeof process._fatalException === 'function') {
700
+ const handled = process._fatalException(err);
701
+ if (!handled) {
702
+ console.error(err);
703
+ process.exit(1);
704
+ }
705
+ }
706
+ else {
707
+ throw err;
708
+ }
709
+ }
710
+ }
711
+ callbackIntoModule(enforceUncaughtExceptionPolicy, fn) {
712
+ return this.callIntoModule(fn, (envObject, err) => {
713
+ if (envObject.terminatedOrTerminating()) {
714
+ return;
715
+ }
716
+ const hasProcess = typeof process === 'object' && process !== null;
717
+ const hasForceFlag = hasProcess ? Boolean(process.execArgv && (process.execArgv.indexOf('--force-node-api-uncaught-exceptions-policy') !== -1)) : false;
718
+ if (!hasForceFlag && !enforceUncaughtExceptionPolicy) {
719
+ if (hasProcess && typeof process.emitWarning === 'function') {
720
+ 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');
721
+ }
722
+ else {
723
+ throw err;
724
+ }
725
+ }
726
+ envObject.triggerFatalException(err);
727
+ });
728
+ }
729
+ callFinalizer(cb, data, hint) {
730
+ this.callFinalizerInternal(1, cb, data, hint);
731
+ }
732
+ callFinalizerInternal(forceUncaught, cb, data, hint) {
733
+ const f = this.makeDynCall_vppp(cb);
734
+ const env = this.id;
735
+ const scope = this.ctx.openScope(this);
736
+ try {
737
+ this.callbackIntoModule(Boolean(forceUncaught), () => { f(env, data, hint); });
738
+ }
739
+ finally {
740
+ this.ctx.closeScope(this, scope);
741
+ }
742
+ }
743
+ enqueueFinalizer(finalizer) {
744
+ super.enqueueFinalizer(finalizer);
745
+ if (!this.finalizationScheduled && !this.destructing) {
746
+ this.finalizationScheduled = true;
747
+ this.ref();
748
+ _setImmediate(() => {
749
+ this.finalizationScheduled = false;
750
+ this.unref();
751
+ this.drainFinalizerQueue();
752
+ });
753
+ }
754
+ }
755
+ drainFinalizerQueue() {
756
+ while (this.pendingFinalizers.length > 0) {
757
+ const refTracker = this.pendingFinalizers.shift();
758
+ refTracker.finalize();
759
+ }
760
+ }
761
+ }
762
+ function newEnv(ctx, filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort, nodeBinding) {
763
+ moduleApiVersion = typeof moduleApiVersion !== 'number' ? NODE_API_DEFAULT_MODULE_API_VERSION : moduleApiVersion;
764
+ if (moduleApiVersion < NODE_API_DEFAULT_MODULE_API_VERSION) {
765
+ moduleApiVersion = NODE_API_DEFAULT_MODULE_API_VERSION;
766
+ }
767
+ else if (moduleApiVersion > NAPI_VERSION && moduleApiVersion !== NAPI_VERSION_EXPERIMENTAL) {
768
+ throwNodeApiVersionError(filename, moduleApiVersion);
769
+ }
770
+ const env = new NodeEnv(ctx, filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort, nodeBinding);
771
+ ctx.envStore.add(env);
772
+ ctx.addCleanupHook(env, () => { env.unref(); }, 0);
773
+ return env;
774
+ }
727
775
 
728
776
  class EmnapiError extends Error {
729
777
  constructor(message) {
@@ -807,7 +855,12 @@ class Persistent {
807
855
  this._param = param;
808
856
  this._callback = callback;
809
857
  }
810
- catch (_) { }
858
+ catch (err) {
859
+ if (typeof value === 'symbol') ;
860
+ else {
861
+ throw err;
862
+ }
863
+ }
811
864
  }
812
865
  clearWeak() {
813
866
  if (!supportFinalizer || this._ref === undefined)
@@ -868,6 +921,9 @@ function weakCallback(ref) {
868
921
  ref.persistent.reset();
869
922
  ref.envObject.enqueueFinalizer(ref);
870
923
  }
924
+ function canBeHeldWeakly(value) {
925
+ return value.isObject() || value.isFunction() || value.isSymbol();
926
+ }
871
927
  class Reference extends RefBase {
872
928
  constructor(envObject, initialRefcount, ownership, finalize_callback = 0, finalize_data = 0, finalize_hint = 0) {
873
929
  super(envObject, initialRefcount >>> 0, ownership, finalize_callback, finalize_data, finalize_hint);
@@ -877,6 +933,7 @@ class Reference extends RefBase {
877
933
  const handle = envObject.ctx.handleStore.get(handle_id);
878
934
  const ref = new Reference(envObject, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint);
879
935
  envObject.ctx.refStore.add(ref);
936
+ ref.canBeWeak = canBeHeldWeakly(handle);
880
937
  ref.persistent = new Persistent(handle.value);
881
938
  if (initialRefcount === 0) {
882
939
  ref._setWeak();
@@ -888,11 +945,8 @@ class Reference extends RefBase {
888
945
  return 0;
889
946
  }
890
947
  const count = super.ref();
891
- if (count === 1) {
892
- const obj = this.persistent.deref();
893
- if (obj) {
894
- this.persistent.clearWeak();
895
- }
948
+ if (count === 1 && this.canBeWeak) {
949
+ this.persistent.clearWeak();
896
950
  }
897
951
  return count;
898
952
  }
@@ -903,23 +957,25 @@ class Reference extends RefBase {
903
957
  const oldRefcount = this.refCount();
904
958
  const refcount = super.unref();
905
959
  if (oldRefcount === 1 && refcount === 0) {
906
- const obj = this.persistent.deref();
907
- if (obj) {
908
- this._setWeak();
909
- }
960
+ this._setWeak();
910
961
  }
911
962
  return refcount;
912
963
  }
913
964
  get() {
914
- const obj = this.persistent.deref();
915
- if (obj) {
916
- const handle = this.envObject.ensureHandle(obj);
917
- return handle.id;
965
+ if (this.persistent.isEmpty()) {
966
+ return 0;
918
967
  }
919
- return 0;
968
+ const obj = this.persistent.deref();
969
+ const handle = this.envObject.ensureHandle(obj);
970
+ return handle.id;
920
971
  }
921
972
  _setWeak() {
922
- this.persistent.setWeak(this, weakCallback);
973
+ if (this.canBeWeak) {
974
+ this.persistent.setWeak(this, weakCallback);
975
+ }
976
+ else {
977
+ this.persistent.reset();
978
+ }
923
979
  }
924
980
  finalize() {
925
981
  this.persistent.reset();
@@ -962,6 +1018,54 @@ class Deferred {
962
1018
  }
963
1019
  }
964
1020
 
1021
+ class Store {
1022
+ constructor() {
1023
+ this._values = [undefined];
1024
+ this._values.length = 4;
1025
+ this._size = 1;
1026
+ this._freeList = [];
1027
+ }
1028
+ add(value) {
1029
+ let id;
1030
+ if (this._freeList.length) {
1031
+ id = this._freeList.shift();
1032
+ }
1033
+ else {
1034
+ id = this._size;
1035
+ this._size++;
1036
+ const capacity = this._values.length;
1037
+ if (id >= capacity) {
1038
+ this._values.length = capacity + (capacity >> 1) + 16;
1039
+ }
1040
+ }
1041
+ value.id = id;
1042
+ this._values[id] = value;
1043
+ }
1044
+ get(id) {
1045
+ return this._values[id];
1046
+ }
1047
+ has(id) {
1048
+ return this._values[id] !== undefined;
1049
+ }
1050
+ remove(id) {
1051
+ const value = this._values[id];
1052
+ if (value) {
1053
+ value.id = 0;
1054
+ this._values[id] = undefined;
1055
+ this._freeList.push(Number(id));
1056
+ }
1057
+ }
1058
+ dispose() {
1059
+ for (let i = 1; i < this._size; ++i) {
1060
+ const value = this._values[i];
1061
+ value === null || value === void 0 ? void 0 : value.dispose();
1062
+ }
1063
+ this._values = [undefined];
1064
+ this._size = 1;
1065
+ this._freeList = [];
1066
+ }
1067
+ }
1068
+
965
1069
  class CleanupHookCallback {
966
1070
  constructor(envObject, fn, arg, order) {
967
1071
  this.envObject = envObject;
@@ -1014,7 +1118,7 @@ class CleanupQueue {
1014
1118
  }
1015
1119
  class NodejsWaitingRequestCounter {
1016
1120
  constructor() {
1017
- this.refHandle = new MessageChannel().port1;
1121
+ this.refHandle = new _MessageChannel().port1;
1018
1122
  this.count = 0;
1019
1123
  }
1020
1124
  increase() {
@@ -1034,10 +1138,12 @@ class NodejsWaitingRequestCounter {
1034
1138
  }
1035
1139
  class Context {
1036
1140
  constructor() {
1037
- this.envStore = new EnvStore();
1141
+ this._isStopping = false;
1142
+ this._canCallIntoJs = true;
1143
+ this.envStore = new Store();
1038
1144
  this.scopeStore = new ScopeStore();
1039
- this.refStore = new RefStore();
1040
- this.deferredStore = new DeferredStore();
1145
+ this.refStore = new Store();
1146
+ this.deferredStore = new Store();
1041
1147
  this.handleStore = new HandleStore();
1042
1148
  this.cbinfoStack = new CallbackInfoStack();
1043
1149
  this.feature = {
@@ -1048,16 +1154,25 @@ class Context {
1048
1154
  supportNewFunction,
1049
1155
  canSetFunctionName,
1050
1156
  setImmediate: _setImmediate,
1051
- Buffer
1157
+ Buffer: _Buffer,
1158
+ MessageChannel: _MessageChannel
1052
1159
  };
1053
1160
  this.cleanupQueue = new CleanupQueue();
1054
1161
  if (typeof process === 'object' && process !== null && typeof process.once === 'function') {
1055
1162
  this.refCounter = new NodejsWaitingRequestCounter();
1056
1163
  process.once('beforeExit', () => {
1057
- this.runCleanup();
1164
+ this.destroy();
1058
1165
  });
1059
1166
  }
1060
1167
  }
1168
+ getRuntimeVersions() {
1169
+ return {
1170
+ version,
1171
+ NAPI_VERSION,
1172
+ NAPI_VERSION_EXPERIMENTAL,
1173
+ NODE_API_DEFAULT_MODULE_API_VERSION
1174
+ };
1175
+ }
1061
1176
  createNotSupportWeakRefError(api, message) {
1062
1177
  return new NotSupportWeakRefError(api, message);
1063
1178
  }
@@ -1073,8 +1188,8 @@ class Context {
1073
1188
  createDeferred(value) {
1074
1189
  return Deferred.create(this, value);
1075
1190
  }
1076
- createEnv(makeDynCall_vppp, makeDynCall_vp) {
1077
- return Env.create(this, makeDynCall_vppp, makeDynCall_vp);
1191
+ createEnv(filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort, nodeBinding) {
1192
+ return newEnv(this, filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort, nodeBinding);
1078
1193
  }
1079
1194
  getCurrentScope() {
1080
1195
  return this.scopeStore.currentScope;
@@ -1117,7 +1232,18 @@ class Context {
1117
1232
  var _a;
1118
1233
  (_a = this.refCounter) === null || _a === void 0 ? void 0 : _a.decrease();
1119
1234
  }
1120
- dispose() {
1235
+ setCanCallIntoJs(value) {
1236
+ this._canCallIntoJs = value;
1237
+ }
1238
+ setStopping(value) {
1239
+ this._isStopping = value;
1240
+ }
1241
+ canCallIntoJs() {
1242
+ return this._canCallIntoJs && !this._isStopping;
1243
+ }
1244
+ destroy() {
1245
+ this.setStopping(true);
1246
+ this.setCanCallIntoJs(false);
1121
1247
  this.runCleanup();
1122
1248
  }
1123
1249
  }
@@ -1132,27 +1258,26 @@ function getDefaultContext() {
1132
1258
  return defaultContext;
1133
1259
  }
1134
1260
 
1135
- const version = "0.38.2";
1136
-
1137
1261
  exports.CallbackInfo = CallbackInfo;
1138
1262
  exports.CallbackInfoStack = CallbackInfoStack;
1139
1263
  exports.ConstHandle = ConstHandle;
1140
1264
  exports.Context = Context;
1141
1265
  exports.Deferred = Deferred;
1142
- exports.DeferredStore = DeferredStore;
1143
1266
  exports.EmnapiError = EmnapiError;
1144
1267
  exports.Env = Env;
1145
- exports.EnvStore = EnvStore;
1146
1268
  exports.Finalizer = Finalizer;
1147
1269
  exports.Handle = Handle;
1148
1270
  exports.HandleScope = HandleScope;
1149
1271
  exports.HandleStore = HandleStore;
1272
+ exports.NAPI_VERSION = NAPI_VERSION;
1273
+ exports.NAPI_VERSION_EXPERIMENTAL = NAPI_VERSION_EXPERIMENTAL;
1274
+ exports.NODE_API_DEFAULT_MODULE_API_VERSION = NODE_API_DEFAULT_MODULE_API_VERSION;
1275
+ exports.NodeEnv = NodeEnv;
1150
1276
  exports.NotSupportBigIntError = NotSupportBigIntError;
1151
1277
  exports.NotSupportBufferError = NotSupportBufferError;
1152
1278
  exports.NotSupportWeakRefError = NotSupportWeakRefError;
1153
1279
  exports.Persistent = Persistent;
1154
1280
  exports.RefBase = RefBase;
1155
- exports.RefStore = RefStore;
1156
1281
  exports.RefTracker = RefTracker;
1157
1282
  exports.Reference = Reference;
1158
1283
  exports.ScopeStore = ScopeStore;