@emnapi/runtime 0.42.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.mjs CHANGED
@@ -199,7 +199,7 @@ const _Buffer = typeof Buffer === 'function'
199
199
  catch (_) { }
200
200
  return undefined;
201
201
  })();
202
- const version = "0.42.0";
202
+ const version = "0.43.0";
203
203
  const NODE_API_SUPPORTED_VERSION_MIN = 1;
204
204
  const NODE_API_SUPPORTED_VERSION_MAX = 9;
205
205
  const NAPI_VERSION_EXPERIMENTAL = 2147483647;
@@ -489,7 +489,7 @@ class Finalizer {
489
489
  }
490
490
  }
491
491
 
492
- class RefBase extends Finalizer {
492
+ class TrackedFinalizer extends Finalizer {
493
493
  static finalizeAll(list) {
494
494
  RefTracker.finalizeAll(list);
495
495
  }
@@ -499,12 +499,13 @@ class RefBase extends Finalizer {
499
499
  unlink() {
500
500
  RefTracker.prototype.unlink.call(this);
501
501
  }
502
- 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) {
503
506
  super(envObject, finalize_callback, finalize_data, finalize_hint);
504
507
  this._next = null;
505
508
  this._prev = null;
506
- this._refcount = initial_refcount;
507
- this._ownership = ownership;
508
509
  this.link(!finalize_callback ? envObject.reflist : envObject.finalizing_reflist);
509
510
  }
510
511
  dispose() {
@@ -512,26 +513,10 @@ class RefBase extends Finalizer {
512
513
  this.envObject.dequeueFinalizer(this);
513
514
  super.dispose();
514
515
  }
515
- data() {
516
- return this._finalizeData;
517
- }
518
- ref() {
519
- return ++this._refcount;
520
- }
521
- unref() {
522
- if (this._refcount === 0) {
523
- return 0;
524
- }
525
- return --this._refcount;
526
- }
527
- refCount() {
528
- return this._refcount;
529
- }
530
- ownership() {
531
- return this._ownership;
532
- }
533
516
  finalize() {
534
- const ownership = this._ownership;
517
+ this.finalizeCore(true);
518
+ }
519
+ finalizeCore(deleteMe) {
535
520
  const finalize_callback = this._finalizeCallback;
536
521
  const finalize_data = this._finalizeData;
537
522
  const finalize_hint = this._finalizeHint;
@@ -549,7 +534,7 @@ class RefBase extends Finalizer {
549
534
  error = err;
550
535
  }
551
536
  }
552
- if (ownership === 0) {
537
+ if (deleteMe) {
553
538
  this.dispose();
554
539
  }
555
540
  if (caught) {
@@ -558,6 +543,35 @@ class RefBase extends Finalizer {
558
543
  }
559
544
  }
560
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
+
561
575
  function throwNodeApiVersionError(moduleName, moduleApiVersion) {
562
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.`;
563
577
  throw new Error(errorMessage);
@@ -587,6 +601,7 @@ class Env {
587
601
  engineErrorCode: 0,
588
602
  engineReserved: 0
589
603
  };
604
+ this.inGcFinalizer = false;
590
605
  this._bindingMap = new WeakMap();
591
606
  this.id = 0;
592
607
  }
@@ -658,6 +673,31 @@ class Env {
658
673
  this.ctx.closeScope(this, scope);
659
674
  }
660
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
+ }
661
701
  enqueueFinalizer(finalizer) {
662
702
  if (this.pendingFinalizers.indexOf(finalizer) === -1) {
663
703
  this.pendingFinalizers.push(finalizer);
@@ -720,7 +760,7 @@ class NodeEnv extends Env {
720
760
  super.deleteMe();
721
761
  }
722
762
  canCallIntoJs() {
723
- return this.ctx.canCallIntoJs();
763
+ return super.canCallIntoJs() && this.ctx.canCallIntoJs();
724
764
  }
725
765
  triggerFatalException(err) {
726
766
  if (this.nodeBinding) {
@@ -746,7 +786,7 @@ class NodeEnv extends Env {
746
786
  }
747
787
  const hasProcess = typeof process === 'object' && process !== null;
748
788
  const hasForceFlag = hasProcess ? Boolean(process.execArgv && (process.execArgv.indexOf('--force-node-api-uncaught-exceptions-policy') !== -1)) : false;
749
- if (!hasForceFlag && !enforceUncaughtExceptionPolicy) {
789
+ if (envObject.moduleApiVersion < NAPI_VERSION_EXPERIMENTAL && !hasForceFlag && !enforceUncaughtExceptionPolicy) {
750
790
  const warn = hasProcess && typeof process.emitWarning === 'function'
751
791
  ? process.emitWarning
752
792
  : function (warning, type, code) {
@@ -947,7 +987,7 @@ Persistent._registry = supportFinalizer
947
987
 
948
988
  function weakCallback(ref) {
949
989
  ref.persistent.reset();
950
- ref.envObject.enqueueFinalizer(ref);
990
+ ref.envObject.invokeFinalizerFromGC(ref);
951
991
  }
952
992
  function canBeHeldWeakly(value) {
953
993
  return value.isObject() || value.isFunction() || value.isSymbol();
@@ -1216,6 +1256,9 @@ class Context {
1216
1256
  createEnv(filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort, nodeBinding) {
1217
1257
  return newEnv(this, filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort, nodeBinding);
1218
1258
  }
1259
+ createTrackedFinalizer(envObject, finalize_callback, finalize_data, finalize_hint) {
1260
+ return TrackedFinalizer.create(envObject, finalize_callback, finalize_data, finalize_hint);
1261
+ }
1219
1262
  getCurrentScope() {
1220
1263
  return this.scopeStore.currentScope;
1221
1264
  }
@@ -1283,4 +1326,4 @@ function getDefaultContext() {
1283
1326
  return defaultContext;
1284
1327
  }
1285
1328
 
1286
- 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, TryCatch, createContext, getDefaultContext, isReferenceType, version };
1329
+ 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 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@emnapi/runtime",
3
- "version": "0.42.0",
3
+ "version": "0.43.0",
4
4
  "description": "emnapi runtime",
5
5
  "main": "index.js",
6
6
  "module": "./dist/emnapi.esm-bundler.js",