@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.d.ts CHANGED
@@ -27,8 +27,9 @@ export declare const enum Ownership {
27
27
  }
28
28
 
29
29
  export declare const enum Version {
30
+ NODE_API_SUPPORTED_VERSION_MIN = 1,
30
31
  NODE_API_DEFAULT_MODULE_API_VERSION = 8,
31
- NAPI_VERSION = 9,
32
+ NODE_API_SUPPORTED_VERSION_MAX = 9,
32
33
  NAPI_VERSION_EXPERIMENTAL = 2147483647 // INT_MAX
33
34
  }
34
35
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -251,19 +252,23 @@ export declare const enum napi_threadsafe_function_release_mode {
251
252
  napi_tsfn_abort
252
253
  }
253
254
  export declare class CallbackInfo {
254
- parent: CallbackInfo | null;
255
+ id: number;
256
+ parent: CallbackInfo;
257
+ child: CallbackInfo | null;
255
258
  thiz: any;
256
259
  data: void_p;
257
260
  args: ArrayLike<any>;
258
261
  fn: Function;
259
- constructor(parent: CallbackInfo | null, thiz: any, data: void_p, args: ArrayLike<any>, fn: Function);
262
+ constructor(id: number, parent: CallbackInfo, child: CallbackInfo | null, thiz: any, data: void_p, args: ArrayLike<any>, fn: Function);
260
263
  getNewTarget(envObject: Env): number;
264
+ dispose(): void;
261
265
  }
262
266
 
263
267
  export declare class CallbackInfoStack {
264
- current: CallbackInfo | null;
268
+ current: CallbackInfo;
269
+ get(id: number): CallbackInfo | null;
265
270
  pop(): void;
266
- push(thiz: any, data: void_p, args: ArrayLike<any>, fn: Function): CallbackInfo;
271
+ push(thiz: any, data: void_p, args: ArrayLike<any>, fn: Function): number;
267
272
  dispose(): void;
268
273
  }
269
274
 
@@ -302,16 +307,16 @@ export declare class Context {
302
307
  constructor();
303
308
  getRuntimeVersions(): {
304
309
  version: string;
305
- NAPI_VERSION: Version;
310
+ NODE_API_SUPPORTED_VERSION_MAX: Version;
306
311
  NAPI_VERSION_EXPERIMENTAL: Version;
307
312
  NODE_API_DEFAULT_MODULE_API_VERSION: Version;
308
313
  };
309
314
  createNotSupportWeakRefError(api: string, message: string): NotSupportWeakRefError;
310
- createNotSupportBigIntError(api: string, message: string): NotSupportBigIntError;
311
315
  createNotSupportBufferError(api: string, message: string): NotSupportBufferError;
312
316
  createReference(envObject: Env, handle_id: napi_value, initialRefcount: uint32_t, ownership: 0 | 1, finalize_callback?: napi_finalize, finalize_data?: void_p, finalize_hint?: void_p): Reference;
313
317
  createDeferred<T = any>(value: IDeferrdValue<T>): Deferred<T>;
314
318
  createEnv(filename: string, moduleApiVersion: number, makeDynCall_vppp: (cb: Ptr) => (a: Ptr, b: Ptr, c: Ptr) => void, makeDynCall_vp: (cb: Ptr) => (a: Ptr) => void, abort: (msg?: string) => never, nodeBinding?: any): Env;
319
+ createTrackedFinalizer(envObject: Env, finalize_callback: napi_finalize, finalize_data: void_p, finalize_hint: void_p): TrackedFinalizer;
315
320
  getCurrentScope(): HandleScope | null;
316
321
  addToCurrentScope<V>(value: V): Handle<V>;
317
322
  openScope(envObject: Env): HandleScope;
@@ -364,6 +369,7 @@ export declare class Env implements IStoreValue {
364
369
  engineErrorCode: number;
365
370
  engineReserved: Ptr;
366
371
  };
372
+ inGcFinalizer: boolean;
367
373
  constructor(ctx: Context, moduleApiVersion: number, makeDynCall_vppp: (cb: Ptr) => (a: Ptr, b: Ptr, c: Ptr) => void, makeDynCall_vp: (cb: Ptr) => (a: Ptr) => void, abort: (msg?: string) => never);
368
374
  canCallIntoJs(): boolean;
369
375
  terminatedOrTerminating(): boolean;
@@ -376,6 +382,8 @@ export declare class Env implements IStoreValue {
376
382
  getReturnStatus(): napi_status;
377
383
  callIntoModule<T>(fn: (env: Env) => T, handleException?: (envObject: Env, value: any) => void): T;
378
384
  callFinalizer(cb: napi_finalize, data: void_p, hint: void_p): void;
385
+ invokeFinalizerFromGC(finalizer: RefTracker): void;
386
+ checkGCAccess(): void;
379
387
  enqueueFinalizer(finalizer: RefTracker): void;
380
388
  dequeueFinalizer(finalizer: RefTracker): void;
381
389
  deleteMe(): void;
@@ -478,12 +486,14 @@ export declare interface IStoreValue {
478
486
  [x: string]: any;
479
487
  }
480
488
 
481
- export declare const NAPI_VERSION = Version.NAPI_VERSION;
482
-
483
489
  export declare const NAPI_VERSION_EXPERIMENTAL = Version.NAPI_VERSION_EXPERIMENTAL;
484
490
 
485
491
  export declare const NODE_API_DEFAULT_MODULE_API_VERSION = Version.NODE_API_DEFAULT_MODULE_API_VERSION;
486
492
 
493
+ export declare const NODE_API_SUPPORTED_VERSION_MAX = Version.NODE_API_SUPPORTED_VERSION_MAX;
494
+
495
+ export declare const NODE_API_SUPPORTED_VERSION_MIN = Version.NODE_API_SUPPORTED_VERSION_MIN;
496
+
487
497
  export declare class NodeEnv extends Env {
488
498
  filename: string;
489
499
  private readonly nodeBinding?;
@@ -500,10 +510,6 @@ export declare class NodeEnv extends Env {
500
510
  drainFinalizerQueue(): void;
501
511
  }
502
512
 
503
- export declare class NotSupportBigIntError extends EmnapiError {
504
- constructor(api: string, message: string);
505
- }
506
-
507
513
  export declare class NotSupportBufferError extends EmnapiError {
508
514
  constructor(api: string, message: string);
509
515
  }
@@ -525,17 +531,10 @@ export declare class Persistent<T> {
525
531
  deref(): T | undefined;
526
532
  }
527
533
 
528
- export declare interface RefBase extends Finalizer, RefTracker {
529
- }
530
-
531
- export declare class RefBase extends Finalizer {
532
- static finalizeAll(list: RefTracker): void;
533
- link(list: RefTracker): void;
534
- unlink(): void;
534
+ export declare class RefBase extends TrackedFinalizer {
535
535
  private _refcount;
536
536
  private readonly _ownership;
537
537
  constructor(envObject: Env, initial_refcount: uint32_t, ownership: number, finalize_callback: napi_finalize, finalize_data: void_p, finalize_hint: void_p);
538
- dispose(): void;
539
538
  data(): void_p;
540
539
  ref(): uint32_t;
541
540
  unref(): uint32_t;
@@ -589,6 +588,20 @@ export declare class Store<V extends IStoreValue> {
589
588
  dispose(): void;
590
589
  }
591
590
 
591
+ export declare interface TrackedFinalizer extends Finalizer, RefTracker {
592
+ }
593
+
594
+ export declare class TrackedFinalizer extends Finalizer {
595
+ static finalizeAll(list: RefTracker): void;
596
+ link(list: RefTracker): void;
597
+ unlink(): void;
598
+ static create(envObject: Env, finalize_callback: napi_finalize, finalize_data: void_p, finalize_hint: void_p): TrackedFinalizer;
599
+ protected constructor(envObject: Env, finalize_callback: napi_finalize, finalize_data: void_p, finalize_hint: void_p);
600
+ dispose(): void;
601
+ finalize(): void;
602
+ protected finalizeCore(deleteMe: boolean): void;
603
+ }
604
+
592
605
  export declare class TryCatch {
593
606
  private _exception;
594
607
  private _caught;
@@ -1,8 +1,11 @@
1
1
  import { __extends } from 'tslib';
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 CallbackInfo = /*#__PURE__*/ (function () {
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;
@@ -174,8 +206,9 @@ var _Buffer = typeof Buffer === 'function'
174
206
  catch (_) { }
175
207
  return undefined;
176
208
  })();
177
- var version = "0.41.0";
178
- var NAPI_VERSION = 9 /* Version.NAPI_VERSION */;
209
+ var version = "0.43.0";
210
+ var NODE_API_SUPPORTED_VERSION_MIN = 1 /* Version.NODE_API_SUPPORTED_VERSION_MIN */;
211
+ var NODE_API_SUPPORTED_VERSION_MAX = 9 /* Version.NODE_API_SUPPORTED_VERSION_MAX */;
179
212
  var NAPI_VERSION_EXPERIMENTAL = 2147483647 /* Version.NAPI_VERSION_EXPERIMENTAL */;
180
213
  var NODE_API_DEFAULT_MODULE_API_VERSION = 8 /* Version.NODE_API_DEFAULT_MODULE_API_VERSION */;
181
214
 
@@ -476,53 +509,36 @@ var Finalizer = /*#__PURE__*/ (function () {
476
509
  return Finalizer;
477
510
  }());
478
511
 
479
- var RefBase = /*#__PURE__*/ (function (_super) {
480
- __extends(RefBase, _super);
481
- function RefBase(envObject, initial_refcount, ownership, finalize_callback, finalize_data, finalize_hint) {
512
+ var TrackedFinalizer = /*#__PURE__*/ (function (_super) {
513
+ __extends(TrackedFinalizer, _super);
514
+ function TrackedFinalizer(envObject, finalize_callback, finalize_data, finalize_hint) {
482
515
  var _this = _super.call(this, envObject, finalize_callback, finalize_data, finalize_hint) || this;
483
516
  _this._next = null;
484
517
  _this._prev = null;
485
- _this._refcount = initial_refcount;
486
- _this._ownership = ownership;
487
518
  _this.link(!finalize_callback ? envObject.reflist : envObject.finalizing_reflist);
488
519
  return _this;
489
520
  }
490
- RefBase.finalizeAll = function (list) {
521
+ TrackedFinalizer.finalizeAll = function (list) {
491
522
  RefTracker.finalizeAll(list);
492
523
  };
493
- RefBase.prototype.link = function (list) {
524
+ TrackedFinalizer.prototype.link = function (list) {
494
525
  RefTracker.prototype.link.call(this, list);
495
526
  };
496
- RefBase.prototype.unlink = function () {
527
+ TrackedFinalizer.prototype.unlink = function () {
497
528
  RefTracker.prototype.unlink.call(this);
498
529
  };
499
- RefBase.prototype.dispose = function () {
530
+ TrackedFinalizer.create = function (envObject, finalize_callback, finalize_data, finalize_hint) {
531
+ return new TrackedFinalizer(envObject, finalize_callback, finalize_data, finalize_hint);
532
+ };
533
+ TrackedFinalizer.prototype.dispose = function () {
500
534
  this.unlink();
501
535
  this.envObject.dequeueFinalizer(this);
502
536
  _super.prototype.dispose.call(this);
503
537
  };
504
- RefBase.prototype.data = function () {
505
- return this._finalizeData;
506
- };
507
- RefBase.prototype.ref = function () {
508
- return ++this._refcount;
538
+ TrackedFinalizer.prototype.finalize = function () {
539
+ this.finalizeCore(true);
509
540
  };
510
- RefBase.prototype.unref = function () {
511
- if (this._refcount === 0) {
512
- return 0;
513
- }
514
- return --this._refcount;
515
- };
516
- RefBase.prototype.refCount = function () {
517
- return this._refcount;
518
- };
519
- RefBase.prototype.ownership = function () {
520
- return this._ownership;
521
- };
522
- RefBase.prototype.finalize = function () {
523
- var ownership = this._ownership;
524
- // Swap out the field finalize_callback so that it can not be accidentally
525
- // called more than once.
541
+ TrackedFinalizer.prototype.finalizeCore = function (deleteMe) {
526
542
  var finalize_callback = this._finalizeCallback;
527
543
  var finalize_data = this._finalizeData;
528
544
  var finalize_hint = this._finalizeHint;
@@ -540,18 +556,50 @@ var RefBase = /*#__PURE__*/ (function (_super) {
540
556
  error = err;
541
557
  }
542
558
  }
543
- if (ownership === 0 /* Ownership.kRuntime */) {
559
+ if (deleteMe) {
544
560
  this.dispose();
545
561
  }
546
562
  if (caught) {
547
563
  throw error;
548
564
  }
549
565
  };
550
- return RefBase;
566
+ return TrackedFinalizer;
551
567
  }(Finalizer));
552
568
 
569
+ var RefBase = /*#__PURE__*/ (function (_super) {
570
+ __extends(RefBase, _super);
571
+ function RefBase(envObject, initial_refcount, ownership, finalize_callback, finalize_data, finalize_hint) {
572
+ var _this = _super.call(this, envObject, finalize_callback, finalize_data, finalize_hint) || this;
573
+ _this._refcount = initial_refcount;
574
+ _this._ownership = ownership;
575
+ return _this;
576
+ }
577
+ RefBase.prototype.data = function () {
578
+ return this._finalizeData;
579
+ };
580
+ RefBase.prototype.ref = function () {
581
+ return ++this._refcount;
582
+ };
583
+ RefBase.prototype.unref = function () {
584
+ if (this._refcount === 0) {
585
+ return 0;
586
+ }
587
+ return --this._refcount;
588
+ };
589
+ RefBase.prototype.refCount = function () {
590
+ return this._refcount;
591
+ };
592
+ RefBase.prototype.ownership = function () {
593
+ return this._ownership;
594
+ };
595
+ RefBase.prototype.finalize = function () {
596
+ this.finalizeCore(this._ownership === 0 /* Ownership.kRuntime */);
597
+ };
598
+ return RefBase;
599
+ }(TrackedFinalizer));
600
+
553
601
  function throwNodeApiVersionError(moduleName, moduleApiVersion) {
554
- var errorMessage = "".concat(moduleName, " requires Node-API version ").concat(moduleApiVersion, ", but this version of Node.js only supports version ").concat(NAPI_VERSION, " add-ons.");
602
+ 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.");
555
603
  throw new Error(errorMessage);
556
604
  }
557
605
  function handleThrow(envObject, value) {
@@ -579,6 +627,7 @@ var Env = /*#__PURE__*/ (function () {
579
627
  engineErrorCode: 0,
580
628
  engineReserved: 0
581
629
  };
630
+ this.inGcFinalizer = false;
582
631
  this._bindingMap = new WeakMap();
583
632
  this.id = 0;
584
633
  }
@@ -655,6 +704,31 @@ var Env = /*#__PURE__*/ (function () {
655
704
  this.ctx.closeScope(this, scope);
656
705
  }
657
706
  };
707
+ Env.prototype.invokeFinalizerFromGC = function (finalizer) {
708
+ if (this.moduleApiVersion !== NAPI_VERSION_EXPERIMENTAL) {
709
+ this.enqueueFinalizer(finalizer);
710
+ }
711
+ else {
712
+ var saved = this.inGcFinalizer;
713
+ this.inGcFinalizer = true;
714
+ try {
715
+ finalizer.finalize();
716
+ }
717
+ finally {
718
+ this.inGcFinalizer = saved;
719
+ }
720
+ }
721
+ };
722
+ Env.prototype.checkGCAccess = function () {
723
+ if (this.moduleApiVersion === NAPI_VERSION_EXPERIMENTAL && this.inGcFinalizer) {
724
+ this.abort('Finalizer is calling a function that may affect GC state.\n' +
725
+ 'The finalizers are run directly from GC and must not affect GC ' +
726
+ 'state.\n' +
727
+ 'Use `node_api_post_finalizer` from inside of the finalizer to work ' +
728
+ 'around this issue.\n' +
729
+ 'It schedules the call as a new task in the event loop.');
730
+ }
731
+ };
658
732
  /** @virtual */
659
733
  Env.prototype.enqueueFinalizer = function (finalizer) {
660
734
  if (this.pendingFinalizers.indexOf(finalizer) === -1) {
@@ -723,7 +797,7 @@ var NodeEnv = /*#__PURE__*/ (function (_super) {
723
797
  _super.prototype.deleteMe.call(this);
724
798
  };
725
799
  NodeEnv.prototype.canCallIntoJs = function () {
726
- return this.ctx.canCallIntoJs();
800
+ return _super.prototype.canCallIntoJs.call(this) && this.ctx.canCallIntoJs();
727
801
  };
728
802
  NodeEnv.prototype.triggerFatalException = function (err) {
729
803
  if (this.nodeBinding) {
@@ -749,13 +823,21 @@ var NodeEnv = /*#__PURE__*/ (function (_super) {
749
823
  }
750
824
  var hasProcess = typeof process === 'object' && process !== null;
751
825
  var hasForceFlag = hasProcess ? Boolean(process.execArgv && (process.execArgv.indexOf('--force-node-api-uncaught-exceptions-policy') !== -1)) : false;
752
- if (!hasForceFlag && !enforceUncaughtExceptionPolicy) {
753
- if (hasProcess && typeof process.emitWarning === 'function') {
754
- 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');
755
- }
756
- else {
757
- throw err;
758
- }
826
+ if (envObject.moduleApiVersion < NAPI_VERSION_EXPERIMENTAL && !hasForceFlag && !enforceUncaughtExceptionPolicy) {
827
+ var warn = hasProcess && typeof process.emitWarning === 'function'
828
+ ? process.emitWarning
829
+ : function (warning, type, code) {
830
+ if (warning instanceof Error) {
831
+ console.warn(warning.toString());
832
+ }
833
+ else {
834
+ var prefix = code ? "[".concat(code, "] ") : '';
835
+ // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
836
+ console.warn("".concat(prefix).concat(type || 'Warning', ": ").concat(warning));
837
+ }
838
+ };
839
+ 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');
840
+ return;
759
841
  }
760
842
  envObject.triggerFatalException(err);
761
843
  });
@@ -801,7 +883,7 @@ function newEnv(ctx, filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_v
801
883
  if (moduleApiVersion < NODE_API_DEFAULT_MODULE_API_VERSION) {
802
884
  moduleApiVersion = NODE_API_DEFAULT_MODULE_API_VERSION;
803
885
  }
804
- else if (moduleApiVersion > NAPI_VERSION && moduleApiVersion !== NAPI_VERSION_EXPERIMENTAL) {
886
+ else if (moduleApiVersion > NODE_API_SUPPORTED_VERSION_MAX && moduleApiVersion !== NAPI_VERSION_EXPERIMENTAL) {
805
887
  throwNodeApiVersionError(filename, moduleApiVersion);
806
888
  }
807
889
  var env = new NodeEnv(ctx, filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort, nodeBinding);
@@ -851,18 +933,6 @@ Object.defineProperty(NotSupportWeakRefError.prototype, 'name', {
851
933
  writable: true,
852
934
  value: 'NotSupportWeakRefError'
853
935
  });
854
- var NotSupportBigIntError = /*#__PURE__*/ (function (_super) {
855
- __extends(NotSupportBigIntError, _super);
856
- function NotSupportBigIntError(api, message) {
857
- return _super.call(this, "".concat(api, ": The current runtime does not support \"BigInt\".").concat(message ? " ".concat(message) : '')) || this;
858
- }
859
- return NotSupportBigIntError;
860
- }(EmnapiError));
861
- Object.defineProperty(NotSupportBigIntError.prototype, 'name', {
862
- configurable: true,
863
- writable: true,
864
- value: 'NotSupportBigIntError'
865
- });
866
936
  var NotSupportBufferError = /*#__PURE__*/ (function (_super) {
867
937
  __extends(NotSupportBufferError, _super);
868
938
  function NotSupportBufferError(api, message) {
@@ -969,7 +1039,7 @@ var Persistent = /*#__PURE__*/ (function () {
969
1039
 
970
1040
  function weakCallback(ref) {
971
1041
  ref.persistent.reset();
972
- ref.envObject.enqueueFinalizer(ref);
1042
+ ref.envObject.invokeFinalizerFromGC(ref);
973
1043
  }
974
1044
  function canBeHeldWeakly(value) {
975
1045
  return value.isObject() || value.isFunction() || value.isSymbol();
@@ -1234,7 +1304,7 @@ var Context = /*#__PURE__*/ (function () {
1234
1304
  Context.prototype.getRuntimeVersions = function () {
1235
1305
  return {
1236
1306
  version: version,
1237
- NAPI_VERSION: NAPI_VERSION,
1307
+ NODE_API_SUPPORTED_VERSION_MAX: NODE_API_SUPPORTED_VERSION_MAX,
1238
1308
  NAPI_VERSION_EXPERIMENTAL: NAPI_VERSION_EXPERIMENTAL,
1239
1309
  NODE_API_DEFAULT_MODULE_API_VERSION: NODE_API_DEFAULT_MODULE_API_VERSION
1240
1310
  };
@@ -1242,9 +1312,6 @@ var Context = /*#__PURE__*/ (function () {
1242
1312
  Context.prototype.createNotSupportWeakRefError = function (api, message) {
1243
1313
  return new NotSupportWeakRefError(api, message);
1244
1314
  };
1245
- Context.prototype.createNotSupportBigIntError = function (api, message) {
1246
- return new NotSupportBigIntError(api, message);
1247
- };
1248
1315
  Context.prototype.createNotSupportBufferError = function (api, message) {
1249
1316
  return new NotSupportBufferError(api, message);
1250
1317
  };
@@ -1260,6 +1327,9 @@ var Context = /*#__PURE__*/ (function () {
1260
1327
  Context.prototype.createEnv = function (filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort, nodeBinding) {
1261
1328
  return newEnv(this, filename, moduleApiVersion, makeDynCall_vppp, makeDynCall_vp, abort, nodeBinding);
1262
1329
  };
1330
+ Context.prototype.createTrackedFinalizer = function (envObject, finalize_callback, finalize_data, finalize_hint) {
1331
+ return TrackedFinalizer.create(envObject, finalize_callback, finalize_data, finalize_hint);
1332
+ };
1263
1333
  Context.prototype.getCurrentScope = function () {
1264
1334
  return this.scopeStore.currentScope;
1265
1335
  };
@@ -1328,4 +1398,4 @@ function getDefaultContext() {
1328
1398
  return defaultContext;
1329
1399
  }
1330
1400
 
1331
- export { CallbackInfo, CallbackInfoStack, ConstHandle, Context, Deferred, EmnapiError, Env, Finalizer, Handle, HandleScope, HandleStore, NAPI_VERSION, NAPI_VERSION_EXPERIMENTAL, NODE_API_DEFAULT_MODULE_API_VERSION, NodeEnv, NotSupportBigIntError, NotSupportBufferError, NotSupportWeakRefError, Persistent, RefBase, RefTracker, Reference, ScopeStore, Store, TryCatch, createContext, getDefaultContext, isReferenceType, version };
1401
+ 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 };
@@ -1,19 +1,23 @@
1
1
  declare namespace emnapi {
2
2
 
3
3
  export class CallbackInfo {
4
- parent: CallbackInfo | null;
4
+ id: number;
5
+ parent: CallbackInfo;
6
+ child: CallbackInfo | null;
5
7
  thiz: any;
6
8
  data: void_p;
7
9
  args: ArrayLike<any>;
8
10
  fn: Function;
9
- constructor(parent: CallbackInfo | null, thiz: any, data: void_p, args: ArrayLike<any>, fn: Function);
11
+ constructor(id: number, parent: CallbackInfo, child: CallbackInfo | null, thiz: any, data: void_p, args: ArrayLike<any>, fn: Function);
10
12
  getNewTarget(envObject: Env): number;
13
+ dispose(): void;
11
14
  }
12
15
 
13
16
  export class CallbackInfoStack {
14
- current: CallbackInfo | null;
17
+ current: CallbackInfo;
18
+ get(id: number): CallbackInfo | null;
15
19
  pop(): void;
16
- push(thiz: any, data: void_p, args: ArrayLike<any>, fn: Function): CallbackInfo;
20
+ push(thiz: any, data: void_p, args: ArrayLike<any>, fn: Function): number;
17
21
  dispose(): void;
18
22
  }
19
23
 
@@ -52,16 +56,16 @@ export class Context {
52
56
  constructor();
53
57
  getRuntimeVersions(): {
54
58
  version: string;
55
- NAPI_VERSION: Version;
59
+ NODE_API_SUPPORTED_VERSION_MAX: Version;
56
60
  NAPI_VERSION_EXPERIMENTAL: Version;
57
61
  NODE_API_DEFAULT_MODULE_API_VERSION: Version;
58
62
  };
59
63
  createNotSupportWeakRefError(api: string, message: string): NotSupportWeakRefError;
60
- createNotSupportBigIntError(api: string, message: string): NotSupportBigIntError;
61
64
  createNotSupportBufferError(api: string, message: string): NotSupportBufferError;
62
65
  createReference(envObject: Env, handle_id: napi_value, initialRefcount: uint32_t, ownership: 0 | 1, finalize_callback?: napi_finalize, finalize_data?: void_p, finalize_hint?: void_p): Reference;
63
66
  createDeferred<T = any>(value: IDeferrdValue<T>): Deferred<T>;
64
67
  createEnv(filename: string, moduleApiVersion: number, makeDynCall_vppp: (cb: Ptr) => (a: Ptr, b: Ptr, c: Ptr) => void, makeDynCall_vp: (cb: Ptr) => (a: Ptr) => void, abort: (msg?: string) => never, nodeBinding?: any): Env;
68
+ createTrackedFinalizer(envObject: Env, finalize_callback: napi_finalize, finalize_data: void_p, finalize_hint: void_p): TrackedFinalizer;
65
69
  getCurrentScope(): HandleScope | null;
66
70
  addToCurrentScope<V>(value: V): Handle<V>;
67
71
  openScope(envObject: Env): HandleScope;
@@ -114,6 +118,7 @@ export class Env implements IStoreValue {
114
118
  engineErrorCode: number;
115
119
  engineReserved: Ptr;
116
120
  };
121
+ inGcFinalizer: boolean;
117
122
  constructor(ctx: Context, moduleApiVersion: number, makeDynCall_vppp: (cb: Ptr) => (a: Ptr, b: Ptr, c: Ptr) => void, makeDynCall_vp: (cb: Ptr) => (a: Ptr) => void, abort: (msg?: string) => never);
118
123
  canCallIntoJs(): boolean;
119
124
  terminatedOrTerminating(): boolean;
@@ -126,6 +131,8 @@ export class Env implements IStoreValue {
126
131
  getReturnStatus(): napi_status;
127
132
  callIntoModule<T>(fn: (env: Env) => T, handleException?: (envObject: Env, value: any) => void): T;
128
133
  callFinalizer(cb: napi_finalize, data: void_p, hint: void_p): void;
134
+ invokeFinalizerFromGC(finalizer: RefTracker): void;
135
+ checkGCAccess(): void;
129
136
  enqueueFinalizer(finalizer: RefTracker): void;
130
137
  dequeueFinalizer(finalizer: RefTracker): void;
131
138
  deleteMe(): void;
@@ -228,12 +235,14 @@ export interface IStoreValue {
228
235
  [x: string]: any;
229
236
  }
230
237
 
231
- export const NAPI_VERSION = Version.NAPI_VERSION;
232
-
233
238
  export const NAPI_VERSION_EXPERIMENTAL = Version.NAPI_VERSION_EXPERIMENTAL;
234
239
 
235
240
  export const NODE_API_DEFAULT_MODULE_API_VERSION = Version.NODE_API_DEFAULT_MODULE_API_VERSION;
236
241
 
242
+ export const NODE_API_SUPPORTED_VERSION_MAX = Version.NODE_API_SUPPORTED_VERSION_MAX;
243
+
244
+ export const NODE_API_SUPPORTED_VERSION_MIN = Version.NODE_API_SUPPORTED_VERSION_MIN;
245
+
237
246
  export class NodeEnv extends Env {
238
247
  filename: string;
239
248
  private readonly nodeBinding?;
@@ -250,10 +259,6 @@ export class NodeEnv extends Env {
250
259
  drainFinalizerQueue(): void;
251
260
  }
252
261
 
253
- export class NotSupportBigIntError extends EmnapiError {
254
- constructor(api: string, message: string);
255
- }
256
-
257
262
  export class NotSupportBufferError extends EmnapiError {
258
263
  constructor(api: string, message: string);
259
264
  }
@@ -275,17 +280,10 @@ export class Persistent<T> {
275
280
  deref(): T | undefined;
276
281
  }
277
282
 
278
- export interface RefBase extends Finalizer, RefTracker {
279
- }
280
-
281
- export class RefBase extends Finalizer {
282
- static finalizeAll(list: RefTracker): void;
283
- link(list: RefTracker): void;
284
- unlink(): void;
283
+ export class RefBase extends TrackedFinalizer {
285
284
  private _refcount;
286
285
  private readonly _ownership;
287
286
  constructor(envObject: Env, initial_refcount: uint32_t, ownership: number, finalize_callback: napi_finalize, finalize_data: void_p, finalize_hint: void_p);
288
- dispose(): void;
289
287
  data(): void_p;
290
288
  ref(): uint32_t;
291
289
  unref(): uint32_t;
@@ -339,6 +337,20 @@ export class Store<V extends IStoreValue> {
339
337
  dispose(): void;
340
338
  }
341
339
 
340
+ export interface TrackedFinalizer extends Finalizer, RefTracker {
341
+ }
342
+
343
+ export class TrackedFinalizer extends Finalizer {
344
+ static finalizeAll(list: RefTracker): void;
345
+ link(list: RefTracker): void;
346
+ unlink(): void;
347
+ static create(envObject: Env, finalize_callback: napi_finalize, finalize_data: void_p, finalize_hint: void_p): TrackedFinalizer;
348
+ protected constructor(envObject: Env, finalize_callback: napi_finalize, finalize_data: void_p, finalize_hint: void_p);
349
+ dispose(): void;
350
+ finalize(): void;
351
+ protected finalizeCore(deleteMe: boolean): void;
352
+ }
353
+
342
354
  export class TryCatch {
343
355
  private _exception;
344
356
  private _caught;