node-addon-api 2.0.1 → 3.0.2

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 (63) hide show
  1. package/.travis.yml +1 -4
  2. package/CHANGELOG.md +172 -10
  3. package/README.md +81 -30
  4. package/appveyor.yml +3 -14
  5. package/benchmark/README.md +47 -0
  6. package/benchmark/binding.gyp +25 -0
  7. package/benchmark/function_args.cc +217 -0
  8. package/benchmark/function_args.js +60 -0
  9. package/benchmark/index.js +34 -0
  10. package/benchmark/property_descriptor.cc +91 -0
  11. package/benchmark/property_descriptor.js +37 -0
  12. package/common.gypi +21 -0
  13. package/doc/addon.md +157 -0
  14. package/doc/array.md +81 -0
  15. package/doc/array_buffer.md +4 -0
  16. package/doc/async_worker.md +33 -4
  17. package/doc/{async_progress_worker.md → async_worker_variants.md} +115 -3
  18. package/doc/bigint.md +7 -2
  19. package/doc/boolean.md +4 -0
  20. package/doc/buffer.md +4 -0
  21. package/doc/class_property_descriptor.md +3 -3
  22. package/doc/creating_a_release.md +5 -5
  23. package/doc/dataview.md +4 -0
  24. package/doc/date.md +2 -2
  25. package/doc/env.md +69 -0
  26. package/doc/error.md +5 -0
  27. package/doc/external.md +4 -0
  28. package/doc/function.md +109 -1
  29. package/doc/hierarchy.md +91 -0
  30. package/doc/instance_wrap.md +408 -0
  31. package/doc/name.md +29 -0
  32. package/doc/object.md +44 -1
  33. package/doc/object_lifetime_management.md +1 -1
  34. package/doc/object_wrap.md +219 -215
  35. package/doc/promises.md +5 -0
  36. package/doc/property_descriptor.md +64 -9
  37. package/doc/setup.md +1 -2
  38. package/doc/string.md +5 -1
  39. package/doc/symbol.md +5 -1
  40. package/doc/typed_array.md +4 -0
  41. package/doc/typed_array_of.md +4 -0
  42. package/doc/value.md +166 -104
  43. package/except.gypi +16 -0
  44. package/index.js +7 -41
  45. package/napi-inl.h +1116 -400
  46. package/napi.h +414 -142
  47. package/node_api.gyp +9 -0
  48. package/noexcept.gypi +16 -0
  49. package/{src/nothing.c → nothing.c} +0 -0
  50. package/package.json +63 -1
  51. package/tools/README.md +4 -4
  52. package/tools/conversion.js +4 -8
  53. package/doc/basic_types.md +0 -423
  54. package/doc/working_with_javascript_values.md +0 -14
  55. package/external-napi/node_api.h +0 -7
  56. package/src/node_api.cc +0 -3655
  57. package/src/node_api.gyp +0 -21
  58. package/src/node_api.h +0 -588
  59. package/src/node_api_types.h +0 -115
  60. package/src/node_internals.cc +0 -142
  61. package/src/node_internals.h +0 -157
  62. package/src/util-inl.h +0 -38
  63. 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,
@@ -1579,6 +1643,114 @@ namespace Napi {
1579
1643
  napi_property_descriptor _desc;
1580
1644
  };
1581
1645
 
1646
+ template <typename T, typename TCallback>
1647
+ struct MethodCallbackData {
1648
+ TCallback callback;
1649
+ void* data;
1650
+ };
1651
+
1652
+ template <typename T, typename TGetterCallback, typename TSetterCallback>
1653
+ struct AccessorCallbackData {
1654
+ TGetterCallback getterCallback;
1655
+ TSetterCallback setterCallback;
1656
+ void* data;
1657
+ };
1658
+
1659
+ template <typename T>
1660
+ class InstanceWrap {
1661
+ public:
1662
+
1663
+ typedef void (T::*InstanceVoidMethodCallback)(const CallbackInfo& info);
1664
+ typedef Napi::Value (T::*InstanceMethodCallback)(const CallbackInfo& info);
1665
+ typedef Napi::Value (T::*InstanceGetterCallback)(const CallbackInfo& info);
1666
+ typedef void (T::*InstanceSetterCallback)(const CallbackInfo& info, const Napi::Value& value);
1667
+
1668
+ typedef ClassPropertyDescriptor<T> PropertyDescriptor;
1669
+
1670
+ static PropertyDescriptor InstanceMethod(const char* utf8name,
1671
+ InstanceVoidMethodCallback method,
1672
+ napi_property_attributes attributes = napi_default,
1673
+ void* data = nullptr);
1674
+ static PropertyDescriptor InstanceMethod(const char* utf8name,
1675
+ InstanceMethodCallback method,
1676
+ napi_property_attributes attributes = napi_default,
1677
+ void* data = nullptr);
1678
+ static PropertyDescriptor InstanceMethod(Symbol name,
1679
+ InstanceVoidMethodCallback method,
1680
+ napi_property_attributes attributes = napi_default,
1681
+ void* data = nullptr);
1682
+ static PropertyDescriptor InstanceMethod(Symbol name,
1683
+ InstanceMethodCallback method,
1684
+ napi_property_attributes attributes = napi_default,
1685
+ void* data = nullptr);
1686
+ template <InstanceVoidMethodCallback method>
1687
+ static PropertyDescriptor InstanceMethod(const char* utf8name,
1688
+ napi_property_attributes attributes = napi_default,
1689
+ void* data = nullptr);
1690
+ template <InstanceMethodCallback method>
1691
+ static PropertyDescriptor InstanceMethod(const char* utf8name,
1692
+ napi_property_attributes attributes = napi_default,
1693
+ void* data = nullptr);
1694
+ template <InstanceVoidMethodCallback method>
1695
+ static PropertyDescriptor InstanceMethod(Symbol name,
1696
+ napi_property_attributes attributes = napi_default,
1697
+ void* data = nullptr);
1698
+ template <InstanceMethodCallback method>
1699
+ static PropertyDescriptor InstanceMethod(Symbol name,
1700
+ napi_property_attributes attributes = napi_default,
1701
+ void* data = nullptr);
1702
+ static PropertyDescriptor InstanceAccessor(const char* utf8name,
1703
+ InstanceGetterCallback getter,
1704
+ InstanceSetterCallback setter,
1705
+ napi_property_attributes attributes = napi_default,
1706
+ void* data = nullptr);
1707
+ static PropertyDescriptor InstanceAccessor(Symbol name,
1708
+ InstanceGetterCallback getter,
1709
+ InstanceSetterCallback setter,
1710
+ napi_property_attributes attributes = napi_default,
1711
+ void* data = nullptr);
1712
+ template <InstanceGetterCallback getter, InstanceSetterCallback setter=nullptr>
1713
+ static PropertyDescriptor InstanceAccessor(const char* utf8name,
1714
+ napi_property_attributes attributes = napi_default,
1715
+ void* data = nullptr);
1716
+ template <InstanceGetterCallback getter, InstanceSetterCallback setter=nullptr>
1717
+ static PropertyDescriptor InstanceAccessor(Symbol name,
1718
+ napi_property_attributes attributes = napi_default,
1719
+ void* data = nullptr);
1720
+ static PropertyDescriptor InstanceValue(const char* utf8name,
1721
+ Napi::Value value,
1722
+ napi_property_attributes attributes = napi_default);
1723
+ static PropertyDescriptor InstanceValue(Symbol name,
1724
+ Napi::Value value,
1725
+ napi_property_attributes attributes = napi_default);
1726
+
1727
+ protected:
1728
+ static void AttachPropData(napi_env env, napi_value value, const napi_property_descriptor* prop);
1729
+
1730
+ private:
1731
+ using This = InstanceWrap<T>;
1732
+
1733
+ typedef MethodCallbackData<T, InstanceVoidMethodCallback> InstanceVoidMethodCallbackData;
1734
+ typedef MethodCallbackData<T, InstanceMethodCallback> InstanceMethodCallbackData;
1735
+ typedef AccessorCallbackData<T,
1736
+ InstanceGetterCallback,
1737
+ InstanceSetterCallback> InstanceAccessorCallbackData;
1738
+
1739
+ static napi_value InstanceVoidMethodCallbackWrapper(napi_env env, napi_callback_info info);
1740
+ static napi_value InstanceMethodCallbackWrapper(napi_env env, napi_callback_info info);
1741
+ static napi_value InstanceGetterCallbackWrapper(napi_env env, napi_callback_info info);
1742
+ static napi_value InstanceSetterCallbackWrapper(napi_env env, napi_callback_info info);
1743
+
1744
+ template <InstanceSetterCallback method>
1745
+ static napi_value WrappedMethod(napi_env env, napi_callback_info info) noexcept;
1746
+
1747
+ template <InstanceSetterCallback setter> struct SetterTag {};
1748
+
1749
+ template <InstanceSetterCallback setter>
1750
+ static napi_callback WrapSetter(SetterTag<setter>) noexcept { return &This::WrappedMethod<setter>; }
1751
+ static napi_callback WrapSetter(SetterTag<nullptr>) noexcept { return nullptr; }
1752
+ };
1753
+
1582
1754
  /// Base class to be extended by C++ classes exposed to JavaScript; each C++ class instance gets
1583
1755
  /// "wrapped" by a JavaScript object that is managed by this class.
1584
1756
  ///
@@ -1593,8 +1765,8 @@ namespace Napi {
1593
1765
  /// public:
1594
1766
  /// static void Initialize(Napi::Env& env, Napi::Object& target) {
1595
1767
  /// Napi::Function constructor = DefineClass(env, "Example", {
1596
- /// InstanceAccessor("value", &Example::GetSomething, &Example::SetSomething),
1597
- /// InstanceMethod("doSomething", &Example::DoSomething),
1768
+ /// InstanceAccessor<&Example::GetSomething, &Example::SetSomething>("value"),
1769
+ /// InstanceMethod<&Example::DoSomething>("doSomething"),
1598
1770
  /// });
1599
1771
  /// target.Set("Example", constructor);
1600
1772
  /// }
@@ -1605,7 +1777,7 @@ namespace Napi {
1605
1777
  /// Napi::Value DoSomething(const Napi::CallbackInfo& info);
1606
1778
  /// }
1607
1779
  template <typename T>
1608
- class ObjectWrap : public Reference<Object> {
1780
+ class ObjectWrap : public InstanceWrap<T>, public Reference<Object> {
1609
1781
  public:
1610
1782
  ObjectWrap(const CallbackInfo& callbackInfo);
1611
1783
  virtual ~ObjectWrap();
@@ -1617,10 +1789,6 @@ namespace Napi {
1617
1789
  typedef Napi::Value (*StaticMethodCallback)(const CallbackInfo& info);
1618
1790
  typedef Napi::Value (*StaticGetterCallback)(const CallbackInfo& info);
1619
1791
  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
1792
 
1625
1793
  typedef ClassPropertyDescriptor<T> PropertyDescriptor;
1626
1794
 
@@ -1648,6 +1816,22 @@ namespace Napi {
1648
1816
  StaticMethodCallback method,
1649
1817
  napi_property_attributes attributes = napi_default,
1650
1818
  void* data = nullptr);
1819
+ template <StaticVoidMethodCallback method>
1820
+ static PropertyDescriptor StaticMethod(const char* utf8name,
1821
+ napi_property_attributes attributes = napi_default,
1822
+ void* data = nullptr);
1823
+ template <StaticVoidMethodCallback method>
1824
+ static PropertyDescriptor StaticMethod(Symbol name,
1825
+ napi_property_attributes attributes = napi_default,
1826
+ void* data = nullptr);
1827
+ template <StaticMethodCallback method>
1828
+ static PropertyDescriptor StaticMethod(const char* utf8name,
1829
+ napi_property_attributes attributes = napi_default,
1830
+ void* data = nullptr);
1831
+ template <StaticMethodCallback method>
1832
+ static PropertyDescriptor StaticMethod(Symbol name,
1833
+ napi_property_attributes attributes = napi_default,
1834
+ void* data = nullptr);
1651
1835
  static PropertyDescriptor StaticAccessor(const char* utf8name,
1652
1836
  StaticGetterCallback getter,
1653
1837
  StaticSetterCallback setter,
@@ -1658,56 +1842,30 @@ namespace Napi {
1658
1842
  StaticSetterCallback setter,
1659
1843
  napi_property_attributes attributes = napi_default,
1660
1844
  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,
1845
+ template <StaticGetterCallback getter, StaticSetterCallback setter=nullptr>
1846
+ static PropertyDescriptor StaticAccessor(const char* utf8name,
1671
1847
  napi_property_attributes attributes = napi_default,
1672
1848
  void* data = nullptr);
1673
- static PropertyDescriptor InstanceMethod(Symbol name,
1674
- InstanceMethodCallback method,
1849
+ template <StaticGetterCallback getter, StaticSetterCallback setter=nullptr>
1850
+ static PropertyDescriptor StaticAccessor(Symbol name,
1675
1851
  napi_property_attributes attributes = napi_default,
1676
1852
  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
1853
  static PropertyDescriptor StaticValue(const char* utf8name,
1688
1854
  Napi::Value value,
1689
1855
  napi_property_attributes attributes = napi_default);
1690
1856
  static PropertyDescriptor StaticValue(Symbol name,
1691
1857
  Napi::Value value,
1692
1858
  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
1859
  virtual void Finalize(Napi::Env env);
1700
1860
 
1701
1861
  private:
1862
+ using This = ObjectWrap<T>;
1863
+
1702
1864
  static napi_value ConstructorCallbackWrapper(napi_env env, napi_callback_info info);
1703
1865
  static napi_value StaticVoidMethodCallbackWrapper(napi_env env, napi_callback_info info);
1704
1866
  static napi_value StaticMethodCallbackWrapper(napi_env env, napi_callback_info info);
1705
1867
  static napi_value StaticGetterCallbackWrapper(napi_env env, napi_callback_info info);
1706
1868
  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
1869
  static void FinalizeCallback(napi_env env, void* data, void* hint);
1712
1870
  static Function DefineClass(Napi::Env env,
1713
1871
  const char* utf8name,
@@ -1715,26 +1873,23 @@ namespace Napi {
1715
1873
  const napi_property_descriptor* props,
1716
1874
  void* data = nullptr);
1717
1875
 
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;
1876
+ typedef MethodCallbackData<T, StaticVoidMethodCallback> StaticVoidMethodCallbackData;
1877
+ typedef MethodCallbackData<T, StaticMethodCallback> StaticMethodCallbackData;
1878
+
1879
+ typedef AccessorCallbackData<T,
1880
+ StaticGetterCallback,
1881
+ StaticSetterCallback> StaticAccessorCallbackData;
1882
+
1883
+ template <StaticSetterCallback method>
1884
+ static napi_value WrappedMethod(napi_env env, napi_callback_info info) noexcept;
1885
+
1886
+ template <StaticSetterCallback setter> struct StaticSetterTag {};
1887
+
1888
+ template <StaticSetterCallback setter>
1889
+ static napi_callback WrapStaticSetter(StaticSetterTag<setter>) noexcept { return &This::WrappedMethod<setter>; }
1890
+ static napi_callback WrapStaticSetter(StaticSetterTag<nullptr>) noexcept { return nullptr; }
1891
+
1892
+ bool _construction_failed = true;
1738
1893
  };
1739
1894
 
1740
1895
  class HandleScope {
@@ -1744,8 +1899,7 @@ namespace Napi {
1744
1899
  ~HandleScope();
1745
1900
 
1746
1901
  // Disallow copying to prevent double close of napi_handle_scope
1747
- HandleScope(HandleScope const &) = delete;
1748
- void operator=(HandleScope const &) = delete;
1902
+ NAPI_DISALLOW_ASSIGN_COPY(HandleScope)
1749
1903
 
1750
1904
  operator napi_handle_scope() const;
1751
1905
 
@@ -1763,8 +1917,7 @@ namespace Napi {
1763
1917
  ~EscapableHandleScope();
1764
1918
 
1765
1919
  // Disallow copying to prevent double close of napi_escapable_handle_scope
1766
- EscapableHandleScope(EscapableHandleScope const &) = delete;
1767
- void operator=(EscapableHandleScope const &) = delete;
1920
+ NAPI_DISALLOW_ASSIGN_COPY(EscapableHandleScope)
1768
1921
 
1769
1922
  operator napi_escapable_handle_scope() const;
1770
1923
 
@@ -1784,8 +1937,7 @@ namespace Napi {
1784
1937
  virtual ~CallbackScope();
1785
1938
 
1786
1939
  // Disallow copying to prevent double close of napi_callback_scope
1787
- CallbackScope(CallbackScope const &) = delete;
1788
- void operator=(CallbackScope const &) = delete;
1940
+ NAPI_DISALLOW_ASSIGN_COPY(CallbackScope)
1789
1941
 
1790
1942
  operator napi_callback_scope() const;
1791
1943
 
@@ -1805,8 +1957,7 @@ namespace Napi {
1805
1957
 
1806
1958
  AsyncContext(AsyncContext&& other);
1807
1959
  AsyncContext& operator =(AsyncContext&& other);
1808
- AsyncContext(const AsyncContext&) = delete;
1809
- AsyncContext& operator =(AsyncContext&) = delete;
1960
+ NAPI_DISALLOW_ASSIGN_COPY(AsyncContext)
1810
1961
 
1811
1962
  operator napi_async_context() const;
1812
1963
 
@@ -1824,8 +1975,7 @@ namespace Napi {
1824
1975
  // An async worker can be moved but cannot be copied.
1825
1976
  AsyncWorker(AsyncWorker&& other);
1826
1977
  AsyncWorker& operator =(AsyncWorker&& other);
1827
- AsyncWorker(const AsyncWorker&) = delete;
1828
- AsyncWorker& operator =(AsyncWorker&) = delete;
1978
+ NAPI_DISALLOW_ASSIGN_COPY(AsyncWorker)
1829
1979
 
1830
1980
  operator napi_async_work() const;
1831
1981
 
@@ -1838,6 +1988,10 @@ namespace Napi {
1838
1988
  ObjectReference& Receiver();
1839
1989
  FunctionReference& Callback();
1840
1990
 
1991
+ virtual void OnExecute(Napi::Env env);
1992
+ virtual void OnWorkComplete(Napi::Env env,
1993
+ napi_status status);
1994
+
1841
1995
  protected:
1842
1996
  explicit AsyncWorker(const Function& callback);
1843
1997
  explicit AsyncWorker(const Function& callback,
@@ -1871,10 +2025,10 @@ namespace Napi {
1871
2025
  void SetError(const std::string& error);
1872
2026
 
1873
2027
  private:
1874
- static void OnExecute(napi_env env, void* this_pointer);
1875
- static void OnWorkComplete(napi_env env,
1876
- napi_status status,
1877
- void* this_pointer);
2028
+ static inline void OnAsyncWorkExecute(napi_env env, void* asyncworker);
2029
+ static inline void OnAsyncWorkComplete(napi_env env,
2030
+ napi_status status,
2031
+ void* asyncworker);
1878
2032
 
1879
2033
  napi_env _env;
1880
2034
  napi_async_work _work;
@@ -1884,7 +2038,7 @@ namespace Napi {
1884
2038
  bool _suppress_destruct;
1885
2039
  };
1886
2040
 
1887
- #if (NAPI_VERSION > 3)
2041
+ #if (NAPI_VERSION > 3 && !defined(__wasm32__))
1888
2042
  class ThreadSafeFunction {
1889
2043
  public:
1890
2044
  // This API may only be called from the main thread.
@@ -2090,8 +2244,55 @@ namespace Napi {
2090
2244
  napi_threadsafe_function _tsfn;
2091
2245
  };
2092
2246
 
2247
+ template <typename DataType>
2248
+ class AsyncProgressWorkerBase : public AsyncWorker {
2249
+ public:
2250
+ virtual void OnWorkProgress(DataType* data) = 0;
2251
+ class ThreadSafeData {
2252
+ public:
2253
+ ThreadSafeData(AsyncProgressWorkerBase* asyncprogressworker, DataType* data)
2254
+ : _asyncprogressworker(asyncprogressworker), _data(data) {}
2255
+
2256
+ AsyncProgressWorkerBase* asyncprogressworker() { return _asyncprogressworker; };
2257
+ DataType* data() { return _data; };
2258
+
2259
+ private:
2260
+ AsyncProgressWorkerBase* _asyncprogressworker;
2261
+ DataType* _data;
2262
+ };
2263
+ void OnWorkComplete(Napi::Env env, napi_status status) override;
2264
+ protected:
2265
+ explicit AsyncProgressWorkerBase(const Object& receiver,
2266
+ const Function& callback,
2267
+ const char* resource_name,
2268
+ const Object& resource,
2269
+ size_t queue_size = 1);
2270
+ virtual ~AsyncProgressWorkerBase();
2271
+
2272
+ // Optional callback of Napi::ThreadSafeFunction only available after NAPI_VERSION 4.
2273
+ // Refs: https://github.com/nodejs/node/pull/27791
2274
+ #if NAPI_VERSION > 4
2275
+ explicit AsyncProgressWorkerBase(Napi::Env env,
2276
+ const char* resource_name,
2277
+ const Object& resource,
2278
+ size_t queue_size = 1);
2279
+ #endif
2280
+
2281
+ static inline void OnAsyncWorkProgress(Napi::Env env,
2282
+ Napi::Function jsCallback,
2283
+ void* data);
2284
+
2285
+ napi_status NonBlockingCall(DataType* data);
2286
+
2287
+ private:
2288
+ ThreadSafeFunction _tsfn;
2289
+ bool _work_completed = false;
2290
+ napi_status _complete_status;
2291
+ static inline void OnThreadSafeFunctionFinalize(Napi::Env env, void* data, AsyncProgressWorkerBase* context);
2292
+ };
2293
+
2093
2294
  template<class T>
2094
- class AsyncProgressWorker : public AsyncWorker {
2295
+ class AsyncProgressWorker : public AsyncProgressWorkerBase<void> {
2095
2296
  public:
2096
2297
  virtual ~AsyncProgressWorker();
2097
2298
 
@@ -2105,40 +2306,39 @@ namespace Napi {
2105
2306
  AsyncProgressWorker* const _worker;
2106
2307
  };
2107
2308
 
2309
+ void OnWorkProgress(void*) override;
2310
+
2108
2311
  protected:
2109
- explicit AsyncProgressWorker(const Function& callback);
2110
- explicit AsyncProgressWorker(const Function& callback,
2111
- const char* resource_name);
2112
- explicit AsyncProgressWorker(const Function& callback,
2113
- const char* resource_name,
2114
- const Object& resource);
2115
- explicit AsyncProgressWorker(const Object& receiver,
2116
- const Function& callback);
2117
- explicit AsyncProgressWorker(const Object& receiver,
2118
- const Function& callback,
2119
- const char* resource_name);
2120
- explicit AsyncProgressWorker(const Object& receiver,
2121
- const Function& callback,
2122
- const char* resource_name,
2123
- const Object& resource);
2312
+ explicit AsyncProgressWorker(const Function& callback);
2313
+ explicit AsyncProgressWorker(const Function& callback,
2314
+ const char* resource_name);
2315
+ explicit AsyncProgressWorker(const Function& callback,
2316
+ const char* resource_name,
2317
+ const Object& resource);
2318
+ explicit AsyncProgressWorker(const Object& receiver,
2319
+ const Function& callback);
2320
+ explicit AsyncProgressWorker(const Object& receiver,
2321
+ const Function& callback,
2322
+ const char* resource_name);
2323
+ explicit AsyncProgressWorker(const Object& receiver,
2324
+ const Function& callback,
2325
+ const char* resource_name,
2326
+ const Object& resource);
2124
2327
 
2125
2328
  // Optional callback of Napi::ThreadSafeFunction only available after NAPI_VERSION 4.
2126
2329
  // Refs: https://github.com/nodejs/node/pull/27791
2127
2330
  #if NAPI_VERSION > 4
2128
- explicit AsyncProgressWorker(Napi::Env env);
2129
- explicit AsyncProgressWorker(Napi::Env env,
2130
- const char* resource_name);
2131
- explicit AsyncProgressWorker(Napi::Env env,
2132
- const char* resource_name,
2133
- const Object& resource);
2331
+ explicit AsyncProgressWorker(Napi::Env env);
2332
+ explicit AsyncProgressWorker(Napi::Env env,
2333
+ const char* resource_name);
2334
+ explicit AsyncProgressWorker(Napi::Env env,
2335
+ const char* resource_name,
2336
+ const Object& resource);
2134
2337
  #endif
2135
-
2136
2338
  virtual void Execute(const ExecutionProgress& progress) = 0;
2137
2339
  virtual void OnProgress(const T* data, size_t count) = 0;
2138
2340
 
2139
2341
  private:
2140
- static void WorkProgress_(Napi::Env env, Napi::Function jsCallback, void* data);
2141
-
2142
2342
  void Execute() override;
2143
2343
  void Signal() const;
2144
2344
  void SendProgress_(const T* data, size_t count);
@@ -2146,9 +2346,62 @@ namespace Napi {
2146
2346
  std::mutex _mutex;
2147
2347
  T* _asyncdata;
2148
2348
  size_t _asyncsize;
2149
- ThreadSafeFunction _tsfn;
2150
2349
  };
2151
- #endif
2350
+
2351
+ template<class T>
2352
+ class AsyncProgressQueueWorker : public AsyncProgressWorkerBase<std::pair<T*, size_t>> {
2353
+ public:
2354
+ virtual ~AsyncProgressQueueWorker() {};
2355
+
2356
+ class ExecutionProgress {
2357
+ friend class AsyncProgressQueueWorker;
2358
+ public:
2359
+ void Signal() const;
2360
+ void Send(const T* data, size_t count) const;
2361
+ private:
2362
+ explicit ExecutionProgress(AsyncProgressQueueWorker* worker) : _worker(worker) {}
2363
+ AsyncProgressQueueWorker* const _worker;
2364
+ };
2365
+
2366
+ void OnWorkComplete(Napi::Env env, napi_status status) override;
2367
+ void OnWorkProgress(std::pair<T*, size_t>*) override;
2368
+
2369
+ protected:
2370
+ explicit AsyncProgressQueueWorker(const Function& callback);
2371
+ explicit AsyncProgressQueueWorker(const Function& callback,
2372
+ const char* resource_name);
2373
+ explicit AsyncProgressQueueWorker(const Function& callback,
2374
+ const char* resource_name,
2375
+ const Object& resource);
2376
+ explicit AsyncProgressQueueWorker(const Object& receiver,
2377
+ const Function& callback);
2378
+ explicit AsyncProgressQueueWorker(const Object& receiver,
2379
+ const Function& callback,
2380
+ const char* resource_name);
2381
+ explicit AsyncProgressQueueWorker(const Object& receiver,
2382
+ const Function& callback,
2383
+ const char* resource_name,
2384
+ const Object& resource);
2385
+
2386
+ // Optional callback of Napi::ThreadSafeFunction only available after NAPI_VERSION 4.
2387
+ // Refs: https://github.com/nodejs/node/pull/27791
2388
+ #if NAPI_VERSION > 4
2389
+ explicit AsyncProgressQueueWorker(Napi::Env env);
2390
+ explicit AsyncProgressQueueWorker(Napi::Env env,
2391
+ const char* resource_name);
2392
+ explicit AsyncProgressQueueWorker(Napi::Env env,
2393
+ const char* resource_name,
2394
+ const Object& resource);
2395
+ #endif
2396
+ virtual void Execute(const ExecutionProgress& progress) = 0;
2397
+ virtual void OnProgress(const T* data, size_t count) = 0;
2398
+
2399
+ private:
2400
+ void Execute() override;
2401
+ void Signal() const;
2402
+ void SendProgress_(const T* data, size_t count);
2403
+ };
2404
+ #endif // NAPI_VERSION > 3 && !defined(__wasm32__)
2152
2405
 
2153
2406
  // Memory management.
2154
2407
  class MemoryManagement {
@@ -2163,6 +2416,25 @@ namespace Napi {
2163
2416
  static const napi_node_version* GetNodeVersion(Env env);
2164
2417
  };
2165
2418
 
2419
+ #if NAPI_VERSION > 5
2420
+ template <typename T>
2421
+ class Addon : public InstanceWrap<T> {
2422
+ public:
2423
+ static inline Object Init(Env env, Object exports);
2424
+ static T* Unwrap(Object wrapper);
2425
+
2426
+ protected:
2427
+ typedef ClassPropertyDescriptor<T> AddonProp;
2428
+ void DefineAddon(Object exports,
2429
+ const std::initializer_list<AddonProp>& props);
2430
+ Napi::Object DefineProperties(Object object,
2431
+ const std::initializer_list<AddonProp>& props);
2432
+
2433
+ private:
2434
+ Object entry_point_;
2435
+ };
2436
+ #endif // NAPI_VERSION > 5
2437
+
2166
2438
  } // namespace Napi
2167
2439
 
2168
2440
  // Inline implementations of all the above class methods are included here.