@emnapi/runtime 1.2.0 → 1.3.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.mjs CHANGED
@@ -202,7 +202,7 @@ const _Buffer = typeof Buffer === 'function'
202
202
  catch (_) { }
203
203
  return undefined;
204
204
  })();
205
- const version = "1.2.0";
205
+ const version = "1.3.1";
206
206
  const NODE_API_SUPPORTED_VERSION_MIN = 1;
207
207
  const NODE_API_SUPPORTED_VERSION_MAX = 9;
208
208
  const NAPI_VERSION_EXPERIMENTAL = 2147483647;
@@ -244,6 +244,8 @@ class Handle {
244
244
  return (ArrayBuffer.isView(this.value)) && !(this.value instanceof DataView);
245
245
  }
246
246
  isBuffer(BufferConstructor) {
247
+ if (ArrayBuffer.isView(this.value))
248
+ return true;
247
249
  BufferConstructor !== null && BufferConstructor !== void 0 ? BufferConstructor : (BufferConstructor = _Buffer);
248
250
  return typeof BufferConstructor === 'function' && BufferConstructor.isBuffer(this.value);
249
251
  }
@@ -447,6 +449,7 @@ class RefTracker {
447
449
  this._next = null;
448
450
  this._prev = null;
449
451
  }
452
+ dispose() { }
450
453
  finalize() { }
451
454
  link(list) {
452
455
  this._prev = list;
@@ -479,103 +482,80 @@ class Finalizer {
479
482
  this._finalizeCallback = _finalizeCallback;
480
483
  this._finalizeData = _finalizeData;
481
484
  this._finalizeHint = _finalizeHint;
485
+ this._makeDynCall_vppp = envObject.makeDynCall_vppp;
482
486
  }
483
487
  callback() { return this._finalizeCallback; }
484
488
  data() { return this._finalizeData; }
485
489
  hint() { return this._finalizeHint; }
490
+ resetEnv() {
491
+ this.envObject = undefined;
492
+ }
486
493
  resetFinalizer() {
487
494
  this._finalizeCallback = 0;
488
495
  this._finalizeData = 0;
489
496
  this._finalizeHint = 0;
490
497
  }
498
+ callFinalizer() {
499
+ const finalize_callback = this._finalizeCallback;
500
+ const finalize_data = this._finalizeData;
501
+ const finalize_hint = this._finalizeHint;
502
+ this.resetFinalizer();
503
+ if (!finalize_callback)
504
+ return;
505
+ const fini = Number(finalize_callback);
506
+ if (!this.envObject) {
507
+ this._makeDynCall_vppp(fini)(0, finalize_data, finalize_hint);
508
+ }
509
+ else {
510
+ this.envObject.callFinalizer(fini, finalize_data, finalize_hint);
511
+ }
512
+ }
491
513
  dispose() {
492
514
  this.envObject = undefined;
515
+ this._makeDynCall_vppp = undefined;
493
516
  }
494
517
  }
495
518
 
496
- class TrackedFinalizer extends Finalizer {
497
- static finalizeAll(list) {
498
- RefTracker.finalizeAll(list);
499
- }
500
- link(list) {
501
- RefTracker.prototype.link.call(this, list);
502
- }
503
- unlink() {
504
- RefTracker.prototype.unlink.call(this);
505
- }
519
+ class TrackedFinalizer extends RefTracker {
506
520
  static create(envObject, finalize_callback, finalize_data, finalize_hint) {
507
- return new TrackedFinalizer(envObject, finalize_callback, finalize_data, finalize_hint);
521
+ const finalizer = new TrackedFinalizer(envObject, finalize_callback, finalize_data, finalize_hint);
522
+ finalizer.link(envObject.finalizing_reflist);
523
+ return finalizer;
508
524
  }
509
525
  constructor(envObject, finalize_callback, finalize_data, finalize_hint) {
510
- super(envObject, finalize_callback, finalize_data, finalize_hint);
511
- this._next = null;
512
- this._prev = null;
513
- this.link(!finalize_callback ? envObject.reflist : envObject.finalizing_reflist);
526
+ super();
527
+ this._finalizer = new Finalizer(envObject, finalize_callback, finalize_data, finalize_hint);
528
+ }
529
+ data() {
530
+ return this._finalizer.data();
514
531
  }
515
532
  dispose() {
533
+ if (!this._finalizer)
534
+ return;
516
535
  this.unlink();
517
- this.envObject.dequeueFinalizer(this);
536
+ this._finalizer.envObject.dequeueFinalizer(this);
537
+ this._finalizer.dispose();
538
+ this._finalizer = undefined;
518
539
  super.dispose();
519
540
  }
520
541
  finalize() {
521
- this.finalizeCore(true);
522
- }
523
- finalizeCore(deleteMe) {
524
- const finalize_callback = this._finalizeCallback;
525
- const finalize_data = this._finalizeData;
526
- const finalize_hint = this._finalizeHint;
527
- this.resetFinalizer();
528
542
  this.unlink();
529
543
  let error;
530
544
  let caught = false;
531
- if (finalize_callback) {
532
- const fini = Number(finalize_callback);
533
- try {
534
- this.envObject.callFinalizer(fini, finalize_data, finalize_hint);
535
- }
536
- catch (err) {
537
- caught = true;
538
- error = err;
539
- }
545
+ try {
546
+ this._finalizer.callFinalizer();
540
547
  }
541
- if (deleteMe) {
542
- this.dispose();
548
+ catch (err) {
549
+ caught = true;
550
+ error = err;
543
551
  }
552
+ this.dispose();
544
553
  if (caught) {
545
554
  throw error;
546
555
  }
547
556
  }
548
557
  }
549
558
 
550
- class RefBase extends TrackedFinalizer {
551
- constructor(envObject, initial_refcount, ownership, finalize_callback, finalize_data, finalize_hint) {
552
- super(envObject, finalize_callback, finalize_data, finalize_hint);
553
- this._refcount = initial_refcount;
554
- this._ownership = ownership;
555
- }
556
- data() {
557
- return this._finalizeData;
558
- }
559
- ref() {
560
- return ++this._refcount;
561
- }
562
- unref() {
563
- if (this._refcount === 0) {
564
- return 0;
565
- }
566
- return --this._refcount;
567
- }
568
- refCount() {
569
- return this._refcount;
570
- }
571
- ownership() {
572
- return this._ownership;
573
- }
574
- finalize() {
575
- this.finalizeCore(this._ownership === 0);
576
- }
577
- }
578
-
579
559
  function throwNodeApiVersionError(moduleName, moduleApiVersion) {
580
560
  const errorMessage = `${moduleName} requires Node-API version ${moduleApiVersion}, but this version of Node.js only supports version ${NODE_API_SUPPORTED_VERSION_MAX} add-ons.`;
581
561
  throw new Error(errorMessage);
@@ -666,17 +646,6 @@ class Env {
666
646
  }
667
647
  return r;
668
648
  }
669
- callFinalizer(cb, data, hint) {
670
- const f = this.makeDynCall_vppp(cb);
671
- const env = this.id;
672
- const scope = this.ctx.openScope(this);
673
- try {
674
- this.callIntoModule(() => { f(env, data, hint); });
675
- }
676
- finally {
677
- this.ctx.closeScope(this, scope);
678
- }
679
- }
680
649
  invokeFinalizerFromGC(finalizer) {
681
650
  if (this.moduleApiVersion !== NAPI_VERSION_EXPERIMENTAL) {
682
651
  this.enqueueFinalizer(finalizer);
@@ -714,8 +683,8 @@ class Env {
714
683
  }
715
684
  }
716
685
  deleteMe() {
717
- RefBase.finalizeAll(this.finalizing_reflist);
718
- RefBase.finalizeAll(this.reflist);
686
+ RefTracker.finalizeAll(this.finalizing_reflist);
687
+ RefTracker.finalizeAll(this.reflist);
719
688
  this.tryCatch.extractException();
720
689
  this.ctx.envStore.remove(this.id);
721
690
  }
@@ -723,6 +692,8 @@ class Env {
723
692
  if (this.id === 0)
724
693
  return;
725
694
  this.deleteMe();
695
+ this.finalizing_reflist.dispose();
696
+ this.reflist.dispose();
726
697
  this.id = 0;
727
698
  }
728
699
  initObjectBinding(value) {
@@ -744,7 +715,7 @@ class Env {
744
715
  if (this.instanceData) {
745
716
  this.instanceData.dispose();
746
717
  }
747
- this.instanceData = new RefBase(this, 0, 0, finalize_cb, data, finalize_hint);
718
+ this.instanceData = TrackedFinalizer.create(this, finalize_cb, data, finalize_hint);
748
719
  }
749
720
  getInstanceData() {
750
721
  return this.instanceData ? this.instanceData.data() : 0;
@@ -989,61 +960,75 @@ Persistent._registry = supportFinalizer
989
960
  })
990
961
  : undefined;
991
962
 
992
- function weakCallback(ref) {
993
- ref.persistent.reset();
994
- ref.envObject.invokeFinalizerFromGC(ref);
995
- }
963
+ var ReferenceOwnership;
964
+ (function (ReferenceOwnership) {
965
+ ReferenceOwnership[ReferenceOwnership["kRuntime"] = 0] = "kRuntime";
966
+ ReferenceOwnership[ReferenceOwnership["kUserland"] = 1] = "kUserland";
967
+ })(ReferenceOwnership || (ReferenceOwnership = {}));
996
968
  function canBeHeldWeakly(value) {
997
969
  return value.isObject() || value.isFunction() || value.isSymbol();
998
970
  }
999
- class Reference extends RefBase {
1000
- static create(envObject, handle_id, initialRefcount, ownership, finalize_callback = 0, finalize_data = 0, finalize_hint = 0) {
1001
- const handle = envObject.ctx.handleStore.get(handle_id);
1002
- const ref = new Reference(envObject, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint);
971
+ class Reference extends RefTracker {
972
+ static weakCallback(ref) {
973
+ ref.persistent.reset();
974
+ ref.invokeFinalizerFromGC();
975
+ }
976
+ static create(envObject, handle_id, initialRefcount, ownership, _unused1, _unused2, _unused3) {
977
+ const ref = new Reference(envObject, handle_id, initialRefcount, ownership);
1003
978
  envObject.ctx.refStore.add(ref);
1004
- ref.canBeWeak = canBeHeldWeakly(handle);
1005
- ref.persistent = new Persistent(handle.value);
1006
- if (initialRefcount === 0) {
1007
- ref._setWeak();
1008
- }
979
+ ref.link(envObject.reflist);
1009
980
  return ref;
1010
981
  }
1011
- constructor(envObject, initialRefcount, ownership, finalize_callback = 0, finalize_data = 0, finalize_hint = 0) {
1012
- super(envObject, initialRefcount >>> 0, ownership, finalize_callback, finalize_data, finalize_hint);
982
+ constructor(envObject, handle_id, initialRefcount, ownership) {
983
+ super();
984
+ this.envObject = envObject;
985
+ this._refcount = initialRefcount;
986
+ this._ownership = ownership;
987
+ const handle = envObject.ctx.handleStore.get(handle_id);
988
+ this.canBeWeak = canBeHeldWeakly(handle);
989
+ this.persistent = new Persistent(handle.value);
1013
990
  this.id = 0;
991
+ if (initialRefcount === 0) {
992
+ this._setWeak();
993
+ }
1014
994
  }
1015
995
  ref() {
1016
996
  if (this.persistent.isEmpty()) {
1017
997
  return 0;
1018
998
  }
1019
- const count = super.ref();
1020
- if (count === 1 && this.canBeWeak) {
999
+ if (++this._refcount === 1 && this.canBeWeak) {
1021
1000
  this.persistent.clearWeak();
1022
1001
  }
1023
- return count;
1002
+ return this._refcount;
1024
1003
  }
1025
1004
  unref() {
1026
- if (this.persistent.isEmpty()) {
1005
+ if (this.persistent.isEmpty() || this._refcount === 0) {
1027
1006
  return 0;
1028
1007
  }
1029
- const oldRefcount = this.refCount();
1030
- const refcount = super.unref();
1031
- if (oldRefcount === 1 && refcount === 0) {
1008
+ if (--this._refcount === 0) {
1032
1009
  this._setWeak();
1033
1010
  }
1034
- return refcount;
1011
+ return this._refcount;
1035
1012
  }
1036
- get() {
1013
+ get(envObject = this.envObject) {
1037
1014
  if (this.persistent.isEmpty()) {
1038
1015
  return 0;
1039
1016
  }
1040
1017
  const obj = this.persistent.deref();
1041
- const handle = this.envObject.ensureHandle(obj);
1018
+ const handle = envObject.ensureHandle(obj);
1042
1019
  return handle.id;
1043
1020
  }
1021
+ resetFinalizer() { }
1022
+ data() { return 0; }
1023
+ refcount() { return this._refcount; }
1024
+ ownership() { return this._ownership; }
1025
+ callUserFinalizer() { }
1026
+ invokeFinalizerFromGC() {
1027
+ this.finalize();
1028
+ }
1044
1029
  _setWeak() {
1045
1030
  if (this.canBeWeak) {
1046
- this.persistent.setWeak(this, weakCallback);
1031
+ this.persistent.setWeak(this, Reference.weakCallback);
1047
1032
  }
1048
1033
  else {
1049
1034
  this.persistent.reset();
@@ -1051,17 +1036,71 @@ class Reference extends RefBase {
1051
1036
  }
1052
1037
  finalize() {
1053
1038
  this.persistent.reset();
1054
- super.finalize();
1039
+ const deleteMe = this._ownership === ReferenceOwnership.kRuntime;
1040
+ this.unlink();
1041
+ this.callUserFinalizer();
1042
+ if (deleteMe) {
1043
+ this.dispose();
1044
+ }
1055
1045
  }
1056
1046
  dispose() {
1057
1047
  if (this.id === 0)
1058
1048
  return;
1049
+ this.unlink();
1059
1050
  this.persistent.reset();
1060
1051
  this.envObject.ctx.refStore.remove(this.id);
1061
1052
  super.dispose();
1053
+ this.envObject = undefined;
1062
1054
  this.id = 0;
1063
1055
  }
1064
1056
  }
1057
+ class ReferenceWithData extends Reference {
1058
+ static create(envObject, value, initialRefcount, ownership, data) {
1059
+ const reference = new ReferenceWithData(envObject, value, initialRefcount, ownership, data);
1060
+ envObject.ctx.refStore.add(reference);
1061
+ reference.link(envObject.reflist);
1062
+ return reference;
1063
+ }
1064
+ constructor(envObject, value, initialRefcount, ownership, _data) {
1065
+ super(envObject, value, initialRefcount, ownership);
1066
+ this._data = _data;
1067
+ }
1068
+ data() {
1069
+ return this._data;
1070
+ }
1071
+ }
1072
+ class ReferenceWithFinalizer extends Reference {
1073
+ static create(envObject, value, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint) {
1074
+ const reference = new ReferenceWithFinalizer(envObject, value, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint);
1075
+ envObject.ctx.refStore.add(reference);
1076
+ reference.link(envObject.finalizing_reflist);
1077
+ return reference;
1078
+ }
1079
+ constructor(envObject, value, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint) {
1080
+ super(envObject, value, initialRefcount, ownership);
1081
+ this._finalizer = new Finalizer(envObject, finalize_callback, finalize_data, finalize_hint);
1082
+ }
1083
+ resetFinalizer() {
1084
+ this._finalizer.resetFinalizer();
1085
+ }
1086
+ data() {
1087
+ return this._finalizer.data();
1088
+ }
1089
+ callUserFinalizer() {
1090
+ this._finalizer.callFinalizer();
1091
+ }
1092
+ invokeFinalizerFromGC() {
1093
+ this._finalizer.envObject.invokeFinalizerFromGC(this);
1094
+ }
1095
+ dispose() {
1096
+ if (!this._finalizer)
1097
+ return;
1098
+ this._finalizer.envObject.dequeueFinalizer(this);
1099
+ this._finalizer.dispose();
1100
+ super.dispose();
1101
+ this._finalizer = undefined;
1102
+ }
1103
+ }
1065
1104
 
1066
1105
  class Deferred {
1067
1106
  static create(ctx, value) {
@@ -1212,6 +1251,7 @@ class Context {
1212
1251
  constructor() {
1213
1252
  this._isStopping = false;
1214
1253
  this._canCallIntoJs = true;
1254
+ this._suppressDestroy = false;
1215
1255
  this.envStore = new Store();
1216
1256
  this.scopeStore = new ScopeStore();
1217
1257
  this.refStore = new Store();
@@ -1233,10 +1273,15 @@ class Context {
1233
1273
  if (typeof process === 'object' && process !== null && typeof process.once === 'function') {
1234
1274
  this.refCounter = new NodejsWaitingRequestCounter();
1235
1275
  process.once('beforeExit', () => {
1236
- this.destroy();
1276
+ if (!this._suppressDestroy) {
1277
+ this.destroy();
1278
+ }
1237
1279
  });
1238
1280
  }
1239
1281
  }
1282
+ suppressDestroy() {
1283
+ this._suppressDestroy = true;
1284
+ }
1240
1285
  getRuntimeVersions() {
1241
1286
  return {
1242
1287
  version,
@@ -1251,8 +1296,14 @@ class Context {
1251
1296
  createNotSupportBufferError(api, message) {
1252
1297
  return new NotSupportBufferError(api, message);
1253
1298
  }
1254
- createReference(envObject, handle_id, initialRefcount, ownership, finalize_callback = 0, finalize_data = 0, finalize_hint = 0) {
1255
- return Reference.create(envObject, handle_id, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint);
1299
+ createReference(envObject, handle_id, initialRefcount, ownership) {
1300
+ return Reference.create(envObject, handle_id, initialRefcount, ownership);
1301
+ }
1302
+ createReferenceWithData(envObject, handle_id, initialRefcount, ownership, data) {
1303
+ return ReferenceWithData.create(envObject, handle_id, initialRefcount, ownership, data);
1304
+ }
1305
+ createReferenceWithFinalizer(envObject, handle_id, initialRefcount, ownership, finalize_callback = 0, finalize_data = 0, finalize_hint = 0) {
1306
+ return ReferenceWithFinalizer.create(envObject, handle_id, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint);
1256
1307
  }
1257
1308
  createDeferred(value) {
1258
1309
  return Deferred.create(this, value);
@@ -1330,4 +1381,4 @@ function getDefaultContext() {
1330
1381
  return defaultContext;
1331
1382
  }
1332
1383
 
1333
- export { CallbackInfo, CallbackInfoStack, ConstHandle, Context, Deferred, EmnapiError, Env, Finalizer, Handle, HandleScope, HandleStore, NAPI_VERSION_EXPERIMENTAL, NODE_API_DEFAULT_MODULE_API_VERSION, NODE_API_SUPPORTED_VERSION_MAX, NODE_API_SUPPORTED_VERSION_MIN, NodeEnv, NotSupportBufferError, NotSupportWeakRefError, Persistent, RefBase, RefTracker, Reference, ScopeStore, Store, TrackedFinalizer, TryCatch, createContext, getDefaultContext, isReferenceType, version };
1384
+ 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 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@emnapi/runtime",
3
- "version": "1.2.0",
3
+ "version": "1.3.1",
4
4
  "description": "emnapi runtime",
5
5
  "main": "index.js",
6
6
  "module": "./dist/emnapi.esm-bundler.js",