node-addon-api 2.0.2 → 3.1.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.
Files changed (82) hide show
  1. package/.clang-format +111 -0
  2. package/.github/workflows/ci.yml +55 -0
  3. package/.github/workflows/linter.yml +24 -0
  4. package/.github/workflows/stale.yml +18 -0
  5. package/.travis.yml +1 -5
  6. package/CHANGELOG.md +237 -23
  7. package/README.md +101 -31
  8. package/appveyor.yml +3 -14
  9. package/benchmark/README.md +47 -0
  10. package/benchmark/binding.gyp +25 -0
  11. package/benchmark/function_args.cc +217 -0
  12. package/benchmark/function_args.js +60 -0
  13. package/benchmark/index.js +34 -0
  14. package/benchmark/property_descriptor.cc +91 -0
  15. package/benchmark/property_descriptor.js +37 -0
  16. package/common.gypi +21 -0
  17. package/doc/addon.md +157 -0
  18. package/doc/array.md +81 -0
  19. package/doc/array_buffer.md +20 -0
  20. package/doc/async_context.md +1 -1
  21. package/doc/async_worker.md +34 -5
  22. package/doc/{async_progress_worker.md → async_worker_variants.md} +236 -23
  23. package/doc/bigint.md +7 -2
  24. package/doc/boolean.md +5 -1
  25. package/doc/buffer.md +4 -0
  26. package/doc/checker-tool.md +1 -1
  27. package/doc/class_property_descriptor.md +3 -3
  28. package/doc/creating_a_release.md +6 -6
  29. package/doc/dataview.md +4 -0
  30. package/doc/date.md +2 -2
  31. package/doc/env.md +69 -0
  32. package/doc/error.md +5 -0
  33. package/doc/escapable_handle_scope.md +1 -1
  34. package/doc/external.md +4 -0
  35. package/doc/function.md +111 -3
  36. package/doc/function_reference.md +1 -1
  37. package/doc/handle_scope.md +1 -1
  38. package/doc/hierarchy.md +91 -0
  39. package/doc/instance_wrap.md +408 -0
  40. package/doc/name.md +29 -0
  41. package/doc/number.md +1 -1
  42. package/doc/object.md +44 -1
  43. package/doc/object_lifetime_management.md +2 -2
  44. package/doc/object_reference.md +1 -1
  45. package/doc/object_wrap.md +220 -216
  46. package/doc/prebuild_tools.md +2 -2
  47. package/doc/promises.md +5 -0
  48. package/doc/property_descriptor.md +67 -12
  49. package/doc/setup.md +1 -2
  50. package/doc/string.md +5 -1
  51. package/doc/symbol.md +5 -1
  52. package/doc/threadsafe.md +121 -0
  53. package/doc/threadsafe_function.md +16 -46
  54. package/doc/typed_array.md +4 -0
  55. package/doc/typed_array_of.md +4 -0
  56. package/doc/typed_threadsafe_function.md +307 -0
  57. package/doc/value.md +166 -104
  58. package/doc/version_management.md +2 -2
  59. package/except.gypi +16 -0
  60. package/index.js +7 -41
  61. package/napi-inl.h +1685 -464
  62. package/napi.h +606 -141
  63. package/node_api.gyp +9 -0
  64. package/noexcept.gypi +16 -0
  65. package/{src/nothing.c → nothing.c} +0 -0
  66. package/package-support.json +21 -0
  67. package/package.json +106 -2
  68. package/tools/README.md +12 -6
  69. package/tools/clang-format.js +47 -0
  70. package/tools/conversion.js +4 -8
  71. package/doc/Doxyfile +0 -2450
  72. package/doc/basic_types.md +0 -423
  73. package/doc/working_with_javascript_values.md +0 -14
  74. package/external-napi/node_api.h +0 -7
  75. package/src/node_api.cc +0 -3655
  76. package/src/node_api.gyp +0 -21
  77. package/src/node_api.h +0 -588
  78. package/src/node_api_types.h +0 -115
  79. package/src/node_internals.cc +0 -142
  80. package/src/node_internals.h +0 -157
  81. package/src/util-inl.h +0 -38
  82. package/src/util.h +0 -7
package/napi.h CHANGED
@@ -91,6 +91,13 @@ static_assert(sizeof(char16_t) == sizeof(wchar_t), "Size mismatch between char16
91
91
 
92
92
  #endif // NAPI_CPP_EXCEPTIONS
93
93
 
94
+ # define NAPI_DISALLOW_ASSIGN(CLASS) void operator=(const CLASS&) = delete;
95
+ # define NAPI_DISALLOW_COPY(CLASS) CLASS(const CLASS&) = delete;
96
+
97
+ #define NAPI_DISALLOW_ASSIGN_COPY(CLASS) \
98
+ NAPI_DISALLOW_ASSIGN(CLASS) \
99
+ NAPI_DISALLOW_COPY(CLASS)
100
+
94
101
  #define NAPI_FATAL_IF_FAILED(status, location, message) \
95
102
  do { \
96
103
  if ((status) != napi_ok) { \
@@ -112,24 +119,20 @@ namespace Napi {
112
119
  class Value;
113
120
  class Boolean;
114
121
  class Number;
115
- // Currently experimental guard with the definition of NAPI_EXPERIMENTAL.
116
- // Once it is no longer experimental guard with the NAPI_VERSION in which it is
117
- // released instead.
118
- #ifdef NAPI_EXPERIMENTAL
122
+ #if NAPI_VERSION > 5
119
123
  class BigInt;
120
- #endif // NAPI_EXPERIMENTAL
124
+ #endif // NAPI_VERSION > 5
121
125
  #if (NAPI_VERSION > 4)
122
126
  class Date;
123
127
  #endif
124
128
  class String;
125
129
  class Object;
126
130
  class Array;
131
+ class ArrayBuffer;
127
132
  class Function;
128
- template <typename T> class Buffer;
129
133
  class Error;
130
134
  class PropertyDescriptor;
131
135
  class CallbackInfo;
132
- template <typename T> class Reference;
133
136
  class TypedArray;
134
137
  template <typename T> class TypedArrayOf;
135
138
 
@@ -141,13 +144,10 @@ namespace Napi {
141
144
  typedef TypedArrayOf<uint32_t> Uint32Array; ///< Typed-array of unsigned 32-bit integers
142
145
  typedef TypedArrayOf<float> Float32Array; ///< Typed-array of 32-bit floating-point values
143
146
  typedef TypedArrayOf<double> Float64Array; ///< Typed-array of 64-bit floating-point values
144
- // Currently experimental guard with the definition of NAPI_EXPERIMENTAL.
145
- // Once it is no longer experimental guard with the NAPI_VERSION in which it is
146
- // released instead.
147
- #ifdef NAPI_EXPERIMENTAL
147
+ #if NAPI_VERSION > 5
148
148
  typedef TypedArrayOf<int64_t> BigInt64Array; ///< Typed array of signed 64-bit integers
149
149
  typedef TypedArrayOf<uint64_t> BigUint64Array; ///< Typed array of unsigned 64-bit integers
150
- #endif // NAPI_EXPERIMENTAL
150
+ #endif // NAPI_VERSION > 5
151
151
 
152
152
  /// Defines the signature of a N-API C++ module's registration callback (init) function.
153
153
  typedef Object (*ModuleRegisterCallback)(Env env, Object exports);
@@ -166,6 +166,12 @@ namespace Napi {
166
166
  ///
167
167
  /// In the V8 JavaScript engine, a N-API environment approximately corresponds to an Isolate.
168
168
  class Env {
169
+ #if NAPI_VERSION > 5
170
+ private:
171
+ template <typename T> static void DefaultFini(Env, T* data);
172
+ template <typename DataType, typename HintType>
173
+ static void DefaultFiniWithHint(Env, DataType* data, HintType* hint);
174
+ #endif // NAPI_VERSION > 5
169
175
  public:
170
176
  Env(napi_env env);
171
177
 
@@ -178,6 +184,26 @@ namespace Napi {
178
184
  bool IsExceptionPending() const;
179
185
  Error GetAndClearPendingException();
180
186
 
187
+ Value RunScript(const char* utf8script);
188
+ Value RunScript(const std::string& utf8script);
189
+ Value RunScript(String script);
190
+
191
+ #if NAPI_VERSION > 5
192
+ template <typename T> T* GetInstanceData();
193
+
194
+ template <typename T> using Finalizer = void (*)(Env, T*);
195
+ template <typename T, Finalizer<T> fini = Env::DefaultFini<T>>
196
+ void SetInstanceData(T* data);
197
+
198
+ template <typename DataType, typename HintType>
199
+ using FinalizerWithHint = void (*)(Env, DataType*, HintType*);
200
+ template <typename DataType,
201
+ typename HintType,
202
+ FinalizerWithHint<DataType, HintType> fini =
203
+ Env::DefaultFiniWithHint<DataType, HintType>>
204
+ void SetInstanceData(DataType* data, HintType* hint);
205
+ #endif // NAPI_VERSION > 5
206
+
181
207
  private:
182
208
  napi_env _env;
183
209
  };
@@ -247,12 +273,9 @@ namespace Napi {
247
273
  bool IsNull() const; ///< Tests if a value is a null JavaScript value.
248
274
  bool IsBoolean() const; ///< Tests if a value is a JavaScript boolean.
249
275
  bool IsNumber() const; ///< Tests if a value is a JavaScript number.
250
- // Currently experimental guard with the definition of NAPI_EXPERIMENTAL.
251
- // Once it is no longer experimental guard with the NAPI_VERSION in which it is
252
- // released instead.
253
- #ifdef NAPI_EXPERIMENTAL
276
+ #if NAPI_VERSION > 5
254
277
  bool IsBigInt() const; ///< Tests if a value is a JavaScript bigint.
255
- #endif // NAPI_EXPERIMENTAL
278
+ #endif // NAPI_VERSION > 5
256
279
  #if (NAPI_VERSION > 4)
257
280
  bool IsDate() const; ///< Tests if a value is a JavaScript date.
258
281
  #endif
@@ -325,10 +348,7 @@ namespace Napi {
325
348
  double DoubleValue() const; ///< Converts a Number value to a 64-bit floating-point value.
326
349
  };
327
350
 
328
- // Currently experimental guard with the definition of NAPI_EXPERIMENTAL.
329
- // Once it is no longer experimental guard with the NAPI_VERSION in which it is
330
- // released instead.
331
- #ifdef NAPI_EXPERIMENTAL
351
+ #if NAPI_VERSION > 5
332
352
  /// A JavaScript bigint value.
333
353
  class BigInt : public Value {
334
354
  public:
@@ -367,7 +387,7 @@ namespace Napi {
367
387
  /// be needed to store this BigInt (i.e. the return value of `WordCount()`).
368
388
  void ToWords(int* sign_bit, size_t* word_count, uint64_t* words);
369
389
  };
370
- #endif // NAPI_EXPERIMENTAL
390
+ #endif // NAPI_VERSION > 5
371
391
 
372
392
  #if (NAPI_VERSION > 4)
373
393
  /// A JavaScript date value.
@@ -803,12 +823,10 @@ namespace Napi {
803
823
  void* Data(); ///< Gets a pointer to the data buffer.
804
824
  size_t ByteLength(); ///< Gets the length of the array buffer in bytes.
805
825
 
806
- private:
807
- mutable void* _data;
808
- mutable size_t _length;
809
-
810
- ArrayBuffer(napi_env env, napi_value value, void* data, size_t length);
811
- void EnsureInfo() const;
826
+ #if NAPI_VERSION >= 7
827
+ bool IsDetached() const;
828
+ void Detach();
829
+ #endif // NAPI_VERSION >= 7
812
830
  };
813
831
 
814
832
  /// A JavaScript typed-array value with unknown array type.
@@ -856,13 +874,10 @@ namespace Napi {
856
874
  : std::is_same<T, uint32_t>::value ? napi_uint32_array
857
875
  : std::is_same<T, float>::value ? napi_float32_array
858
876
  : std::is_same<T, double>::value ? napi_float64_array
859
- // Currently experimental guard with the definition of NAPI_EXPERIMENTAL.
860
- // Once it is no longer experimental guard with the NAPI_VERSION in which it is
861
- // released instead.
862
- #ifdef NAPI_EXPERIMENTAL
877
+ #if NAPI_VERSION > 5
863
878
  : std::is_same<T, int64_t>::value ? napi_bigint64_array
864
879
  : std::is_same<T, uint64_t>::value ? napi_biguint64_array
865
- #endif // NAPI_EXPERIMENTAL
880
+ #endif // NAPI_VERSION > 5
866
881
  : unknown_array_type;
867
882
  }
868
883
  /// !endcond
@@ -993,6 +1008,29 @@ namespace Napi {
993
1008
 
994
1009
  class Function : public Object {
995
1010
  public:
1011
+ typedef void (*VoidCallback)(const CallbackInfo& info);
1012
+ typedef Value (*Callback)(const CallbackInfo& info);
1013
+
1014
+ template <VoidCallback cb>
1015
+ static Function New(napi_env env,
1016
+ const char* utf8name = nullptr,
1017
+ void* data = nullptr);
1018
+
1019
+ template <Callback cb>
1020
+ static Function New(napi_env env,
1021
+ const char* utf8name = nullptr,
1022
+ void* data = nullptr);
1023
+
1024
+ template <VoidCallback cb>
1025
+ static Function New(napi_env env,
1026
+ const std::string& utf8name,
1027
+ void* data = nullptr);
1028
+
1029
+ template <Callback cb>
1030
+ static Function New(napi_env env,
1031
+ const std::string& utf8name,
1032
+ void* data = nullptr);
1033
+
996
1034
  /// Callable must implement operator() accepting a const CallbackInfo&
997
1035
  /// and return either void or Value.
998
1036
  template <typename Callable>
@@ -1108,7 +1146,7 @@ namespace Napi {
1108
1146
  // A reference can be moved but cannot be copied.
1109
1147
  Reference(Reference<T>&& other);
1110
1148
  Reference<T>& operator =(Reference<T>&& other);
1111
- Reference<T>& operator =(Reference<T>&) = delete;
1149
+ NAPI_DISALLOW_ASSIGN(Reference<T>)
1112
1150
 
1113
1151
  operator napi_ref() const;
1114
1152
  bool operator ==(const Reference<T> &other) const;
@@ -1153,7 +1191,7 @@ namespace Napi {
1153
1191
  ObjectReference& operator =(Reference<Object>&& other);
1154
1192
  ObjectReference(ObjectReference&& other);
1155
1193
  ObjectReference& operator =(ObjectReference&& other);
1156
- ObjectReference& operator =(ObjectReference&) = delete;
1194
+ NAPI_DISALLOW_ASSIGN(ObjectReference)
1157
1195
 
1158
1196
  Napi::Value Get(const char* utf8name) const;
1159
1197
  Napi::Value Get(const std::string& utf8name) const;
@@ -1190,8 +1228,7 @@ namespace Napi {
1190
1228
  FunctionReference& operator =(Reference<Function>&& other);
1191
1229
  FunctionReference(FunctionReference&& other);
1192
1230
  FunctionReference& operator =(FunctionReference&& other);
1193
- FunctionReference(const FunctionReference&) = delete;
1194
- FunctionReference& operator =(FunctionReference&) = delete;
1231
+ NAPI_DISALLOW_ASSIGN_COPY(FunctionReference)
1195
1232
 
1196
1233
  Napi::Value operator ()(const std::initializer_list<napi_value>& args) const;
1197
1234
 
@@ -1333,7 +1370,7 @@ namespace Napi {
1333
1370
  Error(Error&& other);
1334
1371
  Error& operator =(Error&& other);
1335
1372
  Error(const Error&);
1336
- Error& operator =(Error&);
1373
+ Error& operator =(const Error&);
1337
1374
 
1338
1375
  const std::string& Message() const NAPI_NOEXCEPT;
1339
1376
  void ThrowAsJavaScriptException() const;
@@ -1381,8 +1418,7 @@ namespace Napi {
1381
1418
  ~CallbackInfo();
1382
1419
 
1383
1420
  // Disallow copying to prevent multiple free of _dynamicArgs
1384
- CallbackInfo(CallbackInfo const &) = delete;
1385
- void operator=(CallbackInfo const &) = delete;
1421
+ NAPI_DISALLOW_ASSIGN_COPY(CallbackInfo)
1386
1422
 
1387
1423
  Napi::Env Env() const;
1388
1424
  Value NewTarget() const;
@@ -1407,6 +1443,9 @@ namespace Napi {
1407
1443
 
1408
1444
  class PropertyDescriptor {
1409
1445
  public:
1446
+ typedef Napi::Value (*GetterCallback)(const Napi::CallbackInfo& info);
1447
+ typedef void (*SetterCallback)(const Napi::CallbackInfo& info);
1448
+
1410
1449
  #ifndef NODE_ADDON_API_DISABLE_DEPRECATED
1411
1450
  template <typename Getter>
1412
1451
  static PropertyDescriptor Accessor(const char* utf8name,
@@ -1474,6 +1513,36 @@ namespace Napi {
1474
1513
  void* data = nullptr);
1475
1514
  #endif // !NODE_ADDON_API_DISABLE_DEPRECATED
1476
1515
 
1516
+ template <GetterCallback Getter>
1517
+ static PropertyDescriptor Accessor(const char* utf8name,
1518
+ napi_property_attributes attributes = napi_default,
1519
+ void* data = nullptr);
1520
+
1521
+ template <GetterCallback Getter>
1522
+ static PropertyDescriptor Accessor(const std::string& utf8name,
1523
+ napi_property_attributes attributes = napi_default,
1524
+ void* data = nullptr);
1525
+
1526
+ template <GetterCallback Getter>
1527
+ static PropertyDescriptor Accessor(Name name,
1528
+ napi_property_attributes attributes = napi_default,
1529
+ void* data = nullptr);
1530
+
1531
+ template <GetterCallback Getter, SetterCallback Setter>
1532
+ static PropertyDescriptor Accessor(const char* utf8name,
1533
+ napi_property_attributes attributes = napi_default,
1534
+ void* data = nullptr);
1535
+
1536
+ template <GetterCallback Getter, SetterCallback Setter>
1537
+ static PropertyDescriptor Accessor(const std::string& utf8name,
1538
+ napi_property_attributes attributes = napi_default,
1539
+ void* data = nullptr);
1540
+
1541
+ template <GetterCallback Getter, SetterCallback Setter>
1542
+ static PropertyDescriptor Accessor(Name name,
1543
+ napi_property_attributes attributes = napi_default,
1544
+ void* data = nullptr);
1545
+
1477
1546
  template <typename Getter>
1478
1547
  static PropertyDescriptor Accessor(Napi::Env env,
1479
1548
  Napi::Object object,
@@ -1579,6 +1648,114 @@ namespace Napi {
1579
1648
  napi_property_descriptor _desc;
1580
1649
  };
1581
1650
 
1651
+ template <typename T, typename TCallback>
1652
+ struct MethodCallbackData {
1653
+ TCallback callback;
1654
+ void* data;
1655
+ };
1656
+
1657
+ template <typename T, typename TGetterCallback, typename TSetterCallback>
1658
+ struct AccessorCallbackData {
1659
+ TGetterCallback getterCallback;
1660
+ TSetterCallback setterCallback;
1661
+ void* data;
1662
+ };
1663
+
1664
+ template <typename T>
1665
+ class InstanceWrap {
1666
+ public:
1667
+
1668
+ typedef void (T::*InstanceVoidMethodCallback)(const CallbackInfo& info);
1669
+ typedef Napi::Value (T::*InstanceMethodCallback)(const CallbackInfo& info);
1670
+ typedef Napi::Value (T::*InstanceGetterCallback)(const CallbackInfo& info);
1671
+ typedef void (T::*InstanceSetterCallback)(const CallbackInfo& info, const Napi::Value& value);
1672
+
1673
+ typedef ClassPropertyDescriptor<T> PropertyDescriptor;
1674
+
1675
+ static PropertyDescriptor InstanceMethod(const char* utf8name,
1676
+ InstanceVoidMethodCallback method,
1677
+ napi_property_attributes attributes = napi_default,
1678
+ void* data = nullptr);
1679
+ static PropertyDescriptor InstanceMethod(const char* utf8name,
1680
+ InstanceMethodCallback method,
1681
+ napi_property_attributes attributes = napi_default,
1682
+ void* data = nullptr);
1683
+ static PropertyDescriptor InstanceMethod(Symbol name,
1684
+ InstanceVoidMethodCallback method,
1685
+ napi_property_attributes attributes = napi_default,
1686
+ void* data = nullptr);
1687
+ static PropertyDescriptor InstanceMethod(Symbol name,
1688
+ InstanceMethodCallback method,
1689
+ napi_property_attributes attributes = napi_default,
1690
+ void* data = nullptr);
1691
+ template <InstanceVoidMethodCallback method>
1692
+ static PropertyDescriptor InstanceMethod(const char* utf8name,
1693
+ napi_property_attributes attributes = napi_default,
1694
+ void* data = nullptr);
1695
+ template <InstanceMethodCallback method>
1696
+ static PropertyDescriptor InstanceMethod(const char* utf8name,
1697
+ napi_property_attributes attributes = napi_default,
1698
+ void* data = nullptr);
1699
+ template <InstanceVoidMethodCallback method>
1700
+ static PropertyDescriptor InstanceMethod(Symbol name,
1701
+ napi_property_attributes attributes = napi_default,
1702
+ void* data = nullptr);
1703
+ template <InstanceMethodCallback method>
1704
+ static PropertyDescriptor InstanceMethod(Symbol name,
1705
+ napi_property_attributes attributes = napi_default,
1706
+ void* data = nullptr);
1707
+ static PropertyDescriptor InstanceAccessor(const char* utf8name,
1708
+ InstanceGetterCallback getter,
1709
+ InstanceSetterCallback setter,
1710
+ napi_property_attributes attributes = napi_default,
1711
+ void* data = nullptr);
1712
+ static PropertyDescriptor InstanceAccessor(Symbol name,
1713
+ InstanceGetterCallback getter,
1714
+ InstanceSetterCallback setter,
1715
+ napi_property_attributes attributes = napi_default,
1716
+ void* data = nullptr);
1717
+ template <InstanceGetterCallback getter, InstanceSetterCallback setter=nullptr>
1718
+ static PropertyDescriptor InstanceAccessor(const char* utf8name,
1719
+ napi_property_attributes attributes = napi_default,
1720
+ void* data = nullptr);
1721
+ template <InstanceGetterCallback getter, InstanceSetterCallback setter=nullptr>
1722
+ static PropertyDescriptor InstanceAccessor(Symbol name,
1723
+ napi_property_attributes attributes = napi_default,
1724
+ void* data = nullptr);
1725
+ static PropertyDescriptor InstanceValue(const char* utf8name,
1726
+ Napi::Value value,
1727
+ napi_property_attributes attributes = napi_default);
1728
+ static PropertyDescriptor InstanceValue(Symbol name,
1729
+ Napi::Value value,
1730
+ napi_property_attributes attributes = napi_default);
1731
+
1732
+ protected:
1733
+ static void AttachPropData(napi_env env, napi_value value, const napi_property_descriptor* prop);
1734
+
1735
+ private:
1736
+ using This = InstanceWrap<T>;
1737
+
1738
+ typedef MethodCallbackData<T, InstanceVoidMethodCallback> InstanceVoidMethodCallbackData;
1739
+ typedef MethodCallbackData<T, InstanceMethodCallback> InstanceMethodCallbackData;
1740
+ typedef AccessorCallbackData<T,
1741
+ InstanceGetterCallback,
1742
+ InstanceSetterCallback> InstanceAccessorCallbackData;
1743
+
1744
+ static napi_value InstanceVoidMethodCallbackWrapper(napi_env env, napi_callback_info info);
1745
+ static napi_value InstanceMethodCallbackWrapper(napi_env env, napi_callback_info info);
1746
+ static napi_value InstanceGetterCallbackWrapper(napi_env env, napi_callback_info info);
1747
+ static napi_value InstanceSetterCallbackWrapper(napi_env env, napi_callback_info info);
1748
+
1749
+ template <InstanceSetterCallback method>
1750
+ static napi_value WrappedMethod(napi_env env, napi_callback_info info) noexcept;
1751
+
1752
+ template <InstanceSetterCallback setter> struct SetterTag {};
1753
+
1754
+ template <InstanceSetterCallback setter>
1755
+ static napi_callback WrapSetter(SetterTag<setter>) noexcept { return &This::WrappedMethod<setter>; }
1756
+ static napi_callback WrapSetter(SetterTag<nullptr>) noexcept { return nullptr; }
1757
+ };
1758
+
1582
1759
  /// Base class to be extended by C++ classes exposed to JavaScript; each C++ class instance gets
1583
1760
  /// "wrapped" by a JavaScript object that is managed by this class.
1584
1761
  ///
@@ -1593,8 +1770,8 @@ namespace Napi {
1593
1770
  /// public:
1594
1771
  /// static void Initialize(Napi::Env& env, Napi::Object& target) {
1595
1772
  /// Napi::Function constructor = DefineClass(env, "Example", {
1596
- /// InstanceAccessor("value", &Example::GetSomething, &Example::SetSomething),
1597
- /// InstanceMethod("doSomething", &Example::DoSomething),
1773
+ /// InstanceAccessor<&Example::GetSomething, &Example::SetSomething>("value"),
1774
+ /// InstanceMethod<&Example::DoSomething>("doSomething"),
1598
1775
  /// });
1599
1776
  /// target.Set("Example", constructor);
1600
1777
  /// }
@@ -1605,7 +1782,7 @@ namespace Napi {
1605
1782
  /// Napi::Value DoSomething(const Napi::CallbackInfo& info);
1606
1783
  /// }
1607
1784
  template <typename T>
1608
- class ObjectWrap : public Reference<Object> {
1785
+ class ObjectWrap : public InstanceWrap<T>, public Reference<Object> {
1609
1786
  public:
1610
1787
  ObjectWrap(const CallbackInfo& callbackInfo);
1611
1788
  virtual ~ObjectWrap();
@@ -1617,10 +1794,6 @@ namespace Napi {
1617
1794
  typedef Napi::Value (*StaticMethodCallback)(const CallbackInfo& info);
1618
1795
  typedef Napi::Value (*StaticGetterCallback)(const CallbackInfo& info);
1619
1796
  typedef void (*StaticSetterCallback)(const CallbackInfo& info, const Napi::Value& value);
1620
- typedef void (T::*InstanceVoidMethodCallback)(const CallbackInfo& info);
1621
- typedef Napi::Value (T::*InstanceMethodCallback)(const CallbackInfo& info);
1622
- typedef Napi::Value (T::*InstanceGetterCallback)(const CallbackInfo& info);
1623
- typedef void (T::*InstanceSetterCallback)(const CallbackInfo& info, const Napi::Value& value);
1624
1797
 
1625
1798
  typedef ClassPropertyDescriptor<T> PropertyDescriptor;
1626
1799
 
@@ -1648,6 +1821,22 @@ namespace Napi {
1648
1821
  StaticMethodCallback method,
1649
1822
  napi_property_attributes attributes = napi_default,
1650
1823
  void* data = nullptr);
1824
+ template <StaticVoidMethodCallback method>
1825
+ static PropertyDescriptor StaticMethod(const char* utf8name,
1826
+ napi_property_attributes attributes = napi_default,
1827
+ void* data = nullptr);
1828
+ template <StaticVoidMethodCallback method>
1829
+ static PropertyDescriptor StaticMethod(Symbol name,
1830
+ napi_property_attributes attributes = napi_default,
1831
+ void* data = nullptr);
1832
+ template <StaticMethodCallback method>
1833
+ static PropertyDescriptor StaticMethod(const char* utf8name,
1834
+ napi_property_attributes attributes = napi_default,
1835
+ void* data = nullptr);
1836
+ template <StaticMethodCallback method>
1837
+ static PropertyDescriptor StaticMethod(Symbol name,
1838
+ napi_property_attributes attributes = napi_default,
1839
+ void* data = nullptr);
1651
1840
  static PropertyDescriptor StaticAccessor(const char* utf8name,
1652
1841
  StaticGetterCallback getter,
1653
1842
  StaticSetterCallback setter,
@@ -1658,56 +1847,30 @@ namespace Napi {
1658
1847
  StaticSetterCallback setter,
1659
1848
  napi_property_attributes attributes = napi_default,
1660
1849
  void* data = nullptr);
1661
- static PropertyDescriptor InstanceMethod(const char* utf8name,
1662
- InstanceVoidMethodCallback method,
1663
- napi_property_attributes attributes = napi_default,
1664
- void* data = nullptr);
1665
- static PropertyDescriptor InstanceMethod(const char* utf8name,
1666
- InstanceMethodCallback method,
1667
- napi_property_attributes attributes = napi_default,
1668
- void* data = nullptr);
1669
- static PropertyDescriptor InstanceMethod(Symbol name,
1670
- InstanceVoidMethodCallback method,
1850
+ template <StaticGetterCallback getter, StaticSetterCallback setter=nullptr>
1851
+ static PropertyDescriptor StaticAccessor(const char* utf8name,
1671
1852
  napi_property_attributes attributes = napi_default,
1672
1853
  void* data = nullptr);
1673
- static PropertyDescriptor InstanceMethod(Symbol name,
1674
- InstanceMethodCallback method,
1854
+ template <StaticGetterCallback getter, StaticSetterCallback setter=nullptr>
1855
+ static PropertyDescriptor StaticAccessor(Symbol name,
1675
1856
  napi_property_attributes attributes = napi_default,
1676
1857
  void* data = nullptr);
1677
- static PropertyDescriptor InstanceAccessor(const char* utf8name,
1678
- InstanceGetterCallback getter,
1679
- InstanceSetterCallback setter,
1680
- napi_property_attributes attributes = napi_default,
1681
- void* data = nullptr);
1682
- static PropertyDescriptor InstanceAccessor(Symbol name,
1683
- InstanceGetterCallback getter,
1684
- InstanceSetterCallback setter,
1685
- napi_property_attributes attributes = napi_default,
1686
- void* data = nullptr);
1687
1858
  static PropertyDescriptor StaticValue(const char* utf8name,
1688
1859
  Napi::Value value,
1689
1860
  napi_property_attributes attributes = napi_default);
1690
1861
  static PropertyDescriptor StaticValue(Symbol name,
1691
1862
  Napi::Value value,
1692
1863
  napi_property_attributes attributes = napi_default);
1693
- static PropertyDescriptor InstanceValue(const char* utf8name,
1694
- Napi::Value value,
1695
- napi_property_attributes attributes = napi_default);
1696
- static PropertyDescriptor InstanceValue(Symbol name,
1697
- Napi::Value value,
1698
- napi_property_attributes attributes = napi_default);
1699
1864
  virtual void Finalize(Napi::Env env);
1700
1865
 
1701
1866
  private:
1867
+ using This = ObjectWrap<T>;
1868
+
1702
1869
  static napi_value ConstructorCallbackWrapper(napi_env env, napi_callback_info info);
1703
1870
  static napi_value StaticVoidMethodCallbackWrapper(napi_env env, napi_callback_info info);
1704
1871
  static napi_value StaticMethodCallbackWrapper(napi_env env, napi_callback_info info);
1705
1872
  static napi_value StaticGetterCallbackWrapper(napi_env env, napi_callback_info info);
1706
1873
  static napi_value StaticSetterCallbackWrapper(napi_env env, napi_callback_info info);
1707
- static napi_value InstanceVoidMethodCallbackWrapper(napi_env env, napi_callback_info info);
1708
- static napi_value InstanceMethodCallbackWrapper(napi_env env, napi_callback_info info);
1709
- static napi_value InstanceGetterCallbackWrapper(napi_env env, napi_callback_info info);
1710
- static napi_value InstanceSetterCallbackWrapper(napi_env env, napi_callback_info info);
1711
1874
  static void FinalizeCallback(napi_env env, void* data, void* hint);
1712
1875
  static Function DefineClass(Napi::Env env,
1713
1876
  const char* utf8name,
@@ -1715,26 +1878,21 @@ namespace Napi {
1715
1878
  const napi_property_descriptor* props,
1716
1879
  void* data = nullptr);
1717
1880
 
1718
- template <typename TCallback>
1719
- struct MethodCallbackData {
1720
- TCallback callback;
1721
- void* data;
1722
- };
1723
- typedef MethodCallbackData<StaticVoidMethodCallback> StaticVoidMethodCallbackData;
1724
- typedef MethodCallbackData<StaticMethodCallback> StaticMethodCallbackData;
1725
- typedef MethodCallbackData<InstanceVoidMethodCallback> InstanceVoidMethodCallbackData;
1726
- typedef MethodCallbackData<InstanceMethodCallback> InstanceMethodCallbackData;
1727
-
1728
- template <typename TGetterCallback, typename TSetterCallback>
1729
- struct AccessorCallbackData {
1730
- TGetterCallback getterCallback;
1731
- TSetterCallback setterCallback;
1732
- void* data;
1733
- };
1734
- typedef AccessorCallbackData<StaticGetterCallback, StaticSetterCallback>
1735
- StaticAccessorCallbackData;
1736
- typedef AccessorCallbackData<InstanceGetterCallback, InstanceSetterCallback>
1737
- InstanceAccessorCallbackData;
1881
+ typedef MethodCallbackData<T, StaticVoidMethodCallback> StaticVoidMethodCallbackData;
1882
+ typedef MethodCallbackData<T, StaticMethodCallback> StaticMethodCallbackData;
1883
+
1884
+ typedef AccessorCallbackData<T,
1885
+ StaticGetterCallback,
1886
+ StaticSetterCallback> StaticAccessorCallbackData;
1887
+
1888
+ template <StaticSetterCallback method>
1889
+ static napi_value WrappedMethod(napi_env env, napi_callback_info info) noexcept;
1890
+
1891
+ template <StaticSetterCallback setter> struct StaticSetterTag {};
1892
+
1893
+ template <StaticSetterCallback setter>
1894
+ static napi_callback WrapStaticSetter(StaticSetterTag<setter>) noexcept { return &This::WrappedMethod<setter>; }
1895
+ static napi_callback WrapStaticSetter(StaticSetterTag<nullptr>) noexcept { return nullptr; }
1738
1896
 
1739
1897
  bool _construction_failed = true;
1740
1898
  };
@@ -1746,8 +1904,7 @@ namespace Napi {
1746
1904
  ~HandleScope();
1747
1905
 
1748
1906
  // Disallow copying to prevent double close of napi_handle_scope
1749
- HandleScope(HandleScope const &) = delete;
1750
- void operator=(HandleScope const &) = delete;
1907
+ NAPI_DISALLOW_ASSIGN_COPY(HandleScope)
1751
1908
 
1752
1909
  operator napi_handle_scope() const;
1753
1910
 
@@ -1765,8 +1922,7 @@ namespace Napi {
1765
1922
  ~EscapableHandleScope();
1766
1923
 
1767
1924
  // Disallow copying to prevent double close of napi_escapable_handle_scope
1768
- EscapableHandleScope(EscapableHandleScope const &) = delete;
1769
- void operator=(EscapableHandleScope const &) = delete;
1925
+ NAPI_DISALLOW_ASSIGN_COPY(EscapableHandleScope)
1770
1926
 
1771
1927
  operator napi_escapable_handle_scope() const;
1772
1928
 
@@ -1786,8 +1942,7 @@ namespace Napi {
1786
1942
  virtual ~CallbackScope();
1787
1943
 
1788
1944
  // Disallow copying to prevent double close of napi_callback_scope
1789
- CallbackScope(CallbackScope const &) = delete;
1790
- void operator=(CallbackScope const &) = delete;
1945
+ NAPI_DISALLOW_ASSIGN_COPY(CallbackScope)
1791
1946
 
1792
1947
  operator napi_callback_scope() const;
1793
1948
 
@@ -1807,8 +1962,7 @@ namespace Napi {
1807
1962
 
1808
1963
  AsyncContext(AsyncContext&& other);
1809
1964
  AsyncContext& operator =(AsyncContext&& other);
1810
- AsyncContext(const AsyncContext&) = delete;
1811
- AsyncContext& operator =(AsyncContext&) = delete;
1965
+ NAPI_DISALLOW_ASSIGN_COPY(AsyncContext)
1812
1966
 
1813
1967
  operator napi_async_context() const;
1814
1968
 
@@ -1826,8 +1980,7 @@ namespace Napi {
1826
1980
  // An async worker can be moved but cannot be copied.
1827
1981
  AsyncWorker(AsyncWorker&& other);
1828
1982
  AsyncWorker& operator =(AsyncWorker&& other);
1829
- AsyncWorker(const AsyncWorker&) = delete;
1830
- AsyncWorker& operator =(AsyncWorker&) = delete;
1983
+ NAPI_DISALLOW_ASSIGN_COPY(AsyncWorker)
1831
1984
 
1832
1985
  operator napi_async_work() const;
1833
1986
 
@@ -1840,6 +1993,10 @@ namespace Napi {
1840
1993
  ObjectReference& Receiver();
1841
1994
  FunctionReference& Callback();
1842
1995
 
1996
+ virtual void OnExecute(Napi::Env env);
1997
+ virtual void OnWorkComplete(Napi::Env env,
1998
+ napi_status status);
1999
+
1843
2000
  protected:
1844
2001
  explicit AsyncWorker(const Function& callback);
1845
2002
  explicit AsyncWorker(const Function& callback,
@@ -1873,10 +2030,10 @@ namespace Napi {
1873
2030
  void SetError(const std::string& error);
1874
2031
 
1875
2032
  private:
1876
- static void OnExecute(napi_env env, void* this_pointer);
1877
- static void OnWorkComplete(napi_env env,
1878
- napi_status status,
1879
- void* this_pointer);
2033
+ static inline void OnAsyncWorkExecute(napi_env env, void* asyncworker);
2034
+ static inline void OnAsyncWorkComplete(napi_env env,
2035
+ napi_status status,
2036
+ void* asyncworker);
1880
2037
 
1881
2038
  napi_env _env;
1882
2039
  napi_async_work _work;
@@ -1886,7 +2043,7 @@ namespace Napi {
1886
2043
  bool _suppress_destruct;
1887
2044
  };
1888
2045
 
1889
- #if (NAPI_VERSION > 3)
2046
+ #if (NAPI_VERSION > 3 && !defined(__wasm32__))
1890
2047
  class ThreadSafeFunction {
1891
2048
  public:
1892
2049
  // This API may only be called from the main thread.
@@ -2092,8 +2249,245 @@ namespace Napi {
2092
2249
  napi_threadsafe_function _tsfn;
2093
2250
  };
2094
2251
 
2252
+ // A TypedThreadSafeFunction by default has no context (nullptr) and can
2253
+ // accept any type (void) to its CallJs.
2254
+ template <typename ContextType = std::nullptr_t,
2255
+ typename DataType = void,
2256
+ void (*CallJs)(Napi::Env, Napi::Function, ContextType*, DataType*) =
2257
+ nullptr>
2258
+ class TypedThreadSafeFunction {
2259
+ public:
2260
+ // This API may only be called from the main thread.
2261
+ // Helper function that returns nullptr if running N-API 5+, otherwise a
2262
+ // non-empty, no-op Function. This provides the ability to specify at
2263
+ // compile-time a callback parameter to `New` that safely does no action
2264
+ // when targeting _any_ N-API version.
2265
+ #if NAPI_VERSION > 4
2266
+ static std::nullptr_t EmptyFunctionFactory(Napi::Env env);
2267
+ #else
2268
+ static Napi::Function EmptyFunctionFactory(Napi::Env env);
2269
+ #endif
2270
+ static Napi::Function FunctionOrEmpty(Napi::Env env,
2271
+ Napi::Function& callback);
2272
+
2273
+ #if NAPI_VERSION > 4
2274
+ // This API may only be called from the main thread.
2275
+ // Creates a new threadsafe function with:
2276
+ // Callback [missing] Resource [missing] Finalizer [missing]
2277
+ template <typename ResourceString>
2278
+ static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
2279
+ napi_env env,
2280
+ ResourceString resourceName,
2281
+ size_t maxQueueSize,
2282
+ size_t initialThreadCount,
2283
+ ContextType* context = nullptr);
2284
+
2285
+ // This API may only be called from the main thread.
2286
+ // Creates a new threadsafe function with:
2287
+ // Callback [missing] Resource [passed] Finalizer [missing]
2288
+ template <typename ResourceString>
2289
+ static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
2290
+ napi_env env,
2291
+ const Object& resource,
2292
+ ResourceString resourceName,
2293
+ size_t maxQueueSize,
2294
+ size_t initialThreadCount,
2295
+ ContextType* context = nullptr);
2296
+
2297
+ // This API may only be called from the main thread.
2298
+ // Creates a new threadsafe function with:
2299
+ // Callback [missing] Resource [missing] Finalizer [passed]
2300
+ template <typename ResourceString,
2301
+ typename Finalizer,
2302
+ typename FinalizerDataType = void>
2303
+ static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
2304
+ napi_env env,
2305
+ ResourceString resourceName,
2306
+ size_t maxQueueSize,
2307
+ size_t initialThreadCount,
2308
+ ContextType* context,
2309
+ Finalizer finalizeCallback,
2310
+ FinalizerDataType* data = nullptr);
2311
+
2312
+ // This API may only be called from the main thread.
2313
+ // Creates a new threadsafe function with:
2314
+ // Callback [missing] Resource [passed] Finalizer [passed]
2315
+ template <typename ResourceString,
2316
+ typename Finalizer,
2317
+ typename FinalizerDataType = void>
2318
+ static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
2319
+ napi_env env,
2320
+ const Object& resource,
2321
+ ResourceString resourceName,
2322
+ size_t maxQueueSize,
2323
+ size_t initialThreadCount,
2324
+ ContextType* context,
2325
+ Finalizer finalizeCallback,
2326
+ FinalizerDataType* data = nullptr);
2327
+ #endif
2328
+
2329
+ // This API may only be called from the main thread.
2330
+ // Creates a new threadsafe function with:
2331
+ // Callback [passed] Resource [missing] Finalizer [missing]
2332
+ template <typename ResourceString>
2333
+ static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
2334
+ napi_env env,
2335
+ const Function& callback,
2336
+ ResourceString resourceName,
2337
+ size_t maxQueueSize,
2338
+ size_t initialThreadCount,
2339
+ ContextType* context = nullptr);
2340
+
2341
+ // This API may only be called from the main thread.
2342
+ // Creates a new threadsafe function with:
2343
+ // Callback [passed] Resource [passed] Finalizer [missing]
2344
+ template <typename ResourceString>
2345
+ static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
2346
+ napi_env env,
2347
+ const Function& callback,
2348
+ const Object& resource,
2349
+ ResourceString resourceName,
2350
+ size_t maxQueueSize,
2351
+ size_t initialThreadCount,
2352
+ ContextType* context = nullptr);
2353
+
2354
+ // This API may only be called from the main thread.
2355
+ // Creates a new threadsafe function with:
2356
+ // Callback [passed] Resource [missing] Finalizer [passed]
2357
+ template <typename ResourceString,
2358
+ typename Finalizer,
2359
+ typename FinalizerDataType = void>
2360
+ static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
2361
+ napi_env env,
2362
+ const Function& callback,
2363
+ ResourceString resourceName,
2364
+ size_t maxQueueSize,
2365
+ size_t initialThreadCount,
2366
+ ContextType* context,
2367
+ Finalizer finalizeCallback,
2368
+ FinalizerDataType* data = nullptr);
2369
+
2370
+ // This API may only be called from the main thread.
2371
+ // Creates a new threadsafe function with:
2372
+ // Callback [passed] Resource [passed] Finalizer [passed]
2373
+ template <typename CallbackType,
2374
+ typename ResourceString,
2375
+ typename Finalizer,
2376
+ typename FinalizerDataType>
2377
+ static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
2378
+ napi_env env,
2379
+ CallbackType callback,
2380
+ const Object& resource,
2381
+ ResourceString resourceName,
2382
+ size_t maxQueueSize,
2383
+ size_t initialThreadCount,
2384
+ ContextType* context,
2385
+ Finalizer finalizeCallback,
2386
+ FinalizerDataType* data = nullptr);
2387
+
2388
+ TypedThreadSafeFunction<ContextType, DataType, CallJs>();
2389
+ TypedThreadSafeFunction<ContextType, DataType, CallJs>(
2390
+ napi_threadsafe_function tsFunctionValue);
2391
+
2392
+ operator napi_threadsafe_function() const;
2393
+
2394
+ // This API may be called from any thread.
2395
+ napi_status BlockingCall(DataType* data = nullptr) const;
2396
+
2397
+ // This API may be called from any thread.
2398
+ napi_status NonBlockingCall(DataType* data = nullptr) const;
2399
+
2400
+ // This API may only be called from the main thread.
2401
+ void Ref(napi_env env) const;
2402
+
2403
+ // This API may only be called from the main thread.
2404
+ void Unref(napi_env env) const;
2405
+
2406
+ // This API may be called from any thread.
2407
+ napi_status Acquire() const;
2408
+
2409
+ // This API may be called from any thread.
2410
+ napi_status Release();
2411
+
2412
+ // This API may be called from any thread.
2413
+ napi_status Abort();
2414
+
2415
+ // This API may be called from any thread.
2416
+ ContextType* GetContext() const;
2417
+
2418
+ private:
2419
+ template <typename ResourceString,
2420
+ typename Finalizer,
2421
+ typename FinalizerDataType>
2422
+ static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
2423
+ napi_env env,
2424
+ const Function& callback,
2425
+ const Object& resource,
2426
+ ResourceString resourceName,
2427
+ size_t maxQueueSize,
2428
+ size_t initialThreadCount,
2429
+ ContextType* context,
2430
+ Finalizer finalizeCallback,
2431
+ FinalizerDataType* data,
2432
+ napi_finalize wrapper);
2433
+
2434
+ static void CallJsInternal(napi_env env,
2435
+ napi_value jsCallback,
2436
+ void* context,
2437
+ void* data);
2438
+
2439
+ protected:
2440
+ napi_threadsafe_function _tsfn;
2441
+ };
2442
+ template <typename DataType>
2443
+ class AsyncProgressWorkerBase : public AsyncWorker {
2444
+ public:
2445
+ virtual void OnWorkProgress(DataType* data) = 0;
2446
+ class ThreadSafeData {
2447
+ public:
2448
+ ThreadSafeData(AsyncProgressWorkerBase* asyncprogressworker, DataType* data)
2449
+ : _asyncprogressworker(asyncprogressworker), _data(data) {}
2450
+
2451
+ AsyncProgressWorkerBase* asyncprogressworker() { return _asyncprogressworker; };
2452
+ DataType* data() { return _data; };
2453
+
2454
+ private:
2455
+ AsyncProgressWorkerBase* _asyncprogressworker;
2456
+ DataType* _data;
2457
+ };
2458
+ void OnWorkComplete(Napi::Env env, napi_status status) override;
2459
+ protected:
2460
+ explicit AsyncProgressWorkerBase(const Object& receiver,
2461
+ const Function& callback,
2462
+ const char* resource_name,
2463
+ const Object& resource,
2464
+ size_t queue_size = 1);
2465
+ virtual ~AsyncProgressWorkerBase();
2466
+
2467
+ // Optional callback of Napi::ThreadSafeFunction only available after NAPI_VERSION 4.
2468
+ // Refs: https://github.com/nodejs/node/pull/27791
2469
+ #if NAPI_VERSION > 4
2470
+ explicit AsyncProgressWorkerBase(Napi::Env env,
2471
+ const char* resource_name,
2472
+ const Object& resource,
2473
+ size_t queue_size = 1);
2474
+ #endif
2475
+
2476
+ static inline void OnAsyncWorkProgress(Napi::Env env,
2477
+ Napi::Function jsCallback,
2478
+ void* data);
2479
+
2480
+ napi_status NonBlockingCall(DataType* data);
2481
+
2482
+ private:
2483
+ ThreadSafeFunction _tsfn;
2484
+ bool _work_completed = false;
2485
+ napi_status _complete_status;
2486
+ static inline void OnThreadSafeFunctionFinalize(Napi::Env env, void* data, AsyncProgressWorkerBase* context);
2487
+ };
2488
+
2095
2489
  template<class T>
2096
- class AsyncProgressWorker : public AsyncWorker {
2490
+ class AsyncProgressWorker : public AsyncProgressWorkerBase<void> {
2097
2491
  public:
2098
2492
  virtual ~AsyncProgressWorker();
2099
2493
 
@@ -2107,40 +2501,39 @@ namespace Napi {
2107
2501
  AsyncProgressWorker* const _worker;
2108
2502
  };
2109
2503
 
2504
+ void OnWorkProgress(void*) override;
2505
+
2110
2506
  protected:
2111
- explicit AsyncProgressWorker(const Function& callback);
2112
- explicit AsyncProgressWorker(const Function& callback,
2113
- const char* resource_name);
2114
- explicit AsyncProgressWorker(const Function& callback,
2115
- const char* resource_name,
2116
- const Object& resource);
2117
- explicit AsyncProgressWorker(const Object& receiver,
2118
- const Function& callback);
2119
- explicit AsyncProgressWorker(const Object& receiver,
2120
- const Function& callback,
2121
- const char* resource_name);
2122
- explicit AsyncProgressWorker(const Object& receiver,
2123
- const Function& callback,
2124
- const char* resource_name,
2125
- const Object& resource);
2507
+ explicit AsyncProgressWorker(const Function& callback);
2508
+ explicit AsyncProgressWorker(const Function& callback,
2509
+ const char* resource_name);
2510
+ explicit AsyncProgressWorker(const Function& callback,
2511
+ const char* resource_name,
2512
+ const Object& resource);
2513
+ explicit AsyncProgressWorker(const Object& receiver,
2514
+ const Function& callback);
2515
+ explicit AsyncProgressWorker(const Object& receiver,
2516
+ const Function& callback,
2517
+ const char* resource_name);
2518
+ explicit AsyncProgressWorker(const Object& receiver,
2519
+ const Function& callback,
2520
+ const char* resource_name,
2521
+ const Object& resource);
2126
2522
 
2127
2523
  // Optional callback of Napi::ThreadSafeFunction only available after NAPI_VERSION 4.
2128
2524
  // Refs: https://github.com/nodejs/node/pull/27791
2129
2525
  #if NAPI_VERSION > 4
2130
- explicit AsyncProgressWorker(Napi::Env env);
2131
- explicit AsyncProgressWorker(Napi::Env env,
2132
- const char* resource_name);
2133
- explicit AsyncProgressWorker(Napi::Env env,
2134
- const char* resource_name,
2135
- const Object& resource);
2526
+ explicit AsyncProgressWorker(Napi::Env env);
2527
+ explicit AsyncProgressWorker(Napi::Env env,
2528
+ const char* resource_name);
2529
+ explicit AsyncProgressWorker(Napi::Env env,
2530
+ const char* resource_name,
2531
+ const Object& resource);
2136
2532
  #endif
2137
-
2138
2533
  virtual void Execute(const ExecutionProgress& progress) = 0;
2139
2534
  virtual void OnProgress(const T* data, size_t count) = 0;
2140
2535
 
2141
2536
  private:
2142
- static void WorkProgress_(Napi::Env env, Napi::Function jsCallback, void* data);
2143
-
2144
2537
  void Execute() override;
2145
2538
  void Signal() const;
2146
2539
  void SendProgress_(const T* data, size_t count);
@@ -2148,9 +2541,62 @@ namespace Napi {
2148
2541
  std::mutex _mutex;
2149
2542
  T* _asyncdata;
2150
2543
  size_t _asyncsize;
2151
- ThreadSafeFunction _tsfn;
2152
2544
  };
2153
- #endif
2545
+
2546
+ template<class T>
2547
+ class AsyncProgressQueueWorker : public AsyncProgressWorkerBase<std::pair<T*, size_t>> {
2548
+ public:
2549
+ virtual ~AsyncProgressQueueWorker() {};
2550
+
2551
+ class ExecutionProgress {
2552
+ friend class AsyncProgressQueueWorker;
2553
+ public:
2554
+ void Signal() const;
2555
+ void Send(const T* data, size_t count) const;
2556
+ private:
2557
+ explicit ExecutionProgress(AsyncProgressQueueWorker* worker) : _worker(worker) {}
2558
+ AsyncProgressQueueWorker* const _worker;
2559
+ };
2560
+
2561
+ void OnWorkComplete(Napi::Env env, napi_status status) override;
2562
+ void OnWorkProgress(std::pair<T*, size_t>*) override;
2563
+
2564
+ protected:
2565
+ explicit AsyncProgressQueueWorker(const Function& callback);
2566
+ explicit AsyncProgressQueueWorker(const Function& callback,
2567
+ const char* resource_name);
2568
+ explicit AsyncProgressQueueWorker(const Function& callback,
2569
+ const char* resource_name,
2570
+ const Object& resource);
2571
+ explicit AsyncProgressQueueWorker(const Object& receiver,
2572
+ const Function& callback);
2573
+ explicit AsyncProgressQueueWorker(const Object& receiver,
2574
+ const Function& callback,
2575
+ const char* resource_name);
2576
+ explicit AsyncProgressQueueWorker(const Object& receiver,
2577
+ const Function& callback,
2578
+ const char* resource_name,
2579
+ const Object& resource);
2580
+
2581
+ // Optional callback of Napi::ThreadSafeFunction only available after NAPI_VERSION 4.
2582
+ // Refs: https://github.com/nodejs/node/pull/27791
2583
+ #if NAPI_VERSION > 4
2584
+ explicit AsyncProgressQueueWorker(Napi::Env env);
2585
+ explicit AsyncProgressQueueWorker(Napi::Env env,
2586
+ const char* resource_name);
2587
+ explicit AsyncProgressQueueWorker(Napi::Env env,
2588
+ const char* resource_name,
2589
+ const Object& resource);
2590
+ #endif
2591
+ virtual void Execute(const ExecutionProgress& progress) = 0;
2592
+ virtual void OnProgress(const T* data, size_t count) = 0;
2593
+
2594
+ private:
2595
+ void Execute() override;
2596
+ void Signal() const;
2597
+ void SendProgress_(const T* data, size_t count);
2598
+ };
2599
+ #endif // NAPI_VERSION > 3 && !defined(__wasm32__)
2154
2600
 
2155
2601
  // Memory management.
2156
2602
  class MemoryManagement {
@@ -2165,6 +2611,25 @@ namespace Napi {
2165
2611
  static const napi_node_version* GetNodeVersion(Env env);
2166
2612
  };
2167
2613
 
2614
+ #if NAPI_VERSION > 5
2615
+ template <typename T>
2616
+ class Addon : public InstanceWrap<T> {
2617
+ public:
2618
+ static inline Object Init(Env env, Object exports);
2619
+ static T* Unwrap(Object wrapper);
2620
+
2621
+ protected:
2622
+ typedef ClassPropertyDescriptor<T> AddonProp;
2623
+ void DefineAddon(Object exports,
2624
+ const std::initializer_list<AddonProp>& props);
2625
+ Napi::Object DefineProperties(Object object,
2626
+ const std::initializer_list<AddonProp>& props);
2627
+
2628
+ private:
2629
+ Object entry_point_;
2630
+ };
2631
+ #endif // NAPI_VERSION > 5
2632
+
2168
2633
  } // namespace Napi
2169
2634
 
2170
2635
  // Inline implementations of all the above class methods are included here.