@emnapi/runtime 1.3.1 → 1.4.1

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
@@ -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,74 +1,26 @@
1
1
  import { __extends } from 'tslib';
2
2
 
3
- var EMPTY_ARGS = [];
4
- var CallbackInfo = /*#__PURE__*/ (function () {
5
- function CallbackInfo(id, parent, child, thiz, data, args, fn) {
6
- this.id = id;
7
- this.parent = parent;
8
- this.child = child;
9
- this.thiz = thiz;
10
- this.data = data;
11
- this.args = args;
12
- this.fn = fn;
3
+ var externalValue = new WeakMap();
4
+ /** @public */
5
+ function isExternal(object) {
6
+ return externalValue.has(object);
7
+ }
8
+ /** @public */ // eslint-disable-next-line @typescript-eslint/no-redeclare
9
+ var External = (function () {
10
+ function External(value) {
11
+ Object.setPrototypeOf(this, null);
12
+ externalValue.set(this, value);
13
13
  }
14
- CallbackInfo.prototype.getNewTarget = function (envObject) {
15
- var thiz = this.thiz;
16
- // eslint-disable-next-line @typescript-eslint/prefer-optional-chain
17
- if (thiz == null || thiz.constructor == null)
18
- return 0;
19
- return thiz instanceof this.fn ? envObject.ensureHandleId(thiz.constructor) : 0;
20
- };
21
- CallbackInfo.prototype.dispose = function () {
22
- if (this.thiz !== undefined)
23
- this.thiz = undefined;
24
- this.args = EMPTY_ARGS;
25
- this.fn = null;
26
- };
27
- return CallbackInfo;
28
- }());
29
- var ROOT_CBINFO = new CallbackInfo(0, null, null, null, 0, null, null);
30
- var CallbackInfoStack = /*#__PURE__*/ (function () {
31
- function CallbackInfoStack() {
32
- this.current = ROOT_CBINFO;
14
+ External.prototype = null;
15
+ return External;
16
+ })();
17
+ /** @public */
18
+ function getExternalValue(external) {
19
+ if (!isExternal(external)) {
20
+ throw new TypeError('not external');
33
21
  }
34
- CallbackInfoStack.prototype.get = function (id) {
35
- if (id === 1)
36
- return ROOT_CBINFO.child;
37
- var info = ROOT_CBINFO;
38
- for (var i = 0; i < id; ++i) {
39
- info = info.child;
40
- if (info === null)
41
- return null;
42
- }
43
- return info === ROOT_CBINFO ? null : info;
44
- };
45
- CallbackInfoStack.prototype.pop = function () {
46
- var current = this.current;
47
- if (current === ROOT_CBINFO)
48
- return;
49
- this.current = current.parent;
50
- current.dispose();
51
- };
52
- CallbackInfoStack.prototype.push = function (thiz, data, args, fn) {
53
- var info = this.current.child;
54
- if (info) {
55
- info.thiz = thiz;
56
- info.data = data;
57
- info.args = args;
58
- info.fn = fn;
59
- }
60
- else {
61
- info = new CallbackInfo(this.current.id + 1, this.current, null, thiz, data, args, fn);
62
- this.current.child = info;
63
- }
64
- this.current = info;
65
- return info.id;
66
- };
67
- CallbackInfoStack.prototype.dispose = function () {
68
- this.current = null;
69
- };
70
- return CallbackInfoStack;
71
- }());
22
+ return externalValue.get(external);
23
+ }
72
24
 
73
25
  var supportNewFunction = /*#__PURE__*/ (function () {
74
26
  var f;
@@ -210,9 +162,9 @@ var _Buffer = typeof Buffer === 'function'
210
162
  catch (_) { }
211
163
  return undefined;
212
164
  })();
213
- var version = "1.3.1";
165
+ var version = "1.4.1";
214
166
  var NODE_API_SUPPORTED_VERSION_MIN = 1 /* Version.NODE_API_SUPPORTED_VERSION_MIN */;
215
- var NODE_API_SUPPORTED_VERSION_MAX = 9 /* Version.NODE_API_SUPPORTED_VERSION_MAX */;
167
+ var NODE_API_SUPPORTED_VERSION_MAX = 10 /* Version.NODE_API_SUPPORTED_VERSION_MAX */;
216
168
  var NAPI_VERSION_EXPERIMENTAL = 2147483647 /* Version.NAPI_VERSION_EXPERIMENTAL */;
217
169
  var NODE_API_DEFAULT_MODULE_API_VERSION = 8 /* Version.NODE_API_DEFAULT_MODULE_API_VERSION */;
218
170
 
@@ -221,8 +173,8 @@ var Handle = /*#__PURE__*/ (function () {
221
173
  this.id = id;
222
174
  this.value = value;
223
175
  }
224
- Handle.prototype.data = function (envObject) {
225
- return envObject.getObjectBinding(this.value).data;
176
+ Handle.prototype.data = function () {
177
+ return getExternalValue(this.value);
226
178
  };
227
179
  Handle.prototype.isNumber = function () {
228
180
  return typeof this.value === 'number';
@@ -237,7 +189,7 @@ var Handle = /*#__PURE__*/ (function () {
237
189
  return typeof this.value === 'function';
238
190
  };
239
191
  Handle.prototype.isExternal = function () {
240
- return (isReferenceType(this.value) && Object.getPrototypeOf(this.value) === null);
192
+ return isExternal(this.value);
241
193
  };
242
194
  Handle.prototype.isObject = function () {
243
195
  return typeof this.value === 'object' && this.value !== null;
@@ -291,10 +243,6 @@ var ConstHandle = /*#__PURE__*/ (function (_super) {
291
243
  ConstHandle.prototype.dispose = function () { };
292
244
  return ConstHandle;
293
245
  }(Handle));
294
- function External() {
295
- Object.setPrototypeOf(this, null);
296
- }
297
- External.prototype = null;
298
246
  var HandleStore = /*#__PURE__*/ (function () {
299
247
  function HandleStore() {
300
248
  this._values = [
@@ -365,21 +313,24 @@ var HandleScope = /*#__PURE__*/ (function () {
365
313
  this.start = start;
366
314
  this.end = end;
367
315
  this._escapeCalled = false;
316
+ this.callbackInfo = {
317
+ thiz: undefined,
318
+ data: 0,
319
+ args: undefined,
320
+ fn: undefined
321
+ };
368
322
  }
369
323
  HandleScope.prototype.add = function (value) {
370
324
  var h = this.handleStore.push(value);
371
325
  this.end++;
372
326
  return h;
373
327
  };
374
- HandleScope.prototype.addExternal = function (envObject, data) {
375
- var value = new External();
376
- var h = envObject.ctx.handleStore.push(value);
377
- var binding = envObject.initObjectBinding(value);
378
- binding.data = data;
379
- this.end++;
380
- return h;
328
+ HandleScope.prototype.addExternal = function (data) {
329
+ return this.add(new External(data));
381
330
  };
382
331
  HandleScope.prototype.dispose = function () {
332
+ if (this._escapeCalled)
333
+ this._escapeCalled = false;
383
334
  if (this.start === this.end)
384
335
  return;
385
336
  this.handleStore.erase(this.start, this.end);
@@ -407,27 +358,21 @@ var ScopeStore = /*#__PURE__*/ (function () {
407
358
  function ScopeStore() {
408
359
  this._rootScope = new HandleScope(null, 0, null, 1, HandleStore.MIN_ID);
409
360
  this.currentScope = this._rootScope;
361
+ this._values = [undefined];
410
362
  }
411
363
  ScopeStore.prototype.get = function (id) {
412
- id = Number(id);
413
- var scope = this.currentScope;
414
- while (scope !== this._rootScope) {
415
- if (scope.id === id) {
416
- return scope;
417
- }
418
- scope = scope.parent;
419
- }
420
- return undefined;
364
+ return this._values[id];
421
365
  };
422
366
  ScopeStore.prototype.openScope = function (envObject) {
423
367
  var currentScope = this.currentScope;
424
368
  var scope = currentScope.child;
425
369
  if (scope !== null) {
426
370
  scope.start = scope.end = currentScope.end;
427
- scope._escapeCalled = false;
428
371
  }
429
372
  else {
430
- scope = new HandleScope(envObject.ctx.handleStore, currentScope.id + 1, currentScope, currentScope.end);
373
+ var id = currentScope.id + 1;
374
+ scope = new HandleScope(envObject.ctx.handleStore, id, currentScope, currentScope.end);
375
+ this._values[id] = scope;
431
376
  }
432
377
  this.currentScope = scope;
433
378
  envObject.openHandleScopes++;
@@ -442,19 +387,8 @@ var ScopeStore = /*#__PURE__*/ (function () {
442
387
  envObject.openHandleScopes--;
443
388
  };
444
389
  ScopeStore.prototype.dispose = function () {
445
- var scope = this.currentScope;
446
- while (scope !== null) {
447
- scope.handleStore = null;
448
- scope.id = 0;
449
- scope.parent = null;
450
- scope.start = HandleStore.MIN_ID;
451
- scope.end = HandleStore.MIN_ID;
452
- scope._escapeCalled = false;
453
- var child = scope.child;
454
- scope.child = null;
455
- scope = child;
456
- }
457
- this.currentScope = null;
390
+ this.currentScope = this._rootScope;
391
+ this._values.length = 1;
458
392
  };
459
393
  return ScopeStore;
460
394
  }());
@@ -731,8 +665,7 @@ var Env = /*#__PURE__*/ (function () {
731
665
  Env.prototype.initObjectBinding = function (value) {
732
666
  var binding = {
733
667
  wrapped: 0,
734
- tag: null,
735
- data: 0
668
+ tag: null
736
669
  };
737
670
  this._bindingMap.set(value, binding);
738
671
  return binding;
@@ -796,7 +729,7 @@ var NodeEnv = /*#__PURE__*/ (function (_super) {
796
729
  }
797
730
  var hasProcess = typeof process === 'object' && process !== null;
798
731
  var hasForceFlag = hasProcess ? Boolean(process.execArgv && (process.execArgv.indexOf('--force-node-api-uncaught-exceptions-policy') !== -1)) : false;
799
- if (envObject.moduleApiVersion < NAPI_VERSION_EXPERIMENTAL && !hasForceFlag && !enforceUncaughtExceptionPolicy) {
732
+ if (envObject.moduleApiVersion < 10 && !hasForceFlag && !enforceUncaughtExceptionPolicy) {
800
733
  var warn = hasProcess && typeof process.emitWarning === 'function'
801
734
  ? process.emitWarning
802
735
  : function (warning, type, code) {
@@ -1302,7 +1235,9 @@ var NodejsWaitingRequestCounter = /*#__PURE__*/ (function () {
1302
1235
  }
1303
1236
  NodejsWaitingRequestCounter.prototype.increase = function () {
1304
1237
  if (this.count === 0) {
1305
- this.refHandle.ref();
1238
+ if (this.refHandle.ref) {
1239
+ this.refHandle.ref();
1240
+ }
1306
1241
  }
1307
1242
  this.count++;
1308
1243
  };
@@ -1310,7 +1245,9 @@ var NodejsWaitingRequestCounter = /*#__PURE__*/ (function () {
1310
1245
  if (this.count === 0)
1311
1246
  return;
1312
1247
  if (this.count === 1) {
1313
- this.refHandle.unref();
1248
+ if (this.refHandle.unref) {
1249
+ this.refHandle.unref();
1250
+ }
1314
1251
  }
1315
1252
  this.count--;
1316
1253
  };
@@ -1327,7 +1264,6 @@ var Context = /*#__PURE__*/ (function () {
1327
1264
  this.refStore = new Store();
1328
1265
  this.deferredStore = new Store();
1329
1266
  this.handleStore = new HandleStore();
1330
- this.cbinfoStack = new CallbackInfoStack();
1331
1267
  this.feature = {
1332
1268
  supportReflect: supportReflect,
1333
1269
  supportFinalizer: supportFinalizer,
@@ -1468,4 +1404,4 @@ function getDefaultContext() {
1468
1404
  return defaultContext;
1469
1405
  }
1470
1406
 
1471
- 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, RefTracker, Reference, ReferenceOwnership, ReferenceWithData, ReferenceWithFinalizer, ScopeStore, Store, TrackedFinalizer, TryCatch, createContext, getDefaultContext, isReferenceType, version };
1407
+ export { ConstHandle, Context, Deferred, EmnapiError, Env, External, 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, RefTracker, Reference, ReferenceOwnership, ReferenceWithData, ReferenceWithFinalizer, ScopeStore, Store, TrackedFinalizer, TryCatch, createContext, getDefaultContext, getExternalValue, isExternal, isReferenceType, version };
@@ -1,26 +1,5 @@
1
1
  declare namespace emnapi {
2
2
 
3
- export class CallbackInfo {
4
- id: number;
5
- parent: CallbackInfo;
6
- child: CallbackInfo | null;
7
- thiz: any;
8
- data: void_p;
9
- args: ArrayLike<any>;
10
- fn: Function;
11
- constructor(id: number, parent: CallbackInfo, child: CallbackInfo | null, thiz: any, data: void_p, args: ArrayLike<any>, fn: Function);
12
- getNewTarget(envObject: Env): number;
13
- dispose(): void;
14
- }
15
-
16
- export class CallbackInfoStack {
17
- current: CallbackInfo;
18
- get(id: number): CallbackInfo | null;
19
- pop(): void;
20
- push(thiz: any, data: void_p, args: ArrayLike<any>, fn: Function): number;
21
- dispose(): void;
22
- }
23
-
24
3
  export type CleanupHookCallbackFunction = number | ((arg: number) => void);
25
4
 
26
5
  export class ConstHandle<S extends undefined | null | boolean | typeof globalThis> extends Handle<S> {
@@ -37,7 +16,6 @@ export class Context {
37
16
  refStore: Store<Reference>;
38
17
  deferredStore: Store<Deferred<any>>;
39
18
  handleStore: HandleStore;
40
- cbinfoStack: CallbackInfoStack;
41
19
  private readonly refCounter?;
42
20
  private readonly cleanupQueue;
43
21
  feature: {
@@ -165,6 +143,17 @@ export abstract class Env implements IStoreValue {
165
143
  getInstanceData(): number;
166
144
  }
167
145
 
146
+ /** @public */
147
+ interface External_2 extends Record<any, any> {
148
+ }
149
+
150
+ /** @public */
151
+ const External_2: {
152
+ new (value: number | bigint): External_2;
153
+ prototype: null;
154
+ };
155
+ export { External_2 as External }
156
+
168
157
  export class Finalizer {
169
158
  envObject: Env;
170
159
  private _finalizeCallback;
@@ -183,11 +172,14 @@ export class Finalizer {
183
172
 
184
173
  export function getDefaultContext(): Context;
185
174
 
175
+ /** @public */
176
+ export function getExternalValue(external: External_2): number | bigint;
177
+
186
178
  export class Handle<S> {
187
179
  id: number;
188
180
  value: S;
189
181
  constructor(id: number, value: S);
190
- data(envObject: Env): void_p;
182
+ data(): void_p;
191
183
  isNumber(): boolean;
192
184
  isBigInt(): boolean;
193
185
  isString(): boolean;
@@ -215,10 +207,11 @@ export class HandleScope {
215
207
  child: HandleScope | null;
216
208
  start: number;
217
209
  end: number;
218
- _escapeCalled: boolean;
210
+ private _escapeCalled;
211
+ callbackInfo: ICallbackInfo;
219
212
  constructor(handleStore: HandleStore, id: number, parentScope: HandleScope | null, start: number, end?: number);
220
213
  add<V>(value: V): Handle<V>;
221
- addExternal(envObject: Env, data: void_p): Handle<object>;
214
+ addExternal(data: void_p): Handle<object>;
222
215
  dispose(): void;
223
216
  escape(handle: number): Handle<any> | null;
224
217
  escapeCalled(): boolean;
@@ -240,6 +233,13 @@ export class HandleStore {
240
233
  dispose(): void;
241
234
  }
242
235
 
236
+ export interface ICallbackInfo {
237
+ thiz: any;
238
+ data: void_p;
239
+ args: ArrayLike<any>;
240
+ fn: Function;
241
+ }
242
+
243
243
  export interface IDeferrdValue<T = any> {
244
244
  resolve: (value: T) => void;
245
245
  reject: (reason?: any) => void;
@@ -248,9 +248,11 @@ export interface IDeferrdValue<T = any> {
248
248
  export interface IReferenceBinding {
249
249
  wrapped: number;
250
250
  tag: Uint32Array | null;
251
- data: void_p;
252
251
  }
253
252
 
253
+ /** @public */
254
+ export function isExternal(object: unknown): object is External_2;
255
+
254
256
  export function isReferenceType(v: any): v is object;
255
257
 
256
258
  export interface IStoreValue {
@@ -370,6 +372,7 @@ export class RefTracker {
370
372
  export class ScopeStore {
371
373
  private readonly _rootScope;
372
374
  currentScope: HandleScope;
375
+ private readonly _values;
373
376
  constructor();
374
377
  get(id: number): HandleScope | undefined;
375
378
  openScope(envObject: Env): HandleScope;