@emnapi/runtime 1.3.0 → 1.4.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,68 +1,20 @@
1
- const EMPTY_ARGS = [];
2
- class CallbackInfo {
3
- constructor(id, parent, child, thiz, data, args, fn) {
4
- this.id = id;
5
- this.parent = parent;
6
- this.child = child;
7
- this.thiz = thiz;
8
- this.data = data;
9
- this.args = args;
10
- this.fn = fn;
11
- }
12
- getNewTarget(envObject) {
13
- const thiz = this.thiz;
14
- if (thiz == null || thiz.constructor == null)
15
- return 0;
16
- return thiz instanceof this.fn ? envObject.ensureHandleId(thiz.constructor) : 0;
17
- }
18
- dispose() {
19
- if (this.thiz !== undefined)
20
- this.thiz = undefined;
21
- this.args = EMPTY_ARGS;
22
- this.fn = null;
23
- }
1
+ const externalValue = new WeakMap();
2
+ function isExternal(object) {
3
+ return externalValue.has(object);
24
4
  }
25
- const ROOT_CBINFO = new CallbackInfo(0, null, null, null, 0, null, null);
26
- class CallbackInfoStack {
27
- constructor() {
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;
40
- }
41
- pop() {
42
- const current = this.current;
43
- if (current === ROOT_CBINFO)
44
- return;
45
- this.current = current.parent;
46
- current.dispose();
47
- }
48
- push(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
- }
60
- this.current = info;
61
- return info.id;
5
+ const External = (() => {
6
+ function External(value) {
7
+ Object.setPrototypeOf(this, null);
8
+ externalValue.set(this, value);
62
9
  }
63
- dispose() {
64
- this.current = null;
10
+ External.prototype = null;
11
+ return External;
12
+ })();
13
+ function getExternalValue(external) {
14
+ if (!isExternal(external)) {
15
+ throw new TypeError('not external');
65
16
  }
17
+ return externalValue.get(external);
66
18
  }
67
19
 
68
20
  const supportNewFunction = (function () {
@@ -202,9 +154,9 @@ const _Buffer = typeof Buffer === 'function'
202
154
  catch (_) { }
203
155
  return undefined;
204
156
  })();
205
- const version = "1.3.0";
157
+ const version = "1.4.0";
206
158
  const NODE_API_SUPPORTED_VERSION_MIN = 1;
207
- const NODE_API_SUPPORTED_VERSION_MAX = 9;
159
+ const NODE_API_SUPPORTED_VERSION_MAX = 10;
208
160
  const NAPI_VERSION_EXPERIMENTAL = 2147483647;
209
161
  const NODE_API_DEFAULT_MODULE_API_VERSION = 8;
210
162
 
@@ -213,8 +165,8 @@ class Handle {
213
165
  this.id = id;
214
166
  this.value = value;
215
167
  }
216
- data(envObject) {
217
- return envObject.getObjectBinding(this.value).data;
168
+ data() {
169
+ return getExternalValue(this.value);
218
170
  }
219
171
  isNumber() {
220
172
  return typeof this.value === 'number';
@@ -229,7 +181,7 @@ class Handle {
229
181
  return typeof this.value === 'function';
230
182
  }
231
183
  isExternal() {
232
- return (isReferenceType(this.value) && Object.getPrototypeOf(this.value) === null);
184
+ return isExternal(this.value);
233
185
  }
234
186
  isObject() {
235
187
  return typeof this.value === 'object' && this.value !== null;
@@ -244,6 +196,8 @@ class Handle {
244
196
  return (ArrayBuffer.isView(this.value)) && !(this.value instanceof DataView);
245
197
  }
246
198
  isBuffer(BufferConstructor) {
199
+ if (ArrayBuffer.isView(this.value))
200
+ return true;
247
201
  BufferConstructor !== null && BufferConstructor !== void 0 ? BufferConstructor : (BufferConstructor = _Buffer);
248
202
  return typeof BufferConstructor === 'function' && BufferConstructor.isBuffer(this.value);
249
203
  }
@@ -278,10 +232,6 @@ class ConstHandle extends Handle {
278
232
  }
279
233
  dispose() { }
280
234
  }
281
- function External() {
282
- Object.setPrototypeOf(this, null);
283
- }
284
- External.prototype = null;
285
235
  class HandleStore {
286
236
  constructor() {
287
237
  this._values = [
@@ -350,21 +300,24 @@ class HandleScope {
350
300
  this.start = start;
351
301
  this.end = end;
352
302
  this._escapeCalled = false;
303
+ this.callbackInfo = {
304
+ thiz: undefined,
305
+ data: 0,
306
+ args: undefined,
307
+ fn: undefined
308
+ };
353
309
  }
354
310
  add(value) {
355
311
  const h = this.handleStore.push(value);
356
312
  this.end++;
357
313
  return h;
358
314
  }
359
- addExternal(envObject, data) {
360
- const value = new External();
361
- const h = envObject.ctx.handleStore.push(value);
362
- const binding = envObject.initObjectBinding(value);
363
- binding.data = data;
364
- this.end++;
365
- return h;
315
+ addExternal(data) {
316
+ return this.add(new External(data));
366
317
  }
367
318
  dispose() {
319
+ if (this._escapeCalled)
320
+ this._escapeCalled = false;
368
321
  if (this.start === this.end)
369
322
  return;
370
323
  this.handleStore.erase(this.start, this.end);
@@ -391,27 +344,21 @@ class ScopeStore {
391
344
  constructor() {
392
345
  this._rootScope = new HandleScope(null, 0, null, 1, HandleStore.MIN_ID);
393
346
  this.currentScope = this._rootScope;
347
+ this._values = [undefined];
394
348
  }
395
349
  get(id) {
396
- id = Number(id);
397
- let scope = this.currentScope;
398
- while (scope !== this._rootScope) {
399
- if (scope.id === id) {
400
- return scope;
401
- }
402
- scope = scope.parent;
403
- }
404
- return undefined;
350
+ return this._values[id];
405
351
  }
406
352
  openScope(envObject) {
407
353
  const currentScope = this.currentScope;
408
354
  let scope = currentScope.child;
409
355
  if (scope !== null) {
410
356
  scope.start = scope.end = currentScope.end;
411
- scope._escapeCalled = false;
412
357
  }
413
358
  else {
414
- scope = new HandleScope(envObject.ctx.handleStore, currentScope.id + 1, currentScope, currentScope.end);
359
+ const id = currentScope.id + 1;
360
+ scope = new HandleScope(envObject.ctx.handleStore, id, currentScope, currentScope.end);
361
+ this._values[id] = scope;
415
362
  }
416
363
  this.currentScope = scope;
417
364
  envObject.openHandleScopes++;
@@ -426,19 +373,8 @@ class ScopeStore {
426
373
  envObject.openHandleScopes--;
427
374
  }
428
375
  dispose() {
429
- let scope = this.currentScope;
430
- while (scope !== null) {
431
- scope.handleStore = null;
432
- scope.id = 0;
433
- scope.parent = null;
434
- scope.start = HandleStore.MIN_ID;
435
- scope.end = HandleStore.MIN_ID;
436
- scope._escapeCalled = false;
437
- const child = scope.child;
438
- scope.child = null;
439
- scope = child;
440
- }
441
- this.currentScope = null;
376
+ this.currentScope = this._rootScope;
377
+ this._values.length = 1;
442
378
  }
443
379
  }
444
380
 
@@ -697,8 +633,7 @@ class Env {
697
633
  initObjectBinding(value) {
698
634
  const binding = {
699
635
  wrapped: 0,
700
- tag: null,
701
- data: 0
636
+ tag: null
702
637
  };
703
638
  this._bindingMap.set(value, binding);
704
639
  return binding;
@@ -759,7 +694,7 @@ class NodeEnv extends Env {
759
694
  }
760
695
  const hasProcess = typeof process === 'object' && process !== null;
761
696
  const hasForceFlag = hasProcess ? Boolean(process.execArgv && (process.execArgv.indexOf('--force-node-api-uncaught-exceptions-policy') !== -1)) : false;
762
- if (envObject.moduleApiVersion < NAPI_VERSION_EXPERIMENTAL && !hasForceFlag && !enforceUncaughtExceptionPolicy) {
697
+ if (envObject.moduleApiVersion < 10 && !hasForceFlag && !enforceUncaughtExceptionPolicy) {
763
698
  const warn = hasProcess && typeof process.emitWarning === 'function'
764
699
  ? process.emitWarning
765
700
  : function (warning, type, code) {
@@ -1255,7 +1190,6 @@ class Context {
1255
1190
  this.refStore = new Store();
1256
1191
  this.deferredStore = new Store();
1257
1192
  this.handleStore = new HandleStore();
1258
- this.cbinfoStack = new CallbackInfoStack();
1259
1193
  this.feature = {
1260
1194
  supportReflect,
1261
1195
  supportFinalizer,
@@ -1379,13 +1313,12 @@ function getDefaultContext() {
1379
1313
  return defaultContext;
1380
1314
  }
1381
1315
 
1382
- exports.CallbackInfo = CallbackInfo;
1383
- exports.CallbackInfoStack = CallbackInfoStack;
1384
1316
  exports.ConstHandle = ConstHandle;
1385
1317
  exports.Context = Context;
1386
1318
  exports.Deferred = Deferred;
1387
1319
  exports.EmnapiError = EmnapiError;
1388
1320
  exports.Env = Env;
1321
+ exports.External = External;
1389
1322
  exports.Finalizer = Finalizer;
1390
1323
  exports.Handle = Handle;
1391
1324
  exports.HandleScope = HandleScope;
@@ -1408,5 +1341,7 @@ exports.TrackedFinalizer = TrackedFinalizer;
1408
1341
  exports.TryCatch = TryCatch;
1409
1342
  exports.createContext = createContext;
1410
1343
  exports.getDefaultContext = getDefaultContext;
1344
+ exports.getExternalValue = getExternalValue;
1345
+ exports.isExternal = isExternal;
1411
1346
  exports.isReferenceType = isReferenceType;
1412
1347
  exports.version = version;
@@ -24,7 +24,7 @@ export declare const enum GlobalHandle {
24
24
  export declare const enum Version {
25
25
  NODE_API_SUPPORTED_VERSION_MIN = 1,
26
26
  NODE_API_DEFAULT_MODULE_API_VERSION = 8,
27
- NODE_API_SUPPORTED_VERSION_MAX = 9,
27
+ NODE_API_SUPPORTED_VERSION_MAX = 10,
28
28
  NAPI_VERSION_EXPERIMENTAL = 2147483647 // INT_MAX
29
29
  }
30
30
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -246,27 +246,6 @@ export declare const enum napi_threadsafe_function_release_mode {
246
246
  napi_tsfn_release,
247
247
  napi_tsfn_abort
248
248
  }
249
- export declare class CallbackInfo {
250
- id: number;
251
- parent: CallbackInfo;
252
- child: CallbackInfo | null;
253
- thiz: any;
254
- data: void_p;
255
- args: ArrayLike<any>;
256
- fn: Function;
257
- constructor(id: number, parent: CallbackInfo, child: CallbackInfo | null, thiz: any, data: void_p, args: ArrayLike<any>, fn: Function);
258
- getNewTarget(envObject: Env): number;
259
- dispose(): void;
260
- }
261
-
262
- export declare class CallbackInfoStack {
263
- current: CallbackInfo;
264
- get(id: number): CallbackInfo | null;
265
- pop(): void;
266
- push(thiz: any, data: void_p, args: ArrayLike<any>, fn: Function): number;
267
- dispose(): void;
268
- }
269
-
270
249
  export declare type CleanupHookCallbackFunction = number | ((arg: number) => void);
271
250
 
272
251
  export declare class ConstHandle<S extends undefined | null | boolean | typeof globalThis> extends Handle<S> {
@@ -283,7 +262,6 @@ export declare class Context {
283
262
  refStore: Store<Reference>;
284
263
  deferredStore: Store<Deferred<any>>;
285
264
  handleStore: HandleStore;
286
- cbinfoStack: CallbackInfoStack;
287
265
  private readonly refCounter?;
288
266
  private readonly cleanupQueue;
289
267
  feature: {
@@ -411,6 +389,17 @@ export declare abstract class Env implements IStoreValue {
411
389
  getInstanceData(): number;
412
390
  }
413
391
 
392
+ /** @public */
393
+ declare interface External_2 extends Record<any, any> {
394
+ }
395
+
396
+ /** @public */
397
+ declare const External_2: {
398
+ new (value: number | bigint): External_2;
399
+ prototype: null;
400
+ };
401
+ export { External_2 as External }
402
+
414
403
  export declare class Finalizer {
415
404
  envObject: Env;
416
405
  private _finalizeCallback;
@@ -429,11 +418,14 @@ export declare class Finalizer {
429
418
 
430
419
  export declare function getDefaultContext(): Context;
431
420
 
421
+ /** @public */
422
+ export declare function getExternalValue(external: External_2): number | bigint;
423
+
432
424
  export declare class Handle<S> {
433
425
  id: number;
434
426
  value: S;
435
427
  constructor(id: number, value: S);
436
- data(envObject: Env): void_p;
428
+ data(): void_p;
437
429
  isNumber(): boolean;
438
430
  isBigInt(): boolean;
439
431
  isString(): boolean;
@@ -461,10 +453,11 @@ export declare class HandleScope {
461
453
  child: HandleScope | null;
462
454
  start: number;
463
455
  end: number;
464
- _escapeCalled: boolean;
456
+ private _escapeCalled;
457
+ callbackInfo: ICallbackInfo;
465
458
  constructor(handleStore: HandleStore, id: number, parentScope: HandleScope | null, start: number, end?: number);
466
459
  add<V>(value: V): Handle<V>;
467
- addExternal(envObject: Env, data: void_p): Handle<object>;
460
+ addExternal(data: void_p): Handle<object>;
468
461
  dispose(): void;
469
462
  escape(handle: number): Handle<any> | null;
470
463
  escapeCalled(): boolean;
@@ -486,6 +479,13 @@ export declare class HandleStore {
486
479
  dispose(): void;
487
480
  }
488
481
 
482
+ export declare interface ICallbackInfo {
483
+ thiz: any;
484
+ data: void_p;
485
+ args: ArrayLike<any>;
486
+ fn: Function;
487
+ }
488
+
489
489
  export declare interface IDeferrdValue<T = any> {
490
490
  resolve: (value: T) => void;
491
491
  reject: (reason?: any) => void;
@@ -494,9 +494,11 @@ export declare interface IDeferrdValue<T = any> {
494
494
  export declare interface IReferenceBinding {
495
495
  wrapped: number;
496
496
  tag: Uint32Array | null;
497
- data: void_p;
498
497
  }
499
498
 
499
+ /** @public */
500
+ export declare function isExternal(object: unknown): object is External_2;
501
+
500
502
  export declare function isReferenceType(v: any): v is object;
501
503
 
502
504
  export declare interface IStoreValue {
@@ -616,6 +618,7 @@ export declare class RefTracker {
616
618
  export declare class ScopeStore {
617
619
  private readonly _rootScope;
618
620
  currentScope: HandleScope;
621
+ private readonly _values;
619
622
  constructor();
620
623
  get(id: number): HandleScope | undefined;
621
624
  openScope(envObject: Env): HandleScope;
@@ -1 +1 @@
1
- const e=[];class t{constructor(e,t,i,s,n,r,o){this.id=e,this.parent=t,this.child=i,this.thiz=s,this.data=n,this.args=r,this.fn=o}getNewTarget(e){const t=this.thiz;return null==t||null==t.constructor?0:t instanceof this.fn?e.ensureHandleId(t.constructor):0}dispose(){void 0!==this.thiz&&(this.thiz=void 0),this.args=e,this.fn=null}}const i=new t(0,null,null,null,0,null,null);class s{constructor(){this.current=i}get(e){if(1===e)return i.child;let t=i;for(let i=0;i<e;++i)if(t=t.child,null===t)return null;return t===i?null:t}pop(){const e=this.current;e!==i&&(this.current=e.parent,e.dispose())}push(e,i,s,n){let r=this.current.child;return r?(r.thiz=e,r.data=i,r.args=s,r.fn=n):(r=new t(this.current.id+1,this.current,null,e,i,s,n),this.current.child=r),this.current=r,r.id}dispose(){this.current=null}}const n=function(){let e;try{e=new Function}catch(e){return!1}return"function"==typeof e}(),r=function(){if("undefined"!=typeof globalThis)return globalThis;let e=function(){return this}();if(!e&&n)try{e=new Function("return this")()}catch(e){}if(!e){if("undefined"==typeof __webpack_public_path__&&"undefined"!=typeof global)return global;if("undefined"!=typeof window)return window;if("undefined"!=typeof self)return self}return e}();class o{constructor(){this._exception=void 0,this._caught=!1}isEmpty(){return!this._caught}hasCaught(){return this._caught}exception(){return this._exception}setError(e){this._caught=!0,this._exception=e}reset(){this._caught=!1,this._exception=void 0}extractException(){const e=this._exception;return this.reset(),e}}const a=function(){var e;try{return Boolean(null===(e=Object.getOwnPropertyDescriptor(Function.prototype,"name"))||void 0===e?void 0:e.configurable)}catch(e){return!1}}(),l="object"==typeof Reflect,c="undefined"!=typeof FinalizationRegistry&&"undefined"!=typeof WeakRef,h=function(){try{const e=Symbol();new WeakRef(e),(new WeakMap).set(e,void 0)}catch(e){return!1}return!0}(),u="undefined"!=typeof BigInt;function p(e){return"object"==typeof e&&null!==e||"function"==typeof e}const d=function(){let e;return e="undefined"!=typeof __webpack_public_path__||"undefined"!=typeof __webpack_public_path__?"undefined"!=typeof __non_webpack_require__?__non_webpack_require__:void 0:"undefined"!=typeof require?require:void 0,e}(),f="function"==typeof MessageChannel?MessageChannel:function(){try{return d("worker_threads").MessageChannel}catch(e){}}(),_="function"==typeof setImmediate?setImmediate:function(e){if("function"!=typeof e)throw new TypeError('The "callback" argument must be of type function');if(f){let t=new f;t.port1.onmessage=function(){t.port1.onmessage=null,t=void 0,e()},t.port2.postMessage(null)}else setTimeout(e,0)},v="function"==typeof Buffer?Buffer:function(){try{return d("buffer").Buffer}catch(e){}}(),g="1.3.0",y=2147483647;class x{constructor(e,t){this.id=e,this.value=t}data(e){return e.getObjectBinding(this.value).data}isNumber(){return"number"==typeof this.value}isBigInt(){return"bigint"==typeof this.value}isString(){return"string"==typeof this.value}isFunction(){return"function"==typeof this.value}isExternal(){return p(this.value)&&null===Object.getPrototypeOf(this.value)}isObject(){return"object"==typeof this.value&&null!==this.value}isArray(){return Array.isArray(this.value)}isArrayBuffer(){return this.value instanceof ArrayBuffer}isTypedArray(){return ArrayBuffer.isView(this.value)&&!(this.value instanceof DataView)}isBuffer(e){return null!=e||(e=v),"function"==typeof e&&e.isBuffer(this.value)}isDataView(){return this.value instanceof DataView}isDate(){return this.value instanceof Date}isPromise(){return this.value instanceof Promise}isBoolean(){return"boolean"==typeof this.value}isUndefined(){return void 0===this.value}isSymbol(){return"symbol"==typeof this.value}isNull(){return null===this.value}dispose(){this.value=void 0}}class z extends x{constructor(e,t){super(e,t)}dispose(){}}function b(){Object.setPrototypeOf(this,null)}b.prototype=null;class k{constructor(){this._values=[void 0,k.UNDEFINED,k.NULL,k.FALSE,k.TRUE,k.GLOBAL],this._next=k.MIN_ID}push(e){let t;const i=this._next,s=this._values;return i<s.length?(t=s[i],t.value=e):(t=new x(i,e),s[i]=t),this._next++,t}erase(e,t){this._next=e;const i=this._values;for(let s=e;s<t;++s)i[s].dispose()}get(e){return this._values[e]}swap(e,t){const i=this._values,s=i[e];i[e]=i[t],i[e].id=Number(e),i[t]=s,s.id=Number(t)}dispose(){this._values.length=k.MIN_ID,this._next=k.MIN_ID}}k.UNDEFINED=new z(1,void 0),k.NULL=new z(2,null),k.FALSE=new z(3,!1),k.TRUE=new z(4,!0),k.GLOBAL=new z(5,r),k.MIN_ID=6;class S{constructor(e,t,i,s,n=s){this.handleStore=e,this.id=t,this.parent=i,this.child=null,null!==i&&(i.child=this),this.start=s,this.end=n,this._escapeCalled=!1}add(e){const t=this.handleStore.push(e);return this.end++,t}addExternal(e,t){const i=new b,s=e.ctx.handleStore.push(i);return e.initObjectBinding(i).data=t,this.end++,s}dispose(){this.start!==this.end&&this.handleStore.erase(this.start,this.end)}escape(e){if(this._escapeCalled)return null;if(this._escapeCalled=!0,e<this.start||e>=this.end)return null;this.handleStore.swap(e,this.start);const t=this.handleStore.get(this.start);return this.start++,this.parent.end++,t}escapeCalled(){return this._escapeCalled}}class w{constructor(){this._rootScope=new S(null,0,null,1,k.MIN_ID),this.currentScope=this._rootScope}get(e){e=Number(e);let t=this.currentScope;for(;t!==this._rootScope;){if(t.id===e)return t;t=t.parent}}openScope(e){const t=this.currentScope;let i=t.child;return null!==i?(i.start=i.end=t.end,i._escapeCalled=!1):i=new S(e.ctx.handleStore,t.id+1,t,t.end),this.currentScope=i,e.openHandleScopes++,i}closeScope(e){if(0===e.openHandleScopes)return;const t=this.currentScope;this.currentScope=t.parent,t.dispose(),e.openHandleScopes--}dispose(){let e=this.currentScope;for(;null!==e;){e.handleStore=null,e.id=0,e.parent=null,e.start=k.MIN_ID,e.end=k.MIN_ID,e._escapeCalled=!1;const t=e.child;e.child=null,e=t}this.currentScope=null}}class m{constructor(){this._next=null,this._prev=null}dispose(){}finalize(){}link(e){this._prev=e,this._next=e._next,null!==this._next&&(this._next._prev=this),e._next=this}unlink(){null!==this._prev&&(this._prev._next=this._next),null!==this._next&&(this._next._prev=this._prev),this._prev=null,this._next=null}static finalizeAll(e){for(;null!==e._next;)e._next.finalize()}}class C{constructor(e,t=0,i=0,s=0){this.envObject=e,this._finalizeCallback=t,this._finalizeData=i,this._finalizeHint=s,this._makeDynCall_vppp=e.makeDynCall_vppp}callback(){return this._finalizeCallback}data(){return this._finalizeData}hint(){return this._finalizeHint}resetEnv(){this.envObject=void 0}resetFinalizer(){this._finalizeCallback=0,this._finalizeData=0,this._finalizeHint=0}callFinalizer(){const e=this._finalizeCallback,t=this._finalizeData,i=this._finalizeHint;if(this.resetFinalizer(),!e)return;const s=Number(e);this.envObject?this.envObject.callFinalizer(s,t,i):this._makeDynCall_vppp(s)(0,t,i)}dispose(){this.envObject=void 0,this._makeDynCall_vppp=void 0}}class E extends m{static create(e,t,i,s){const n=new E(e,t,i,s);return n.link(e.finalizing_reflist),n}constructor(e,t,i,s){super(),this._finalizer=new C(e,t,i,s)}data(){return this._finalizer.data()}dispose(){this._finalizer&&(this.unlink(),this._finalizer.envObject.dequeueFinalizer(this),this._finalizer.dispose(),this._finalizer=void 0,super.dispose())}finalize(){let e;this.unlink();let t=!1;try{this._finalizer.callFinalizer()}catch(i){t=!0,e=i}if(this.dispose(),t)throw e}}function F(e,t){if(!e.terminatedOrTerminating())throw t}class I{constructor(e,t,i,s,n){this.ctx=e,this.moduleApiVersion=t,this.makeDynCall_vppp=i,this.makeDynCall_vp=s,this.abort=n,this.openHandleScopes=0,this.instanceData=null,this.tryCatch=new o,this.refs=1,this.reflist=new m,this.finalizing_reflist=new m,this.pendingFinalizers=[],this.lastError={errorCode:0,engineErrorCode:0,engineReserved:0},this.inGcFinalizer=!1,this._bindingMap=new WeakMap,this.id=0}canCallIntoJs(){return!0}terminatedOrTerminating(){return!this.canCallIntoJs()}ref(){this.refs++}unref(){this.refs--,0===this.refs&&this.dispose()}ensureHandle(e){return this.ctx.ensureHandle(e)}ensureHandleId(e){return this.ensureHandle(e).id}clearLastError(){const e=this.lastError;return 0!==e.errorCode&&(e.errorCode=0),0!==e.engineErrorCode&&(e.engineErrorCode=0),0!==e.engineReserved&&(e.engineReserved=0),0}setLastError(e,t=0,i=0){const s=this.lastError;return s.errorCode!==e&&(s.errorCode=e),s.engineErrorCode!==t&&(s.engineErrorCode=t),s.engineReserved!==i&&(s.engineReserved=i),e}getReturnStatus(){return this.tryCatch.hasCaught()?this.setLastError(10):0}callIntoModule(e,t=F){const i=this.openHandleScopes;this.clearLastError();const s=e(this);if(i!==this.openHandleScopes&&this.abort("open_handle_scopes != open_handle_scopes_before"),this.tryCatch.hasCaught()){t(this,this.tryCatch.extractException())}return s}invokeFinalizerFromGC(e){if(this.moduleApiVersion!==y)this.enqueueFinalizer(e);else{const t=this.inGcFinalizer;this.inGcFinalizer=!0;try{e.finalize()}finally{this.inGcFinalizer=t}}}checkGCAccess(){this.moduleApiVersion===y&&this.inGcFinalizer&&this.abort("Finalizer is calling a function that may affect GC state.\nThe finalizers are run directly from GC and must not affect GC state.\nUse `node_api_post_finalizer` from inside of the finalizer to work around this issue.\nIt schedules the call as a new task in the event loop.")}enqueueFinalizer(e){-1===this.pendingFinalizers.indexOf(e)&&this.pendingFinalizers.push(e)}dequeueFinalizer(e){const t=this.pendingFinalizers.indexOf(e);-1!==t&&this.pendingFinalizers.splice(t,1)}deleteMe(){m.finalizeAll(this.finalizing_reflist),m.finalizeAll(this.reflist),this.tryCatch.extractException(),this.ctx.envStore.remove(this.id)}dispose(){0!==this.id&&(this.deleteMe(),this.finalizing_reflist.dispose(),this.reflist.dispose(),this.id=0)}initObjectBinding(e){const t={wrapped:0,tag:null,data:0};return this._bindingMap.set(e,t),t}getObjectBinding(e){return this._bindingMap.has(e)?this._bindingMap.get(e):this.initObjectBinding(e)}setInstanceData(e,t,i){this.instanceData&&this.instanceData.dispose(),this.instanceData=E.create(this,t,e,i)}getInstanceData(){return this.instanceData?this.instanceData.data():0}}class O extends I{constructor(e,t,i,s,n,r,o){super(e,i,s,n,r),this.filename=t,this.nodeBinding=o,this.destructing=!1,this.finalizationScheduled=!1}deleteMe(){this.destructing=!0,this.drainFinalizerQueue(),super.deleteMe()}canCallIntoJs(){return super.canCallIntoJs()&&this.ctx.canCallIntoJs()}triggerFatalException(e){if(this.nodeBinding)this.nodeBinding.napi.fatalException(e);else{if("object"!=typeof process||null===process||"function"!=typeof process._fatalException)throw e;process._fatalException(e)||(console.error(e),process.exit(1))}}callbackIntoModule(e,t){return this.callIntoModule(t,((t,i)=>{if(t.terminatedOrTerminating())return;const s="object"==typeof process&&null!==process,n=!!s&&Boolean(process.execArgv&&-1!==process.execArgv.indexOf("--force-node-api-uncaught-exceptions-policy"));if(t.moduleApiVersion<y&&!n&&!e){(s&&"function"==typeof process.emitWarning?process.emitWarning:function(e,t,i){if(e instanceof Error)console.warn(e.toString());else{const s=i?`[${i}] `:"";console.warn(`${s}${t||"Warning"}: ${e}`)}})("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")}else t.triggerFatalException(i)}))}callFinalizer(e,t,i){this.callFinalizerInternal(1,e,t,i)}callFinalizerInternal(e,t,i,s){const n=this.makeDynCall_vppp(t),r=this.id,o=this.ctx.openScope(this);try{this.callbackIntoModule(Boolean(e),(()=>{n(r,i,s)}))}finally{this.ctx.closeScope(this,o)}}enqueueFinalizer(e){super.enqueueFinalizer(e),this.finalizationScheduled||this.destructing||(this.finalizationScheduled=!0,this.ref(),_((()=>{this.finalizationScheduled=!1,this.unref(),this.drainFinalizerQueue()})))}drainFinalizerQueue(){for(;this.pendingFinalizers.length>0;){this.pendingFinalizers.shift().finalize()}}}function D(e,t,i,s,n,r,o){(i="number"!=typeof i?8:i)<8?i=8:i>9&&i!==y&&function(e,t){throw new Error(`${e} requires Node-API version ${t}, but this version of Node.js only supports version 9 add-ons.`)}(t,i);const a=new O(e,t,i,s,n,r,o);return e.envStore.add(a),e.addCleanupHook(a,(()=>{a.unref()}),0),a}class N extends Error{constructor(e){super(e);const t=new.target,i=t.prototype;if(!(this instanceof N)){const e=Object.setPrototypeOf;"function"==typeof e?e.call(Object,this,i):this.__proto__=i,"function"==typeof Error.captureStackTrace&&Error.captureStackTrace(this,t)}}}Object.defineProperty(N.prototype,"name",{configurable:!0,writable:!0,value:"EmnapiError"});class R extends N{constructor(e,t){super(`${e}: The current runtime does not support "FinalizationRegistry" and "WeakRef".${t?` ${t}`:""}`)}}Object.defineProperty(R.prototype,"name",{configurable:!0,writable:!0,value:"NotSupportWeakRefError"});class H extends N{constructor(e,t){super(`${e}: The current runtime does not support "Buffer". Consider using buffer polyfill to make sure \`globalThis.Buffer\` is defined.${t?` ${t}`:""}`)}}Object.defineProperty(H.prototype,"name",{configurable:!0,writable:!0,value:"NotSupportBufferError"});class j{constructor(e){this._value=e}deref(){return this._value}dispose(){this._value=void 0}}class A{constructor(e){this._ref=new j(e)}setWeak(e,t){if(!c||void 0===this._ref||this._ref instanceof WeakRef)return;const i=this._ref.deref();try{A._registry.register(i,this,this);const s=new WeakRef(i);this._ref.dispose(),this._ref=s,this._param=e,this._callback=t}catch(e){if("symbol"!=typeof i)throw e}}clearWeak(){if(c&&void 0!==this._ref&&this._ref instanceof WeakRef){try{A._registry.unregister(this)}catch(e){}this._param=void 0,this._callback=void 0;const e=this._ref.deref();this._ref=void 0===e?e:new j(e)}}reset(){if(c)try{A._registry.unregister(this)}catch(e){}this._param=void 0,this._callback=void 0,this._ref instanceof j&&this._ref.dispose(),this._ref=void 0}isEmpty(){return void 0===this._ref}deref(){if(void 0!==this._ref)return this._ref.deref()}}var B;A._registry=c?new FinalizationRegistry((e=>{e._ref=void 0;const t=e._callback,i=e._param;e._callback=void 0,e._param=void 0,"function"==typeof t&&t(i)})):void 0,exports.ReferenceOwnership=void 0,(B=exports.ReferenceOwnership||(exports.ReferenceOwnership={}))[B.kRuntime=0]="kRuntime",B[B.kUserland=1]="kUserland";class M extends m{static weakCallback(e){e.persistent.reset(),e.invokeFinalizerFromGC()}static create(e,t,i,s,n,r,o){const a=new M(e,t,i,s);return e.ctx.refStore.add(a),a.link(e.reflist),a}constructor(e,t,i,s){super(),this.envObject=e,this._refcount=i,this._ownership=s;const n=e.ctx.handleStore.get(t);var r;this.canBeWeak=(r=n).isObject()||r.isFunction()||r.isSymbol(),this.persistent=new A(n.value),this.id=0,0===i&&this._setWeak()}ref(){return this.persistent.isEmpty()?0:(1==++this._refcount&&this.canBeWeak&&this.persistent.clearWeak(),this._refcount)}unref(){return this.persistent.isEmpty()||0===this._refcount?0:(0==--this._refcount&&this._setWeak(),this._refcount)}get(e=this.envObject){if(this.persistent.isEmpty())return 0;const t=this.persistent.deref();return e.ensureHandle(t).id}resetFinalizer(){}data(){return 0}refcount(){return this._refcount}ownership(){return this._ownership}callUserFinalizer(){}invokeFinalizerFromGC(){this.finalize()}_setWeak(){this.canBeWeak?this.persistent.setWeak(this,M.weakCallback):this.persistent.reset()}finalize(){this.persistent.reset();const e=this._ownership===exports.ReferenceOwnership.kRuntime;this.unlink(),this.callUserFinalizer(),e&&this.dispose()}dispose(){0!==this.id&&(this.unlink(),this.persistent.reset(),this.envObject.ctx.refStore.remove(this.id),super.dispose(),this.envObject=void 0,this.id=0)}}class T extends M{static create(e,t,i,s,n){const r=new T(e,t,i,s,n);return e.ctx.refStore.add(r),r.link(e.reflist),r}constructor(e,t,i,s,n){super(e,t,i,s),this._data=n}data(){return this._data}}class W extends M{static create(e,t,i,s,n,r,o){const a=new W(e,t,i,s,n,r,o);return e.ctx.refStore.add(a),a.link(e.finalizing_reflist),a}constructor(e,t,i,s,n,r,o){super(e,t,i,s),this._finalizer=new C(e,n,r,o)}resetFinalizer(){this._finalizer.resetFinalizer()}data(){return this._finalizer.data()}callUserFinalizer(){this._finalizer.callFinalizer()}invokeFinalizerFromGC(){this._finalizer.envObject.invokeFinalizerFromGC(this)}dispose(){this._finalizer&&(this._finalizer.envObject.dequeueFinalizer(this),this._finalizer.dispose(),super.dispose(),this._finalizer=void 0)}}class L{static create(e,t){const i=new L(e,t);return e.deferredStore.add(i),i}constructor(e,t){this.id=0,this.ctx=e,this.value=t}resolve(e){this.value.resolve(e),this.dispose()}reject(e){this.value.reject(e),this.dispose()}dispose(){this.ctx.deferredStore.remove(this.id),this.id=0,this.value=null,this.ctx=null}}class P{constructor(){this._values=[void 0],this._values.length=4,this._size=1,this._freeList=[]}add(e){let t;if(this._freeList.length)t=this._freeList.shift();else{t=this._size,this._size++;const e=this._values.length;t>=e&&(this._values.length=e+(e>>1)+16)}e.id=t,this._values[t]=e}get(e){return this._values[e]}has(e){return void 0!==this._values[e]}remove(e){const t=this._values[e];t&&(t.id=0,this._values[e]=void 0,this._freeList.push(Number(e)))}dispose(){for(let e=1;e<this._size;++e){const t=this._values[e];null==t||t.dispose()}this._values=[void 0],this._size=1,this._freeList=[]}}class U{constructor(e,t,i,s){this.envObject=e,this.fn=t,this.arg=i,this.order=s}}class G{constructor(){this._cleanupHooks=[],this._cleanupHookCounter=0}empty(){return 0===this._cleanupHooks.length}add(e,t,i){if(this._cleanupHooks.filter((s=>s.envObject===e&&s.fn===t&&s.arg===i)).length>0)throw new Error("Can not add same fn and arg twice");this._cleanupHooks.push(new U(e,t,i,this._cleanupHookCounter++))}remove(e,t,i){for(let s=0;s<this._cleanupHooks.length;++s){const n=this._cleanupHooks[s];if(n.envObject===e&&n.fn===t&&n.arg===i)return void this._cleanupHooks.splice(s,1)}}drain(){const e=this._cleanupHooks.slice();e.sort(((e,t)=>t.order-e.order));for(let t=0;t<e.length;++t){const i=e[t];"number"==typeof i.fn?i.envObject.makeDynCall_vp(i.fn)(i.arg):i.fn(i.arg),this._cleanupHooks.splice(this._cleanupHooks.indexOf(i),1)}}dispose(){this._cleanupHooks.length=0,this._cleanupHookCounter=0}}class V{constructor(){this.refHandle=(new f).port1,this.count=0}increase(){0===this.count&&this.refHandle.ref(),this.count++}decrease(){0!==this.count&&(1===this.count&&this.refHandle.unref(),this.count--)}}class q{constructor(){this._isStopping=!1,this._canCallIntoJs=!0,this._suppressDestroy=!1,this.envStore=new P,this.scopeStore=new w,this.refStore=new P,this.deferredStore=new P,this.handleStore=new k,this.cbinfoStack=new s,this.feature={supportReflect:l,supportFinalizer:c,supportWeakSymbol:h,supportBigInt:u,supportNewFunction:n,canSetFunctionName:a,setImmediate:_,Buffer:v,MessageChannel:f},this.cleanupQueue=new G,"object"==typeof process&&null!==process&&"function"==typeof process.once&&(this.refCounter=new V,process.once("beforeExit",(()=>{this._suppressDestroy||this.destroy()})))}suppressDestroy(){this._suppressDestroy=!0}getRuntimeVersions(){return{version:g,NODE_API_SUPPORTED_VERSION_MAX:9,NAPI_VERSION_EXPERIMENTAL:y,NODE_API_DEFAULT_MODULE_API_VERSION:8}}createNotSupportWeakRefError(e,t){return new R(e,t)}createNotSupportBufferError(e,t){return new H(e,t)}createReference(e,t,i,s){return M.create(e,t,i,s)}createReferenceWithData(e,t,i,s,n){return T.create(e,t,i,s,n)}createReferenceWithFinalizer(e,t,i,s,n=0,r=0,o=0){return W.create(e,t,i,s,n,r,o)}createDeferred(e){return L.create(this,e)}createEnv(e,t,i,s,n,r){return D(this,e,t,i,s,n,r)}createTrackedFinalizer(e,t,i,s){return E.create(e,t,i,s)}getCurrentScope(){return this.scopeStore.currentScope}addToCurrentScope(e){return this.scopeStore.currentScope.add(e)}openScope(e){return this.scopeStore.openScope(e)}closeScope(e,t){this.scopeStore.closeScope(e)}ensureHandle(e){switch(e){case void 0:return k.UNDEFINED;case null:return k.NULL;case!0:return k.TRUE;case!1:return k.FALSE;case r:return k.GLOBAL}return this.addToCurrentScope(e)}addCleanupHook(e,t,i){this.cleanupQueue.add(e,t,i)}removeCleanupHook(e,t,i){this.cleanupQueue.remove(e,t,i)}runCleanup(){for(;!this.cleanupQueue.empty();)this.cleanupQueue.drain()}increaseWaitingRequestCounter(){var e;null===(e=this.refCounter)||void 0===e||e.increase()}decreaseWaitingRequestCounter(){var e;null===(e=this.refCounter)||void 0===e||e.decrease()}setCanCallIntoJs(e){this._canCallIntoJs=e}setStopping(e){this._isStopping=e}canCallIntoJs(){return this._canCallIntoJs&&!this._isStopping}destroy(){this.setStopping(!0),this.setCanCallIntoJs(!1),this.runCleanup()}}let $;function J(){return new q}exports.CallbackInfo=t,exports.CallbackInfoStack=s,exports.ConstHandle=z,exports.Context=q,exports.Deferred=L,exports.EmnapiError=N,exports.Env=I,exports.Finalizer=C,exports.Handle=x,exports.HandleScope=S,exports.HandleStore=k,exports.NAPI_VERSION_EXPERIMENTAL=y,exports.NODE_API_DEFAULT_MODULE_API_VERSION=8,exports.NODE_API_SUPPORTED_VERSION_MAX=9,exports.NODE_API_SUPPORTED_VERSION_MIN=1,exports.NodeEnv=O,exports.NotSupportBufferError=H,exports.NotSupportWeakRefError=R,exports.Persistent=A,exports.RefTracker=m,exports.Reference=M,exports.ReferenceWithData=T,exports.ReferenceWithFinalizer=W,exports.ScopeStore=w,exports.Store=P,exports.TrackedFinalizer=E,exports.TryCatch=o,exports.createContext=J,exports.getDefaultContext=function(){return $||($=J()),$},exports.isReferenceType=p,exports.version=g;
1
+ const e=new WeakMap;function t(t){return e.has(t)}const i=(()=>{function t(t){Object.setPrototypeOf(this,null),e.set(this,t)}return t.prototype=null,t})();function s(i){if(!t(i))throw new TypeError("not external");return e.get(i)}const n=function(){let e;try{e=new Function}catch(e){return!1}return"function"==typeof e}(),r=function(){if("undefined"!=typeof globalThis)return globalThis;let e=function(){return this}();if(!e&&n)try{e=new Function("return this")()}catch(e){}if(!e){if("undefined"==typeof __webpack_public_path__&&"undefined"!=typeof global)return global;if("undefined"!=typeof window)return window;if("undefined"!=typeof self)return self}return e}();class o{constructor(){this._exception=void 0,this._caught=!1}isEmpty(){return!this._caught}hasCaught(){return this._caught}exception(){return this._exception}setError(e){this._caught=!0,this._exception=e}reset(){this._caught=!1,this._exception=void 0}extractException(){const e=this._exception;return this.reset(),e}}const a=function(){var e;try{return Boolean(null===(e=Object.getOwnPropertyDescriptor(Function.prototype,"name"))||void 0===e?void 0:e.configurable)}catch(e){return!1}}(),l="object"==typeof Reflect,c="undefined"!=typeof FinalizationRegistry&&"undefined"!=typeof WeakRef,h=function(){try{const e=Symbol();new WeakRef(e),(new WeakMap).set(e,void 0)}catch(e){return!1}return!0}(),u="undefined"!=typeof BigInt;const p=function(){let e;return e="undefined"!=typeof __webpack_public_path__||"undefined"!=typeof __webpack_public_path__?"undefined"!=typeof __non_webpack_require__?__non_webpack_require__:void 0:"undefined"!=typeof require?require:void 0,e}(),f="function"==typeof MessageChannel?MessageChannel:function(){try{return p("worker_threads").MessageChannel}catch(e){}}(),d="function"==typeof setImmediate?setImmediate:function(e){if("function"!=typeof e)throw new TypeError('The "callback" argument must be of type function');if(f){let t=new f;t.port1.onmessage=function(){t.port1.onmessage=null,t=void 0,e()},t.port2.postMessage(null)}else setTimeout(e,0)},_="function"==typeof Buffer?Buffer:function(){try{return p("buffer").Buffer}catch(e){}}(),v="1.4.0",g=2147483647;class y{constructor(e,t){this.id=e,this.value=t}data(){return s(this.value)}isNumber(){return"number"==typeof this.value}isBigInt(){return"bigint"==typeof this.value}isString(){return"string"==typeof this.value}isFunction(){return"function"==typeof this.value}isExternal(){return t(this.value)}isObject(){return"object"==typeof this.value&&null!==this.value}isArray(){return Array.isArray(this.value)}isArrayBuffer(){return this.value instanceof ArrayBuffer}isTypedArray(){return ArrayBuffer.isView(this.value)&&!(this.value instanceof DataView)}isBuffer(e){return!!ArrayBuffer.isView(this.value)||(null!=e||(e=_),"function"==typeof e&&e.isBuffer(this.value))}isDataView(){return this.value instanceof DataView}isDate(){return this.value instanceof Date}isPromise(){return this.value instanceof Promise}isBoolean(){return"boolean"==typeof this.value}isUndefined(){return void 0===this.value}isSymbol(){return"symbol"==typeof this.value}isNull(){return null===this.value}dispose(){this.value=void 0}}class x extends y{constructor(e,t){super(e,t)}dispose(){}}class z{constructor(){this._values=[void 0,z.UNDEFINED,z.NULL,z.FALSE,z.TRUE,z.GLOBAL],this._next=z.MIN_ID}push(e){let t;const i=this._next,s=this._values;return i<s.length?(t=s[i],t.value=e):(t=new y(i,e),s[i]=t),this._next++,t}erase(e,t){this._next=e;const i=this._values;for(let s=e;s<t;++s)i[s].dispose()}get(e){return this._values[e]}swap(e,t){const i=this._values,s=i[e];i[e]=i[t],i[e].id=Number(e),i[t]=s,s.id=Number(t)}dispose(){this._values.length=z.MIN_ID,this._next=z.MIN_ID}}z.UNDEFINED=new x(1,void 0),z.NULL=new x(2,null),z.FALSE=new x(3,!1),z.TRUE=new x(4,!0),z.GLOBAL=new x(5,r),z.MIN_ID=6;class k{constructor(e,t,i,s,n=s){this.handleStore=e,this.id=t,this.parent=i,this.child=null,null!==i&&(i.child=this),this.start=s,this.end=n,this._escapeCalled=!1,this.callbackInfo={thiz:void 0,data:0,args:void 0,fn:void 0}}add(e){const t=this.handleStore.push(e);return this.end++,t}addExternal(e){return this.add(new i(e))}dispose(){this._escapeCalled&&(this._escapeCalled=!1),this.start!==this.end&&this.handleStore.erase(this.start,this.end)}escape(e){if(this._escapeCalled)return null;if(this._escapeCalled=!0,e<this.start||e>=this.end)return null;this.handleStore.swap(e,this.start);const t=this.handleStore.get(this.start);return this.start++,this.parent.end++,t}escapeCalled(){return this._escapeCalled}}class b{constructor(){this._rootScope=new k(null,0,null,1,z.MIN_ID),this.currentScope=this._rootScope,this._values=[void 0]}get(e){return this._values[e]}openScope(e){const t=this.currentScope;let i=t.child;if(null!==i)i.start=i.end=t.end;else{const s=t.id+1;i=new k(e.ctx.handleStore,s,t,t.end),this._values[s]=i}return this.currentScope=i,e.openHandleScopes++,i}closeScope(e){if(0===e.openHandleScopes)return;const t=this.currentScope;this.currentScope=t.parent,t.dispose(),e.openHandleScopes--}dispose(){this.currentScope=this._rootScope,this._values.length=1}}class w{constructor(){this._next=null,this._prev=null}dispose(){}finalize(){}link(e){this._prev=e,this._next=e._next,null!==this._next&&(this._next._prev=this),e._next=this}unlink(){null!==this._prev&&(this._prev._next=this._next),null!==this._next&&(this._next._prev=this._prev),this._prev=null,this._next=null}static finalizeAll(e){for(;null!==e._next;)e._next.finalize()}}class S{constructor(e,t=0,i=0,s=0){this.envObject=e,this._finalizeCallback=t,this._finalizeData=i,this._finalizeHint=s,this._makeDynCall_vppp=e.makeDynCall_vppp}callback(){return this._finalizeCallback}data(){return this._finalizeData}hint(){return this._finalizeHint}resetEnv(){this.envObject=void 0}resetFinalizer(){this._finalizeCallback=0,this._finalizeData=0,this._finalizeHint=0}callFinalizer(){const e=this._finalizeCallback,t=this._finalizeData,i=this._finalizeHint;if(this.resetFinalizer(),!e)return;const s=Number(e);this.envObject?this.envObject.callFinalizer(s,t,i):this._makeDynCall_vppp(s)(0,t,i)}dispose(){this.envObject=void 0,this._makeDynCall_vppp=void 0}}class E extends w{static create(e,t,i,s){const n=new E(e,t,i,s);return n.link(e.finalizing_reflist),n}constructor(e,t,i,s){super(),this._finalizer=new S(e,t,i,s)}data(){return this._finalizer.data()}dispose(){this._finalizer&&(this.unlink(),this._finalizer.envObject.dequeueFinalizer(this),this._finalizer.dispose(),this._finalizer=void 0,super.dispose())}finalize(){let e;this.unlink();let t=!1;try{this._finalizer.callFinalizer()}catch(i){t=!0,e=i}if(this.dispose(),t)throw e}}function m(e,t){if(!e.terminatedOrTerminating())throw t}class C{constructor(e,t,i,s,n){this.ctx=e,this.moduleApiVersion=t,this.makeDynCall_vppp=i,this.makeDynCall_vp=s,this.abort=n,this.openHandleScopes=0,this.instanceData=null,this.tryCatch=new o,this.refs=1,this.reflist=new w,this.finalizing_reflist=new w,this.pendingFinalizers=[],this.lastError={errorCode:0,engineErrorCode:0,engineReserved:0},this.inGcFinalizer=!1,this._bindingMap=new WeakMap,this.id=0}canCallIntoJs(){return!0}terminatedOrTerminating(){return!this.canCallIntoJs()}ref(){this.refs++}unref(){this.refs--,0===this.refs&&this.dispose()}ensureHandle(e){return this.ctx.ensureHandle(e)}ensureHandleId(e){return this.ensureHandle(e).id}clearLastError(){const e=this.lastError;return 0!==e.errorCode&&(e.errorCode=0),0!==e.engineErrorCode&&(e.engineErrorCode=0),0!==e.engineReserved&&(e.engineReserved=0),0}setLastError(e,t=0,i=0){const s=this.lastError;return s.errorCode!==e&&(s.errorCode=e),s.engineErrorCode!==t&&(s.engineErrorCode=t),s.engineReserved!==i&&(s.engineReserved=i),e}getReturnStatus(){return this.tryCatch.hasCaught()?this.setLastError(10):0}callIntoModule(e,t=m){const i=this.openHandleScopes;this.clearLastError();const s=e(this);if(i!==this.openHandleScopes&&this.abort("open_handle_scopes != open_handle_scopes_before"),this.tryCatch.hasCaught()){t(this,this.tryCatch.extractException())}return s}invokeFinalizerFromGC(e){if(this.moduleApiVersion!==g)this.enqueueFinalizer(e);else{const t=this.inGcFinalizer;this.inGcFinalizer=!0;try{e.finalize()}finally{this.inGcFinalizer=t}}}checkGCAccess(){this.moduleApiVersion===g&&this.inGcFinalizer&&this.abort("Finalizer is calling a function that may affect GC state.\nThe finalizers are run directly from GC and must not affect GC state.\nUse `node_api_post_finalizer` from inside of the finalizer to work around this issue.\nIt schedules the call as a new task in the event loop.")}enqueueFinalizer(e){-1===this.pendingFinalizers.indexOf(e)&&this.pendingFinalizers.push(e)}dequeueFinalizer(e){const t=this.pendingFinalizers.indexOf(e);-1!==t&&this.pendingFinalizers.splice(t,1)}deleteMe(){w.finalizeAll(this.finalizing_reflist),w.finalizeAll(this.reflist),this.tryCatch.extractException(),this.ctx.envStore.remove(this.id)}dispose(){0!==this.id&&(this.deleteMe(),this.finalizing_reflist.dispose(),this.reflist.dispose(),this.id=0)}initObjectBinding(e){const t={wrapped:0,tag:null};return this._bindingMap.set(e,t),t}getObjectBinding(e){return this._bindingMap.has(e)?this._bindingMap.get(e):this.initObjectBinding(e)}setInstanceData(e,t,i){this.instanceData&&this.instanceData.dispose(),this.instanceData=E.create(this,t,e,i)}getInstanceData(){return this.instanceData?this.instanceData.data():0}}class F extends C{constructor(e,t,i,s,n,r,o){super(e,i,s,n,r),this.filename=t,this.nodeBinding=o,this.destructing=!1,this.finalizationScheduled=!1}deleteMe(){this.destructing=!0,this.drainFinalizerQueue(),super.deleteMe()}canCallIntoJs(){return super.canCallIntoJs()&&this.ctx.canCallIntoJs()}triggerFatalException(e){if(this.nodeBinding)this.nodeBinding.napi.fatalException(e);else{if("object"!=typeof process||null===process||"function"!=typeof process._fatalException)throw e;process._fatalException(e)||(console.error(e),process.exit(1))}}callbackIntoModule(e,t){return this.callIntoModule(t,((t,i)=>{if(t.terminatedOrTerminating())return;const s="object"==typeof process&&null!==process,n=!!s&&Boolean(process.execArgv&&-1!==process.execArgv.indexOf("--force-node-api-uncaught-exceptions-policy"));if(t.moduleApiVersion<10&&!n&&!e){(s&&"function"==typeof process.emitWarning?process.emitWarning:function(e,t,i){if(e instanceof Error)console.warn(e.toString());else{const s=i?`[${i}] `:"";console.warn(`${s}${t||"Warning"}: ${e}`)}})("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")}else t.triggerFatalException(i)}))}callFinalizer(e,t,i){this.callFinalizerInternal(1,e,t,i)}callFinalizerInternal(e,t,i,s){const n=this.makeDynCall_vppp(t),r=this.id,o=this.ctx.openScope(this);try{this.callbackIntoModule(Boolean(e),(()=>{n(r,i,s)}))}finally{this.ctx.closeScope(this,o)}}enqueueFinalizer(e){super.enqueueFinalizer(e),this.finalizationScheduled||this.destructing||(this.finalizationScheduled=!0,this.ref(),d((()=>{this.finalizationScheduled=!1,this.unref(),this.drainFinalizerQueue()})))}drainFinalizerQueue(){for(;this.pendingFinalizers.length>0;){this.pendingFinalizers.shift().finalize()}}}function I(e,t,i,s,n,r,o){(i="number"!=typeof i?8:i)<8?i=8:i>10&&i!==g&&function(e,t){throw new Error(`${e} requires Node-API version ${t}, but this version of Node.js only supports version 10 add-ons.`)}(t,i);const a=new F(e,t,i,s,n,r,o);return e.envStore.add(a),e.addCleanupHook(a,(()=>{a.unref()}),0),a}class O extends Error{constructor(e){super(e);const t=new.target,i=t.prototype;if(!(this instanceof O)){const e=Object.setPrototypeOf;"function"==typeof e?e.call(Object,this,i):this.__proto__=i,"function"==typeof Error.captureStackTrace&&Error.captureStackTrace(this,t)}}}Object.defineProperty(O.prototype,"name",{configurable:!0,writable:!0,value:"EmnapiError"});class D extends O{constructor(e,t){super(`${e}: The current runtime does not support "FinalizationRegistry" and "WeakRef".${t?` ${t}`:""}`)}}Object.defineProperty(D.prototype,"name",{configurable:!0,writable:!0,value:"NotSupportWeakRefError"});class R extends O{constructor(e,t){super(`${e}: The current runtime does not support "Buffer". Consider using buffer polyfill to make sure \`globalThis.Buffer\` is defined.${t?` ${t}`:""}`)}}Object.defineProperty(R.prototype,"name",{configurable:!0,writable:!0,value:"NotSupportBufferError"});class N{constructor(e){this._value=e}deref(){return this._value}dispose(){this._value=void 0}}class A{constructor(e){this._ref=new N(e)}setWeak(e,t){if(!c||void 0===this._ref||this._ref instanceof WeakRef)return;const i=this._ref.deref();try{A._registry.register(i,this,this);const s=new WeakRef(i);this._ref.dispose(),this._ref=s,this._param=e,this._callback=t}catch(e){if("symbol"!=typeof i)throw e}}clearWeak(){if(c&&void 0!==this._ref&&this._ref instanceof WeakRef){try{A._registry.unregister(this)}catch(e){}this._param=void 0,this._callback=void 0;const e=this._ref.deref();this._ref=void 0===e?e:new N(e)}}reset(){if(c)try{A._registry.unregister(this)}catch(e){}this._param=void 0,this._callback=void 0,this._ref instanceof N&&this._ref.dispose(),this._ref=void 0}isEmpty(){return void 0===this._ref}deref(){if(void 0!==this._ref)return this._ref.deref()}}var H;A._registry=c?new FinalizationRegistry((e=>{e._ref=void 0;const t=e._callback,i=e._param;e._callback=void 0,e._param=void 0,"function"==typeof t&&t(i)})):void 0,exports.ReferenceOwnership=void 0,(H=exports.ReferenceOwnership||(exports.ReferenceOwnership={}))[H.kRuntime=0]="kRuntime",H[H.kUserland=1]="kUserland";class j extends w{static weakCallback(e){e.persistent.reset(),e.invokeFinalizerFromGC()}static create(e,t,i,s,n,r,o){const a=new j(e,t,i,s);return e.ctx.refStore.add(a),a.link(e.reflist),a}constructor(e,t,i,s){super(),this.envObject=e,this._refcount=i,this._ownership=s;const n=e.ctx.handleStore.get(t);var r;this.canBeWeak=(r=n).isObject()||r.isFunction()||r.isSymbol(),this.persistent=new A(n.value),this.id=0,0===i&&this._setWeak()}ref(){return this.persistent.isEmpty()?0:(1==++this._refcount&&this.canBeWeak&&this.persistent.clearWeak(),this._refcount)}unref(){return this.persistent.isEmpty()||0===this._refcount?0:(0==--this._refcount&&this._setWeak(),this._refcount)}get(e=this.envObject){if(this.persistent.isEmpty())return 0;const t=this.persistent.deref();return e.ensureHandle(t).id}resetFinalizer(){}data(){return 0}refcount(){return this._refcount}ownership(){return this._ownership}callUserFinalizer(){}invokeFinalizerFromGC(){this.finalize()}_setWeak(){this.canBeWeak?this.persistent.setWeak(this,j.weakCallback):this.persistent.reset()}finalize(){this.persistent.reset();const e=this._ownership===exports.ReferenceOwnership.kRuntime;this.unlink(),this.callUserFinalizer(),e&&this.dispose()}dispose(){0!==this.id&&(this.unlink(),this.persistent.reset(),this.envObject.ctx.refStore.remove(this.id),super.dispose(),this.envObject=void 0,this.id=0)}}class B extends j{static create(e,t,i,s,n){const r=new B(e,t,i,s,n);return e.ctx.refStore.add(r),r.link(e.reflist),r}constructor(e,t,i,s,n){super(e,t,i,s),this._data=n}data(){return this._data}}class T extends j{static create(e,t,i,s,n,r,o){const a=new T(e,t,i,s,n,r,o);return e.ctx.refStore.add(a),a.link(e.finalizing_reflist),a}constructor(e,t,i,s,n,r,o){super(e,t,i,s),this._finalizer=new S(e,n,r,o)}resetFinalizer(){this._finalizer.resetFinalizer()}data(){return this._finalizer.data()}callUserFinalizer(){this._finalizer.callFinalizer()}invokeFinalizerFromGC(){this._finalizer.envObject.invokeFinalizerFromGC(this)}dispose(){this._finalizer&&(this._finalizer.envObject.dequeueFinalizer(this),this._finalizer.dispose(),super.dispose(),this._finalizer=void 0)}}class W{static create(e,t){const i=new W(e,t);return e.deferredStore.add(i),i}constructor(e,t){this.id=0,this.ctx=e,this.value=t}resolve(e){this.value.resolve(e),this.dispose()}reject(e){this.value.reject(e),this.dispose()}dispose(){this.ctx.deferredStore.remove(this.id),this.id=0,this.value=null,this.ctx=null}}class M{constructor(){this._values=[void 0],this._values.length=4,this._size=1,this._freeList=[]}add(e){let t;if(this._freeList.length)t=this._freeList.shift();else{t=this._size,this._size++;const e=this._values.length;t>=e&&(this._values.length=e+(e>>1)+16)}e.id=t,this._values[t]=e}get(e){return this._values[e]}has(e){return void 0!==this._values[e]}remove(e){const t=this._values[e];t&&(t.id=0,this._values[e]=void 0,this._freeList.push(Number(e)))}dispose(){for(let e=1;e<this._size;++e){const t=this._values[e];null==t||t.dispose()}this._values=[void 0],this._size=1,this._freeList=[]}}class L{constructor(e,t,i,s){this.envObject=e,this.fn=t,this.arg=i,this.order=s}}class P{constructor(){this._cleanupHooks=[],this._cleanupHookCounter=0}empty(){return 0===this._cleanupHooks.length}add(e,t,i){if(this._cleanupHooks.filter((s=>s.envObject===e&&s.fn===t&&s.arg===i)).length>0)throw new Error("Can not add same fn and arg twice");this._cleanupHooks.push(new L(e,t,i,this._cleanupHookCounter++))}remove(e,t,i){for(let s=0;s<this._cleanupHooks.length;++s){const n=this._cleanupHooks[s];if(n.envObject===e&&n.fn===t&&n.arg===i)return void this._cleanupHooks.splice(s,1)}}drain(){const e=this._cleanupHooks.slice();e.sort(((e,t)=>t.order-e.order));for(let t=0;t<e.length;++t){const i=e[t];"number"==typeof i.fn?i.envObject.makeDynCall_vp(i.fn)(i.arg):i.fn(i.arg),this._cleanupHooks.splice(this._cleanupHooks.indexOf(i),1)}}dispose(){this._cleanupHooks.length=0,this._cleanupHookCounter=0}}class U{constructor(){this.refHandle=(new f).port1,this.count=0}increase(){0===this.count&&this.refHandle.ref(),this.count++}decrease(){0!==this.count&&(1===this.count&&this.refHandle.unref(),this.count--)}}class V{constructor(){this._isStopping=!1,this._canCallIntoJs=!0,this._suppressDestroy=!1,this.envStore=new M,this.scopeStore=new b,this.refStore=new M,this.deferredStore=new M,this.handleStore=new z,this.feature={supportReflect:l,supportFinalizer:c,supportWeakSymbol:h,supportBigInt:u,supportNewFunction:n,canSetFunctionName:a,setImmediate:d,Buffer:_,MessageChannel:f},this.cleanupQueue=new P,"object"==typeof process&&null!==process&&"function"==typeof process.once&&(this.refCounter=new U,process.once("beforeExit",(()=>{this._suppressDestroy||this.destroy()})))}suppressDestroy(){this._suppressDestroy=!0}getRuntimeVersions(){return{version:v,NODE_API_SUPPORTED_VERSION_MAX:10,NAPI_VERSION_EXPERIMENTAL:g,NODE_API_DEFAULT_MODULE_API_VERSION:8}}createNotSupportWeakRefError(e,t){return new D(e,t)}createNotSupportBufferError(e,t){return new R(e,t)}createReference(e,t,i,s){return j.create(e,t,i,s)}createReferenceWithData(e,t,i,s,n){return B.create(e,t,i,s,n)}createReferenceWithFinalizer(e,t,i,s,n=0,r=0,o=0){return T.create(e,t,i,s,n,r,o)}createDeferred(e){return W.create(this,e)}createEnv(e,t,i,s,n,r){return I(this,e,t,i,s,n,r)}createTrackedFinalizer(e,t,i,s){return E.create(e,t,i,s)}getCurrentScope(){return this.scopeStore.currentScope}addToCurrentScope(e){return this.scopeStore.currentScope.add(e)}openScope(e){return this.scopeStore.openScope(e)}closeScope(e,t){this.scopeStore.closeScope(e)}ensureHandle(e){switch(e){case void 0:return z.UNDEFINED;case null:return z.NULL;case!0:return z.TRUE;case!1:return z.FALSE;case r:return z.GLOBAL}return this.addToCurrentScope(e)}addCleanupHook(e,t,i){this.cleanupQueue.add(e,t,i)}removeCleanupHook(e,t,i){this.cleanupQueue.remove(e,t,i)}runCleanup(){for(;!this.cleanupQueue.empty();)this.cleanupQueue.drain()}increaseWaitingRequestCounter(){var e;null===(e=this.refCounter)||void 0===e||e.increase()}decreaseWaitingRequestCounter(){var e;null===(e=this.refCounter)||void 0===e||e.decrease()}setCanCallIntoJs(e){this._canCallIntoJs=e}setStopping(e){this._isStopping=e}canCallIntoJs(){return this._canCallIntoJs&&!this._isStopping}destroy(){this.setStopping(!0),this.setCanCallIntoJs(!1),this.runCleanup()}}let G;function q(){return new V}exports.ConstHandle=x,exports.Context=V,exports.Deferred=W,exports.EmnapiError=O,exports.Env=C,exports.External=i,exports.Finalizer=S,exports.Handle=y,exports.HandleScope=k,exports.HandleStore=z,exports.NAPI_VERSION_EXPERIMENTAL=g,exports.NODE_API_DEFAULT_MODULE_API_VERSION=8,exports.NODE_API_SUPPORTED_VERSION_MAX=10,exports.NODE_API_SUPPORTED_VERSION_MIN=1,exports.NodeEnv=F,exports.NotSupportBufferError=R,exports.NotSupportWeakRefError=D,exports.Persistent=A,exports.RefTracker=w,exports.Reference=j,exports.ReferenceWithData=B,exports.ReferenceWithFinalizer=T,exports.ScopeStore=b,exports.Store=M,exports.TrackedFinalizer=E,exports.TryCatch=o,exports.createContext=q,exports.getDefaultContext=function(){return G||(G=q()),G},exports.getExternalValue=s,exports.isExternal=t,exports.isReferenceType=function(e){return"object"==typeof e&&null!==e||"function"==typeof e},exports.version=v;
package/dist/emnapi.d.mts CHANGED
@@ -24,7 +24,7 @@ export declare const enum GlobalHandle {
24
24
  export declare const enum Version {
25
25
  NODE_API_SUPPORTED_VERSION_MIN = 1,
26
26
  NODE_API_DEFAULT_MODULE_API_VERSION = 8,
27
- NODE_API_SUPPORTED_VERSION_MAX = 9,
27
+ NODE_API_SUPPORTED_VERSION_MAX = 10,
28
28
  NAPI_VERSION_EXPERIMENTAL = 2147483647 // INT_MAX
29
29
  }
30
30
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -246,27 +246,6 @@ export declare const enum napi_threadsafe_function_release_mode {
246
246
  napi_tsfn_release,
247
247
  napi_tsfn_abort
248
248
  }
249
- export declare class CallbackInfo {
250
- id: number;
251
- parent: CallbackInfo;
252
- child: CallbackInfo | null;
253
- thiz: any;
254
- data: void_p;
255
- args: ArrayLike<any>;
256
- fn: Function;
257
- constructor(id: number, parent: CallbackInfo, child: CallbackInfo | null, thiz: any, data: void_p, args: ArrayLike<any>, fn: Function);
258
- getNewTarget(envObject: Env): number;
259
- dispose(): void;
260
- }
261
-
262
- export declare class CallbackInfoStack {
263
- current: CallbackInfo;
264
- get(id: number): CallbackInfo | null;
265
- pop(): void;
266
- push(thiz: any, data: void_p, args: ArrayLike<any>, fn: Function): number;
267
- dispose(): void;
268
- }
269
-
270
249
  export declare type CleanupHookCallbackFunction = number | ((arg: number) => void);
271
250
 
272
251
  export declare class ConstHandle<S extends undefined | null | boolean | typeof globalThis> extends Handle<S> {
@@ -283,7 +262,6 @@ export declare class Context {
283
262
  refStore: Store<Reference>;
284
263
  deferredStore: Store<Deferred<any>>;
285
264
  handleStore: HandleStore;
286
- cbinfoStack: CallbackInfoStack;
287
265
  private readonly refCounter?;
288
266
  private readonly cleanupQueue;
289
267
  feature: {
@@ -411,6 +389,17 @@ export declare abstract class Env implements IStoreValue {
411
389
  getInstanceData(): number;
412
390
  }
413
391
 
392
+ /** @public */
393
+ declare interface External_2 extends Record<any, any> {
394
+ }
395
+
396
+ /** @public */
397
+ declare const External_2: {
398
+ new (value: number | bigint): External_2;
399
+ prototype: null;
400
+ };
401
+ export { External_2 as External }
402
+
414
403
  export declare class Finalizer {
415
404
  envObject: Env;
416
405
  private _finalizeCallback;
@@ -429,11 +418,14 @@ export declare class Finalizer {
429
418
 
430
419
  export declare function getDefaultContext(): Context;
431
420
 
421
+ /** @public */
422
+ export declare function getExternalValue(external: External_2): number | bigint;
423
+
432
424
  export declare class Handle<S> {
433
425
  id: number;
434
426
  value: S;
435
427
  constructor(id: number, value: S);
436
- data(envObject: Env): void_p;
428
+ data(): void_p;
437
429
  isNumber(): boolean;
438
430
  isBigInt(): boolean;
439
431
  isString(): boolean;
@@ -461,10 +453,11 @@ export declare class HandleScope {
461
453
  child: HandleScope | null;
462
454
  start: number;
463
455
  end: number;
464
- _escapeCalled: boolean;
456
+ private _escapeCalled;
457
+ callbackInfo: ICallbackInfo;
465
458
  constructor(handleStore: HandleStore, id: number, parentScope: HandleScope | null, start: number, end?: number);
466
459
  add<V>(value: V): Handle<V>;
467
- addExternal(envObject: Env, data: void_p): Handle<object>;
460
+ addExternal(data: void_p): Handle<object>;
468
461
  dispose(): void;
469
462
  escape(handle: number): Handle<any> | null;
470
463
  escapeCalled(): boolean;
@@ -486,6 +479,13 @@ export declare class HandleStore {
486
479
  dispose(): void;
487
480
  }
488
481
 
482
+ export declare interface ICallbackInfo {
483
+ thiz: any;
484
+ data: void_p;
485
+ args: ArrayLike<any>;
486
+ fn: Function;
487
+ }
488
+
489
489
  export declare interface IDeferrdValue<T = any> {
490
490
  resolve: (value: T) => void;
491
491
  reject: (reason?: any) => void;
@@ -494,9 +494,11 @@ export declare interface IDeferrdValue<T = any> {
494
494
  export declare interface IReferenceBinding {
495
495
  wrapped: number;
496
496
  tag: Uint32Array | null;
497
- data: void_p;
498
497
  }
499
498
 
499
+ /** @public */
500
+ export declare function isExternal(object: unknown): object is External_2;
501
+
500
502
  export declare function isReferenceType(v: any): v is object;
501
503
 
502
504
  export declare interface IStoreValue {
@@ -616,6 +618,7 @@ export declare class RefTracker {
616
618
  export declare class ScopeStore {
617
619
  private readonly _rootScope;
618
620
  currentScope: HandleScope;
621
+ private readonly _values;
619
622
  constructor();
620
623
  get(id: number): HandleScope | undefined;
621
624
  openScope(envObject: Env): HandleScope;