node-addon-api 2.0.2 → 3.0.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 (45) hide show
  1. package/.travis.yml +1 -4
  2. package/CHANGELOG.md +88 -30
  3. package/README.md +53 -7
  4. package/benchmark/README.md +47 -0
  5. package/benchmark/binding.gyp +25 -0
  6. package/benchmark/function_args.cc +153 -0
  7. package/benchmark/function_args.js +52 -0
  8. package/benchmark/index.js +34 -0
  9. package/benchmark/property_descriptor.cc +60 -0
  10. package/benchmark/property_descriptor.js +29 -0
  11. package/common.gypi +21 -0
  12. package/doc/async_worker.md +33 -4
  13. package/doc/{async_progress_worker.md → async_worker_variants.md} +115 -3
  14. package/doc/bigint.md +2 -1
  15. package/doc/class_property_descriptor.md +3 -3
  16. package/doc/creating_a_release.md +5 -5
  17. package/doc/env.md +14 -0
  18. package/doc/function.md +108 -1
  19. package/doc/object.md +40 -1
  20. package/doc/object_lifetime_management.md +1 -1
  21. package/doc/object_wrap.md +278 -2
  22. package/doc/property_descriptor.md +64 -9
  23. package/doc/setup.md +0 -1
  24. package/doc/string.md +1 -1
  25. package/doc/symbol.md +1 -1
  26. package/doc/value.md +1 -1
  27. package/except.gypi +16 -0
  28. package/index.js +5 -42
  29. package/napi-inl.h +727 -141
  30. package/napi.h +338 -83
  31. package/node_api.gyp +9 -0
  32. package/noexcept.gypi +16 -0
  33. package/{src/nothing.c → nothing.c} +0 -0
  34. package/package.json +33 -1
  35. package/tools/README.md +4 -4
  36. package/tools/conversion.js +0 -4
  37. package/external-napi/node_api.h +0 -7
  38. package/src/node_api.cc +0 -3655
  39. package/src/node_api.gyp +0 -21
  40. package/src/node_api.h +0 -588
  41. package/src/node_api_types.h +0 -115
  42. package/src/node_internals.cc +0 -142
  43. package/src/node_internals.h +0 -157
  44. package/src/util-inl.h +0 -38
  45. 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.
@@ -802,13 +822,6 @@ namespace Napi {
802
822
 
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
-
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;
812
825
  };
813
826
 
814
827
  /// A JavaScript typed-array value with unknown array type.
@@ -856,13 +869,10 @@ namespace Napi {
856
869
  : std::is_same<T, uint32_t>::value ? napi_uint32_array
857
870
  : std::is_same<T, float>::value ? napi_float32_array
858
871
  : 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
872
+ #if NAPI_VERSION > 5
863
873
  : std::is_same<T, int64_t>::value ? napi_bigint64_array
864
874
  : std::is_same<T, uint64_t>::value ? napi_biguint64_array
865
- #endif // NAPI_EXPERIMENTAL
875
+ #endif // NAPI_VERSION > 5
866
876
  : unknown_array_type;
867
877
  }
868
878
  /// !endcond
@@ -993,6 +1003,29 @@ namespace Napi {
993
1003
 
994
1004
  class Function : public Object {
995
1005
  public:
1006
+ typedef void (*VoidCallback)(const CallbackInfo& info);
1007
+ typedef Value (*Callback)(const CallbackInfo& info);
1008
+
1009
+ template <VoidCallback cb>
1010
+ static Function New(napi_env env,
1011
+ const char* utf8name = nullptr,
1012
+ void* data = nullptr);
1013
+
1014
+ template <Callback cb>
1015
+ static Function New(napi_env env,
1016
+ const char* utf8name = nullptr,
1017
+ void* data = nullptr);
1018
+
1019
+ template <VoidCallback cb>
1020
+ static Function New(napi_env env,
1021
+ const std::string& utf8name,
1022
+ void* data = nullptr);
1023
+
1024
+ template <Callback cb>
1025
+ static Function New(napi_env env,
1026
+ const std::string& utf8name,
1027
+ void* data = nullptr);
1028
+
996
1029
  /// Callable must implement operator() accepting a const CallbackInfo&
997
1030
  /// and return either void or Value.
998
1031
  template <typename Callable>
@@ -1108,7 +1141,7 @@ namespace Napi {
1108
1141
  // A reference can be moved but cannot be copied.
1109
1142
  Reference(Reference<T>&& other);
1110
1143
  Reference<T>& operator =(Reference<T>&& other);
1111
- Reference<T>& operator =(Reference<T>&) = delete;
1144
+ NAPI_DISALLOW_ASSIGN(Reference<T>)
1112
1145
 
1113
1146
  operator napi_ref() const;
1114
1147
  bool operator ==(const Reference<T> &other) const;
@@ -1153,7 +1186,7 @@ namespace Napi {
1153
1186
  ObjectReference& operator =(Reference<Object>&& other);
1154
1187
  ObjectReference(ObjectReference&& other);
1155
1188
  ObjectReference& operator =(ObjectReference&& other);
1156
- ObjectReference& operator =(ObjectReference&) = delete;
1189
+ NAPI_DISALLOW_ASSIGN(ObjectReference)
1157
1190
 
1158
1191
  Napi::Value Get(const char* utf8name) const;
1159
1192
  Napi::Value Get(const std::string& utf8name) const;
@@ -1190,8 +1223,7 @@ namespace Napi {
1190
1223
  FunctionReference& operator =(Reference<Function>&& other);
1191
1224
  FunctionReference(FunctionReference&& other);
1192
1225
  FunctionReference& operator =(FunctionReference&& other);
1193
- FunctionReference(const FunctionReference&) = delete;
1194
- FunctionReference& operator =(FunctionReference&) = delete;
1226
+ NAPI_DISALLOW_ASSIGN_COPY(FunctionReference)
1195
1227
 
1196
1228
  Napi::Value operator ()(const std::initializer_list<napi_value>& args) const;
1197
1229
 
@@ -1333,7 +1365,7 @@ namespace Napi {
1333
1365
  Error(Error&& other);
1334
1366
  Error& operator =(Error&& other);
1335
1367
  Error(const Error&);
1336
- Error& operator =(Error&);
1368
+ Error& operator =(const Error&);
1337
1369
 
1338
1370
  const std::string& Message() const NAPI_NOEXCEPT;
1339
1371
  void ThrowAsJavaScriptException() const;
@@ -1381,8 +1413,7 @@ namespace Napi {
1381
1413
  ~CallbackInfo();
1382
1414
 
1383
1415
  // Disallow copying to prevent multiple free of _dynamicArgs
1384
- CallbackInfo(CallbackInfo const &) = delete;
1385
- void operator=(CallbackInfo const &) = delete;
1416
+ NAPI_DISALLOW_ASSIGN_COPY(CallbackInfo)
1386
1417
 
1387
1418
  Napi::Env Env() const;
1388
1419
  Value NewTarget() const;
@@ -1407,6 +1438,9 @@ namespace Napi {
1407
1438
 
1408
1439
  class PropertyDescriptor {
1409
1440
  public:
1441
+ typedef Napi::Value (*GetterCallback)(const Napi::CallbackInfo& info);
1442
+ typedef void (*SetterCallback)(const Napi::CallbackInfo& info);
1443
+
1410
1444
  #ifndef NODE_ADDON_API_DISABLE_DEPRECATED
1411
1445
  template <typename Getter>
1412
1446
  static PropertyDescriptor Accessor(const char* utf8name,
@@ -1474,6 +1508,36 @@ namespace Napi {
1474
1508
  void* data = nullptr);
1475
1509
  #endif // !NODE_ADDON_API_DISABLE_DEPRECATED
1476
1510
 
1511
+ template <GetterCallback Getter>
1512
+ static PropertyDescriptor Accessor(const char* utf8name,
1513
+ napi_property_attributes attributes = napi_default,
1514
+ void* data = nullptr);
1515
+
1516
+ template <GetterCallback Getter>
1517
+ static PropertyDescriptor Accessor(const std::string& utf8name,
1518
+ napi_property_attributes attributes = napi_default,
1519
+ void* data = nullptr);
1520
+
1521
+ template <GetterCallback Getter>
1522
+ static PropertyDescriptor Accessor(Name name,
1523
+ napi_property_attributes attributes = napi_default,
1524
+ void* data = nullptr);
1525
+
1526
+ template <GetterCallback Getter, SetterCallback Setter>
1527
+ static PropertyDescriptor Accessor(const char* utf8name,
1528
+ napi_property_attributes attributes = napi_default,
1529
+ void* data = nullptr);
1530
+
1531
+ template <GetterCallback Getter, SetterCallback Setter>
1532
+ static PropertyDescriptor Accessor(const std::string& utf8name,
1533
+ napi_property_attributes attributes = napi_default,
1534
+ void* data = nullptr);
1535
+
1536
+ template <GetterCallback Getter, SetterCallback Setter>
1537
+ static PropertyDescriptor Accessor(Name name,
1538
+ napi_property_attributes attributes = napi_default,
1539
+ void* data = nullptr);
1540
+
1477
1541
  template <typename Getter>
1478
1542
  static PropertyDescriptor Accessor(Napi::Env env,
1479
1543
  Napi::Object object,
@@ -1559,6 +1623,10 @@ namespace Napi {
1559
1623
  operator const napi_property_descriptor&() const;
1560
1624
 
1561
1625
  private:
1626
+ template <GetterCallback Getter>
1627
+ static napi_value GetterCallbackWrapper(napi_env env, napi_callback_info info);
1628
+ template <SetterCallback Setter>
1629
+ static napi_value SetterCallbackWrapper(napi_env env, napi_callback_info info);
1562
1630
  napi_property_descriptor _desc;
1563
1631
  };
1564
1632
 
@@ -1593,8 +1661,8 @@ namespace Napi {
1593
1661
  /// public:
1594
1662
  /// static void Initialize(Napi::Env& env, Napi::Object& target) {
1595
1663
  /// Napi::Function constructor = DefineClass(env, "Example", {
1596
- /// InstanceAccessor("value", &Example::GetSomething, &Example::SetSomething),
1597
- /// InstanceMethod("doSomething", &Example::DoSomething),
1664
+ /// InstanceAccessor<&Example::GetSomething, &Example::SetSomething>("value"),
1665
+ /// InstanceMethod<&Example::DoSomething>("doSomething"),
1598
1666
  /// });
1599
1667
  /// target.Set("Example", constructor);
1600
1668
  /// }
@@ -1648,6 +1716,22 @@ namespace Napi {
1648
1716
  StaticMethodCallback method,
1649
1717
  napi_property_attributes attributes = napi_default,
1650
1718
  void* data = nullptr);
1719
+ template <StaticVoidMethodCallback method>
1720
+ static PropertyDescriptor StaticMethod(const char* utf8name,
1721
+ napi_property_attributes attributes = napi_default,
1722
+ void* data = nullptr);
1723
+ template <StaticVoidMethodCallback method>
1724
+ static PropertyDescriptor StaticMethod(Symbol name,
1725
+ napi_property_attributes attributes = napi_default,
1726
+ void* data = nullptr);
1727
+ template <StaticMethodCallback method>
1728
+ static PropertyDescriptor StaticMethod(const char* utf8name,
1729
+ napi_property_attributes attributes = napi_default,
1730
+ void* data = nullptr);
1731
+ template <StaticMethodCallback method>
1732
+ static PropertyDescriptor StaticMethod(Symbol name,
1733
+ napi_property_attributes attributes = napi_default,
1734
+ void* data = nullptr);
1651
1735
  static PropertyDescriptor StaticAccessor(const char* utf8name,
1652
1736
  StaticGetterCallback getter,
1653
1737
  StaticSetterCallback setter,
@@ -1658,6 +1742,14 @@ namespace Napi {
1658
1742
  StaticSetterCallback setter,
1659
1743
  napi_property_attributes attributes = napi_default,
1660
1744
  void* data = nullptr);
1745
+ template <StaticGetterCallback getter, StaticSetterCallback setter=nullptr>
1746
+ static PropertyDescriptor StaticAccessor(const char* utf8name,
1747
+ napi_property_attributes attributes = napi_default,
1748
+ void* data = nullptr);
1749
+ template <StaticGetterCallback getter, StaticSetterCallback setter=nullptr>
1750
+ static PropertyDescriptor StaticAccessor(Symbol name,
1751
+ napi_property_attributes attributes = napi_default,
1752
+ void* data = nullptr);
1661
1753
  static PropertyDescriptor InstanceMethod(const char* utf8name,
1662
1754
  InstanceVoidMethodCallback method,
1663
1755
  napi_property_attributes attributes = napi_default,
@@ -1674,6 +1766,22 @@ namespace Napi {
1674
1766
  InstanceMethodCallback method,
1675
1767
  napi_property_attributes attributes = napi_default,
1676
1768
  void* data = nullptr);
1769
+ template <InstanceVoidMethodCallback method>
1770
+ static PropertyDescriptor InstanceMethod(const char* utf8name,
1771
+ napi_property_attributes attributes = napi_default,
1772
+ void* data = nullptr);
1773
+ template <InstanceMethodCallback method>
1774
+ static PropertyDescriptor InstanceMethod(const char* utf8name,
1775
+ napi_property_attributes attributes = napi_default,
1776
+ void* data = nullptr);
1777
+ template <InstanceVoidMethodCallback method>
1778
+ static PropertyDescriptor InstanceMethod(Symbol name,
1779
+ napi_property_attributes attributes = napi_default,
1780
+ void* data = nullptr);
1781
+ template <InstanceMethodCallback method>
1782
+ static PropertyDescriptor InstanceMethod(Symbol name,
1783
+ napi_property_attributes attributes = napi_default,
1784
+ void* data = nullptr);
1677
1785
  static PropertyDescriptor InstanceAccessor(const char* utf8name,
1678
1786
  InstanceGetterCallback getter,
1679
1787
  InstanceSetterCallback setter,
@@ -1684,6 +1792,14 @@ namespace Napi {
1684
1792
  InstanceSetterCallback setter,
1685
1793
  napi_property_attributes attributes = napi_default,
1686
1794
  void* data = nullptr);
1795
+ template <InstanceGetterCallback getter, InstanceSetterCallback setter=nullptr>
1796
+ static PropertyDescriptor InstanceAccessor(const char* utf8name,
1797
+ napi_property_attributes attributes = napi_default,
1798
+ void* data = nullptr);
1799
+ template <InstanceGetterCallback getter, InstanceSetterCallback setter=nullptr>
1800
+ static PropertyDescriptor InstanceAccessor(Symbol name,
1801
+ napi_property_attributes attributes = napi_default,
1802
+ void* data = nullptr);
1687
1803
  static PropertyDescriptor StaticValue(const char* utf8name,
1688
1804
  Napi::Value value,
1689
1805
  napi_property_attributes attributes = napi_default);
@@ -1699,6 +1815,8 @@ namespace Napi {
1699
1815
  virtual void Finalize(Napi::Env env);
1700
1816
 
1701
1817
  private:
1818
+ using This = ObjectWrap<T>;
1819
+
1702
1820
  static napi_value ConstructorCallbackWrapper(napi_env env, napi_callback_info info);
1703
1821
  static napi_value StaticVoidMethodCallbackWrapper(napi_env env, napi_callback_info info);
1704
1822
  static napi_value StaticMethodCallbackWrapper(napi_env env, napi_callback_info info);
@@ -1736,6 +1854,45 @@ namespace Napi {
1736
1854
  typedef AccessorCallbackData<InstanceGetterCallback, InstanceSetterCallback>
1737
1855
  InstanceAccessorCallbackData;
1738
1856
 
1857
+ template <StaticVoidMethodCallback method>
1858
+ static napi_value WrappedMethod(napi_env env, napi_callback_info info) noexcept;
1859
+
1860
+ template <StaticMethodCallback method>
1861
+ static napi_value WrappedMethod(napi_env env, napi_callback_info info) noexcept;
1862
+
1863
+ template <InstanceVoidMethodCallback method>
1864
+ static napi_value WrappedMethod(napi_env env, napi_callback_info info) noexcept;
1865
+
1866
+ template <InstanceMethodCallback method>
1867
+ static napi_value WrappedMethod(napi_env env, napi_callback_info info) noexcept;
1868
+
1869
+ template <StaticSetterCallback method>
1870
+ static napi_value WrappedMethod(napi_env env, napi_callback_info info) noexcept;
1871
+
1872
+ template <InstanceSetterCallback method>
1873
+ static napi_value WrappedMethod(napi_env env, napi_callback_info info) noexcept;
1874
+
1875
+ template <StaticGetterCallback getter> struct StaticGetterTag {};
1876
+ template <StaticSetterCallback setter> struct StaticSetterTag {};
1877
+ template <InstanceGetterCallback getter> struct GetterTag {};
1878
+ template <InstanceSetterCallback setter> struct SetterTag {};
1879
+
1880
+ template <StaticGetterCallback getter>
1881
+ static napi_callback WrapStaticGetter(StaticGetterTag<getter>) noexcept { return &This::WrappedMethod<getter>; }
1882
+ static napi_callback WrapStaticGetter(StaticGetterTag<nullptr>) noexcept { return nullptr; }
1883
+
1884
+ template <StaticSetterCallback setter>
1885
+ static napi_callback WrapStaticSetter(StaticSetterTag<setter>) noexcept { return &This::WrappedMethod<setter>; }
1886
+ static napi_callback WrapStaticSetter(StaticSetterTag<nullptr>) noexcept { return nullptr; }
1887
+
1888
+ template <InstanceGetterCallback getter>
1889
+ static napi_callback WrapGetter(GetterTag<getter>) noexcept { return &This::WrappedMethod<getter>; }
1890
+ static napi_callback WrapGetter(GetterTag<nullptr>) noexcept { return nullptr; }
1891
+
1892
+ template <InstanceSetterCallback setter>
1893
+ static napi_callback WrapSetter(SetterTag<setter>) noexcept { return &This::WrappedMethod<setter>; }
1894
+ static napi_callback WrapSetter(SetterTag<nullptr>) noexcept { return nullptr; }
1895
+
1739
1896
  bool _construction_failed = true;
1740
1897
  };
1741
1898
 
@@ -1746,8 +1903,7 @@ namespace Napi {
1746
1903
  ~HandleScope();
1747
1904
 
1748
1905
  // Disallow copying to prevent double close of napi_handle_scope
1749
- HandleScope(HandleScope const &) = delete;
1750
- void operator=(HandleScope const &) = delete;
1906
+ NAPI_DISALLOW_ASSIGN_COPY(HandleScope)
1751
1907
 
1752
1908
  operator napi_handle_scope() const;
1753
1909
 
@@ -1765,8 +1921,7 @@ namespace Napi {
1765
1921
  ~EscapableHandleScope();
1766
1922
 
1767
1923
  // Disallow copying to prevent double close of napi_escapable_handle_scope
1768
- EscapableHandleScope(EscapableHandleScope const &) = delete;
1769
- void operator=(EscapableHandleScope const &) = delete;
1924
+ NAPI_DISALLOW_ASSIGN_COPY(EscapableHandleScope)
1770
1925
 
1771
1926
  operator napi_escapable_handle_scope() const;
1772
1927
 
@@ -1786,8 +1941,7 @@ namespace Napi {
1786
1941
  virtual ~CallbackScope();
1787
1942
 
1788
1943
  // Disallow copying to prevent double close of napi_callback_scope
1789
- CallbackScope(CallbackScope const &) = delete;
1790
- void operator=(CallbackScope const &) = delete;
1944
+ NAPI_DISALLOW_ASSIGN_COPY(CallbackScope)
1791
1945
 
1792
1946
  operator napi_callback_scope() const;
1793
1947
 
@@ -1807,8 +1961,7 @@ namespace Napi {
1807
1961
 
1808
1962
  AsyncContext(AsyncContext&& other);
1809
1963
  AsyncContext& operator =(AsyncContext&& other);
1810
- AsyncContext(const AsyncContext&) = delete;
1811
- AsyncContext& operator =(AsyncContext&) = delete;
1964
+ NAPI_DISALLOW_ASSIGN_COPY(AsyncContext)
1812
1965
 
1813
1966
  operator napi_async_context() const;
1814
1967
 
@@ -1826,8 +1979,7 @@ namespace Napi {
1826
1979
  // An async worker can be moved but cannot be copied.
1827
1980
  AsyncWorker(AsyncWorker&& other);
1828
1981
  AsyncWorker& operator =(AsyncWorker&& other);
1829
- AsyncWorker(const AsyncWorker&) = delete;
1830
- AsyncWorker& operator =(AsyncWorker&) = delete;
1982
+ NAPI_DISALLOW_ASSIGN_COPY(AsyncWorker)
1831
1983
 
1832
1984
  operator napi_async_work() const;
1833
1985
 
@@ -1840,6 +1992,10 @@ namespace Napi {
1840
1992
  ObjectReference& Receiver();
1841
1993
  FunctionReference& Callback();
1842
1994
 
1995
+ virtual void OnExecute(Napi::Env env);
1996
+ virtual void OnWorkComplete(Napi::Env env,
1997
+ napi_status status);
1998
+
1843
1999
  protected:
1844
2000
  explicit AsyncWorker(const Function& callback);
1845
2001
  explicit AsyncWorker(const Function& callback,
@@ -1873,10 +2029,10 @@ namespace Napi {
1873
2029
  void SetError(const std::string& error);
1874
2030
 
1875
2031
  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);
2032
+ static inline void OnAsyncWorkExecute(napi_env env, void* asyncworker);
2033
+ static inline void OnAsyncWorkComplete(napi_env env,
2034
+ napi_status status,
2035
+ void* asyncworker);
1880
2036
 
1881
2037
  napi_env _env;
1882
2038
  napi_async_work _work;
@@ -2092,8 +2248,55 @@ namespace Napi {
2092
2248
  napi_threadsafe_function _tsfn;
2093
2249
  };
2094
2250
 
2251
+ template <typename DataType>
2252
+ class AsyncProgressWorkerBase : public AsyncWorker {
2253
+ public:
2254
+ virtual void OnWorkProgress(DataType* data) = 0;
2255
+ class ThreadSafeData {
2256
+ public:
2257
+ ThreadSafeData(AsyncProgressWorkerBase* asyncprogressworker, DataType* data)
2258
+ : _asyncprogressworker(asyncprogressworker), _data(data) {}
2259
+
2260
+ AsyncProgressWorkerBase* asyncprogressworker() { return _asyncprogressworker; };
2261
+ DataType* data() { return _data; };
2262
+
2263
+ private:
2264
+ AsyncProgressWorkerBase* _asyncprogressworker;
2265
+ DataType* _data;
2266
+ };
2267
+ void OnWorkComplete(Napi::Env env, napi_status status) override;
2268
+ protected:
2269
+ explicit AsyncProgressWorkerBase(const Object& receiver,
2270
+ const Function& callback,
2271
+ const char* resource_name,
2272
+ const Object& resource,
2273
+ size_t queue_size = 1);
2274
+ virtual ~AsyncProgressWorkerBase();
2275
+
2276
+ // Optional callback of Napi::ThreadSafeFunction only available after NAPI_VERSION 4.
2277
+ // Refs: https://github.com/nodejs/node/pull/27791
2278
+ #if NAPI_VERSION > 4
2279
+ explicit AsyncProgressWorkerBase(Napi::Env env,
2280
+ const char* resource_name,
2281
+ const Object& resource,
2282
+ size_t queue_size = 1);
2283
+ #endif
2284
+
2285
+ static inline void OnAsyncWorkProgress(Napi::Env env,
2286
+ Napi::Function jsCallback,
2287
+ void* data);
2288
+
2289
+ napi_status NonBlockingCall(DataType* data);
2290
+
2291
+ private:
2292
+ ThreadSafeFunction _tsfn;
2293
+ bool _work_completed = false;
2294
+ napi_status _complete_status;
2295
+ static inline void OnThreadSafeFunctionFinalize(Napi::Env env, void* data, AsyncProgressWorkerBase* context);
2296
+ };
2297
+
2095
2298
  template<class T>
2096
- class AsyncProgressWorker : public AsyncWorker {
2299
+ class AsyncProgressWorker : public AsyncProgressWorkerBase<void> {
2097
2300
  public:
2098
2301
  virtual ~AsyncProgressWorker();
2099
2302
 
@@ -2107,40 +2310,39 @@ namespace Napi {
2107
2310
  AsyncProgressWorker* const _worker;
2108
2311
  };
2109
2312
 
2313
+ void OnWorkProgress(void*) override;
2314
+
2110
2315
  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);
2316
+ explicit AsyncProgressWorker(const Function& callback);
2317
+ explicit AsyncProgressWorker(const Function& callback,
2318
+ const char* resource_name);
2319
+ explicit AsyncProgressWorker(const Function& callback,
2320
+ const char* resource_name,
2321
+ const Object& resource);
2322
+ explicit AsyncProgressWorker(const Object& receiver,
2323
+ const Function& callback);
2324
+ explicit AsyncProgressWorker(const Object& receiver,
2325
+ const Function& callback,
2326
+ const char* resource_name);
2327
+ explicit AsyncProgressWorker(const Object& receiver,
2328
+ const Function& callback,
2329
+ const char* resource_name,
2330
+ const Object& resource);
2126
2331
 
2127
2332
  // Optional callback of Napi::ThreadSafeFunction only available after NAPI_VERSION 4.
2128
2333
  // Refs: https://github.com/nodejs/node/pull/27791
2129
2334
  #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);
2335
+ explicit AsyncProgressWorker(Napi::Env env);
2336
+ explicit AsyncProgressWorker(Napi::Env env,
2337
+ const char* resource_name);
2338
+ explicit AsyncProgressWorker(Napi::Env env,
2339
+ const char* resource_name,
2340
+ const Object& resource);
2136
2341
  #endif
2137
-
2138
2342
  virtual void Execute(const ExecutionProgress& progress) = 0;
2139
2343
  virtual void OnProgress(const T* data, size_t count) = 0;
2140
2344
 
2141
2345
  private:
2142
- static void WorkProgress_(Napi::Env env, Napi::Function jsCallback, void* data);
2143
-
2144
2346
  void Execute() override;
2145
2347
  void Signal() const;
2146
2348
  void SendProgress_(const T* data, size_t count);
@@ -2148,7 +2350,60 @@ namespace Napi {
2148
2350
  std::mutex _mutex;
2149
2351
  T* _asyncdata;
2150
2352
  size_t _asyncsize;
2151
- ThreadSafeFunction _tsfn;
2353
+ };
2354
+
2355
+ template<class T>
2356
+ class AsyncProgressQueueWorker : public AsyncProgressWorkerBase<std::pair<T*, size_t>> {
2357
+ public:
2358
+ virtual ~AsyncProgressQueueWorker() {};
2359
+
2360
+ class ExecutionProgress {
2361
+ friend class AsyncProgressQueueWorker;
2362
+ public:
2363
+ void Signal() const;
2364
+ void Send(const T* data, size_t count) const;
2365
+ private:
2366
+ explicit ExecutionProgress(AsyncProgressQueueWorker* worker) : _worker(worker) {}
2367
+ AsyncProgressQueueWorker* const _worker;
2368
+ };
2369
+
2370
+ void OnWorkComplete(Napi::Env env, napi_status status) override;
2371
+ void OnWorkProgress(std::pair<T*, size_t>*) override;
2372
+
2373
+ protected:
2374
+ explicit AsyncProgressQueueWorker(const Function& callback);
2375
+ explicit AsyncProgressQueueWorker(const Function& callback,
2376
+ const char* resource_name);
2377
+ explicit AsyncProgressQueueWorker(const Function& callback,
2378
+ const char* resource_name,
2379
+ const Object& resource);
2380
+ explicit AsyncProgressQueueWorker(const Object& receiver,
2381
+ const Function& callback);
2382
+ explicit AsyncProgressQueueWorker(const Object& receiver,
2383
+ const Function& callback,
2384
+ const char* resource_name);
2385
+ explicit AsyncProgressQueueWorker(const Object& receiver,
2386
+ const Function& callback,
2387
+ const char* resource_name,
2388
+ const Object& resource);
2389
+
2390
+ // Optional callback of Napi::ThreadSafeFunction only available after NAPI_VERSION 4.
2391
+ // Refs: https://github.com/nodejs/node/pull/27791
2392
+ #if NAPI_VERSION > 4
2393
+ explicit AsyncProgressQueueWorker(Napi::Env env);
2394
+ explicit AsyncProgressQueueWorker(Napi::Env env,
2395
+ const char* resource_name);
2396
+ explicit AsyncProgressQueueWorker(Napi::Env env,
2397
+ const char* resource_name,
2398
+ const Object& resource);
2399
+ #endif
2400
+ virtual void Execute(const ExecutionProgress& progress) = 0;
2401
+ virtual void OnProgress(const T* data, size_t count) = 0;
2402
+
2403
+ private:
2404
+ void Execute() override;
2405
+ void Signal() const;
2406
+ void SendProgress_(const T* data, size_t count);
2152
2407
  };
2153
2408
  #endif
2154
2409