@emnapi/runtime 1.2.0 → 1.3.1

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.
@@ -210,7 +210,7 @@ var _Buffer = typeof Buffer === 'function'
210
210
  catch (_) { }
211
211
  return undefined;
212
212
  })();
213
- var version = "1.2.0";
213
+ var version = "1.3.1";
214
214
  var NODE_API_SUPPORTED_VERSION_MIN = 1 /* Version.NODE_API_SUPPORTED_VERSION_MIN */;
215
215
  var NODE_API_SUPPORTED_VERSION_MAX = 9 /* Version.NODE_API_SUPPORTED_VERSION_MAX */;
216
216
  var NAPI_VERSION_EXPERIMENTAL = 2147483647 /* Version.NAPI_VERSION_EXPERIMENTAL */;
@@ -252,6 +252,8 @@ var Handle = /*#__PURE__*/ (function () {
252
252
  return (ArrayBuffer.isView(this.value)) && !(this.value instanceof DataView);
253
253
  };
254
254
  Handle.prototype.isBuffer = function (BufferConstructor) {
255
+ if (ArrayBuffer.isView(this.value))
256
+ return true;
255
257
  BufferConstructor !== null && BufferConstructor !== void 0 ? BufferConstructor : (BufferConstructor = _Buffer);
256
258
  return typeof BufferConstructor === 'function' && BufferConstructor.isBuffer(this.value);
257
259
  };
@@ -463,6 +465,8 @@ var RefTracker = /*#__PURE__*/ (function () {
463
465
  this._prev = null;
464
466
  }
465
467
  /** @virtual */
468
+ RefTracker.prototype.dispose = function () { };
469
+ /** @virtual */
466
470
  RefTracker.prototype.finalize = function () { };
467
471
  RefTracker.prototype.link = function (list) {
468
472
  this._prev = list;
@@ -499,17 +503,37 @@ var Finalizer = /*#__PURE__*/ (function () {
499
503
  this._finalizeCallback = _finalizeCallback;
500
504
  this._finalizeData = _finalizeData;
501
505
  this._finalizeHint = _finalizeHint;
506
+ this._makeDynCall_vppp = envObject.makeDynCall_vppp;
502
507
  }
503
508
  Finalizer.prototype.callback = function () { return this._finalizeCallback; };
504
509
  Finalizer.prototype.data = function () { return this._finalizeData; };
505
510
  Finalizer.prototype.hint = function () { return this._finalizeHint; };
511
+ Finalizer.prototype.resetEnv = function () {
512
+ this.envObject = undefined;
513
+ };
506
514
  Finalizer.prototype.resetFinalizer = function () {
507
515
  this._finalizeCallback = 0;
508
516
  this._finalizeData = 0;
509
517
  this._finalizeHint = 0;
510
518
  };
519
+ Finalizer.prototype.callFinalizer = function () {
520
+ var finalize_callback = this._finalizeCallback;
521
+ var finalize_data = this._finalizeData;
522
+ var finalize_hint = this._finalizeHint;
523
+ this.resetFinalizer();
524
+ if (!finalize_callback)
525
+ return;
526
+ var fini = Number(finalize_callback);
527
+ if (!this.envObject) {
528
+ this._makeDynCall_vppp(fini)(0, finalize_data, finalize_hint);
529
+ }
530
+ else {
531
+ this.envObject.callFinalizer(fini, finalize_data, finalize_hint);
532
+ }
533
+ };
511
534
  Finalizer.prototype.dispose = function () {
512
535
  this.envObject = undefined;
536
+ this._makeDynCall_vppp = undefined;
513
537
  };
514
538
  return Finalizer;
515
539
  }());
@@ -517,91 +541,45 @@ var Finalizer = /*#__PURE__*/ (function () {
517
541
  var TrackedFinalizer = /*#__PURE__*/ (function (_super) {
518
542
  __extends(TrackedFinalizer, _super);
519
543
  function TrackedFinalizer(envObject, finalize_callback, finalize_data, finalize_hint) {
520
- var _this = _super.call(this, envObject, finalize_callback, finalize_data, finalize_hint) || this;
521
- _this._next = null;
522
- _this._prev = null;
523
- _this.link(!finalize_callback ? envObject.reflist : envObject.finalizing_reflist);
544
+ var _this = _super.call(this) || this;
545
+ _this._finalizer = new Finalizer(envObject, finalize_callback, finalize_data, finalize_hint);
524
546
  return _this;
525
547
  }
526
- TrackedFinalizer.finalizeAll = function (list) {
527
- RefTracker.finalizeAll(list);
528
- };
529
- TrackedFinalizer.prototype.link = function (list) {
530
- RefTracker.prototype.link.call(this, list);
531
- };
532
- TrackedFinalizer.prototype.unlink = function () {
533
- RefTracker.prototype.unlink.call(this);
534
- };
535
548
  TrackedFinalizer.create = function (envObject, finalize_callback, finalize_data, finalize_hint) {
536
- return new TrackedFinalizer(envObject, finalize_callback, finalize_data, finalize_hint);
549
+ var finalizer = new TrackedFinalizer(envObject, finalize_callback, finalize_data, finalize_hint);
550
+ finalizer.link(envObject.finalizing_reflist);
551
+ return finalizer;
552
+ };
553
+ TrackedFinalizer.prototype.data = function () {
554
+ return this._finalizer.data();
537
555
  };
538
556
  TrackedFinalizer.prototype.dispose = function () {
557
+ if (!this._finalizer)
558
+ return;
539
559
  this.unlink();
540
- this.envObject.dequeueFinalizer(this);
560
+ this._finalizer.envObject.dequeueFinalizer(this);
561
+ this._finalizer.dispose();
562
+ this._finalizer = undefined;
541
563
  _super.prototype.dispose.call(this);
542
564
  };
543
565
  TrackedFinalizer.prototype.finalize = function () {
544
- this.finalizeCore(true);
545
- };
546
- TrackedFinalizer.prototype.finalizeCore = function (deleteMe) {
547
- var finalize_callback = this._finalizeCallback;
548
- var finalize_data = this._finalizeData;
549
- var finalize_hint = this._finalizeHint;
550
- this.resetFinalizer();
551
566
  this.unlink();
552
567
  var error;
553
568
  var caught = false;
554
- if (finalize_callback) {
555
- var fini = Number(finalize_callback);
556
- try {
557
- this.envObject.callFinalizer(fini, finalize_data, finalize_hint);
558
- }
559
- catch (err) {
560
- caught = true;
561
- error = err;
562
- }
569
+ try {
570
+ this._finalizer.callFinalizer();
563
571
  }
564
- if (deleteMe) {
565
- this.dispose();
572
+ catch (err) {
573
+ caught = true;
574
+ error = err;
566
575
  }
576
+ this.dispose();
567
577
  if (caught) {
568
578
  throw error;
569
579
  }
570
580
  };
571
581
  return TrackedFinalizer;
572
- }(Finalizer));
573
-
574
- var RefBase = /*#__PURE__*/ (function (_super) {
575
- __extends(RefBase, _super);
576
- function RefBase(envObject, initial_refcount, ownership, finalize_callback, finalize_data, finalize_hint) {
577
- var _this = _super.call(this, envObject, finalize_callback, finalize_data, finalize_hint) || this;
578
- _this._refcount = initial_refcount;
579
- _this._ownership = ownership;
580
- return _this;
581
- }
582
- RefBase.prototype.data = function () {
583
- return this._finalizeData;
584
- };
585
- RefBase.prototype.ref = function () {
586
- return ++this._refcount;
587
- };
588
- RefBase.prototype.unref = function () {
589
- if (this._refcount === 0) {
590
- return 0;
591
- }
592
- return --this._refcount;
593
- };
594
- RefBase.prototype.refCount = function () {
595
- return this._refcount;
596
- };
597
- RefBase.prototype.ownership = function () {
598
- return this._ownership;
599
- };
600
- RefBase.prototype.finalize = function () {
601
- this.finalizeCore(this._ownership === 0 /* Ownership.kRuntime */);
602
- };
603
- return RefBase;
604
- }(TrackedFinalizer));
582
+ }(RefTracker));
605
583
 
606
584
  function throwNodeApiVersionError(moduleName, moduleApiVersion) {
607
585
  var errorMessage = "".concat(moduleName, " requires Node-API version ").concat(moduleApiVersion, ", but this version of Node.js only supports version ").concat(NODE_API_SUPPORTED_VERSION_MAX, " add-ons.");
@@ -697,18 +675,6 @@ var Env = /*#__PURE__*/ (function () {
697
675
  }
698
676
  return r;
699
677
  };
700
- /** @virtual */
701
- Env.prototype.callFinalizer = function (cb, data, hint) {
702
- var f = this.makeDynCall_vppp(cb);
703
- var env = this.id;
704
- var scope = this.ctx.openScope(this);
705
- try {
706
- this.callIntoModule(function () { f(env, data, hint); });
707
- }
708
- finally {
709
- this.ctx.closeScope(this, scope);
710
- }
711
- };
712
678
  Env.prototype.invokeFinalizerFromGC = function (finalizer) {
713
679
  if (this.moduleApiVersion !== NAPI_VERSION_EXPERIMENTAL) {
714
680
  this.enqueueFinalizer(finalizer);
@@ -749,8 +715,8 @@ var Env = /*#__PURE__*/ (function () {
749
715
  };
750
716
  /** @virtual */
751
717
  Env.prototype.deleteMe = function () {
752
- RefBase.finalizeAll(this.finalizing_reflist);
753
- RefBase.finalizeAll(this.reflist);
718
+ RefTracker.finalizeAll(this.finalizing_reflist);
719
+ RefTracker.finalizeAll(this.reflist);
754
720
  this.tryCatch.extractException();
755
721
  this.ctx.envStore.remove(this.id);
756
722
  };
@@ -758,6 +724,8 @@ var Env = /*#__PURE__*/ (function () {
758
724
  if (this.id === 0)
759
725
  return;
760
726
  this.deleteMe();
727
+ this.finalizing_reflist.dispose();
728
+ this.reflist.dispose();
761
729
  this.id = 0;
762
730
  };
763
731
  Env.prototype.initObjectBinding = function (value) {
@@ -779,7 +747,7 @@ var Env = /*#__PURE__*/ (function () {
779
747
  if (this.instanceData) {
780
748
  this.instanceData.dispose();
781
749
  }
782
- this.instanceData = new RefBase(this, 0, 0 /* Ownership.kRuntime */, finalize_cb, data, finalize_hint);
750
+ this.instanceData = TrackedFinalizer.create(this, finalize_cb, data, finalize_hint);
783
751
  };
784
752
  Env.prototype.getInstanceData = function () {
785
753
  return this.instanceData ? this.instanceData.data() : 0;
@@ -1042,69 +1010,82 @@ var Persistent = /*#__PURE__*/ (function () {
1042
1010
  return Persistent;
1043
1011
  }());
1044
1012
 
1045
- function weakCallback(ref) {
1046
- ref.persistent.reset();
1047
- ref.envObject.invokeFinalizerFromGC(ref);
1048
- }
1013
+ var ReferenceOwnership;
1014
+ (function (ReferenceOwnership) {
1015
+ ReferenceOwnership[ReferenceOwnership["kRuntime"] = 0] = "kRuntime";
1016
+ ReferenceOwnership[ReferenceOwnership["kUserland"] = 1] = "kUserland";
1017
+ })(ReferenceOwnership || (ReferenceOwnership = {}));
1049
1018
  function canBeHeldWeakly(value) {
1050
1019
  return value.isObject() || value.isFunction() || value.isSymbol();
1051
1020
  }
1052
1021
  var Reference = /*#__PURE__*/ (function (_super) {
1053
1022
  __extends(Reference, _super);
1054
- function Reference(envObject, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint) {
1055
- if (finalize_callback === void 0) { finalize_callback = 0; }
1056
- if (finalize_data === void 0) { finalize_data = 0; }
1057
- if (finalize_hint === void 0) { finalize_hint = 0; }
1058
- var _this = _super.call(this, envObject, initialRefcount >>> 0, ownership, finalize_callback, finalize_data, finalize_hint) || this;
1023
+ function Reference(envObject, handle_id, initialRefcount, ownership) {
1024
+ var _this = _super.call(this) || this;
1025
+ _this.envObject = envObject;
1026
+ _this._refcount = initialRefcount;
1027
+ _this._ownership = ownership;
1028
+ var handle = envObject.ctx.handleStore.get(handle_id);
1029
+ _this.canBeWeak = canBeHeldWeakly(handle);
1030
+ _this.persistent = new Persistent(handle.value);
1059
1031
  _this.id = 0;
1032
+ if (initialRefcount === 0) {
1033
+ _this._setWeak();
1034
+ }
1060
1035
  return _this;
1061
1036
  }
1062
- Reference.create = function (envObject, handle_id, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint) {
1063
- if (finalize_callback === void 0) { finalize_callback = 0; }
1064
- if (finalize_data === void 0) { finalize_data = 0; }
1065
- if (finalize_hint === void 0) { finalize_hint = 0; }
1066
- var handle = envObject.ctx.handleStore.get(handle_id);
1067
- var ref = new Reference(envObject, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint);
1037
+ Reference.weakCallback = function (ref) {
1038
+ ref.persistent.reset();
1039
+ ref.invokeFinalizerFromGC();
1040
+ };
1041
+ Reference.create = function (envObject, handle_id, initialRefcount, ownership, _unused1, _unused2, _unused3) {
1042
+ var ref = new Reference(envObject, handle_id, initialRefcount, ownership);
1068
1043
  envObject.ctx.refStore.add(ref);
1069
- ref.canBeWeak = canBeHeldWeakly(handle);
1070
- ref.persistent = new Persistent(handle.value);
1071
- if (initialRefcount === 0) {
1072
- ref._setWeak();
1073
- }
1044
+ ref.link(envObject.reflist);
1074
1045
  return ref;
1075
1046
  };
1076
1047
  Reference.prototype.ref = function () {
1077
1048
  if (this.persistent.isEmpty()) {
1078
1049
  return 0;
1079
1050
  }
1080
- var count = _super.prototype.ref.call(this);
1081
- if (count === 1 && this.canBeWeak) {
1051
+ if (++this._refcount === 1 && this.canBeWeak) {
1082
1052
  this.persistent.clearWeak();
1083
1053
  }
1084
- return count;
1054
+ return this._refcount;
1085
1055
  };
1086
1056
  Reference.prototype.unref = function () {
1087
- if (this.persistent.isEmpty()) {
1057
+ if (this.persistent.isEmpty() || this._refcount === 0) {
1088
1058
  return 0;
1089
1059
  }
1090
- var oldRefcount = this.refCount();
1091
- var refcount = _super.prototype.unref.call(this);
1092
- if (oldRefcount === 1 && refcount === 0) {
1060
+ if (--this._refcount === 0) {
1093
1061
  this._setWeak();
1094
1062
  }
1095
- return refcount;
1063
+ return this._refcount;
1096
1064
  };
1097
- Reference.prototype.get = function () {
1065
+ Reference.prototype.get = function (envObject) {
1066
+ if (envObject === void 0) { envObject = this.envObject; }
1098
1067
  if (this.persistent.isEmpty()) {
1099
1068
  return 0;
1100
1069
  }
1101
1070
  var obj = this.persistent.deref();
1102
- var handle = this.envObject.ensureHandle(obj);
1071
+ var handle = envObject.ensureHandle(obj);
1103
1072
  return handle.id;
1104
1073
  };
1074
+ /** @virtual */
1075
+ Reference.prototype.resetFinalizer = function () { };
1076
+ /** @virtual */
1077
+ Reference.prototype.data = function () { return 0; };
1078
+ Reference.prototype.refcount = function () { return this._refcount; };
1079
+ Reference.prototype.ownership = function () { return this._ownership; };
1080
+ /** @virtual */
1081
+ Reference.prototype.callUserFinalizer = function () { };
1082
+ /** @virtual */
1083
+ Reference.prototype.invokeFinalizerFromGC = function () {
1084
+ this.finalize();
1085
+ };
1105
1086
  Reference.prototype._setWeak = function () {
1106
1087
  if (this.canBeWeak) {
1107
- this.persistent.setWeak(this, weakCallback);
1088
+ this.persistent.setWeak(this, Reference.weakCallback);
1108
1089
  }
1109
1090
  else {
1110
1091
  this.persistent.reset();
@@ -1112,18 +1093,78 @@ var Reference = /*#__PURE__*/ (function (_super) {
1112
1093
  };
1113
1094
  Reference.prototype.finalize = function () {
1114
1095
  this.persistent.reset();
1115
- _super.prototype.finalize.call(this);
1096
+ var deleteMe = this._ownership === ReferenceOwnership.kRuntime;
1097
+ this.unlink();
1098
+ this.callUserFinalizer();
1099
+ if (deleteMe) {
1100
+ this.dispose();
1101
+ }
1116
1102
  };
1117
1103
  Reference.prototype.dispose = function () {
1118
1104
  if (this.id === 0)
1119
1105
  return;
1106
+ this.unlink();
1120
1107
  this.persistent.reset();
1121
1108
  this.envObject.ctx.refStore.remove(this.id);
1122
1109
  _super.prototype.dispose.call(this);
1110
+ this.envObject = undefined;
1123
1111
  this.id = 0;
1124
1112
  };
1125
1113
  return Reference;
1126
- }(RefBase));
1114
+ }(RefTracker));
1115
+ var ReferenceWithData = /*#__PURE__*/ (function (_super) {
1116
+ __extends(ReferenceWithData, _super);
1117
+ function ReferenceWithData(envObject, value, initialRefcount, ownership, _data) {
1118
+ var _this = _super.call(this, envObject, value, initialRefcount, ownership) || this;
1119
+ _this._data = _data;
1120
+ return _this;
1121
+ }
1122
+ ReferenceWithData.create = function (envObject, value, initialRefcount, ownership, data) {
1123
+ var reference = new ReferenceWithData(envObject, value, initialRefcount, ownership, data);
1124
+ envObject.ctx.refStore.add(reference);
1125
+ reference.link(envObject.reflist);
1126
+ return reference;
1127
+ };
1128
+ ReferenceWithData.prototype.data = function () {
1129
+ return this._data;
1130
+ };
1131
+ return ReferenceWithData;
1132
+ }(Reference));
1133
+ var ReferenceWithFinalizer = /*#__PURE__*/ (function (_super) {
1134
+ __extends(ReferenceWithFinalizer, _super);
1135
+ function ReferenceWithFinalizer(envObject, value, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint) {
1136
+ var _this = _super.call(this, envObject, value, initialRefcount, ownership) || this;
1137
+ _this._finalizer = new Finalizer(envObject, finalize_callback, finalize_data, finalize_hint);
1138
+ return _this;
1139
+ }
1140
+ ReferenceWithFinalizer.create = function (envObject, value, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint) {
1141
+ var reference = new ReferenceWithFinalizer(envObject, value, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint);
1142
+ envObject.ctx.refStore.add(reference);
1143
+ reference.link(envObject.finalizing_reflist);
1144
+ return reference;
1145
+ };
1146
+ ReferenceWithFinalizer.prototype.resetFinalizer = function () {
1147
+ this._finalizer.resetFinalizer();
1148
+ };
1149
+ ReferenceWithFinalizer.prototype.data = function () {
1150
+ return this._finalizer.data();
1151
+ };
1152
+ ReferenceWithFinalizer.prototype.callUserFinalizer = function () {
1153
+ this._finalizer.callFinalizer();
1154
+ };
1155
+ ReferenceWithFinalizer.prototype.invokeFinalizerFromGC = function () {
1156
+ this._finalizer.envObject.invokeFinalizerFromGC(this);
1157
+ };
1158
+ ReferenceWithFinalizer.prototype.dispose = function () {
1159
+ if (!this._finalizer)
1160
+ return;
1161
+ this._finalizer.envObject.dequeueFinalizer(this);
1162
+ this._finalizer.dispose();
1163
+ _super.prototype.dispose.call(this);
1164
+ this._finalizer = undefined;
1165
+ };
1166
+ return ReferenceWithFinalizer;
1167
+ }(Reference));
1127
1168
 
1128
1169
  var Deferred = /*#__PURE__*/ (function () {
1129
1170
  function Deferred(ctx, value) {
@@ -1280,6 +1321,7 @@ var Context = /*#__PURE__*/ (function () {
1280
1321
  var _this = this;
1281
1322
  this._isStopping = false;
1282
1323
  this._canCallIntoJs = true;
1324
+ this._suppressDestroy = false;
1283
1325
  this.envStore = new Store();
1284
1326
  this.scopeStore = new ScopeStore();
1285
1327
  this.refStore = new Store();
@@ -1301,10 +1343,23 @@ var Context = /*#__PURE__*/ (function () {
1301
1343
  if (typeof process === 'object' && process !== null && typeof process.once === 'function') {
1302
1344
  this.refCounter = new NodejsWaitingRequestCounter();
1303
1345
  process.once('beforeExit', function () {
1304
- _this.destroy();
1346
+ if (!_this._suppressDestroy) {
1347
+ _this.destroy();
1348
+ }
1305
1349
  });
1306
1350
  }
1307
1351
  }
1352
+ /**
1353
+ * Suppress the destroy on `beforeExit` event in Node.js.
1354
+ * Call this method if you want to keep the context and
1355
+ * all associated {@link Env | Env} alive,
1356
+ * this also means that cleanup hooks will not be called.
1357
+ * After call this method, you should call
1358
+ * {@link Context.destroy | `Context.prototype.destroy`} method manually.
1359
+ */
1360
+ Context.prototype.suppressDestroy = function () {
1361
+ this._suppressDestroy = true;
1362
+ };
1308
1363
  // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
1309
1364
  Context.prototype.getRuntimeVersions = function () {
1310
1365
  return {
@@ -1320,11 +1375,17 @@ var Context = /*#__PURE__*/ (function () {
1320
1375
  Context.prototype.createNotSupportBufferError = function (api, message) {
1321
1376
  return new NotSupportBufferError(api, message);
1322
1377
  };
1323
- Context.prototype.createReference = function (envObject, handle_id, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint) {
1378
+ Context.prototype.createReference = function (envObject, handle_id, initialRefcount, ownership) {
1379
+ return Reference.create(envObject, handle_id, initialRefcount, ownership);
1380
+ };
1381
+ Context.prototype.createReferenceWithData = function (envObject, handle_id, initialRefcount, ownership, data) {
1382
+ return ReferenceWithData.create(envObject, handle_id, initialRefcount, ownership, data);
1383
+ };
1384
+ Context.prototype.createReferenceWithFinalizer = function (envObject, handle_id, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint) {
1324
1385
  if (finalize_callback === void 0) { finalize_callback = 0; }
1325
1386
  if (finalize_data === void 0) { finalize_data = 0; }
1326
1387
  if (finalize_hint === void 0) { finalize_hint = 0; }
1327
- return Reference.create(envObject, handle_id, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint);
1388
+ return ReferenceWithFinalizer.create(envObject, handle_id, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint);
1328
1389
  };
1329
1390
  Context.prototype.createDeferred = function (value) {
1330
1391
  return Deferred.create(this, value);
@@ -1385,6 +1446,10 @@ var Context = /*#__PURE__*/ (function () {
1385
1446
  Context.prototype.canCallIntoJs = function () {
1386
1447
  return this._canCallIntoJs && !this._isStopping;
1387
1448
  };
1449
+ /**
1450
+ * Destroy the context and call cleanup hooks.
1451
+ * Associated {@link Env | Env} will be destroyed.
1452
+ */
1388
1453
  Context.prototype.destroy = function () {
1389
1454
  this.setStopping(true);
1390
1455
  this.setCanCallIntoJs(false);
@@ -1403,4 +1468,4 @@ function getDefaultContext() {
1403
1468
  return defaultContext;
1404
1469
  }
1405
1470
 
1406
- export { CallbackInfo, CallbackInfoStack, ConstHandle, Context, Deferred, EmnapiError, Env, Finalizer, Handle, HandleScope, HandleStore, NAPI_VERSION_EXPERIMENTAL, NODE_API_DEFAULT_MODULE_API_VERSION, NODE_API_SUPPORTED_VERSION_MAX, NODE_API_SUPPORTED_VERSION_MIN, NodeEnv, NotSupportBufferError, NotSupportWeakRefError, Persistent, RefBase, RefTracker, Reference, ScopeStore, Store, TrackedFinalizer, TryCatch, createContext, getDefaultContext, isReferenceType, version };
1471
+ export { CallbackInfo, CallbackInfoStack, ConstHandle, Context, Deferred, EmnapiError, Env, Finalizer, Handle, HandleScope, HandleStore, NAPI_VERSION_EXPERIMENTAL, NODE_API_DEFAULT_MODULE_API_VERSION, NODE_API_SUPPORTED_VERSION_MAX, NODE_API_SUPPORTED_VERSION_MIN, NodeEnv, NotSupportBufferError, NotSupportWeakRefError, Persistent, RefTracker, Reference, ReferenceOwnership, ReferenceWithData, ReferenceWithFinalizer, ScopeStore, Store, TrackedFinalizer, TryCatch, createContext, getDefaultContext, isReferenceType, version };
@@ -31,6 +31,7 @@ export class ConstHandle<S extends undefined | null | boolean | typeof globalThi
31
31
  export class Context {
32
32
  private _isStopping;
33
33
  private _canCallIntoJs;
34
+ private _suppressDestroy;
34
35
  envStore: Store<Env>;
35
36
  scopeStore: ScopeStore;
36
37
  refStore: Store<Reference>;
@@ -54,6 +55,15 @@ export class Context {
54
55
  } | undefined;
55
56
  };
56
57
  constructor();
58
+ /**
59
+ * Suppress the destroy on `beforeExit` event in Node.js.
60
+ * Call this method if you want to keep the context and
61
+ * all associated {@link Env | Env} alive,
62
+ * this also means that cleanup hooks will not be called.
63
+ * After call this method, you should call
64
+ * {@link Context.destroy | `Context.prototype.destroy`} method manually.
65
+ */
66
+ suppressDestroy(): void;
57
67
  getRuntimeVersions(): {
58
68
  version: string;
59
69
  NODE_API_SUPPORTED_VERSION_MAX: Version;
@@ -62,7 +72,9 @@ export class Context {
62
72
  };
63
73
  createNotSupportWeakRefError(api: string, message: string): NotSupportWeakRefError;
64
74
  createNotSupportBufferError(api: string, message: string): NotSupportBufferError;
65
- createReference(envObject: Env, handle_id: napi_value, initialRefcount: uint32_t, ownership: 0 | 1, finalize_callback?: napi_finalize, finalize_data?: void_p, finalize_hint?: void_p): Reference;
75
+ createReference(envObject: Env, handle_id: napi_value, initialRefcount: uint32_t, ownership: ReferenceOwnership): Reference;
76
+ createReferenceWithData(envObject: Env, handle_id: napi_value, initialRefcount: uint32_t, ownership: ReferenceOwnership, data: void_p): Reference;
77
+ createReferenceWithFinalizer(envObject: Env, handle_id: napi_value, initialRefcount: uint32_t, ownership: ReferenceOwnership, finalize_callback?: napi_finalize, finalize_data?: void_p, finalize_hint?: void_p): Reference;
66
78
  createDeferred<T = any>(value: IDeferrdValue<T>): Deferred<T>;
67
79
  createEnv(filename: string, moduleApiVersion: number, makeDynCall_vppp: (cb: Ptr) => (a: Ptr, b: Ptr, c: Ptr) => void, makeDynCall_vp: (cb: Ptr) => (a: Ptr) => void, abort: (msg?: string) => never, nodeBinding?: any): Env;
68
80
  createTrackedFinalizer(envObject: Env, finalize_callback: napi_finalize, finalize_data: void_p, finalize_hint: void_p): TrackedFinalizer;
@@ -79,6 +91,10 @@ export class Context {
79
91
  setCanCallIntoJs(value: boolean): void;
80
92
  setStopping(value: boolean): void;
81
93
  canCallIntoJs(): boolean;
94
+ /**
95
+ * Destroy the context and call cleanup hooks.
96
+ * Associated {@link Env | Env} will be destroyed.
97
+ */
82
98
  destroy(): void;
83
99
  }
84
100
 
@@ -99,7 +115,7 @@ export class EmnapiError extends Error {
99
115
  constructor(message?: string);
100
116
  }
101
117
 
102
- export class Env implements IStoreValue {
118
+ export abstract class Env implements IStoreValue {
103
119
  readonly ctx: Context;
104
120
  moduleApiVersion: number;
105
121
  makeDynCall_vppp: (cb: Ptr) => (a: Ptr, b: Ptr, c: Ptr) => void;
@@ -107,7 +123,7 @@ export class Env implements IStoreValue {
107
123
  abort: (msg?: string) => never;
108
124
  id: number;
109
125
  openHandleScopes: number;
110
- instanceData: RefBase | null;
126
+ instanceData: TrackedFinalizer | null;
111
127
  tryCatch: TryCatch;
112
128
  refs: number;
113
129
  reflist: RefTracker;
@@ -132,7 +148,7 @@ export class Env implements IStoreValue {
132
148
  getReturnStatus(): napi_status;
133
149
  callIntoModule<T>(fn: (env: Env) => T, handleException?: (envObject: Env, value: any) => void): T;
134
150
  /** @virtual */
135
- callFinalizer(cb: napi_finalize, data: void_p, hint: void_p): void;
151
+ abstract callFinalizer(cb: napi_finalize, data: void_p, hint: void_p): void;
136
152
  invokeFinalizerFromGC(finalizer: RefTracker): void;
137
153
  checkGCAccess(): void;
138
154
  /** @virtual */
@@ -151,14 +167,17 @@ export class Env implements IStoreValue {
151
167
 
152
168
  export class Finalizer {
153
169
  envObject: Env;
154
- protected _finalizeCallback: napi_finalize;
155
- protected _finalizeData: void_p;
156
- protected _finalizeHint: void_p;
170
+ private _finalizeCallback;
171
+ private _finalizeData;
172
+ private _finalizeHint;
173
+ private _makeDynCall_vppp;
157
174
  constructor(envObject: Env, _finalizeCallback?: napi_finalize, _finalizeData?: void_p, _finalizeHint?: void_p);
158
175
  callback(): napi_finalize;
159
176
  data(): void_p;
160
177
  hint(): void_p;
178
+ resetEnv(): void;
161
179
  resetFinalizer(): void;
180
+ callFinalizer(): void;
162
181
  dispose(): void;
163
182
  }
164
183
 
@@ -285,33 +304,60 @@ export class Persistent<T> {
285
304
  deref(): T | undefined;
286
305
  }
287
306
 
288
- export class RefBase extends TrackedFinalizer {
307
+ export class Reference extends RefTracker implements IStoreValue {
308
+ private static weakCallback;
309
+ id: number;
310
+ envObject: Env;
311
+ private readonly canBeWeak;
289
312
  private _refcount;
290
313
  private readonly _ownership;
291
- constructor(envObject: Env, initial_refcount: uint32_t, ownership: number, finalize_callback: napi_finalize, finalize_data: void_p, finalize_hint: void_p);
292
- data(): void_p;
293
- ref(): uint32_t;
294
- unref(): uint32_t;
295
- refCount(): uint32_t;
296
- ownership(): number;
297
- finalize(): void;
298
- }
299
-
300
- export class Reference extends RefBase implements IStoreValue {
301
- id: number;
302
- private canBeWeak;
303
- static create(envObject: Env, handle_id: napi_value, initialRefcount: uint32_t, ownership: 0 | 1, finalize_callback?: napi_finalize, finalize_data?: void_p, finalize_hint?: void_p): Reference;
304
314
  persistent: Persistent<object>;
305
- private constructor();
315
+ static create(envObject: Env, handle_id: napi_value, initialRefcount: uint32_t, ownership: ReferenceOwnership, _unused1?: void_p, _unused2?: void_p, _unused3?: void_p): Reference;
316
+ protected constructor(envObject: Env, handle_id: napi_value, initialRefcount: uint32_t, ownership: ReferenceOwnership);
306
317
  ref(): number;
307
318
  unref(): number;
308
- get(): napi_value;
319
+ get(envObject?: Env): napi_value;
320
+ /** @virtual */
321
+ resetFinalizer(): void;
322
+ /** @virtual */
323
+ data(): void_p;
324
+ refcount(): number;
325
+ ownership(): ReferenceOwnership;
326
+ /** @virtual */
327
+ protected callUserFinalizer(): void;
328
+ /** @virtual */
329
+ protected invokeFinalizerFromGC(): void;
309
330
  private _setWeak;
310
331
  finalize(): void;
311
332
  dispose(): void;
312
333
  }
313
334
 
335
+ export enum ReferenceOwnership {
336
+ kRuntime = 0,
337
+ kUserland = 1
338
+ }
339
+
340
+ export class ReferenceWithData extends Reference {
341
+ private readonly _data;
342
+ static create(envObject: Env, value: napi_value, initialRefcount: uint32_t, ownership: ReferenceOwnership, data: void_p): ReferenceWithData;
343
+ private constructor();
344
+ data(): void_p;
345
+ }
346
+
347
+ export class ReferenceWithFinalizer extends Reference {
348
+ private _finalizer;
349
+ static create(envObject: Env, value: napi_value, initialRefcount: uint32_t, ownership: ReferenceOwnership, finalize_callback: napi_finalize, finalize_data: void_p, finalize_hint: void_p): ReferenceWithFinalizer;
350
+ private constructor();
351
+ resetFinalizer(): void;
352
+ data(): void_p;
353
+ protected callUserFinalizer(): void;
354
+ protected invokeFinalizerFromGC(): void;
355
+ dispose(): void;
356
+ }
357
+
314
358
  export class RefTracker {
359
+ /** @virtual */
360
+ dispose(): void;
315
361
  /** @virtual */
316
362
  finalize(): void;
317
363
  private _next;
@@ -343,18 +389,13 @@ export class Store<V extends IStoreValue> {
343
389
  dispose(): void;
344
390
  }
345
391
 
346
- export interface TrackedFinalizer extends Finalizer, RefTracker {
347
- }
348
-
349
- export class TrackedFinalizer extends Finalizer {
350
- static finalizeAll(list: RefTracker): void;
351
- link(list: RefTracker): void;
352
- unlink(): void;
392
+ export class TrackedFinalizer extends RefTracker {
393
+ private _finalizer;
353
394
  static create(envObject: Env, finalize_callback: napi_finalize, finalize_data: void_p, finalize_hint: void_p): TrackedFinalizer;
354
- protected constructor(envObject: Env, finalize_callback: napi_finalize, finalize_data: void_p, finalize_hint: void_p);
395
+ private constructor();
396
+ data(): void_p;
355
397
  dispose(): void;
356
398
  finalize(): void;
357
- protected finalizeCore(deleteMe: boolean): void;
358
399
  }
359
400
 
360
401
  export class TryCatch {