@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.
package/dist/emnapi.js CHANGED
@@ -87,7 +87,7 @@
87
87
  OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
88
88
  PERFORMANCE OF THIS SOFTWARE.
89
89
  ***************************************************************************** */
90
- /* global Reflect, Promise, SuppressedError, Symbol */
90
+ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
91
91
 
92
92
  var extendStatics = function(d, b) {
93
93
  extendStatics = Object.setPrototypeOf ||
@@ -249,7 +249,7 @@
249
249
  catch (_) { }
250
250
  return undefined;
251
251
  })();
252
- var version = "1.2.0";
252
+ var version = "1.3.0";
253
253
  var NODE_API_SUPPORTED_VERSION_MIN = 1 /* Version.NODE_API_SUPPORTED_VERSION_MIN */;
254
254
  var NODE_API_SUPPORTED_VERSION_MAX = 9 /* Version.NODE_API_SUPPORTED_VERSION_MAX */;
255
255
  var NAPI_VERSION_EXPERIMENTAL = 2147483647 /* Version.NAPI_VERSION_EXPERIMENTAL */;
@@ -502,6 +502,8 @@
502
502
  this._prev = null;
503
503
  }
504
504
  /** @virtual */
505
+ RefTracker.prototype.dispose = function () { };
506
+ /** @virtual */
505
507
  RefTracker.prototype.finalize = function () { };
506
508
  RefTracker.prototype.link = function (list) {
507
509
  this._prev = list;
@@ -538,17 +540,37 @@
538
540
  this._finalizeCallback = _finalizeCallback;
539
541
  this._finalizeData = _finalizeData;
540
542
  this._finalizeHint = _finalizeHint;
543
+ this._makeDynCall_vppp = envObject.makeDynCall_vppp;
541
544
  }
542
545
  Finalizer.prototype.callback = function () { return this._finalizeCallback; };
543
546
  Finalizer.prototype.data = function () { return this._finalizeData; };
544
547
  Finalizer.prototype.hint = function () { return this._finalizeHint; };
548
+ Finalizer.prototype.resetEnv = function () {
549
+ this.envObject = undefined;
550
+ };
545
551
  Finalizer.prototype.resetFinalizer = function () {
546
552
  this._finalizeCallback = 0;
547
553
  this._finalizeData = 0;
548
554
  this._finalizeHint = 0;
549
555
  };
556
+ Finalizer.prototype.callFinalizer = function () {
557
+ var finalize_callback = this._finalizeCallback;
558
+ var finalize_data = this._finalizeData;
559
+ var finalize_hint = this._finalizeHint;
560
+ this.resetFinalizer();
561
+ if (!finalize_callback)
562
+ return;
563
+ var fini = Number(finalize_callback);
564
+ if (!this.envObject) {
565
+ this._makeDynCall_vppp(fini)(0, finalize_data, finalize_hint);
566
+ }
567
+ else {
568
+ this.envObject.callFinalizer(fini, finalize_data, finalize_hint);
569
+ }
570
+ };
550
571
  Finalizer.prototype.dispose = function () {
551
572
  this.envObject = undefined;
573
+ this._makeDynCall_vppp = undefined;
552
574
  };
553
575
  return Finalizer;
554
576
  }());
@@ -556,91 +578,45 @@
556
578
  var TrackedFinalizer = /*#__PURE__*/ (function (_super) {
557
579
  __extends(TrackedFinalizer, _super);
558
580
  function TrackedFinalizer(envObject, finalize_callback, finalize_data, finalize_hint) {
559
- var _this = _super.call(this, envObject, finalize_callback, finalize_data, finalize_hint) || this;
560
- _this._next = null;
561
- _this._prev = null;
562
- _this.link(!finalize_callback ? envObject.reflist : envObject.finalizing_reflist);
581
+ var _this = _super.call(this) || this;
582
+ _this._finalizer = new Finalizer(envObject, finalize_callback, finalize_data, finalize_hint);
563
583
  return _this;
564
584
  }
565
- TrackedFinalizer.finalizeAll = function (list) {
566
- RefTracker.finalizeAll(list);
567
- };
568
- TrackedFinalizer.prototype.link = function (list) {
569
- RefTracker.prototype.link.call(this, list);
570
- };
571
- TrackedFinalizer.prototype.unlink = function () {
572
- RefTracker.prototype.unlink.call(this);
573
- };
574
585
  TrackedFinalizer.create = function (envObject, finalize_callback, finalize_data, finalize_hint) {
575
- return new TrackedFinalizer(envObject, finalize_callback, finalize_data, finalize_hint);
586
+ var finalizer = new TrackedFinalizer(envObject, finalize_callback, finalize_data, finalize_hint);
587
+ finalizer.link(envObject.finalizing_reflist);
588
+ return finalizer;
589
+ };
590
+ TrackedFinalizer.prototype.data = function () {
591
+ return this._finalizer.data();
576
592
  };
577
593
  TrackedFinalizer.prototype.dispose = function () {
594
+ if (!this._finalizer)
595
+ return;
578
596
  this.unlink();
579
- this.envObject.dequeueFinalizer(this);
597
+ this._finalizer.envObject.dequeueFinalizer(this);
598
+ this._finalizer.dispose();
599
+ this._finalizer = undefined;
580
600
  _super.prototype.dispose.call(this);
581
601
  };
582
602
  TrackedFinalizer.prototype.finalize = function () {
583
- this.finalizeCore(true);
584
- };
585
- TrackedFinalizer.prototype.finalizeCore = function (deleteMe) {
586
- var finalize_callback = this._finalizeCallback;
587
- var finalize_data = this._finalizeData;
588
- var finalize_hint = this._finalizeHint;
589
- this.resetFinalizer();
590
603
  this.unlink();
591
604
  var error;
592
605
  var caught = false;
593
- if (finalize_callback) {
594
- var fini = Number(finalize_callback);
595
- try {
596
- this.envObject.callFinalizer(fini, finalize_data, finalize_hint);
597
- }
598
- catch (err) {
599
- caught = true;
600
- error = err;
601
- }
606
+ try {
607
+ this._finalizer.callFinalizer();
602
608
  }
603
- if (deleteMe) {
604
- this.dispose();
609
+ catch (err) {
610
+ caught = true;
611
+ error = err;
605
612
  }
613
+ this.dispose();
606
614
  if (caught) {
607
615
  throw error;
608
616
  }
609
617
  };
610
618
  return TrackedFinalizer;
611
- }(Finalizer));
612
-
613
- var RefBase = /*#__PURE__*/ (function (_super) {
614
- __extends(RefBase, _super);
615
- function RefBase(envObject, initial_refcount, ownership, finalize_callback, finalize_data, finalize_hint) {
616
- var _this = _super.call(this, envObject, finalize_callback, finalize_data, finalize_hint) || this;
617
- _this._refcount = initial_refcount;
618
- _this._ownership = ownership;
619
- return _this;
620
- }
621
- RefBase.prototype.data = function () {
622
- return this._finalizeData;
623
- };
624
- RefBase.prototype.ref = function () {
625
- return ++this._refcount;
626
- };
627
- RefBase.prototype.unref = function () {
628
- if (this._refcount === 0) {
629
- return 0;
630
- }
631
- return --this._refcount;
632
- };
633
- RefBase.prototype.refCount = function () {
634
- return this._refcount;
635
- };
636
- RefBase.prototype.ownership = function () {
637
- return this._ownership;
638
- };
639
- RefBase.prototype.finalize = function () {
640
- this.finalizeCore(this._ownership === 0 /* Ownership.kRuntime */);
641
- };
642
- return RefBase;
643
- }(TrackedFinalizer));
619
+ }(RefTracker));
644
620
 
645
621
  function throwNodeApiVersionError(moduleName, moduleApiVersion) {
646
622
  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.");
@@ -736,18 +712,6 @@
736
712
  }
737
713
  return r;
738
714
  };
739
- /** @virtual */
740
- Env.prototype.callFinalizer = function (cb, data, hint) {
741
- var f = this.makeDynCall_vppp(cb);
742
- var env = this.id;
743
- var scope = this.ctx.openScope(this);
744
- try {
745
- this.callIntoModule(function () { f(env, data, hint); });
746
- }
747
- finally {
748
- this.ctx.closeScope(this, scope);
749
- }
750
- };
751
715
  Env.prototype.invokeFinalizerFromGC = function (finalizer) {
752
716
  if (this.moduleApiVersion !== NAPI_VERSION_EXPERIMENTAL) {
753
717
  this.enqueueFinalizer(finalizer);
@@ -788,8 +752,8 @@
788
752
  };
789
753
  /** @virtual */
790
754
  Env.prototype.deleteMe = function () {
791
- RefBase.finalizeAll(this.finalizing_reflist);
792
- RefBase.finalizeAll(this.reflist);
755
+ RefTracker.finalizeAll(this.finalizing_reflist);
756
+ RefTracker.finalizeAll(this.reflist);
793
757
  this.tryCatch.extractException();
794
758
  this.ctx.envStore.remove(this.id);
795
759
  };
@@ -797,6 +761,8 @@
797
761
  if (this.id === 0)
798
762
  return;
799
763
  this.deleteMe();
764
+ this.finalizing_reflist.dispose();
765
+ this.reflist.dispose();
800
766
  this.id = 0;
801
767
  };
802
768
  Env.prototype.initObjectBinding = function (value) {
@@ -818,7 +784,7 @@
818
784
  if (this.instanceData) {
819
785
  this.instanceData.dispose();
820
786
  }
821
- this.instanceData = new RefBase(this, 0, 0 /* Ownership.kRuntime */, finalize_cb, data, finalize_hint);
787
+ this.instanceData = TrackedFinalizer.create(this, finalize_cb, data, finalize_hint);
822
788
  };
823
789
  Env.prototype.getInstanceData = function () {
824
790
  return this.instanceData ? this.instanceData.data() : 0;
@@ -1081,69 +1047,82 @@
1081
1047
  return Persistent;
1082
1048
  }());
1083
1049
 
1084
- function weakCallback(ref) {
1085
- ref.persistent.reset();
1086
- ref.envObject.invokeFinalizerFromGC(ref);
1087
- }
1050
+ exports.ReferenceOwnership = void 0;
1051
+ (function (ReferenceOwnership) {
1052
+ ReferenceOwnership[ReferenceOwnership["kRuntime"] = 0] = "kRuntime";
1053
+ ReferenceOwnership[ReferenceOwnership["kUserland"] = 1] = "kUserland";
1054
+ })(exports.ReferenceOwnership || (exports.ReferenceOwnership = {}));
1088
1055
  function canBeHeldWeakly(value) {
1089
1056
  return value.isObject() || value.isFunction() || value.isSymbol();
1090
1057
  }
1091
1058
  var Reference = /*#__PURE__*/ (function (_super) {
1092
1059
  __extends(Reference, _super);
1093
- function Reference(envObject, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint) {
1094
- if (finalize_callback === void 0) { finalize_callback = 0; }
1095
- if (finalize_data === void 0) { finalize_data = 0; }
1096
- if (finalize_hint === void 0) { finalize_hint = 0; }
1097
- var _this = _super.call(this, envObject, initialRefcount >>> 0, ownership, finalize_callback, finalize_data, finalize_hint) || this;
1060
+ function Reference(envObject, handle_id, initialRefcount, ownership) {
1061
+ var _this = _super.call(this) || this;
1062
+ _this.envObject = envObject;
1063
+ _this._refcount = initialRefcount;
1064
+ _this._ownership = ownership;
1065
+ var handle = envObject.ctx.handleStore.get(handle_id);
1066
+ _this.canBeWeak = canBeHeldWeakly(handle);
1067
+ _this.persistent = new Persistent(handle.value);
1098
1068
  _this.id = 0;
1069
+ if (initialRefcount === 0) {
1070
+ _this._setWeak();
1071
+ }
1099
1072
  return _this;
1100
1073
  }
1101
- Reference.create = function (envObject, handle_id, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint) {
1102
- if (finalize_callback === void 0) { finalize_callback = 0; }
1103
- if (finalize_data === void 0) { finalize_data = 0; }
1104
- if (finalize_hint === void 0) { finalize_hint = 0; }
1105
- var handle = envObject.ctx.handleStore.get(handle_id);
1106
- var ref = new Reference(envObject, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint);
1074
+ Reference.weakCallback = function (ref) {
1075
+ ref.persistent.reset();
1076
+ ref.invokeFinalizerFromGC();
1077
+ };
1078
+ Reference.create = function (envObject, handle_id, initialRefcount, ownership, _unused1, _unused2, _unused3) {
1079
+ var ref = new Reference(envObject, handle_id, initialRefcount, ownership);
1107
1080
  envObject.ctx.refStore.add(ref);
1108
- ref.canBeWeak = canBeHeldWeakly(handle);
1109
- ref.persistent = new Persistent(handle.value);
1110
- if (initialRefcount === 0) {
1111
- ref._setWeak();
1112
- }
1081
+ ref.link(envObject.reflist);
1113
1082
  return ref;
1114
1083
  };
1115
1084
  Reference.prototype.ref = function () {
1116
1085
  if (this.persistent.isEmpty()) {
1117
1086
  return 0;
1118
1087
  }
1119
- var count = _super.prototype.ref.call(this);
1120
- if (count === 1 && this.canBeWeak) {
1088
+ if (++this._refcount === 1 && this.canBeWeak) {
1121
1089
  this.persistent.clearWeak();
1122
1090
  }
1123
- return count;
1091
+ return this._refcount;
1124
1092
  };
1125
1093
  Reference.prototype.unref = function () {
1126
- if (this.persistent.isEmpty()) {
1094
+ if (this.persistent.isEmpty() || this._refcount === 0) {
1127
1095
  return 0;
1128
1096
  }
1129
- var oldRefcount = this.refCount();
1130
- var refcount = _super.prototype.unref.call(this);
1131
- if (oldRefcount === 1 && refcount === 0) {
1097
+ if (--this._refcount === 0) {
1132
1098
  this._setWeak();
1133
1099
  }
1134
- return refcount;
1100
+ return this._refcount;
1135
1101
  };
1136
- Reference.prototype.get = function () {
1102
+ Reference.prototype.get = function (envObject) {
1103
+ if (envObject === void 0) { envObject = this.envObject; }
1137
1104
  if (this.persistent.isEmpty()) {
1138
1105
  return 0;
1139
1106
  }
1140
1107
  var obj = this.persistent.deref();
1141
- var handle = this.envObject.ensureHandle(obj);
1108
+ var handle = envObject.ensureHandle(obj);
1142
1109
  return handle.id;
1143
1110
  };
1111
+ /** @virtual */
1112
+ Reference.prototype.resetFinalizer = function () { };
1113
+ /** @virtual */
1114
+ Reference.prototype.data = function () { return 0; };
1115
+ Reference.prototype.refcount = function () { return this._refcount; };
1116
+ Reference.prototype.ownership = function () { return this._ownership; };
1117
+ /** @virtual */
1118
+ Reference.prototype.callUserFinalizer = function () { };
1119
+ /** @virtual */
1120
+ Reference.prototype.invokeFinalizerFromGC = function () {
1121
+ this.finalize();
1122
+ };
1144
1123
  Reference.prototype._setWeak = function () {
1145
1124
  if (this.canBeWeak) {
1146
- this.persistent.setWeak(this, weakCallback);
1125
+ this.persistent.setWeak(this, Reference.weakCallback);
1147
1126
  }
1148
1127
  else {
1149
1128
  this.persistent.reset();
@@ -1151,18 +1130,78 @@
1151
1130
  };
1152
1131
  Reference.prototype.finalize = function () {
1153
1132
  this.persistent.reset();
1154
- _super.prototype.finalize.call(this);
1133
+ var deleteMe = this._ownership === exports.ReferenceOwnership.kRuntime;
1134
+ this.unlink();
1135
+ this.callUserFinalizer();
1136
+ if (deleteMe) {
1137
+ this.dispose();
1138
+ }
1155
1139
  };
1156
1140
  Reference.prototype.dispose = function () {
1157
1141
  if (this.id === 0)
1158
1142
  return;
1143
+ this.unlink();
1159
1144
  this.persistent.reset();
1160
1145
  this.envObject.ctx.refStore.remove(this.id);
1161
1146
  _super.prototype.dispose.call(this);
1147
+ this.envObject = undefined;
1162
1148
  this.id = 0;
1163
1149
  };
1164
1150
  return Reference;
1165
- }(RefBase));
1151
+ }(RefTracker));
1152
+ var ReferenceWithData = /*#__PURE__*/ (function (_super) {
1153
+ __extends(ReferenceWithData, _super);
1154
+ function ReferenceWithData(envObject, value, initialRefcount, ownership, _data) {
1155
+ var _this = _super.call(this, envObject, value, initialRefcount, ownership) || this;
1156
+ _this._data = _data;
1157
+ return _this;
1158
+ }
1159
+ ReferenceWithData.create = function (envObject, value, initialRefcount, ownership, data) {
1160
+ var reference = new ReferenceWithData(envObject, value, initialRefcount, ownership, data);
1161
+ envObject.ctx.refStore.add(reference);
1162
+ reference.link(envObject.reflist);
1163
+ return reference;
1164
+ };
1165
+ ReferenceWithData.prototype.data = function () {
1166
+ return this._data;
1167
+ };
1168
+ return ReferenceWithData;
1169
+ }(Reference));
1170
+ var ReferenceWithFinalizer = /*#__PURE__*/ (function (_super) {
1171
+ __extends(ReferenceWithFinalizer, _super);
1172
+ function ReferenceWithFinalizer(envObject, value, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint) {
1173
+ var _this = _super.call(this, envObject, value, initialRefcount, ownership) || this;
1174
+ _this._finalizer = new Finalizer(envObject, finalize_callback, finalize_data, finalize_hint);
1175
+ return _this;
1176
+ }
1177
+ ReferenceWithFinalizer.create = function (envObject, value, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint) {
1178
+ var reference = new ReferenceWithFinalizer(envObject, value, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint);
1179
+ envObject.ctx.refStore.add(reference);
1180
+ reference.link(envObject.finalizing_reflist);
1181
+ return reference;
1182
+ };
1183
+ ReferenceWithFinalizer.prototype.resetFinalizer = function () {
1184
+ this._finalizer.resetFinalizer();
1185
+ };
1186
+ ReferenceWithFinalizer.prototype.data = function () {
1187
+ return this._finalizer.data();
1188
+ };
1189
+ ReferenceWithFinalizer.prototype.callUserFinalizer = function () {
1190
+ this._finalizer.callFinalizer();
1191
+ };
1192
+ ReferenceWithFinalizer.prototype.invokeFinalizerFromGC = function () {
1193
+ this._finalizer.envObject.invokeFinalizerFromGC(this);
1194
+ };
1195
+ ReferenceWithFinalizer.prototype.dispose = function () {
1196
+ if (!this._finalizer)
1197
+ return;
1198
+ this._finalizer.envObject.dequeueFinalizer(this);
1199
+ this._finalizer.dispose();
1200
+ _super.prototype.dispose.call(this);
1201
+ this._finalizer = undefined;
1202
+ };
1203
+ return ReferenceWithFinalizer;
1204
+ }(Reference));
1166
1205
 
1167
1206
  var Deferred = /*#__PURE__*/ (function () {
1168
1207
  function Deferred(ctx, value) {
@@ -1319,6 +1358,7 @@
1319
1358
  var _this = this;
1320
1359
  this._isStopping = false;
1321
1360
  this._canCallIntoJs = true;
1361
+ this._suppressDestroy = false;
1322
1362
  this.envStore = new Store();
1323
1363
  this.scopeStore = new ScopeStore();
1324
1364
  this.refStore = new Store();
@@ -1340,10 +1380,23 @@
1340
1380
  if (typeof process === 'object' && process !== null && typeof process.once === 'function') {
1341
1381
  this.refCounter = new NodejsWaitingRequestCounter();
1342
1382
  process.once('beforeExit', function () {
1343
- _this.destroy();
1383
+ if (!_this._suppressDestroy) {
1384
+ _this.destroy();
1385
+ }
1344
1386
  });
1345
1387
  }
1346
1388
  }
1389
+ /**
1390
+ * Suppress the destroy on `beforeExit` event in Node.js.
1391
+ * Call this method if you want to keep the context and
1392
+ * all associated {@link Env | Env} alive,
1393
+ * this also means that cleanup hooks will not be called.
1394
+ * After call this method, you should call
1395
+ * {@link Context.destroy | `Context.prototype.destroy`} method manually.
1396
+ */
1397
+ Context.prototype.suppressDestroy = function () {
1398
+ this._suppressDestroy = true;
1399
+ };
1347
1400
  // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
1348
1401
  Context.prototype.getRuntimeVersions = function () {
1349
1402
  return {
@@ -1359,11 +1412,17 @@
1359
1412
  Context.prototype.createNotSupportBufferError = function (api, message) {
1360
1413
  return new NotSupportBufferError(api, message);
1361
1414
  };
1362
- Context.prototype.createReference = function (envObject, handle_id, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint) {
1415
+ Context.prototype.createReference = function (envObject, handle_id, initialRefcount, ownership) {
1416
+ return Reference.create(envObject, handle_id, initialRefcount, ownership);
1417
+ };
1418
+ Context.prototype.createReferenceWithData = function (envObject, handle_id, initialRefcount, ownership, data) {
1419
+ return ReferenceWithData.create(envObject, handle_id, initialRefcount, ownership, data);
1420
+ };
1421
+ Context.prototype.createReferenceWithFinalizer = function (envObject, handle_id, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint) {
1363
1422
  if (finalize_callback === void 0) { finalize_callback = 0; }
1364
1423
  if (finalize_data === void 0) { finalize_data = 0; }
1365
1424
  if (finalize_hint === void 0) { finalize_hint = 0; }
1366
- return Reference.create(envObject, handle_id, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint);
1425
+ return ReferenceWithFinalizer.create(envObject, handle_id, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint);
1367
1426
  };
1368
1427
  Context.prototype.createDeferred = function (value) {
1369
1428
  return Deferred.create(this, value);
@@ -1424,6 +1483,10 @@
1424
1483
  Context.prototype.canCallIntoJs = function () {
1425
1484
  return this._canCallIntoJs && !this._isStopping;
1426
1485
  };
1486
+ /**
1487
+ * Destroy the context and call cleanup hooks.
1488
+ * Associated {@link Env | Env} will be destroyed.
1489
+ */
1427
1490
  Context.prototype.destroy = function () {
1428
1491
  this.setStopping(true);
1429
1492
  this.setCanCallIntoJs(false);
@@ -1461,9 +1524,10 @@
1461
1524
  exports.NotSupportBufferError = NotSupportBufferError;
1462
1525
  exports.NotSupportWeakRefError = NotSupportWeakRefError;
1463
1526
  exports.Persistent = Persistent;
1464
- exports.RefBase = RefBase;
1465
1527
  exports.RefTracker = RefTracker;
1466
1528
  exports.Reference = Reference;
1529
+ exports.ReferenceWithData = ReferenceWithData;
1530
+ exports.ReferenceWithFinalizer = ReferenceWithFinalizer;
1467
1531
  exports.ScopeStore = ScopeStore;
1468
1532
  exports.Store = Store;
1469
1533
  exports.TrackedFinalizer = TrackedFinalizer;