@emnapi/runtime 1.2.0 → 1.3.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.
@@ -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.0";
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 */;
@@ -463,6 +463,8 @@ var RefTracker = /*#__PURE__*/ (function () {
463
463
  this._prev = null;
464
464
  }
465
465
  /** @virtual */
466
+ RefTracker.prototype.dispose = function () { };
467
+ /** @virtual */
466
468
  RefTracker.prototype.finalize = function () { };
467
469
  RefTracker.prototype.link = function (list) {
468
470
  this._prev = list;
@@ -499,17 +501,37 @@ var Finalizer = /*#__PURE__*/ (function () {
499
501
  this._finalizeCallback = _finalizeCallback;
500
502
  this._finalizeData = _finalizeData;
501
503
  this._finalizeHint = _finalizeHint;
504
+ this._makeDynCall_vppp = envObject.makeDynCall_vppp;
502
505
  }
503
506
  Finalizer.prototype.callback = function () { return this._finalizeCallback; };
504
507
  Finalizer.prototype.data = function () { return this._finalizeData; };
505
508
  Finalizer.prototype.hint = function () { return this._finalizeHint; };
509
+ Finalizer.prototype.resetEnv = function () {
510
+ this.envObject = undefined;
511
+ };
506
512
  Finalizer.prototype.resetFinalizer = function () {
507
513
  this._finalizeCallback = 0;
508
514
  this._finalizeData = 0;
509
515
  this._finalizeHint = 0;
510
516
  };
517
+ Finalizer.prototype.callFinalizer = function () {
518
+ var finalize_callback = this._finalizeCallback;
519
+ var finalize_data = this._finalizeData;
520
+ var finalize_hint = this._finalizeHint;
521
+ this.resetFinalizer();
522
+ if (!finalize_callback)
523
+ return;
524
+ var fini = Number(finalize_callback);
525
+ if (!this.envObject) {
526
+ this._makeDynCall_vppp(fini)(0, finalize_data, finalize_hint);
527
+ }
528
+ else {
529
+ this.envObject.callFinalizer(fini, finalize_data, finalize_hint);
530
+ }
531
+ };
511
532
  Finalizer.prototype.dispose = function () {
512
533
  this.envObject = undefined;
534
+ this._makeDynCall_vppp = undefined;
513
535
  };
514
536
  return Finalizer;
515
537
  }());
@@ -517,91 +539,45 @@ var Finalizer = /*#__PURE__*/ (function () {
517
539
  var TrackedFinalizer = /*#__PURE__*/ (function (_super) {
518
540
  __extends(TrackedFinalizer, _super);
519
541
  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);
542
+ var _this = _super.call(this) || this;
543
+ _this._finalizer = new Finalizer(envObject, finalize_callback, finalize_data, finalize_hint);
524
544
  return _this;
525
545
  }
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
546
  TrackedFinalizer.create = function (envObject, finalize_callback, finalize_data, finalize_hint) {
536
- return new TrackedFinalizer(envObject, finalize_callback, finalize_data, finalize_hint);
547
+ var finalizer = new TrackedFinalizer(envObject, finalize_callback, finalize_data, finalize_hint);
548
+ finalizer.link(envObject.finalizing_reflist);
549
+ return finalizer;
550
+ };
551
+ TrackedFinalizer.prototype.data = function () {
552
+ return this._finalizer.data();
537
553
  };
538
554
  TrackedFinalizer.prototype.dispose = function () {
555
+ if (!this._finalizer)
556
+ return;
539
557
  this.unlink();
540
- this.envObject.dequeueFinalizer(this);
558
+ this._finalizer.envObject.dequeueFinalizer(this);
559
+ this._finalizer.dispose();
560
+ this._finalizer = undefined;
541
561
  _super.prototype.dispose.call(this);
542
562
  };
543
563
  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
564
  this.unlink();
552
565
  var error;
553
566
  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
- }
567
+ try {
568
+ this._finalizer.callFinalizer();
563
569
  }
564
- if (deleteMe) {
565
- this.dispose();
570
+ catch (err) {
571
+ caught = true;
572
+ error = err;
566
573
  }
574
+ this.dispose();
567
575
  if (caught) {
568
576
  throw error;
569
577
  }
570
578
  };
571
579
  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));
580
+ }(RefTracker));
605
581
 
606
582
  function throwNodeApiVersionError(moduleName, moduleApiVersion) {
607
583
  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 +673,6 @@ var Env = /*#__PURE__*/ (function () {
697
673
  }
698
674
  return r;
699
675
  };
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
676
  Env.prototype.invokeFinalizerFromGC = function (finalizer) {
713
677
  if (this.moduleApiVersion !== NAPI_VERSION_EXPERIMENTAL) {
714
678
  this.enqueueFinalizer(finalizer);
@@ -749,8 +713,8 @@ var Env = /*#__PURE__*/ (function () {
749
713
  };
750
714
  /** @virtual */
751
715
  Env.prototype.deleteMe = function () {
752
- RefBase.finalizeAll(this.finalizing_reflist);
753
- RefBase.finalizeAll(this.reflist);
716
+ RefTracker.finalizeAll(this.finalizing_reflist);
717
+ RefTracker.finalizeAll(this.reflist);
754
718
  this.tryCatch.extractException();
755
719
  this.ctx.envStore.remove(this.id);
756
720
  };
@@ -758,6 +722,8 @@ var Env = /*#__PURE__*/ (function () {
758
722
  if (this.id === 0)
759
723
  return;
760
724
  this.deleteMe();
725
+ this.finalizing_reflist.dispose();
726
+ this.reflist.dispose();
761
727
  this.id = 0;
762
728
  };
763
729
  Env.prototype.initObjectBinding = function (value) {
@@ -779,7 +745,7 @@ var Env = /*#__PURE__*/ (function () {
779
745
  if (this.instanceData) {
780
746
  this.instanceData.dispose();
781
747
  }
782
- this.instanceData = new RefBase(this, 0, 0 /* Ownership.kRuntime */, finalize_cb, data, finalize_hint);
748
+ this.instanceData = TrackedFinalizer.create(this, finalize_cb, data, finalize_hint);
783
749
  };
784
750
  Env.prototype.getInstanceData = function () {
785
751
  return this.instanceData ? this.instanceData.data() : 0;
@@ -1042,69 +1008,82 @@ var Persistent = /*#__PURE__*/ (function () {
1042
1008
  return Persistent;
1043
1009
  }());
1044
1010
 
1045
- function weakCallback(ref) {
1046
- ref.persistent.reset();
1047
- ref.envObject.invokeFinalizerFromGC(ref);
1048
- }
1011
+ var ReferenceOwnership;
1012
+ (function (ReferenceOwnership) {
1013
+ ReferenceOwnership[ReferenceOwnership["kRuntime"] = 0] = "kRuntime";
1014
+ ReferenceOwnership[ReferenceOwnership["kUserland"] = 1] = "kUserland";
1015
+ })(ReferenceOwnership || (ReferenceOwnership = {}));
1049
1016
  function canBeHeldWeakly(value) {
1050
1017
  return value.isObject() || value.isFunction() || value.isSymbol();
1051
1018
  }
1052
1019
  var Reference = /*#__PURE__*/ (function (_super) {
1053
1020
  __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;
1021
+ function Reference(envObject, handle_id, initialRefcount, ownership) {
1022
+ var _this = _super.call(this) || this;
1023
+ _this.envObject = envObject;
1024
+ _this._refcount = initialRefcount;
1025
+ _this._ownership = ownership;
1026
+ var handle = envObject.ctx.handleStore.get(handle_id);
1027
+ _this.canBeWeak = canBeHeldWeakly(handle);
1028
+ _this.persistent = new Persistent(handle.value);
1059
1029
  _this.id = 0;
1030
+ if (initialRefcount === 0) {
1031
+ _this._setWeak();
1032
+ }
1060
1033
  return _this;
1061
1034
  }
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);
1035
+ Reference.weakCallback = function (ref) {
1036
+ ref.persistent.reset();
1037
+ ref.invokeFinalizerFromGC();
1038
+ };
1039
+ Reference.create = function (envObject, handle_id, initialRefcount, ownership, _unused1, _unused2, _unused3) {
1040
+ var ref = new Reference(envObject, handle_id, initialRefcount, ownership);
1068
1041
  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
- }
1042
+ ref.link(envObject.reflist);
1074
1043
  return ref;
1075
1044
  };
1076
1045
  Reference.prototype.ref = function () {
1077
1046
  if (this.persistent.isEmpty()) {
1078
1047
  return 0;
1079
1048
  }
1080
- var count = _super.prototype.ref.call(this);
1081
- if (count === 1 && this.canBeWeak) {
1049
+ if (++this._refcount === 1 && this.canBeWeak) {
1082
1050
  this.persistent.clearWeak();
1083
1051
  }
1084
- return count;
1052
+ return this._refcount;
1085
1053
  };
1086
1054
  Reference.prototype.unref = function () {
1087
- if (this.persistent.isEmpty()) {
1055
+ if (this.persistent.isEmpty() || this._refcount === 0) {
1088
1056
  return 0;
1089
1057
  }
1090
- var oldRefcount = this.refCount();
1091
- var refcount = _super.prototype.unref.call(this);
1092
- if (oldRefcount === 1 && refcount === 0) {
1058
+ if (--this._refcount === 0) {
1093
1059
  this._setWeak();
1094
1060
  }
1095
- return refcount;
1061
+ return this._refcount;
1096
1062
  };
1097
- Reference.prototype.get = function () {
1063
+ Reference.prototype.get = function (envObject) {
1064
+ if (envObject === void 0) { envObject = this.envObject; }
1098
1065
  if (this.persistent.isEmpty()) {
1099
1066
  return 0;
1100
1067
  }
1101
1068
  var obj = this.persistent.deref();
1102
- var handle = this.envObject.ensureHandle(obj);
1069
+ var handle = envObject.ensureHandle(obj);
1103
1070
  return handle.id;
1104
1071
  };
1072
+ /** @virtual */
1073
+ Reference.prototype.resetFinalizer = function () { };
1074
+ /** @virtual */
1075
+ Reference.prototype.data = function () { return 0; };
1076
+ Reference.prototype.refcount = function () { return this._refcount; };
1077
+ Reference.prototype.ownership = function () { return this._ownership; };
1078
+ /** @virtual */
1079
+ Reference.prototype.callUserFinalizer = function () { };
1080
+ /** @virtual */
1081
+ Reference.prototype.invokeFinalizerFromGC = function () {
1082
+ this.finalize();
1083
+ };
1105
1084
  Reference.prototype._setWeak = function () {
1106
1085
  if (this.canBeWeak) {
1107
- this.persistent.setWeak(this, weakCallback);
1086
+ this.persistent.setWeak(this, Reference.weakCallback);
1108
1087
  }
1109
1088
  else {
1110
1089
  this.persistent.reset();
@@ -1112,18 +1091,78 @@ var Reference = /*#__PURE__*/ (function (_super) {
1112
1091
  };
1113
1092
  Reference.prototype.finalize = function () {
1114
1093
  this.persistent.reset();
1115
- _super.prototype.finalize.call(this);
1094
+ var deleteMe = this._ownership === ReferenceOwnership.kRuntime;
1095
+ this.unlink();
1096
+ this.callUserFinalizer();
1097
+ if (deleteMe) {
1098
+ this.dispose();
1099
+ }
1116
1100
  };
1117
1101
  Reference.prototype.dispose = function () {
1118
1102
  if (this.id === 0)
1119
1103
  return;
1104
+ this.unlink();
1120
1105
  this.persistent.reset();
1121
1106
  this.envObject.ctx.refStore.remove(this.id);
1122
1107
  _super.prototype.dispose.call(this);
1108
+ this.envObject = undefined;
1123
1109
  this.id = 0;
1124
1110
  };
1125
1111
  return Reference;
1126
- }(RefBase));
1112
+ }(RefTracker));
1113
+ var ReferenceWithData = /*#__PURE__*/ (function (_super) {
1114
+ __extends(ReferenceWithData, _super);
1115
+ function ReferenceWithData(envObject, value, initialRefcount, ownership, _data) {
1116
+ var _this = _super.call(this, envObject, value, initialRefcount, ownership) || this;
1117
+ _this._data = _data;
1118
+ return _this;
1119
+ }
1120
+ ReferenceWithData.create = function (envObject, value, initialRefcount, ownership, data) {
1121
+ var reference = new ReferenceWithData(envObject, value, initialRefcount, ownership, data);
1122
+ envObject.ctx.refStore.add(reference);
1123
+ reference.link(envObject.reflist);
1124
+ return reference;
1125
+ };
1126
+ ReferenceWithData.prototype.data = function () {
1127
+ return this._data;
1128
+ };
1129
+ return ReferenceWithData;
1130
+ }(Reference));
1131
+ var ReferenceWithFinalizer = /*#__PURE__*/ (function (_super) {
1132
+ __extends(ReferenceWithFinalizer, _super);
1133
+ function ReferenceWithFinalizer(envObject, value, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint) {
1134
+ var _this = _super.call(this, envObject, value, initialRefcount, ownership) || this;
1135
+ _this._finalizer = new Finalizer(envObject, finalize_callback, finalize_data, finalize_hint);
1136
+ return _this;
1137
+ }
1138
+ ReferenceWithFinalizer.create = function (envObject, value, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint) {
1139
+ var reference = new ReferenceWithFinalizer(envObject, value, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint);
1140
+ envObject.ctx.refStore.add(reference);
1141
+ reference.link(envObject.finalizing_reflist);
1142
+ return reference;
1143
+ };
1144
+ ReferenceWithFinalizer.prototype.resetFinalizer = function () {
1145
+ this._finalizer.resetFinalizer();
1146
+ };
1147
+ ReferenceWithFinalizer.prototype.data = function () {
1148
+ return this._finalizer.data();
1149
+ };
1150
+ ReferenceWithFinalizer.prototype.callUserFinalizer = function () {
1151
+ this._finalizer.callFinalizer();
1152
+ };
1153
+ ReferenceWithFinalizer.prototype.invokeFinalizerFromGC = function () {
1154
+ this._finalizer.envObject.invokeFinalizerFromGC(this);
1155
+ };
1156
+ ReferenceWithFinalizer.prototype.dispose = function () {
1157
+ if (!this._finalizer)
1158
+ return;
1159
+ this._finalizer.envObject.dequeueFinalizer(this);
1160
+ this._finalizer.dispose();
1161
+ _super.prototype.dispose.call(this);
1162
+ this._finalizer = undefined;
1163
+ };
1164
+ return ReferenceWithFinalizer;
1165
+ }(Reference));
1127
1166
 
1128
1167
  var Deferred = /*#__PURE__*/ (function () {
1129
1168
  function Deferred(ctx, value) {
@@ -1280,6 +1319,7 @@ var Context = /*#__PURE__*/ (function () {
1280
1319
  var _this = this;
1281
1320
  this._isStopping = false;
1282
1321
  this._canCallIntoJs = true;
1322
+ this._suppressDestroy = false;
1283
1323
  this.envStore = new Store();
1284
1324
  this.scopeStore = new ScopeStore();
1285
1325
  this.refStore = new Store();
@@ -1301,10 +1341,23 @@ var Context = /*#__PURE__*/ (function () {
1301
1341
  if (typeof process === 'object' && process !== null && typeof process.once === 'function') {
1302
1342
  this.refCounter = new NodejsWaitingRequestCounter();
1303
1343
  process.once('beforeExit', function () {
1304
- _this.destroy();
1344
+ if (!_this._suppressDestroy) {
1345
+ _this.destroy();
1346
+ }
1305
1347
  });
1306
1348
  }
1307
1349
  }
1350
+ /**
1351
+ * Suppress the destroy on `beforeExit` event in Node.js.
1352
+ * Call this method if you want to keep the context and
1353
+ * all associated {@link Env | Env} alive,
1354
+ * this also means that cleanup hooks will not be called.
1355
+ * After call this method, you should call
1356
+ * {@link Context.destroy | `Context.prototype.destroy`} method manually.
1357
+ */
1358
+ Context.prototype.suppressDestroy = function () {
1359
+ this._suppressDestroy = true;
1360
+ };
1308
1361
  // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
1309
1362
  Context.prototype.getRuntimeVersions = function () {
1310
1363
  return {
@@ -1320,11 +1373,17 @@ var Context = /*#__PURE__*/ (function () {
1320
1373
  Context.prototype.createNotSupportBufferError = function (api, message) {
1321
1374
  return new NotSupportBufferError(api, message);
1322
1375
  };
1323
- Context.prototype.createReference = function (envObject, handle_id, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint) {
1376
+ Context.prototype.createReference = function (envObject, handle_id, initialRefcount, ownership) {
1377
+ return Reference.create(envObject, handle_id, initialRefcount, ownership);
1378
+ };
1379
+ Context.prototype.createReferenceWithData = function (envObject, handle_id, initialRefcount, ownership, data) {
1380
+ return ReferenceWithData.create(envObject, handle_id, initialRefcount, ownership, data);
1381
+ };
1382
+ Context.prototype.createReferenceWithFinalizer = function (envObject, handle_id, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint) {
1324
1383
  if (finalize_callback === void 0) { finalize_callback = 0; }
1325
1384
  if (finalize_data === void 0) { finalize_data = 0; }
1326
1385
  if (finalize_hint === void 0) { finalize_hint = 0; }
1327
- return Reference.create(envObject, handle_id, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint);
1386
+ return ReferenceWithFinalizer.create(envObject, handle_id, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint);
1328
1387
  };
1329
1388
  Context.prototype.createDeferred = function (value) {
1330
1389
  return Deferred.create(this, value);
@@ -1385,6 +1444,10 @@ var Context = /*#__PURE__*/ (function () {
1385
1444
  Context.prototype.canCallIntoJs = function () {
1386
1445
  return this._canCallIntoJs && !this._isStopping;
1387
1446
  };
1447
+ /**
1448
+ * Destroy the context and call cleanup hooks.
1449
+ * Associated {@link Env | Env} will be destroyed.
1450
+ */
1388
1451
  Context.prototype.destroy = function () {
1389
1452
  this.setStopping(true);
1390
1453
  this.setCanCallIntoJs(false);
@@ -1403,4 +1466,4 @@ function getDefaultContext() {
1403
1466
  return defaultContext;
1404
1467
  }
1405
1468
 
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 };
1469
+ 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 {