@emnapi/runtime 1.1.1 → 1.3.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.
@@ -31,6 +31,7 @@ export class ConstHandle<S extends undefined | null | boolean | typeof globalThi
31
31
  export class Context {
32
32
  private _isStopping;
33
33
  private _canCallIntoJs;
34
+ private _suppressDestroy;
34
35
  envStore: Store<Env>;
35
36
  scopeStore: ScopeStore;
36
37
  refStore: Store<Reference>;
@@ -54,6 +55,15 @@ export class Context {
54
55
  } | undefined;
55
56
  };
56
57
  constructor();
58
+ /**
59
+ * Suppress the destroy on `beforeExit` event in Node.js.
60
+ * Call this method if you want to keep the context and
61
+ * all associated {@link Env | Env} alive,
62
+ * this also means that cleanup hooks will not be called.
63
+ * After call this method, you should call
64
+ * {@link Context.destroy | `Context.prototype.destroy`} method manually.
65
+ */
66
+ suppressDestroy(): void;
57
67
  getRuntimeVersions(): {
58
68
  version: string;
59
69
  NODE_API_SUPPORTED_VERSION_MAX: Version;
@@ -62,7 +72,9 @@ export class Context {
62
72
  };
63
73
  createNotSupportWeakRefError(api: string, message: string): NotSupportWeakRefError;
64
74
  createNotSupportBufferError(api: string, message: string): NotSupportBufferError;
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;
75
+ createReference(envObject: Env, handle_id: napi_value, initialRefcount: uint32_t, ownership: ReferenceOwnership): Reference;
76
+ createReferenceWithData(envObject: Env, handle_id: napi_value, initialRefcount: uint32_t, ownership: ReferenceOwnership, data: void_p): Reference;
77
+ createReferenceWithFinalizer(envObject: Env, handle_id: napi_value, initialRefcount: uint32_t, ownership: ReferenceOwnership, finalize_callback?: napi_finalize, finalize_data?: void_p, finalize_hint?: void_p): Reference;
66
78
  createDeferred<T = any>(value: IDeferrdValue<T>): Deferred<T>;
67
79
  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
80
  createTrackedFinalizer(envObject: Env, finalize_callback: napi_finalize, finalize_data: void_p, finalize_hint: void_p): TrackedFinalizer;
@@ -79,6 +91,10 @@ export class Context {
79
91
  setCanCallIntoJs(value: boolean): void;
80
92
  setStopping(value: boolean): void;
81
93
  canCallIntoJs(): boolean;
94
+ /**
95
+ * Destroy the context and call cleanup hooks.
96
+ * Associated {@link Env | Env} will be destroyed.
97
+ */
82
98
  destroy(): void;
83
99
  }
84
100
 
@@ -99,7 +115,7 @@ export class EmnapiError extends Error {
99
115
  constructor(message?: string);
100
116
  }
101
117
 
102
- export class Env implements IStoreValue {
118
+ export abstract class Env implements IStoreValue {
103
119
  readonly ctx: Context;
104
120
  moduleApiVersion: number;
105
121
  makeDynCall_vppp: (cb: Ptr) => (a: Ptr, b: Ptr, c: Ptr) => void;
@@ -107,7 +123,7 @@ export class Env implements IStoreValue {
107
123
  abort: (msg?: string) => never;
108
124
  id: number;
109
125
  openHandleScopes: number;
110
- instanceData: RefBase | null;
126
+ instanceData: TrackedFinalizer | null;
111
127
  tryCatch: TryCatch;
112
128
  refs: number;
113
129
  reflist: RefTracker;
@@ -132,7 +148,7 @@ export class Env implements IStoreValue {
132
148
  getReturnStatus(): napi_status;
133
149
  callIntoModule<T>(fn: (env: Env) => T, handleException?: (envObject: Env, value: any) => void): T;
134
150
  /** @virtual */
135
- callFinalizer(cb: napi_finalize, data: void_p, hint: void_p): void;
151
+ abstract callFinalizer(cb: napi_finalize, data: void_p, hint: void_p): void;
136
152
  invokeFinalizerFromGC(finalizer: RefTracker): void;
137
153
  checkGCAccess(): void;
138
154
  /** @virtual */
@@ -151,14 +167,17 @@ export class Env implements IStoreValue {
151
167
 
152
168
  export class Finalizer {
153
169
  envObject: Env;
154
- protected _finalizeCallback: napi_finalize;
155
- protected _finalizeData: void_p;
156
- protected _finalizeHint: void_p;
170
+ private _finalizeCallback;
171
+ private _finalizeData;
172
+ private _finalizeHint;
173
+ private _makeDynCall_vppp;
157
174
  constructor(envObject: Env, _finalizeCallback?: napi_finalize, _finalizeData?: void_p, _finalizeHint?: void_p);
158
175
  callback(): napi_finalize;
159
176
  data(): void_p;
160
177
  hint(): void_p;
178
+ resetEnv(): void;
161
179
  resetFinalizer(): void;
180
+ callFinalizer(): void;
162
181
  dispose(): void;
163
182
  }
164
183
 
@@ -178,7 +197,7 @@ export class Handle<S> {
178
197
  isArray(): boolean;
179
198
  isArrayBuffer(): boolean;
180
199
  isTypedArray(): boolean;
181
- isBuffer(): boolean;
200
+ isBuffer(BufferConstructor?: BufferCtor): boolean;
182
201
  isDataView(): boolean;
183
202
  isDate(): boolean;
184
203
  isPromise(): boolean;
@@ -285,33 +304,60 @@ export class Persistent<T> {
285
304
  deref(): T | undefined;
286
305
  }
287
306
 
288
- export class RefBase extends TrackedFinalizer {
307
+ export class Reference extends RefTracker implements IStoreValue {
308
+ private static weakCallback;
309
+ id: number;
310
+ envObject: Env;
311
+ private readonly canBeWeak;
289
312
  private _refcount;
290
313
  private readonly _ownership;
291
- constructor(envObject: Env, initial_refcount: uint32_t, ownership: number, finalize_callback: napi_finalize, finalize_data: void_p, finalize_hint: void_p);
292
- data(): void_p;
293
- ref(): uint32_t;
294
- unref(): uint32_t;
295
- refCount(): uint32_t;
296
- ownership(): number;
297
- finalize(): void;
298
- }
299
-
300
- export class Reference extends RefBase implements IStoreValue {
301
- id: number;
302
- private canBeWeak;
303
- static create(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;
304
314
  persistent: Persistent<object>;
305
- private constructor();
315
+ static create(envObject: Env, handle_id: napi_value, initialRefcount: uint32_t, ownership: ReferenceOwnership, _unused1?: void_p, _unused2?: void_p, _unused3?: void_p): Reference;
316
+ protected constructor(envObject: Env, handle_id: napi_value, initialRefcount: uint32_t, ownership: ReferenceOwnership);
306
317
  ref(): number;
307
318
  unref(): number;
308
- get(): napi_value;
319
+ get(envObject?: Env): napi_value;
320
+ /** @virtual */
321
+ resetFinalizer(): void;
322
+ /** @virtual */
323
+ data(): void_p;
324
+ refcount(): number;
325
+ ownership(): ReferenceOwnership;
326
+ /** @virtual */
327
+ protected callUserFinalizer(): void;
328
+ /** @virtual */
329
+ protected invokeFinalizerFromGC(): void;
309
330
  private _setWeak;
310
331
  finalize(): void;
311
332
  dispose(): void;
312
333
  }
313
334
 
335
+ export enum ReferenceOwnership {
336
+ kRuntime = 0,
337
+ kUserland = 1
338
+ }
339
+
340
+ export class ReferenceWithData extends Reference {
341
+ private readonly _data;
342
+ static create(envObject: Env, value: napi_value, initialRefcount: uint32_t, ownership: ReferenceOwnership, data: void_p): ReferenceWithData;
343
+ private constructor();
344
+ data(): void_p;
345
+ }
346
+
347
+ export class ReferenceWithFinalizer extends Reference {
348
+ private _finalizer;
349
+ static create(envObject: Env, value: napi_value, initialRefcount: uint32_t, ownership: ReferenceOwnership, finalize_callback: napi_finalize, finalize_data: void_p, finalize_hint: void_p): ReferenceWithFinalizer;
350
+ private constructor();
351
+ resetFinalizer(): void;
352
+ data(): void_p;
353
+ protected callUserFinalizer(): void;
354
+ protected invokeFinalizerFromGC(): void;
355
+ dispose(): void;
356
+ }
357
+
314
358
  export class RefTracker {
359
+ /** @virtual */
360
+ dispose(): void;
315
361
  /** @virtual */
316
362
  finalize(): void;
317
363
  private _next;
@@ -343,18 +389,13 @@ export class Store<V extends IStoreValue> {
343
389
  dispose(): void;
344
390
  }
345
391
 
346
- export interface TrackedFinalizer extends Finalizer, RefTracker {
347
- }
348
-
349
- export class TrackedFinalizer extends Finalizer {
350
- static finalizeAll(list: RefTracker): void;
351
- link(list: RefTracker): void;
352
- unlink(): void;
392
+ export class TrackedFinalizer extends RefTracker {
393
+ private _finalizer;
353
394
  static create(envObject: Env, finalize_callback: napi_finalize, finalize_data: void_p, finalize_hint: void_p): TrackedFinalizer;
354
- protected constructor(envObject: Env, finalize_callback: napi_finalize, finalize_data: void_p, finalize_hint: void_p);
395
+ private constructor();
396
+ data(): void_p;
355
397
  dispose(): void;
356
398
  finalize(): void;
357
- protected finalizeCore(deleteMe: boolean): void;
358
399
  }
359
400
 
360
401
  export class TryCatch {