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