@emnapi/runtime 2.0.0-beta.4 → 2.0.0-beta.5

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
@@ -288,10 +288,13 @@ export declare interface CallHandlerConfig {
288
288
  export declare type CleanupHookCallbackFunction = number | ((arg: number) => void);
289
289
 
290
290
  export declare class Context {
291
+ private _isAborted;
291
292
  private _isStopping;
292
293
  private _canCallIntoJs;
293
294
  private _suppressDestroy;
294
295
  private _beforeExitListener?;
296
+ private readonly _pendingTasks;
297
+ private _activeTaskDepth;
295
298
  private envStore;
296
299
  refStore: Map<number, Reference>;
297
300
  private readonly refCounter?;
@@ -339,6 +342,14 @@ export declare class Context {
339
342
  setCanCallIntoJs(value: boolean): void;
340
343
  setStopping(value: boolean): void;
341
344
  canCallIntoJs(): boolean;
345
+ isAborted(): boolean;
346
+ registerPendingTask(task: () => void): () => void;
347
+ scheduleImmediate(callback: () => void): void;
348
+ scheduleMicrotask(callback: () => void): void;
349
+ hasPendingWork(): boolean;
350
+ flush(): void;
351
+ drain(): Promise<void>;
352
+ abort(): void;
342
353
  getDylinkMetadata(binary: WebAssembly.Module | Uint8Array): DylinkMetadata;
343
354
  destroy(): void;
344
355
  }
@@ -465,6 +476,7 @@ export declare class Finalizer {
465
476
  private _finalizeCallback;
466
477
  private _finalizeData;
467
478
  private _finalizeHint;
479
+ private _ctx;
468
480
  private _makeDynCall_vppp;
469
481
  constructor(envObject: Env, _finalizeCallback?: napi_finalize, _finalizeData?: void_p, _finalizeHint?: void_p);
470
482
  copy(): Finalizer;
package/dist/emnapi.js CHANGED
@@ -278,7 +278,7 @@ function getDylinkMetadata(binary) {
278
278
  failIf(offset !== end);
279
279
  return customSection;
280
280
  }
281
- const version = "2.0.0-beta.4";
281
+ const version = "2.0.0-beta.5";
282
282
  const NODE_API_SUPPORTED_VERSION_MIN = 1;
283
283
  const NODE_API_SUPPORTED_VERSION_MAX = 10;
284
284
  const NAPI_VERSION_EXPERIMENTAL = 2147483647;
@@ -346,11 +346,13 @@ class Finalizer {
346
346
  this._finalizeCallback = _finalizeCallback;
347
347
  this._finalizeData = _finalizeData;
348
348
  this._finalizeHint = _finalizeHint;
349
+ this._ctx = envObject.ctx;
349
350
  this._makeDynCall_vppp = envObject.bridge.makeDynCall_vppp;
350
351
  }
351
352
  copy() {
352
353
  const newFina = Object.create(Finalizer.prototype);
353
354
  newFina.envObject = this.envObject;
355
+ newFina._ctx = this._ctx;
354
356
  newFina._makeDynCall_vppp = this._makeDynCall_vppp;
355
357
  newFina._finalizeCallback = this._finalizeCallback;
356
358
  newFina._finalizeData = this._finalizeData;
@@ -362,6 +364,7 @@ class Finalizer {
362
364
  target._finalizeData = this._finalizeData;
363
365
  target._finalizeHint = this._finalizeHint;
364
366
  target.envObject = this.envObject;
367
+ target._ctx = this._ctx;
365
368
  target._makeDynCall_vppp = this._makeDynCall_vppp;
366
369
  this._finalizeCallback = 0;
367
370
  this._finalizeData = 0;
@@ -386,6 +389,8 @@ class Finalizer {
386
389
  this.resetFinalizer();
387
390
  if (!finalize_callback)
388
391
  return;
392
+ if (this._ctx?.isAborted())
393
+ return;
389
394
  const fini = Number(finalize_callback);
390
395
  if (!this.envObject) {
391
396
  this._makeDynCall_vppp(fini)(0, finalize_data, finalize_hint);
@@ -396,6 +401,7 @@ class Finalizer {
396
401
  }
397
402
  dispose() {
398
403
  this.envObject = undefined;
404
+ this._ctx = undefined;
399
405
  this._makeDynCall_vppp = undefined;
400
406
  }
401
407
  }
@@ -821,9 +827,15 @@ class Env extends Disposable {
821
827
  return error_code;
822
828
  }
823
829
  callIntoModule(fn, handleException = handleThrow) {
830
+ if (this.ctx.isAborted()) {
831
+ throw new Error('Context has been aborted');
832
+ }
824
833
  const openHandleScopesBefore = this.openHandleScopes;
825
834
  this.clearLastError();
826
835
  const r = fn(this);
836
+ if (this.ctx.isAborted()) {
837
+ return r;
838
+ }
827
839
  if (openHandleScopesBefore !== this.openHandleScopes) {
828
840
  this.bridge.abort('open_handle_scopes != open_handle_scopes_before');
829
841
  }
@@ -835,6 +847,8 @@ class Env extends Disposable {
835
847
  return r;
836
848
  }
837
849
  invokeFinalizerFromGC(finalizer) {
850
+ if (this.ctx.isAborted())
851
+ return;
838
852
  if (this.moduleApiVersion !== NAPI_VERSION_EXPERIMENTAL) {
839
853
  this.enqueueFinalizer(finalizer);
840
854
  }
@@ -866,10 +880,16 @@ class Env extends Disposable {
866
880
  this.pendingFinalizers.delete(finalizer);
867
881
  }
868
882
  deleteMe() {
883
+ if (this.ctx.isAborted())
884
+ return;
869
885
  RefTracker.finalizeAll(this.finalizing_reflist);
886
+ if (this.ctx.isAborted())
887
+ return;
870
888
  RefTracker.finalizeAll(this.reflist);
871
889
  this.lastException.reset();
872
890
  this.store.dealloc(this.id);
891
+ if (this.ctx.isAborted())
892
+ return;
873
893
  this.bridge.deleteEnv(this.address);
874
894
  }
875
895
  dispose() {
@@ -912,8 +932,12 @@ class NodeEnv extends Env {
912
932
  this.bridge = bridge;
913
933
  }
914
934
  deleteMe() {
935
+ if (this.ctx.isAborted())
936
+ return;
915
937
  this.destructing = true;
916
938
  this.drainFinalizerQueue();
939
+ if (this.ctx.isAborted())
940
+ return;
917
941
  super.deleteMe();
918
942
  }
919
943
  canCallIntoJs() {
@@ -976,10 +1000,21 @@ class NodeEnv extends Env {
976
1000
  }
977
1001
  }
978
1002
  enqueueFinalizer(finalizer) {
1003
+ if (this.ctx.isAborted())
1004
+ return;
979
1005
  super.enqueueFinalizer(finalizer);
980
1006
  if (!this.finalizationScheduled && !this.destructing) {
1007
+ let done = false;
1008
+ let cancelPending;
981
1009
  const drain = () => {
1010
+ if (done)
1011
+ return;
1012
+ done = true;
1013
+ cancelPending?.();
1014
+ cancelPending = undefined;
982
1015
  this.finalizationScheduled = false;
1016
+ if (this.ctx.isAborted())
1017
+ return;
983
1018
  this.unref();
984
1019
  this.drainFinalizerQueue();
985
1020
  };
@@ -994,13 +1029,18 @@ class NodeEnv extends Env {
994
1029
  Promise.resolve().then(drain);
995
1030
  }
996
1031
  }
1032
+ cancelPending = this.ctx.registerPendingTask(drain);
997
1033
  this.finalizationScheduled = true;
998
1034
  this.ref();
999
1035
  }
1000
1036
  }
1001
1037
  drainFinalizerQueue() {
1038
+ if (this.ctx.isAborted())
1039
+ return;
1002
1040
  for (const refTracker of this.pendingFinalizers) {
1003
1041
  this.pendingFinalizers.delete(refTracker);
1042
+ if (this.ctx.isAborted())
1043
+ return;
1004
1044
  refTracker.finalize();
1005
1045
  }
1006
1046
  }
@@ -2380,12 +2420,16 @@ class CleanupQueue {
2380
2420
  const hooks = this._cleanupHooks.slice();
2381
2421
  for (let i = hooks.length - 1; i >= 0; --i) {
2382
2422
  const cb = hooks[i];
2423
+ if (cb.envObject.ctx.isAborted())
2424
+ return;
2383
2425
  if (typeof cb.fn === 'number') {
2384
2426
  cb.envObject.bridge.makeDynCall_vp(cb.fn)(cb.arg);
2385
2427
  }
2386
2428
  else {
2387
2429
  cb.fn(cb.arg);
2388
2430
  }
2431
+ if (cb.envObject.ctx.isAborted())
2432
+ return;
2389
2433
  const lastIndex = this._cleanupHooks.length - 1;
2390
2434
  if (this._cleanupHooks[lastIndex] === cb) {
2391
2435
  this._cleanupHooks.pop();
@@ -2461,12 +2505,25 @@ class NodejsWaitingRequestCounter {
2461
2505
  }
2462
2506
  this.count--;
2463
2507
  }
2508
+ abort() {
2509
+ this.count = 0;
2510
+ if (this.refHandle.unref) {
2511
+ this.refHandle.unref();
2512
+ }
2513
+ try {
2514
+ this.refHandle.close?.();
2515
+ }
2516
+ catch (_) { }
2517
+ }
2464
2518
  }
2465
2519
  class Context {
2466
2520
  constructor(options) {
2521
+ this._isAborted = false;
2467
2522
  this._isStopping = false;
2468
2523
  this._canCallIntoJs = true;
2469
2524
  this._suppressDestroy = false;
2525
+ this._pendingTasks = new Set();
2526
+ this._activeTaskDepth = 0;
2470
2527
  this.envStore = new ArrayStore();
2471
2528
  this.refStore = new Map();
2472
2529
  this.isolate = new Isolate(options);
@@ -2626,20 +2683,28 @@ class Context {
2626
2683
  return this.isolate.isExternal(value);
2627
2684
  }
2628
2685
  addCleanupHook(envObject, fn, arg) {
2686
+ if (this._isAborted)
2687
+ return;
2629
2688
  this.cleanupQueue.add(envObject, fn, arg);
2630
2689
  }
2631
2690
  removeCleanupHook(envObject, fn, arg) {
2632
2691
  this.cleanupQueue.remove(envObject, fn, arg);
2633
2692
  }
2634
2693
  runCleanup() {
2635
- while (!this.cleanupQueue.empty()) {
2694
+ if (this._isAborted)
2695
+ return;
2696
+ while (!this._isAborted && !this.cleanupQueue.empty()) {
2636
2697
  this.cleanupQueue.drain();
2637
2698
  }
2638
2699
  }
2639
2700
  increaseWaitingRequestCounter() {
2701
+ if (this._isAborted)
2702
+ return;
2640
2703
  this.refCounter?.increase();
2641
2704
  }
2642
2705
  decreaseWaitingRequestCounter() {
2706
+ if (this._isAborted)
2707
+ return;
2643
2708
  this.refCounter?.decrease();
2644
2709
  }
2645
2710
  setCanCallIntoJs(value) {
@@ -2649,12 +2714,117 @@ class Context {
2649
2714
  this._isStopping = value;
2650
2715
  }
2651
2716
  canCallIntoJs() {
2652
- return this._canCallIntoJs && !this._isStopping;
2717
+ return this._canCallIntoJs && !this._isStopping && !this._isAborted;
2718
+ }
2719
+ isAborted() {
2720
+ return this._isAborted;
2721
+ }
2722
+ registerPendingTask(task) {
2723
+ if (this._isAborted) {
2724
+ return () => { };
2725
+ }
2726
+ this._pendingTasks.add(task);
2727
+ return () => {
2728
+ this._pendingTasks.delete(task);
2729
+ };
2730
+ }
2731
+ scheduleImmediate(callback) {
2732
+ if (this._isAborted)
2733
+ return;
2734
+ let done = false;
2735
+ const run = () => {
2736
+ if (done)
2737
+ return;
2738
+ done = true;
2739
+ cancel();
2740
+ if (this._isAborted)
2741
+ return;
2742
+ this._activeTaskDepth++;
2743
+ try {
2744
+ callback();
2745
+ }
2746
+ finally {
2747
+ this._activeTaskDepth--;
2748
+ }
2749
+ };
2750
+ const cancel = this.registerPendingTask(run);
2751
+ try {
2752
+ this.features.setImmediate(run);
2753
+ }
2754
+ catch (err) {
2755
+ done = true;
2756
+ cancel();
2757
+ throw err;
2758
+ }
2759
+ }
2760
+ scheduleMicrotask(callback) {
2761
+ if (this._isAborted)
2762
+ return;
2763
+ let done = false;
2764
+ const run = () => {
2765
+ if (done)
2766
+ return;
2767
+ done = true;
2768
+ cancel();
2769
+ if (this._isAborted)
2770
+ return;
2771
+ this._activeTaskDepth++;
2772
+ try {
2773
+ callback();
2774
+ }
2775
+ finally {
2776
+ this._activeTaskDepth--;
2777
+ }
2778
+ };
2779
+ const cancel = this.registerPendingTask(run);
2780
+ Promise.resolve().then(run);
2781
+ }
2782
+ hasPendingWork() {
2783
+ return !this._isAborted && this._pendingTasks.size > 0;
2784
+ }
2785
+ flush() {
2786
+ if (this._activeTaskDepth > 0)
2787
+ return;
2788
+ this._activeTaskDepth++;
2789
+ try {
2790
+ while (!this._isAborted && this._pendingTasks.size > 0) {
2791
+ const task = this._pendingTasks.values().next().value;
2792
+ this._pendingTasks.delete(task);
2793
+ task();
2794
+ }
2795
+ }
2796
+ finally {
2797
+ this._activeTaskDepth--;
2798
+ }
2799
+ }
2800
+ async drain() {
2801
+ while (!this._isAborted) {
2802
+ this.flush();
2803
+ if (this._isAborted)
2804
+ return;
2805
+ await Promise.resolve();
2806
+ if (this._isAborted || this._pendingTasks.size === 0)
2807
+ return;
2808
+ }
2809
+ }
2810
+ abort() {
2811
+ if (this._isAborted)
2812
+ return;
2813
+ this._isAborted = true;
2814
+ this._isStopping = true;
2815
+ this._canCallIntoJs = false;
2816
+ this._suppressDestroy = true;
2817
+ this._removeBeforeExitListener();
2818
+ this.refCounter?.abort();
2819
+ this.cleanupQueue.dispose();
2820
+ this._pendingTasks.clear();
2653
2821
  }
2654
2822
  getDylinkMetadata(binary) {
2655
2823
  return getDylinkMetadata(binary);
2656
2824
  }
2657
2825
  destroy() {
2826
+ if (this._isAborted)
2827
+ return;
2658
2828
  this.setStopping(true);
2659
2829
  this.setCanCallIntoJs(false);
2660
2830
  this.runCleanup();