@emnapi/runtime 0.41.0 → 0.43.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.
@@ -1,8 +1,11 @@
1
1
  var emnapi = (function (exports) {
2
2
 
3
+ var EMPTY_ARGS = [];
3
4
  var CallbackInfo = /*#__PURE__*/ (function () {
4
- function CallbackInfo(parent, thiz, data, args, fn) {
5
+ function CallbackInfo(id, parent, child, thiz, data, args, fn) {
6
+ this.id = id;
5
7
  this.parent = parent;
8
+ this.child = child;
6
9
  this.thiz = thiz;
7
10
  this.data = data;
8
11
  this.args = args;
@@ -14,22 +17,51 @@ var emnapi = (function (exports) {
14
17
  return 0;
15
18
  return thiz instanceof this.fn ? envObject.ensureHandleId(thiz.constructor) : 0;
16
19
  };
20
+ CallbackInfo.prototype.dispose = function () {
21
+ if (this.thiz !== undefined)
22
+ this.thiz = undefined;
23
+ this.args = EMPTY_ARGS;
24
+ this.fn = null;
25
+ };
17
26
  return CallbackInfo;
18
27
  }());
28
+ var ROOT_CBINFO = new CallbackInfo(0, null, null, null, 0, null, null);
19
29
  var CallbackInfoStack = /*#__PURE__*/ (function () {
20
30
  function CallbackInfoStack() {
21
- this.current = null;
31
+ this.current = ROOT_CBINFO;
22
32
  }
33
+ CallbackInfoStack.prototype.get = function (id) {
34
+ if (id === 1)
35
+ return ROOT_CBINFO.child;
36
+ var info = ROOT_CBINFO;
37
+ for (var i = 0; i < id; ++i) {
38
+ info = info.child;
39
+ if (info === null)
40
+ return null;
41
+ }
42
+ return info === ROOT_CBINFO ? null : info;
43
+ };
23
44
  CallbackInfoStack.prototype.pop = function () {
24
45
  var current = this.current;
25
- if (current === null)
46
+ if (current === ROOT_CBINFO)
26
47
  return;
27
48
  this.current = current.parent;
49
+ current.dispose();
28
50
  };
29
51
  CallbackInfoStack.prototype.push = function (thiz, data, args, fn) {
30
- var info = new CallbackInfo(this.current, thiz, data, args, fn);
52
+ var info = this.current.child;
53
+ if (info) {
54
+ info.thiz = thiz;
55
+ info.data = data;
56
+ info.args = args;
57
+ info.fn = fn;
58
+ }
59
+ else {
60
+ info = new CallbackInfo(this.current.id + 1, this.current, null, thiz, data, args, fn);
61
+ this.current.child = info;
62
+ }
31
63
  this.current = info;
32
- return info;
64
+ return info.id;
33
65
  };
34
66
  CallbackInfoStack.prototype.dispose = function () {
35
67
  this.current = null;
@@ -51,7 +83,7 @@ var emnapi = (function (exports) {
51
83
  OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
52
84
  PERFORMANCE OF THIS SOFTWARE.
53
85
  ***************************************************************************** */
54
- /* global Reflect, Promise */
86
+ /* global Reflect, Promise, SuppressedError, Symbol */
55
87
 
56
88
  var extendStatics = function(d, b) {
57
89
  extendStatics = Object.setPrototypeOf ||
@@ -68,6 +100,11 @@ var emnapi = (function (exports) {
68
100
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
69
101
  }
70
102
 
103
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
104
+ var e = new Error(message);
105
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
106
+ };
107
+
71
108
  var supportNewFunction = /*#__PURE__*/ (function () {
72
109
  var f;
73
110
  try {
@@ -205,8 +242,9 @@ var emnapi = (function (exports) {
205
242
  catch (_) { }
206
243
  return undefined;
207
244
  })();
208
- var version = "0.41.0";
209
- var NAPI_VERSION = 9 /* Version.NAPI_VERSION */;
245
+ var version = "0.43.0";
246
+ var NODE_API_SUPPORTED_VERSION_MIN = 1 /* Version.NODE_API_SUPPORTED_VERSION_MIN */;
247
+ var NODE_API_SUPPORTED_VERSION_MAX = 9 /* Version.NODE_API_SUPPORTED_VERSION_MAX */;
210
248
  var NAPI_VERSION_EXPERIMENTAL = 2147483647 /* Version.NAPI_VERSION_EXPERIMENTAL */;
211
249
  var NODE_API_DEFAULT_MODULE_API_VERSION = 8 /* Version.NODE_API_DEFAULT_MODULE_API_VERSION */;
212
250
 
@@ -507,53 +545,36 @@ var emnapi = (function (exports) {
507
545
  return Finalizer;
508
546
  }());
509
547
 
510
- var RefBase = /*#__PURE__*/ (function (_super) {
511
- __extends(RefBase, _super);
512
- function RefBase(envObject, initial_refcount, ownership, finalize_callback, finalize_data, finalize_hint) {
548
+ var TrackedFinalizer = /*#__PURE__*/ (function (_super) {
549
+ __extends(TrackedFinalizer, _super);
550
+ function TrackedFinalizer(envObject, finalize_callback, finalize_data, finalize_hint) {
513
551
  var _this = _super.call(this, envObject, finalize_callback, finalize_data, finalize_hint) || this;
514
552
  _this._next = null;
515
553
  _this._prev = null;
516
- _this._refcount = initial_refcount;
517
- _this._ownership = ownership;
518
554
  _this.link(!finalize_callback ? envObject.reflist : envObject.finalizing_reflist);
519
555
  return _this;
520
556
  }
521
- RefBase.finalizeAll = function (list) {
557
+ TrackedFinalizer.finalizeAll = function (list) {
522
558
  RefTracker.finalizeAll(list);
523
559
  };
524
- RefBase.prototype.link = function (list) {
560
+ TrackedFinalizer.prototype.link = function (list) {
525
561
  RefTracker.prototype.link.call(this, list);
526
562
  };
527
- RefBase.prototype.unlink = function () {
563
+ TrackedFinalizer.prototype.unlink = function () {
528
564
  RefTracker.prototype.unlink.call(this);
529
565
  };
530
- RefBase.prototype.dispose = function () {
566
+ TrackedFinalizer.create = function (envObject, finalize_callback, finalize_data, finalize_hint) {
567
+ return new TrackedFinalizer(envObject, finalize_callback, finalize_data, finalize_hint);
568
+ };
569
+ TrackedFinalizer.prototype.dispose = function () {
531
570
  this.unlink();
532
571
  this.envObject.dequeueFinalizer(this);
533
572
  _super.prototype.dispose.call(this);
534
573
  };
535
- RefBase.prototype.data = function () {
536
- return this._finalizeData;
537
- };
538
- RefBase.prototype.ref = function () {
539
- return ++this._refcount;
540
- };
541
- RefBase.prototype.unref = function () {
542
- if (this._refcount === 0) {
543
- return 0;
544
- }
545
- return --this._refcount;
574
+ TrackedFinalizer.prototype.finalize = function () {
575
+ this.finalizeCore(true);
546
576
  };
547
- RefBase.prototype.refCount = function () {
548
- return this._refcount;
549
- };
550
- RefBase.prototype.ownership = function () {
551
- return this._ownership;
552
- };
553
- RefBase.prototype.finalize = function () {
554
- var ownership = this._ownership;
555
- // Swap out the field finalize_callback so that it can not be accidentally
556
- // called more than once.
577
+ TrackedFinalizer.prototype.finalizeCore = function (deleteMe) {
557
578
  var finalize_callback = this._finalizeCallback;
558
579
  var finalize_data = this._finalizeData;
559
580
  var finalize_hint = this._finalizeHint;
@@ -571,18 +592,50 @@ var emnapi = (function (exports) {
571
592
  error = err;
572
593
  }
573
594
  }
574
- if (ownership === 0 /* Ownership.kRuntime */) {
595
+ if (deleteMe) {
575
596
  this.dispose();
576
597
  }
577
598
  if (caught) {
578
599
  throw error;
579
600
  }
580
601
  };
581
- return RefBase;
602
+ return TrackedFinalizer;
582
603
  }(Finalizer));
583
604
 
605
+ var RefBase = /*#__PURE__*/ (function (_super) {
606
+ __extends(RefBase, _super);
607
+ function RefBase(envObject, initial_refcount, ownership, finalize_callback, finalize_data, finalize_hint) {
608
+ var _this = _super.call(this, envObject, finalize_callback, finalize_data, finalize_hint) || this;
609
+ _this._refcount = initial_refcount;
610
+ _this._ownership = ownership;
611
+ return _this;
612
+ }
613
+ RefBase.prototype.data = function () {
614
+ return this._finalizeData;
615
+ };
616
+ RefBase.prototype.ref = function () {
617
+ return ++this._refcount;
618
+ };
619
+ RefBase.prototype.unref = function () {
620
+ if (this._refcount === 0) {
621
+ return 0;
622
+ }
623
+ return --this._refcount;
624
+ };
625
+ RefBase.prototype.refCount = function () {
626
+ return this._refcount;
627
+ };
628
+ RefBase.prototype.ownership = function () {
629
+ return this._ownership;
630
+ };
631
+ RefBase.prototype.finalize = function () {
632
+ this.finalizeCore(this._ownership === 0 /* Ownership.kRuntime */);
633
+ };
634
+ return RefBase;
635
+ }(TrackedFinalizer));
636
+
584
637
  function throwNodeApiVersionError(moduleName, moduleApiVersion) {
585
- var errorMessage = "".concat(moduleName, " requires Node-API version ").concat(moduleApiVersion, ", but this version of Node.js only supports version ").concat(NAPI_VERSION, " add-ons.");
638
+ 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.");
586
639
  throw new Error(errorMessage);
587
640
  }
588
641
  function handleThrow(envObject, value) {
@@ -610,6 +663,7 @@ var emnapi = (function (exports) {
610
663
  engineErrorCode: 0,
611
664
  engineReserved: 0
612
665
  };
666
+ this.inGcFinalizer = false;
613
667
  this._bindingMap = new WeakMap();
614
668
  this.id = 0;
615
669
  }
@@ -686,6 +740,31 @@ var emnapi = (function (exports) {
686
740
  this.ctx.closeScope(this, scope);
687
741
  }
688
742
  };
743
+ Env.prototype.invokeFinalizerFromGC = function (finalizer) {
744
+ if (this.moduleApiVersion !== NAPI_VERSION_EXPERIMENTAL) {
745
+ this.enqueueFinalizer(finalizer);
746
+ }
747
+ else {
748
+ var saved = this.inGcFinalizer;
749
+ this.inGcFinalizer = true;
750
+ try {
751
+ finalizer.finalize();
752
+ }
753
+ finally {
754
+ this.inGcFinalizer = saved;
755
+ }
756
+ }
757
+ };
758
+ Env.prototype.checkGCAccess = function () {
759
+ if (this.moduleApiVersion === NAPI_VERSION_EXPERIMENTAL && this.inGcFinalizer) {
760
+ this.abort('Finalizer is calling a function that may affect GC state.\n' +
761
+ 'The finalizers are run directly from GC and must not affect GC ' +
762
+ 'state.\n' +
763
+ 'Use `node_api_post_finalizer` from inside of the finalizer to work ' +
764
+ 'around this issue.\n' +
765
+ 'It schedules the call as a new task in the event loop.');
766
+ }
767
+ };
689
768
  /** @virtual */
690
769
  Env.prototype.enqueueFinalizer = function (finalizer) {
691
770
  if (this.pendingFinalizers.indexOf(finalizer) === -1) {
@@ -754,7 +833,7 @@ var emnapi = (function (exports) {
754
833
  _super.prototype.deleteMe.call(this);
755
834
  };
756
835
  NodeEnv.prototype.canCallIntoJs = function () {
757
- return this.ctx.canCallIntoJs();
836
+ return _super.prototype.canCallIntoJs.call(this) && this.ctx.canCallIntoJs();
758
837
  };
759
838
  NodeEnv.prototype.triggerFatalException = function (err) {
760
839
  if (this.nodeBinding) {
@@ -780,13 +859,21 @@ var emnapi = (function (exports) {
780
859
  }
781
860
  var hasProcess = typeof process === 'object' && process !== null;
782
861
  var hasForceFlag = hasProcess ? Boolean(process.execArgv && (process.execArgv.indexOf('--force-node-api-uncaught-exceptions-policy') !== -1)) : false;
783
- if (!hasForceFlag && !enforceUncaughtExceptionPolicy) {
784
- if (hasProcess && typeof process.emitWarning === 'function') {
785
- 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');
786
- }
787
- else {
788
- throw err;
789
- }
862
+ if (envObject.moduleApiVersion < NAPI_VERSION_EXPERIMENTAL && !hasForceFlag && !enforceUncaughtExceptionPolicy) {
863
+ var warn = hasProcess && typeof process.emitWarning === 'function'
864
+ ? process.emitWarning
865
+ : function (warning, type, code) {
866
+ if (warning instanceof Error) {
867
+ console.warn(warning.toString());
868
+ }
869
+ else {
870
+ var prefix = code ? "[".concat(code, "] ") : '';
871
+ // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
872
+ console.warn("".concat(prefix).concat(type || 'Warning', ": ").concat(warning));
873
+ }
874
+ };
875
+ warn('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');
876
+ return;
790
877
  }
791
878
  envObject.triggerFatalException(err);
792
879
  });
@@ -832,7 +919,7 @@ var emnapi = (function (exports) {
832
919
  if (moduleApiVersion < NODE_API_DEFAULT_MODULE_API_VERSION) {
833
920
  moduleApiVersion = NODE_API_DEFAULT_MODULE_API_VERSION;
834
921
  }
835
- else if (moduleApiVersion > NAPI_VERSION && moduleApiVersion !== NAPI_VERSION_EXPERIMENTAL) {
922
+ else if (moduleApiVersion > NODE_API_SUPPORTED_VERSION_MAX && moduleApiVersion !== NAPI_VERSION_EXPERIMENTAL) {
836
923
  throwNodeApiVersionError(filename, moduleApiVersion);
837
924
  }
838
925
  var env = new NodeEnv(ctx, filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort, nodeBinding);
@@ -882,18 +969,6 @@ var emnapi = (function (exports) {
882
969
  writable: true,
883
970
  value: 'NotSupportWeakRefError'
884
971
  });
885
- var NotSupportBigIntError = /*#__PURE__*/ (function (_super) {
886
- __extends(NotSupportBigIntError, _super);
887
- function NotSupportBigIntError(api, message) {
888
- return _super.call(this, "".concat(api, ": The current runtime does not support \"BigInt\".").concat(message ? " ".concat(message) : '')) || this;
889
- }
890
- return NotSupportBigIntError;
891
- }(EmnapiError));
892
- Object.defineProperty(NotSupportBigIntError.prototype, 'name', {
893
- configurable: true,
894
- writable: true,
895
- value: 'NotSupportBigIntError'
896
- });
897
972
  var NotSupportBufferError = /*#__PURE__*/ (function (_super) {
898
973
  __extends(NotSupportBufferError, _super);
899
974
  function NotSupportBufferError(api, message) {
@@ -1000,7 +1075,7 @@ var emnapi = (function (exports) {
1000
1075
 
1001
1076
  function weakCallback(ref) {
1002
1077
  ref.persistent.reset();
1003
- ref.envObject.enqueueFinalizer(ref);
1078
+ ref.envObject.invokeFinalizerFromGC(ref);
1004
1079
  }
1005
1080
  function canBeHeldWeakly(value) {
1006
1081
  return value.isObject() || value.isFunction() || value.isSymbol();
@@ -1265,7 +1340,7 @@ var emnapi = (function (exports) {
1265
1340
  Context.prototype.getRuntimeVersions = function () {
1266
1341
  return {
1267
1342
  version: version,
1268
- NAPI_VERSION: NAPI_VERSION,
1343
+ NODE_API_SUPPORTED_VERSION_MAX: NODE_API_SUPPORTED_VERSION_MAX,
1269
1344
  NAPI_VERSION_EXPERIMENTAL: NAPI_VERSION_EXPERIMENTAL,
1270
1345
  NODE_API_DEFAULT_MODULE_API_VERSION: NODE_API_DEFAULT_MODULE_API_VERSION
1271
1346
  };
@@ -1273,9 +1348,6 @@ var emnapi = (function (exports) {
1273
1348
  Context.prototype.createNotSupportWeakRefError = function (api, message) {
1274
1349
  return new NotSupportWeakRefError(api, message);
1275
1350
  };
1276
- Context.prototype.createNotSupportBigIntError = function (api, message) {
1277
- return new NotSupportBigIntError(api, message);
1278
- };
1279
1351
  Context.prototype.createNotSupportBufferError = function (api, message) {
1280
1352
  return new NotSupportBufferError(api, message);
1281
1353
  };
@@ -1291,6 +1363,9 @@ var emnapi = (function (exports) {
1291
1363
  Context.prototype.createEnv = function (filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort, nodeBinding) {
1292
1364
  return newEnv(this, filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort, nodeBinding);
1293
1365
  };
1366
+ Context.prototype.createTrackedFinalizer = function (envObject, finalize_callback, finalize_data, finalize_hint) {
1367
+ return TrackedFinalizer.create(envObject, finalize_callback, finalize_data, finalize_hint);
1368
+ };
1294
1369
  Context.prototype.getCurrentScope = function () {
1295
1370
  return this.scopeStore.currentScope;
1296
1371
  };
@@ -1370,11 +1445,11 @@ var emnapi = (function (exports) {
1370
1445
  exports.Handle = Handle;
1371
1446
  exports.HandleScope = HandleScope;
1372
1447
  exports.HandleStore = HandleStore;
1373
- exports.NAPI_VERSION = NAPI_VERSION;
1374
1448
  exports.NAPI_VERSION_EXPERIMENTAL = NAPI_VERSION_EXPERIMENTAL;
1375
1449
  exports.NODE_API_DEFAULT_MODULE_API_VERSION = NODE_API_DEFAULT_MODULE_API_VERSION;
1450
+ exports.NODE_API_SUPPORTED_VERSION_MAX = NODE_API_SUPPORTED_VERSION_MAX;
1451
+ exports.NODE_API_SUPPORTED_VERSION_MIN = NODE_API_SUPPORTED_VERSION_MIN;
1376
1452
  exports.NodeEnv = NodeEnv;
1377
- exports.NotSupportBigIntError = NotSupportBigIntError;
1378
1453
  exports.NotSupportBufferError = NotSupportBufferError;
1379
1454
  exports.NotSupportWeakRefError = NotSupportWeakRefError;
1380
1455
  exports.Persistent = Persistent;
@@ -1383,6 +1458,7 @@ var emnapi = (function (exports) {
1383
1458
  exports.Reference = Reference;
1384
1459
  exports.ScopeStore = ScopeStore;
1385
1460
  exports.Store = Store;
1461
+ exports.TrackedFinalizer = TrackedFinalizer;
1386
1462
  exports.TryCatch = TryCatch;
1387
1463
  exports.createContext = createContext;
1388
1464
  exports.getDefaultContext = getDefaultContext;