node-addon-api 1.7.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 (53) hide show
  1. package/.travis.yml +3 -9
  2. package/CHANGELOG.md +154 -9
  3. package/README.md +58 -9
  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/array_buffer.md +1 -1
  13. package/doc/async_context.md +10 -0
  14. package/doc/async_operations.md +1 -1
  15. package/doc/async_worker.md +56 -26
  16. package/doc/async_worker_variants.md +456 -0
  17. package/doc/basic_types.md +8 -0
  18. package/doc/bigint.md +2 -1
  19. package/doc/class_property_descriptor.md +5 -6
  20. package/doc/cmake-js.md +58 -9
  21. package/doc/creating_a_release.md +5 -5
  22. package/doc/date.md +68 -0
  23. package/doc/env.md +14 -0
  24. package/doc/function.md +108 -1
  25. package/doc/object.md +74 -1
  26. package/doc/object_lifetime_management.md +1 -1
  27. package/doc/object_wrap.md +291 -4
  28. package/doc/prebuild_tools.md +1 -1
  29. package/doc/property_descriptor.md +64 -9
  30. package/doc/setup.md +0 -1
  31. package/doc/string.md +1 -1
  32. package/doc/symbol.md +1 -1
  33. package/doc/threadsafe_function.md +18 -1
  34. package/doc/value.md +10 -1
  35. package/except.gypi +16 -0
  36. package/index.js +5 -42
  37. package/napi-inl.h +1048 -147
  38. package/napi.h +424 -49
  39. package/node_api.gyp +9 -0
  40. package/noexcept.gypi +16 -0
  41. package/{src/nothing.c → nothing.c} +0 -0
  42. package/package.json +244 -47
  43. package/tools/README.md +4 -4
  44. package/tools/conversion.js +0 -4
  45. package/external-napi/node_api.h +0 -7
  46. package/src/node_api.cc +0 -3655
  47. package/src/node_api.gyp +0 -21
  48. package/src/node_api.h +0 -588
  49. package/src/node_api_types.h +0 -115
  50. package/src/node_internals.cc +0 -142
  51. package/src/node_internals.h +0 -157
  52. package/src/util-inl.h +0 -38
  53. package/src/util.h +0 -7
package/napi.h CHANGED
@@ -5,6 +5,7 @@
5
5
  #include <functional>
6
6
  #include <initializer_list>
7
7
  #include <memory>
8
+ #include <mutex>
8
9
  #include <string>
9
10
  #include <vector>
10
11
 
@@ -90,6 +91,13 @@ static_assert(sizeof(char16_t) == sizeof(wchar_t), "Size mismatch between char16
90
91
 
91
92
  #endif // NAPI_CPP_EXCEPTIONS
92
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
+
93
101
  #define NAPI_FATAL_IF_FAILED(status, location, message) \
94
102
  do { \
95
103
  if ((status) != napi_ok) { \
@@ -111,20 +119,20 @@ namespace Napi {
111
119
  class Value;
112
120
  class Boolean;
113
121
  class Number;
114
- // currently experimental guard with version of NAPI_VERSION that it is
115
- // released in once it is no longer experimental
116
- #if (NAPI_VERSION > 2147483646)
122
+ #if NAPI_VERSION > 5
117
123
  class BigInt;
118
- #endif // NAPI_EXPERIMENTAL
124
+ #endif // NAPI_VERSION > 5
125
+ #if (NAPI_VERSION > 4)
126
+ class Date;
127
+ #endif
119
128
  class String;
120
129
  class Object;
121
130
  class Array;
131
+ class ArrayBuffer;
122
132
  class Function;
123
- template <typename T> class Buffer;
124
133
  class Error;
125
134
  class PropertyDescriptor;
126
135
  class CallbackInfo;
127
- template <typename T> class Reference;
128
136
  class TypedArray;
129
137
  template <typename T> class TypedArrayOf;
130
138
 
@@ -136,12 +144,10 @@ namespace Napi {
136
144
  typedef TypedArrayOf<uint32_t> Uint32Array; ///< Typed-array of unsigned 32-bit integers
137
145
  typedef TypedArrayOf<float> Float32Array; ///< Typed-array of 32-bit floating-point values
138
146
  typedef TypedArrayOf<double> Float64Array; ///< Typed-array of 64-bit floating-point values
139
- // currently experimental guard with version of NAPI_VERSION that it is
140
- // released in once it is no longer experimental
141
- #if (NAPI_VERSION > 2147483646)
147
+ #if NAPI_VERSION > 5
142
148
  typedef TypedArrayOf<int64_t> BigInt64Array; ///< Typed array of signed 64-bit integers
143
149
  typedef TypedArrayOf<uint64_t> BigUint64Array; ///< Typed array of unsigned 64-bit integers
144
- #endif // NAPI_EXPERIMENTAL
150
+ #endif // NAPI_VERSION > 5
145
151
 
146
152
  /// Defines the signature of a N-API C++ module's registration callback (init) function.
147
153
  typedef Object (*ModuleRegisterCallback)(Env env, Object exports);
@@ -160,6 +166,12 @@ namespace Napi {
160
166
  ///
161
167
  /// In the V8 JavaScript engine, a N-API environment approximately corresponds to an Isolate.
162
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
163
175
  public:
164
176
  Env(napi_env env);
165
177
 
@@ -172,6 +184,26 @@ namespace Napi {
172
184
  bool IsExceptionPending() const;
173
185
  Error GetAndClearPendingException();
174
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
+
175
207
  private:
176
208
  napi_env _env;
177
209
  };
@@ -241,11 +273,12 @@ namespace Napi {
241
273
  bool IsNull() const; ///< Tests if a value is a null JavaScript value.
242
274
  bool IsBoolean() const; ///< Tests if a value is a JavaScript boolean.
243
275
  bool IsNumber() const; ///< Tests if a value is a JavaScript number.
244
- // currently experimental guard with version of NAPI_VERSION that it is
245
- // released in once it is no longer experimental
246
- #if (NAPI_VERSION > 2147483646)
276
+ #if NAPI_VERSION > 5
247
277
  bool IsBigInt() const; ///< Tests if a value is a JavaScript bigint.
248
- #endif // NAPI_EXPERIMENTAL
278
+ #endif // NAPI_VERSION > 5
279
+ #if (NAPI_VERSION > 4)
280
+ bool IsDate() const; ///< Tests if a value is a JavaScript date.
281
+ #endif
249
282
  bool IsString() const; ///< Tests if a value is a JavaScript string.
250
283
  bool IsSymbol() const; ///< Tests if a value is a JavaScript symbol.
251
284
  bool IsArray() const; ///< Tests if a value is a JavaScript array.
@@ -315,9 +348,7 @@ namespace Napi {
315
348
  double DoubleValue() const; ///< Converts a Number value to a 64-bit floating-point value.
316
349
  };
317
350
 
318
- // currently experimental guard with version of NAPI_VERSION that it is
319
- // released in once it is no longer experimental
320
- #if (NAPI_VERSION > 2147483646)
351
+ #if NAPI_VERSION > 5
321
352
  /// A JavaScript bigint value.
322
353
  class BigInt : public Value {
323
354
  public:
@@ -356,7 +387,25 @@ namespace Napi {
356
387
  /// be needed to store this BigInt (i.e. the return value of `WordCount()`).
357
388
  void ToWords(int* sign_bit, size_t* word_count, uint64_t* words);
358
389
  };
359
- #endif // NAPI_EXPERIMENTAL
390
+ #endif // NAPI_VERSION > 5
391
+
392
+ #if (NAPI_VERSION > 4)
393
+ /// A JavaScript date value.
394
+ class Date : public Value {
395
+ public:
396
+ /// Creates a new Date value from a double primitive.
397
+ static Date New(
398
+ napi_env env, ///< N-API environment
399
+ double value ///< Number value
400
+ );
401
+
402
+ Date(); ///< Creates a new _empty_ Date instance.
403
+ Date(napi_env env, napi_value value); ///< Wraps a N-API value primitive.
404
+ operator double() const; ///< Converts a Date value to double primitive
405
+
406
+ double ValueOf() const; ///< Converts a Date value to a double primitive.
407
+ };
408
+ #endif
360
409
 
361
410
  /// A JavaScript string or symbol value (that can be used as a property name).
362
411
  class Name : public Value {
@@ -685,6 +734,14 @@ namespace Napi {
685
734
  bool InstanceOf(
686
735
  const Function& constructor ///< Constructor function
687
736
  ) const;
737
+
738
+ template <typename Finalizer, typename T>
739
+ inline void AddFinalizer(Finalizer finalizeCallback, T* data);
740
+
741
+ template <typename Finalizer, typename T, typename Hint>
742
+ inline void AddFinalizer(Finalizer finalizeCallback,
743
+ T* data,
744
+ Hint* finalizeHint);
688
745
  };
689
746
 
690
747
  template <typename T>
@@ -765,13 +822,6 @@ namespace Napi {
765
822
 
766
823
  void* Data(); ///< Gets a pointer to the data buffer.
767
824
  size_t ByteLength(); ///< Gets the length of the array buffer in bytes.
768
-
769
- private:
770
- mutable void* _data;
771
- mutable size_t _length;
772
-
773
- ArrayBuffer(napi_env env, napi_value value, void* data, size_t length);
774
- void EnsureInfo() const;
775
825
  };
776
826
 
777
827
  /// A JavaScript typed-array value with unknown array type.
@@ -819,12 +869,10 @@ namespace Napi {
819
869
  : std::is_same<T, uint32_t>::value ? napi_uint32_array
820
870
  : std::is_same<T, float>::value ? napi_float32_array
821
871
  : std::is_same<T, double>::value ? napi_float64_array
822
- // currently experimental guard with version of NAPI_VERSION that it is
823
- // released in once it is no longer experimental
824
- #if (NAPI_VERSION > 2147483646)
872
+ #if NAPI_VERSION > 5
825
873
  : std::is_same<T, int64_t>::value ? napi_bigint64_array
826
874
  : std::is_same<T, uint64_t>::value ? napi_biguint64_array
827
- #endif // NAPI_EXPERIMENTAL
875
+ #endif // NAPI_VERSION > 5
828
876
  : unknown_array_type;
829
877
  }
830
878
  /// !endcond
@@ -955,6 +1003,29 @@ namespace Napi {
955
1003
 
956
1004
  class Function : public Object {
957
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
+
958
1029
  /// Callable must implement operator() accepting a const CallbackInfo&
959
1030
  /// and return either void or Value.
960
1031
  template <typename Callable>
@@ -1070,7 +1141,7 @@ namespace Napi {
1070
1141
  // A reference can be moved but cannot be copied.
1071
1142
  Reference(Reference<T>&& other);
1072
1143
  Reference<T>& operator =(Reference<T>&& other);
1073
- Reference<T>& operator =(Reference<T>&) = delete;
1144
+ NAPI_DISALLOW_ASSIGN(Reference<T>)
1074
1145
 
1075
1146
  operator napi_ref() const;
1076
1147
  bool operator ==(const Reference<T> &other) const;
@@ -1115,7 +1186,7 @@ namespace Napi {
1115
1186
  ObjectReference& operator =(Reference<Object>&& other);
1116
1187
  ObjectReference(ObjectReference&& other);
1117
1188
  ObjectReference& operator =(ObjectReference&& other);
1118
- ObjectReference& operator =(ObjectReference&) = delete;
1189
+ NAPI_DISALLOW_ASSIGN(ObjectReference)
1119
1190
 
1120
1191
  Napi::Value Get(const char* utf8name) const;
1121
1192
  Napi::Value Get(const std::string& utf8name) const;
@@ -1152,8 +1223,7 @@ namespace Napi {
1152
1223
  FunctionReference& operator =(Reference<Function>&& other);
1153
1224
  FunctionReference(FunctionReference&& other);
1154
1225
  FunctionReference& operator =(FunctionReference&& other);
1155
- FunctionReference(const FunctionReference&) = delete;
1156
- FunctionReference& operator =(FunctionReference&) = delete;
1226
+ NAPI_DISALLOW_ASSIGN_COPY(FunctionReference)
1157
1227
 
1158
1228
  Napi::Value operator ()(const std::initializer_list<napi_value>& args) const;
1159
1229
 
@@ -1295,7 +1365,7 @@ namespace Napi {
1295
1365
  Error(Error&& other);
1296
1366
  Error& operator =(Error&& other);
1297
1367
  Error(const Error&);
1298
- Error& operator =(Error&);
1368
+ Error& operator =(const Error&);
1299
1369
 
1300
1370
  const std::string& Message() const NAPI_NOEXCEPT;
1301
1371
  void ThrowAsJavaScriptException() const;
@@ -1343,8 +1413,7 @@ namespace Napi {
1343
1413
  ~CallbackInfo();
1344
1414
 
1345
1415
  // Disallow copying to prevent multiple free of _dynamicArgs
1346
- CallbackInfo(CallbackInfo const &) = delete;
1347
- void operator=(CallbackInfo const &) = delete;
1416
+ NAPI_DISALLOW_ASSIGN_COPY(CallbackInfo)
1348
1417
 
1349
1418
  Napi::Env Env() const;
1350
1419
  Value NewTarget() const;
@@ -1369,6 +1438,9 @@ namespace Napi {
1369
1438
 
1370
1439
  class PropertyDescriptor {
1371
1440
  public:
1441
+ typedef Napi::Value (*GetterCallback)(const Napi::CallbackInfo& info);
1442
+ typedef void (*SetterCallback)(const Napi::CallbackInfo& info);
1443
+
1372
1444
  #ifndef NODE_ADDON_API_DISABLE_DEPRECATED
1373
1445
  template <typename Getter>
1374
1446
  static PropertyDescriptor Accessor(const char* utf8name,
@@ -1436,6 +1508,36 @@ namespace Napi {
1436
1508
  void* data = nullptr);
1437
1509
  #endif // !NODE_ADDON_API_DISABLE_DEPRECATED
1438
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
+
1439
1541
  template <typename Getter>
1440
1542
  static PropertyDescriptor Accessor(Napi::Env env,
1441
1543
  Napi::Object object,
@@ -1521,6 +1623,10 @@ namespace Napi {
1521
1623
  operator const napi_property_descriptor&() const;
1522
1624
 
1523
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);
1524
1630
  napi_property_descriptor _desc;
1525
1631
  };
1526
1632
 
@@ -1555,8 +1661,8 @@ namespace Napi {
1555
1661
  /// public:
1556
1662
  /// static void Initialize(Napi::Env& env, Napi::Object& target) {
1557
1663
  /// Napi::Function constructor = DefineClass(env, "Example", {
1558
- /// InstanceAccessor("value", &Example::GetSomething, &Example::SetSomething),
1559
- /// InstanceMethod("doSomething", &Example::DoSomething),
1664
+ /// InstanceAccessor<&Example::GetSomething, &Example::SetSomething>("value"),
1665
+ /// InstanceMethod<&Example::DoSomething>("doSomething"),
1560
1666
  /// });
1561
1667
  /// target.Set("Example", constructor);
1562
1668
  /// }
@@ -1570,6 +1676,7 @@ namespace Napi {
1570
1676
  class ObjectWrap : public Reference<Object> {
1571
1677
  public:
1572
1678
  ObjectWrap(const CallbackInfo& callbackInfo);
1679
+ virtual ~ObjectWrap();
1573
1680
 
1574
1681
  static T* Unwrap(Object wrapper);
1575
1682
 
@@ -1609,6 +1716,22 @@ namespace Napi {
1609
1716
  StaticMethodCallback method,
1610
1717
  napi_property_attributes attributes = napi_default,
1611
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);
1612
1735
  static PropertyDescriptor StaticAccessor(const char* utf8name,
1613
1736
  StaticGetterCallback getter,
1614
1737
  StaticSetterCallback setter,
@@ -1619,6 +1742,14 @@ namespace Napi {
1619
1742
  StaticSetterCallback setter,
1620
1743
  napi_property_attributes attributes = napi_default,
1621
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);
1622
1753
  static PropertyDescriptor InstanceMethod(const char* utf8name,
1623
1754
  InstanceVoidMethodCallback method,
1624
1755
  napi_property_attributes attributes = napi_default,
@@ -1635,6 +1766,22 @@ namespace Napi {
1635
1766
  InstanceMethodCallback method,
1636
1767
  napi_property_attributes attributes = napi_default,
1637
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);
1638
1785
  static PropertyDescriptor InstanceAccessor(const char* utf8name,
1639
1786
  InstanceGetterCallback getter,
1640
1787
  InstanceSetterCallback setter,
@@ -1645,6 +1792,14 @@ namespace Napi {
1645
1792
  InstanceSetterCallback setter,
1646
1793
  napi_property_attributes attributes = napi_default,
1647
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);
1648
1803
  static PropertyDescriptor StaticValue(const char* utf8name,
1649
1804
  Napi::Value value,
1650
1805
  napi_property_attributes attributes = napi_default);
@@ -1657,8 +1812,11 @@ namespace Napi {
1657
1812
  static PropertyDescriptor InstanceValue(Symbol name,
1658
1813
  Napi::Value value,
1659
1814
  napi_property_attributes attributes = napi_default);
1815
+ virtual void Finalize(Napi::Env env);
1660
1816
 
1661
1817
  private:
1818
+ using This = ObjectWrap<T>;
1819
+
1662
1820
  static napi_value ConstructorCallbackWrapper(napi_env env, napi_callback_info info);
1663
1821
  static napi_value StaticVoidMethodCallbackWrapper(napi_env env, napi_callback_info info);
1664
1822
  static napi_value StaticMethodCallbackWrapper(napi_env env, napi_callback_info info);
@@ -1695,6 +1853,47 @@ namespace Napi {
1695
1853
  StaticAccessorCallbackData;
1696
1854
  typedef AccessorCallbackData<InstanceGetterCallback, InstanceSetterCallback>
1697
1855
  InstanceAccessorCallbackData;
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
+
1896
+ bool _construction_failed = true;
1698
1897
  };
1699
1898
 
1700
1899
  class HandleScope {
@@ -1703,6 +1902,9 @@ namespace Napi {
1703
1902
  explicit HandleScope(Napi::Env env);
1704
1903
  ~HandleScope();
1705
1904
 
1905
+ // Disallow copying to prevent double close of napi_handle_scope
1906
+ NAPI_DISALLOW_ASSIGN_COPY(HandleScope)
1907
+
1706
1908
  operator napi_handle_scope() const;
1707
1909
 
1708
1910
  Napi::Env Env() const;
@@ -1718,6 +1920,9 @@ namespace Napi {
1718
1920
  explicit EscapableHandleScope(Napi::Env env);
1719
1921
  ~EscapableHandleScope();
1720
1922
 
1923
+ // Disallow copying to prevent double close of napi_escapable_handle_scope
1924
+ NAPI_DISALLOW_ASSIGN_COPY(EscapableHandleScope)
1925
+
1721
1926
  operator napi_escapable_handle_scope() const;
1722
1927
 
1723
1928
  Napi::Env Env() const;
@@ -1735,6 +1940,9 @@ namespace Napi {
1735
1940
  CallbackScope(napi_env env, napi_async_context context);
1736
1941
  virtual ~CallbackScope();
1737
1942
 
1943
+ // Disallow copying to prevent double close of napi_callback_scope
1944
+ NAPI_DISALLOW_ASSIGN_COPY(CallbackScope)
1945
+
1738
1946
  operator napi_callback_scope() const;
1739
1947
 
1740
1948
  Napi::Env Env() const;
@@ -1753,11 +1961,12 @@ namespace Napi {
1753
1961
 
1754
1962
  AsyncContext(AsyncContext&& other);
1755
1963
  AsyncContext& operator =(AsyncContext&& other);
1756
- AsyncContext(const AsyncContext&) = delete;
1757
- AsyncContext& operator =(AsyncContext&) = delete;
1964
+ NAPI_DISALLOW_ASSIGN_COPY(AsyncContext)
1758
1965
 
1759
1966
  operator napi_async_context() const;
1760
1967
 
1968
+ Napi::Env Env() const;
1969
+
1761
1970
  private:
1762
1971
  napi_env _env;
1763
1972
  napi_async_context _context;
@@ -1770,8 +1979,7 @@ namespace Napi {
1770
1979
  // An async worker can be moved but cannot be copied.
1771
1980
  AsyncWorker(AsyncWorker&& other);
1772
1981
  AsyncWorker& operator =(AsyncWorker&& other);
1773
- AsyncWorker(const AsyncWorker&) = delete;
1774
- AsyncWorker& operator =(AsyncWorker&) = delete;
1982
+ NAPI_DISALLOW_ASSIGN_COPY(AsyncWorker)
1775
1983
 
1776
1984
  operator napi_async_work() const;
1777
1985
 
@@ -1784,6 +1992,10 @@ namespace Napi {
1784
1992
  ObjectReference& Receiver();
1785
1993
  FunctionReference& Callback();
1786
1994
 
1995
+ virtual void OnExecute(Napi::Env env);
1996
+ virtual void OnWorkComplete(Napi::Env env,
1997
+ napi_status status);
1998
+
1787
1999
  protected:
1788
2000
  explicit AsyncWorker(const Function& callback);
1789
2001
  explicit AsyncWorker(const Function& callback,
@@ -1817,10 +2029,10 @@ namespace Napi {
1817
2029
  void SetError(const std::string& error);
1818
2030
 
1819
2031
  private:
1820
- static void OnExecute(napi_env env, void* this_pointer);
1821
- static void OnWorkComplete(napi_env env,
1822
- napi_status status,
1823
- 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);
1824
2036
 
1825
2037
  napi_env _env;
1826
2038
  napi_async_work _work;
@@ -1960,8 +2172,7 @@ namespace Napi {
1960
2172
  ThreadSafeFunction();
1961
2173
  ThreadSafeFunction(napi_threadsafe_function tsFunctionValue);
1962
2174
 
1963
- ThreadSafeFunction(ThreadSafeFunction&& other);
1964
- ThreadSafeFunction& operator=(ThreadSafeFunction&& other);
2175
+ operator napi_threadsafe_function() const;
1965
2176
 
1966
2177
  // This API may be called from any thread.
1967
2178
  napi_status BlockingCall() const;
@@ -1985,6 +2196,12 @@ namespace Napi {
1985
2196
  template <typename DataType, typename Callback>
1986
2197
  napi_status NonBlockingCall(DataType* data, Callback callback) const;
1987
2198
 
2199
+ // This API may only be called from the main thread.
2200
+ void Ref(napi_env env) const;
2201
+
2202
+ // This API may only be called from the main thread.
2203
+ void Unref(napi_env env) const;
2204
+
1988
2205
  // This API may be called from any thread.
1989
2206
  napi_status Acquire() const;
1990
2207
 
@@ -2028,7 +2245,165 @@ namespace Napi {
2028
2245
  void* context,
2029
2246
  void* data);
2030
2247
 
2031
- std::unique_ptr<napi_threadsafe_function> _tsfn;
2248
+ napi_threadsafe_function _tsfn;
2249
+ };
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
+
2298
+ template<class T>
2299
+ class AsyncProgressWorker : public AsyncProgressWorkerBase<void> {
2300
+ public:
2301
+ virtual ~AsyncProgressWorker();
2302
+
2303
+ class ExecutionProgress {
2304
+ friend class AsyncProgressWorker;
2305
+ public:
2306
+ void Signal() const;
2307
+ void Send(const T* data, size_t count) const;
2308
+ private:
2309
+ explicit ExecutionProgress(AsyncProgressWorker* worker) : _worker(worker) {}
2310
+ AsyncProgressWorker* const _worker;
2311
+ };
2312
+
2313
+ void OnWorkProgress(void*) override;
2314
+
2315
+ protected:
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);
2331
+
2332
+ // Optional callback of Napi::ThreadSafeFunction only available after NAPI_VERSION 4.
2333
+ // Refs: https://github.com/nodejs/node/pull/27791
2334
+ #if NAPI_VERSION > 4
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);
2341
+ #endif
2342
+ virtual void Execute(const ExecutionProgress& progress) = 0;
2343
+ virtual void OnProgress(const T* data, size_t count) = 0;
2344
+
2345
+ private:
2346
+ void Execute() override;
2347
+ void Signal() const;
2348
+ void SendProgress_(const T* data, size_t count);
2349
+
2350
+ std::mutex _mutex;
2351
+ T* _asyncdata;
2352
+ size_t _asyncsize;
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);
2032
2407
  };
2033
2408
  #endif
2034
2409