@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,6 +1,9 @@
1
+ const EMPTY_ARGS = [];
1
2
  class CallbackInfo {
2
- constructor(parent, thiz, data, args, fn) {
3
+ constructor(id, parent, child, thiz, data, args, fn) {
4
+ this.id = id;
3
5
  this.parent = parent;
6
+ this.child = child;
4
7
  this.thiz = thiz;
5
8
  this.data = data;
6
9
  this.args = args;
@@ -12,21 +15,50 @@ class CallbackInfo {
12
15
  return 0;
13
16
  return thiz instanceof this.fn ? envObject.ensureHandleId(thiz.constructor) : 0;
14
17
  }
18
+ dispose() {
19
+ if (this.thiz !== undefined)
20
+ this.thiz = undefined;
21
+ this.args = EMPTY_ARGS;
22
+ this.fn = null;
23
+ }
15
24
  }
25
+ const ROOT_CBINFO = new CallbackInfo(0, null, null, null, 0, null, null);
16
26
  class CallbackInfoStack {
17
27
  constructor() {
18
- this.current = null;
28
+ this.current = ROOT_CBINFO;
29
+ }
30
+ get(id) {
31
+ if (id === 1)
32
+ return ROOT_CBINFO.child;
33
+ let info = ROOT_CBINFO;
34
+ for (let i = 0; i < id; ++i) {
35
+ info = info.child;
36
+ if (info === null)
37
+ return null;
38
+ }
39
+ return info === ROOT_CBINFO ? null : info;
19
40
  }
20
41
  pop() {
21
42
  const current = this.current;
22
- if (current === null)
43
+ if (current === ROOT_CBINFO)
23
44
  return;
24
45
  this.current = current.parent;
46
+ current.dispose();
25
47
  }
26
48
  push(thiz, data, args, fn) {
27
- const info = new CallbackInfo(this.current, thiz, data, args, fn);
49
+ let info = this.current.child;
50
+ if (info) {
51
+ info.thiz = thiz;
52
+ info.data = data;
53
+ info.args = args;
54
+ info.fn = fn;
55
+ }
56
+ else {
57
+ info = new CallbackInfo(this.current.id + 1, this.current, null, thiz, data, args, fn);
58
+ this.current.child = info;
59
+ }
28
60
  this.current = info;
29
- return info;
61
+ return info.id;
30
62
  }
31
63
  dispose() {
32
64
  this.current = null;
@@ -167,8 +199,9 @@ const _Buffer = typeof Buffer === 'function'
167
199
  catch (_) { }
168
200
  return undefined;
169
201
  })();
170
- const version = "0.41.0";
171
- const NAPI_VERSION = 9;
202
+ const version = "0.43.0";
203
+ const NODE_API_SUPPORTED_VERSION_MIN = 1;
204
+ const NODE_API_SUPPORTED_VERSION_MAX = 9;
172
205
  const NAPI_VERSION_EXPERIMENTAL = 2147483647;
173
206
  const NODE_API_DEFAULT_MODULE_API_VERSION = 8;
174
207
 
@@ -456,7 +489,7 @@ class Finalizer {
456
489
  }
457
490
  }
458
491
 
459
- class RefBase extends Finalizer {
492
+ class TrackedFinalizer extends Finalizer {
460
493
  static finalizeAll(list) {
461
494
  RefTracker.finalizeAll(list);
462
495
  }
@@ -466,12 +499,13 @@ class RefBase extends Finalizer {
466
499
  unlink() {
467
500
  RefTracker.prototype.unlink.call(this);
468
501
  }
469
- constructor(envObject, initial_refcount, ownership, finalize_callback, finalize_data, finalize_hint) {
502
+ static create(envObject, finalize_callback, finalize_data, finalize_hint) {
503
+ return new TrackedFinalizer(envObject, finalize_callback, finalize_data, finalize_hint);
504
+ }
505
+ constructor(envObject, finalize_callback, finalize_data, finalize_hint) {
470
506
  super(envObject, finalize_callback, finalize_data, finalize_hint);
471
507
  this._next = null;
472
508
  this._prev = null;
473
- this._refcount = initial_refcount;
474
- this._ownership = ownership;
475
509
  this.link(!finalize_callback ? envObject.reflist : envObject.finalizing_reflist);
476
510
  }
477
511
  dispose() {
@@ -479,26 +513,10 @@ class RefBase extends Finalizer {
479
513
  this.envObject.dequeueFinalizer(this);
480
514
  super.dispose();
481
515
  }
482
- data() {
483
- return this._finalizeData;
484
- }
485
- ref() {
486
- return ++this._refcount;
487
- }
488
- unref() {
489
- if (this._refcount === 0) {
490
- return 0;
491
- }
492
- return --this._refcount;
493
- }
494
- refCount() {
495
- return this._refcount;
496
- }
497
- ownership() {
498
- return this._ownership;
499
- }
500
516
  finalize() {
501
- const ownership = this._ownership;
517
+ this.finalizeCore(true);
518
+ }
519
+ finalizeCore(deleteMe) {
502
520
  const finalize_callback = this._finalizeCallback;
503
521
  const finalize_data = this._finalizeData;
504
522
  const finalize_hint = this._finalizeHint;
@@ -516,7 +534,7 @@ class RefBase extends Finalizer {
516
534
  error = err;
517
535
  }
518
536
  }
519
- if (ownership === 0) {
537
+ if (deleteMe) {
520
538
  this.dispose();
521
539
  }
522
540
  if (caught) {
@@ -525,8 +543,37 @@ class RefBase extends Finalizer {
525
543
  }
526
544
  }
527
545
 
546
+ class RefBase extends TrackedFinalizer {
547
+ constructor(envObject, initial_refcount, ownership, finalize_callback, finalize_data, finalize_hint) {
548
+ super(envObject, finalize_callback, finalize_data, finalize_hint);
549
+ this._refcount = initial_refcount;
550
+ this._ownership = ownership;
551
+ }
552
+ data() {
553
+ return this._finalizeData;
554
+ }
555
+ ref() {
556
+ return ++this._refcount;
557
+ }
558
+ unref() {
559
+ if (this._refcount === 0) {
560
+ return 0;
561
+ }
562
+ return --this._refcount;
563
+ }
564
+ refCount() {
565
+ return this._refcount;
566
+ }
567
+ ownership() {
568
+ return this._ownership;
569
+ }
570
+ finalize() {
571
+ this.finalizeCore(this._ownership === 0);
572
+ }
573
+ }
574
+
528
575
  function throwNodeApiVersionError(moduleName, moduleApiVersion) {
529
- const errorMessage = `${moduleName} requires Node-API version ${moduleApiVersion}, but this version of Node.js only supports version ${NAPI_VERSION} add-ons.`;
576
+ const errorMessage = `${moduleName} requires Node-API version ${moduleApiVersion}, but this version of Node.js only supports version ${NODE_API_SUPPORTED_VERSION_MAX} add-ons.`;
530
577
  throw new Error(errorMessage);
531
578
  }
532
579
  function handleThrow(envObject, value) {
@@ -554,6 +601,7 @@ class Env {
554
601
  engineErrorCode: 0,
555
602
  engineReserved: 0
556
603
  };
604
+ this.inGcFinalizer = false;
557
605
  this._bindingMap = new WeakMap();
558
606
  this.id = 0;
559
607
  }
@@ -625,6 +673,31 @@ class Env {
625
673
  this.ctx.closeScope(this, scope);
626
674
  }
627
675
  }
676
+ invokeFinalizerFromGC(finalizer) {
677
+ if (this.moduleApiVersion !== NAPI_VERSION_EXPERIMENTAL) {
678
+ this.enqueueFinalizer(finalizer);
679
+ }
680
+ else {
681
+ const saved = this.inGcFinalizer;
682
+ this.inGcFinalizer = true;
683
+ try {
684
+ finalizer.finalize();
685
+ }
686
+ finally {
687
+ this.inGcFinalizer = saved;
688
+ }
689
+ }
690
+ }
691
+ checkGCAccess() {
692
+ if (this.moduleApiVersion === NAPI_VERSION_EXPERIMENTAL && this.inGcFinalizer) {
693
+ this.abort('Finalizer is calling a function that may affect GC state.\n' +
694
+ 'The finalizers are run directly from GC and must not affect GC ' +
695
+ 'state.\n' +
696
+ 'Use `node_api_post_finalizer` from inside of the finalizer to work ' +
697
+ 'around this issue.\n' +
698
+ 'It schedules the call as a new task in the event loop.');
699
+ }
700
+ }
628
701
  enqueueFinalizer(finalizer) {
629
702
  if (this.pendingFinalizers.indexOf(finalizer) === -1) {
630
703
  this.pendingFinalizers.push(finalizer);
@@ -687,7 +760,7 @@ class NodeEnv extends Env {
687
760
  super.deleteMe();
688
761
  }
689
762
  canCallIntoJs() {
690
- return this.ctx.canCallIntoJs();
763
+ return super.canCallIntoJs() && this.ctx.canCallIntoJs();
691
764
  }
692
765
  triggerFatalException(err) {
693
766
  if (this.nodeBinding) {
@@ -713,13 +786,20 @@ class NodeEnv extends Env {
713
786
  }
714
787
  const hasProcess = typeof process === 'object' && process !== null;
715
788
  const hasForceFlag = hasProcess ? Boolean(process.execArgv && (process.execArgv.indexOf('--force-node-api-uncaught-exceptions-policy') !== -1)) : false;
716
- if (!hasForceFlag && !enforceUncaughtExceptionPolicy) {
717
- if (hasProcess && typeof process.emitWarning === 'function') {
718
- 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');
719
- }
720
- else {
721
- throw err;
722
- }
789
+ if (envObject.moduleApiVersion < NAPI_VERSION_EXPERIMENTAL && !hasForceFlag && !enforceUncaughtExceptionPolicy) {
790
+ const warn = hasProcess && typeof process.emitWarning === 'function'
791
+ ? process.emitWarning
792
+ : function (warning, type, code) {
793
+ if (warning instanceof Error) {
794
+ console.warn(warning.toString());
795
+ }
796
+ else {
797
+ const prefix = code ? `[${code}] ` : '';
798
+ console.warn(`${prefix}${type || 'Warning'}: ${warning}`);
799
+ }
800
+ };
801
+ 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');
802
+ return;
723
803
  }
724
804
  envObject.triggerFatalException(err);
725
805
  });
@@ -762,7 +842,7 @@ function newEnv(ctx, filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_v
762
842
  if (moduleApiVersion < NODE_API_DEFAULT_MODULE_API_VERSION) {
763
843
  moduleApiVersion = NODE_API_DEFAULT_MODULE_API_VERSION;
764
844
  }
765
- else if (moduleApiVersion > NAPI_VERSION && moduleApiVersion !== NAPI_VERSION_EXPERIMENTAL) {
845
+ else if (moduleApiVersion > NODE_API_SUPPORTED_VERSION_MAX && moduleApiVersion !== NAPI_VERSION_EXPERIMENTAL) {
766
846
  throwNodeApiVersionError(filename, moduleApiVersion);
767
847
  }
768
848
  const env = new NodeEnv(ctx, filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort, nodeBinding);
@@ -805,16 +885,6 @@ Object.defineProperty(NotSupportWeakRefError.prototype, 'name', {
805
885
  writable: true,
806
886
  value: 'NotSupportWeakRefError'
807
887
  });
808
- class NotSupportBigIntError extends EmnapiError {
809
- constructor(api, message) {
810
- super(`${api}: The current runtime does not support "BigInt".${message ? ` ${message}` : ''}`);
811
- }
812
- }
813
- Object.defineProperty(NotSupportBigIntError.prototype, 'name', {
814
- configurable: true,
815
- writable: true,
816
- value: 'NotSupportBigIntError'
817
- });
818
888
  class NotSupportBufferError extends EmnapiError {
819
889
  constructor(api, message) {
820
890
  super(`${api}: The current runtime does not support "Buffer". Consider using buffer polyfill to make sure \`globalThis.Buffer\` is defined.${message ? ` ${message}` : ''}`);
@@ -917,7 +987,7 @@ Persistent._registry = supportFinalizer
917
987
 
918
988
  function weakCallback(ref) {
919
989
  ref.persistent.reset();
920
- ref.envObject.enqueueFinalizer(ref);
990
+ ref.envObject.invokeFinalizerFromGC(ref);
921
991
  }
922
992
  function canBeHeldWeakly(value) {
923
993
  return value.isObject() || value.isFunction() || value.isSymbol();
@@ -1166,7 +1236,7 @@ class Context {
1166
1236
  getRuntimeVersions() {
1167
1237
  return {
1168
1238
  version,
1169
- NAPI_VERSION,
1239
+ NODE_API_SUPPORTED_VERSION_MAX,
1170
1240
  NAPI_VERSION_EXPERIMENTAL,
1171
1241
  NODE_API_DEFAULT_MODULE_API_VERSION
1172
1242
  };
@@ -1174,9 +1244,6 @@ class Context {
1174
1244
  createNotSupportWeakRefError(api, message) {
1175
1245
  return new NotSupportWeakRefError(api, message);
1176
1246
  }
1177
- createNotSupportBigIntError(api, message) {
1178
- return new NotSupportBigIntError(api, message);
1179
- }
1180
1247
  createNotSupportBufferError(api, message) {
1181
1248
  return new NotSupportBufferError(api, message);
1182
1249
  }
@@ -1189,6 +1256,9 @@ class Context {
1189
1256
  createEnv(filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort, nodeBinding) {
1190
1257
  return newEnv(this, filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort, nodeBinding);
1191
1258
  }
1259
+ createTrackedFinalizer(envObject, finalize_callback, finalize_data, finalize_hint) {
1260
+ return TrackedFinalizer.create(envObject, finalize_callback, finalize_data, finalize_hint);
1261
+ }
1192
1262
  getCurrentScope() {
1193
1263
  return this.scopeStore.currentScope;
1194
1264
  }
@@ -1267,11 +1337,11 @@ exports.Finalizer = Finalizer;
1267
1337
  exports.Handle = Handle;
1268
1338
  exports.HandleScope = HandleScope;
1269
1339
  exports.HandleStore = HandleStore;
1270
- exports.NAPI_VERSION = NAPI_VERSION;
1271
1340
  exports.NAPI_VERSION_EXPERIMENTAL = NAPI_VERSION_EXPERIMENTAL;
1272
1341
  exports.NODE_API_DEFAULT_MODULE_API_VERSION = NODE_API_DEFAULT_MODULE_API_VERSION;
1342
+ exports.NODE_API_SUPPORTED_VERSION_MAX = NODE_API_SUPPORTED_VERSION_MAX;
1343
+ exports.NODE_API_SUPPORTED_VERSION_MIN = NODE_API_SUPPORTED_VERSION_MIN;
1273
1344
  exports.NodeEnv = NodeEnv;
1274
- exports.NotSupportBigIntError = NotSupportBigIntError;
1275
1345
  exports.NotSupportBufferError = NotSupportBufferError;
1276
1346
  exports.NotSupportWeakRefError = NotSupportWeakRefError;
1277
1347
  exports.Persistent = Persistent;
@@ -1280,6 +1350,7 @@ exports.RefTracker = RefTracker;
1280
1350
  exports.Reference = Reference;
1281
1351
  exports.ScopeStore = ScopeStore;
1282
1352
  exports.Store = Store;
1353
+ exports.TrackedFinalizer = TrackedFinalizer;
1283
1354
  exports.TryCatch = TryCatch;
1284
1355
  exports.createContext = createContext;
1285
1356
  exports.getDefaultContext = getDefaultContext;