node-addon-api 1.6.3 → 2.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.
package/napi.h CHANGED
@@ -1,9 +1,11 @@
1
1
  #ifndef SRC_NAPI_H_
2
2
  #define SRC_NAPI_H_
3
3
 
4
- #include "node_api.h"
4
+ #include <node_api.h>
5
5
  #include <functional>
6
6
  #include <initializer_list>
7
+ #include <memory>
8
+ #include <mutex>
7
9
  #include <string>
8
10
  #include <vector>
9
11
 
@@ -50,10 +52,10 @@ static_assert(sizeof(char16_t) == sizeof(wchar_t), "Size mismatch between char16
50
52
  #define NAPI_THROW_VOID(e) throw e
51
53
 
52
54
  #define NAPI_THROW_IF_FAILED(env, status, ...) \
53
- if ((status) != napi_ok) throw Error::New(env);
55
+ if ((status) != napi_ok) throw Napi::Error::New(env);
54
56
 
55
57
  #define NAPI_THROW_IF_FAILED_VOID(env, status) \
56
- if ((status) != napi_ok) throw Error::New(env);
58
+ if ((status) != napi_ok) throw Napi::Error::New(env);
57
59
 
58
60
  #else // NAPI_CPP_EXCEPTIONS
59
61
 
@@ -77,13 +79,13 @@ static_assert(sizeof(char16_t) == sizeof(wchar_t), "Size mismatch between char16
77
79
 
78
80
  #define NAPI_THROW_IF_FAILED(env, status, ...) \
79
81
  if ((status) != napi_ok) { \
80
- Error::New(env).ThrowAsJavaScriptException(); \
82
+ Napi::Error::New(env).ThrowAsJavaScriptException(); \
81
83
  return __VA_ARGS__; \
82
84
  }
83
85
 
84
86
  #define NAPI_THROW_IF_FAILED_VOID(env, status) \
85
87
  if ((status) != napi_ok) { \
86
- Error::New(env).ThrowAsJavaScriptException(); \
88
+ Napi::Error::New(env).ThrowAsJavaScriptException(); \
87
89
  return; \
88
90
  }
89
91
 
@@ -92,7 +94,7 @@ static_assert(sizeof(char16_t) == sizeof(wchar_t), "Size mismatch between char16
92
94
  #define NAPI_FATAL_IF_FAILED(status, location, message) \
93
95
  do { \
94
96
  if ((status) != napi_ok) { \
95
- Error::Fatal((location), (message)); \
97
+ Napi::Error::Fatal((location), (message)); \
96
98
  } \
97
99
  } while (0)
98
100
 
@@ -110,11 +112,15 @@ namespace Napi {
110
112
  class Value;
111
113
  class Boolean;
112
114
  class Number;
113
- // currently experimental guard with version of NAPI_VERSION that it is
114
- // released in once it is no longer experimental
115
- #if (NAPI_VERSION > 2147483646)
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
116
119
  class BigInt;
117
120
  #endif // NAPI_EXPERIMENTAL
121
+ #if (NAPI_VERSION > 4)
122
+ class Date;
123
+ #endif
118
124
  class String;
119
125
  class Object;
120
126
  class Array;
@@ -135,9 +141,10 @@ namespace Napi {
135
141
  typedef TypedArrayOf<uint32_t> Uint32Array; ///< Typed-array of unsigned 32-bit integers
136
142
  typedef TypedArrayOf<float> Float32Array; ///< Typed-array of 32-bit floating-point values
137
143
  typedef TypedArrayOf<double> Float64Array; ///< Typed-array of 64-bit floating-point values
138
- // currently experimental guard with version of NAPI_VERSION that it is
139
- // released in once it is no longer experimental
140
- #if (NAPI_VERSION > 2147483646)
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
141
148
  typedef TypedArrayOf<int64_t> BigInt64Array; ///< Typed array of signed 64-bit integers
142
149
  typedef TypedArrayOf<uint64_t> BigUint64Array; ///< Typed array of unsigned 64-bit integers
143
150
  #endif // NAPI_EXPERIMENTAL
@@ -240,11 +247,15 @@ namespace Napi {
240
247
  bool IsNull() const; ///< Tests if a value is a null JavaScript value.
241
248
  bool IsBoolean() const; ///< Tests if a value is a JavaScript boolean.
242
249
  bool IsNumber() const; ///< Tests if a value is a JavaScript number.
243
- // currently experimental guard with version of NAPI_VERSION that it is
244
- // released in once it is no longer experimental
245
- #if (NAPI_VERSION > 2147483646)
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
246
254
  bool IsBigInt() const; ///< Tests if a value is a JavaScript bigint.
247
255
  #endif // NAPI_EXPERIMENTAL
256
+ #if (NAPI_VERSION > 4)
257
+ bool IsDate() const; ///< Tests if a value is a JavaScript date.
258
+ #endif
248
259
  bool IsString() const; ///< Tests if a value is a JavaScript string.
249
260
  bool IsSymbol() const; ///< Tests if a value is a JavaScript symbol.
250
261
  bool IsArray() const; ///< Tests if a value is a JavaScript array.
@@ -314,9 +325,10 @@ namespace Napi {
314
325
  double DoubleValue() const; ///< Converts a Number value to a 64-bit floating-point value.
315
326
  };
316
327
 
317
- // currently experimental guard with version of NAPI_VERSION that it is
318
- // released in once it is no longer experimental
319
- #if (NAPI_VERSION > 2147483646)
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
320
332
  /// A JavaScript bigint value.
321
333
  class BigInt : public Value {
322
334
  public:
@@ -357,6 +369,24 @@ namespace Napi {
357
369
  };
358
370
  #endif // NAPI_EXPERIMENTAL
359
371
 
372
+ #if (NAPI_VERSION > 4)
373
+ /// A JavaScript date value.
374
+ class Date : public Value {
375
+ public:
376
+ /// Creates a new Date value from a double primitive.
377
+ static Date New(
378
+ napi_env env, ///< N-API environment
379
+ double value ///< Number value
380
+ );
381
+
382
+ Date(); ///< Creates a new _empty_ Date instance.
383
+ Date(napi_env env, napi_value value); ///< Wraps a N-API value primitive.
384
+ operator double() const; ///< Converts a Date value to double primitive
385
+
386
+ double ValueOf() const; ///< Converts a Date value to a double primitive.
387
+ };
388
+ #endif
389
+
360
390
  /// A JavaScript string or symbol value (that can be used as a property name).
361
391
  class Name : public Value {
362
392
  public:
@@ -684,6 +714,14 @@ namespace Napi {
684
714
  bool InstanceOf(
685
715
  const Function& constructor ///< Constructor function
686
716
  ) const;
717
+
718
+ template <typename Finalizer, typename T>
719
+ inline void AddFinalizer(Finalizer finalizeCallback, T* data);
720
+
721
+ template <typename Finalizer, typename T, typename Hint>
722
+ inline void AddFinalizer(Finalizer finalizeCallback,
723
+ T* data,
724
+ Hint* finalizeHint);
687
725
  };
688
726
 
689
727
  template <typename T>
@@ -818,9 +856,10 @@ namespace Napi {
818
856
  : std::is_same<T, uint32_t>::value ? napi_uint32_array
819
857
  : std::is_same<T, float>::value ? napi_float32_array
820
858
  : std::is_same<T, double>::value ? napi_float64_array
821
- // currently experimental guard with version of NAPI_VERSION that it is
822
- // released in once it is no longer experimental
823
- #if (NAPI_VERSION > 2147483646)
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
824
863
  : std::is_same<T, int64_t>::value ? napi_bigint64_array
825
864
  : std::is_same<T, uint64_t>::value ? napi_biguint64_array
826
865
  #endif // NAPI_EXPERIMENTAL
@@ -1569,6 +1608,7 @@ namespace Napi {
1569
1608
  class ObjectWrap : public Reference<Object> {
1570
1609
  public:
1571
1610
  ObjectWrap(const CallbackInfo& callbackInfo);
1611
+ virtual ~ObjectWrap();
1572
1612
 
1573
1613
  static T* Unwrap(Object wrapper);
1574
1614
 
@@ -1656,6 +1696,7 @@ namespace Napi {
1656
1696
  static PropertyDescriptor InstanceValue(Symbol name,
1657
1697
  Napi::Value value,
1658
1698
  napi_property_attributes attributes = napi_default);
1699
+ virtual void Finalize(Napi::Env env);
1659
1700
 
1660
1701
  private:
1661
1702
  static napi_value ConstructorCallbackWrapper(napi_env env, napi_callback_info info);
@@ -1702,6 +1743,10 @@ namespace Napi {
1702
1743
  explicit HandleScope(Napi::Env env);
1703
1744
  ~HandleScope();
1704
1745
 
1746
+ // Disallow copying to prevent double close of napi_handle_scope
1747
+ HandleScope(HandleScope const &) = delete;
1748
+ void operator=(HandleScope const &) = delete;
1749
+
1705
1750
  operator napi_handle_scope() const;
1706
1751
 
1707
1752
  Napi::Env Env() const;
@@ -1717,6 +1762,10 @@ namespace Napi {
1717
1762
  explicit EscapableHandleScope(Napi::Env env);
1718
1763
  ~EscapableHandleScope();
1719
1764
 
1765
+ // Disallow copying to prevent double close of napi_escapable_handle_scope
1766
+ EscapableHandleScope(EscapableHandleScope const &) = delete;
1767
+ void operator=(EscapableHandleScope const &) = delete;
1768
+
1720
1769
  operator napi_escapable_handle_scope() const;
1721
1770
 
1722
1771
  Napi::Env Env() const;
@@ -1734,6 +1783,10 @@ namespace Napi {
1734
1783
  CallbackScope(napi_env env, napi_async_context context);
1735
1784
  virtual ~CallbackScope();
1736
1785
 
1786
+ // Disallow copying to prevent double close of napi_callback_scope
1787
+ CallbackScope(CallbackScope const &) = delete;
1788
+ void operator=(CallbackScope const &) = delete;
1789
+
1737
1790
  operator napi_callback_scope() const;
1738
1791
 
1739
1792
  Napi::Env Env() const;
@@ -1757,6 +1810,8 @@ namespace Napi {
1757
1810
 
1758
1811
  operator napi_async_context() const;
1759
1812
 
1813
+ Napi::Env Env() const;
1814
+
1760
1815
  private:
1761
1816
  napi_env _env;
1762
1817
  napi_async_context _context;
@@ -1800,9 +1855,18 @@ namespace Napi {
1800
1855
  const char* resource_name,
1801
1856
  const Object& resource);
1802
1857
 
1858
+ explicit AsyncWorker(Napi::Env env);
1859
+ explicit AsyncWorker(Napi::Env env,
1860
+ const char* resource_name);
1861
+ explicit AsyncWorker(Napi::Env env,
1862
+ const char* resource_name,
1863
+ const Object& resource);
1864
+
1803
1865
  virtual void Execute() = 0;
1804
1866
  virtual void OnOK();
1805
1867
  virtual void OnError(const Error& e);
1868
+ virtual void Destroy();
1869
+ virtual std::vector<napi_value> GetResult(Napi::Env env);
1806
1870
 
1807
1871
  void SetError(const std::string& error);
1808
1872
 
@@ -1820,6 +1884,272 @@ namespace Napi {
1820
1884
  bool _suppress_destruct;
1821
1885
  };
1822
1886
 
1887
+ #if (NAPI_VERSION > 3)
1888
+ class ThreadSafeFunction {
1889
+ public:
1890
+ // This API may only be called from the main thread.
1891
+ template <typename ResourceString>
1892
+ static ThreadSafeFunction New(napi_env env,
1893
+ const Function& callback,
1894
+ ResourceString resourceName,
1895
+ size_t maxQueueSize,
1896
+ size_t initialThreadCount);
1897
+
1898
+ // This API may only be called from the main thread.
1899
+ template <typename ResourceString, typename ContextType>
1900
+ static ThreadSafeFunction New(napi_env env,
1901
+ const Function& callback,
1902
+ ResourceString resourceName,
1903
+ size_t maxQueueSize,
1904
+ size_t initialThreadCount,
1905
+ ContextType* context);
1906
+
1907
+ // This API may only be called from the main thread.
1908
+ template <typename ResourceString, typename Finalizer>
1909
+ static ThreadSafeFunction New(napi_env env,
1910
+ const Function& callback,
1911
+ ResourceString resourceName,
1912
+ size_t maxQueueSize,
1913
+ size_t initialThreadCount,
1914
+ Finalizer finalizeCallback);
1915
+
1916
+ // This API may only be called from the main thread.
1917
+ template <typename ResourceString, typename Finalizer,
1918
+ typename FinalizerDataType>
1919
+ static ThreadSafeFunction New(napi_env env,
1920
+ const Function& callback,
1921
+ ResourceString resourceName,
1922
+ size_t maxQueueSize,
1923
+ size_t initialThreadCount,
1924
+ Finalizer finalizeCallback,
1925
+ FinalizerDataType* data);
1926
+
1927
+ // This API may only be called from the main thread.
1928
+ template <typename ResourceString, typename ContextType, typename Finalizer>
1929
+ static ThreadSafeFunction New(napi_env env,
1930
+ const Function& callback,
1931
+ ResourceString resourceName,
1932
+ size_t maxQueueSize,
1933
+ size_t initialThreadCount,
1934
+ ContextType* context,
1935
+ Finalizer finalizeCallback);
1936
+
1937
+ // This API may only be called from the main thread.
1938
+ template <typename ResourceString, typename ContextType,
1939
+ typename Finalizer, typename FinalizerDataType>
1940
+ static ThreadSafeFunction New(napi_env env,
1941
+ const Function& callback,
1942
+ ResourceString resourceName,
1943
+ size_t maxQueueSize,
1944
+ size_t initialThreadCount,
1945
+ ContextType* context,
1946
+ Finalizer finalizeCallback,
1947
+ FinalizerDataType* data);
1948
+
1949
+ // This API may only be called from the main thread.
1950
+ template <typename ResourceString>
1951
+ static ThreadSafeFunction New(napi_env env,
1952
+ const Function& callback,
1953
+ const Object& resource,
1954
+ ResourceString resourceName,
1955
+ size_t maxQueueSize,
1956
+ size_t initialThreadCount);
1957
+
1958
+ // This API may only be called from the main thread.
1959
+ template <typename ResourceString, typename ContextType>
1960
+ static ThreadSafeFunction New(napi_env env,
1961
+ const Function& callback,
1962
+ const Object& resource,
1963
+ ResourceString resourceName,
1964
+ size_t maxQueueSize,
1965
+ size_t initialThreadCount,
1966
+ ContextType* context);
1967
+
1968
+ // This API may only be called from the main thread.
1969
+ template <typename ResourceString, typename Finalizer>
1970
+ static ThreadSafeFunction New(napi_env env,
1971
+ const Function& callback,
1972
+ const Object& resource,
1973
+ ResourceString resourceName,
1974
+ size_t maxQueueSize,
1975
+ size_t initialThreadCount,
1976
+ Finalizer finalizeCallback);
1977
+
1978
+ // This API may only be called from the main thread.
1979
+ template <typename ResourceString, typename Finalizer,
1980
+ typename FinalizerDataType>
1981
+ static ThreadSafeFunction New(napi_env env,
1982
+ const Function& callback,
1983
+ const Object& resource,
1984
+ ResourceString resourceName,
1985
+ size_t maxQueueSize,
1986
+ size_t initialThreadCount,
1987
+ Finalizer finalizeCallback,
1988
+ FinalizerDataType* data);
1989
+
1990
+ // This API may only be called from the main thread.
1991
+ template <typename ResourceString, typename ContextType, typename Finalizer>
1992
+ static ThreadSafeFunction New(napi_env env,
1993
+ const Function& callback,
1994
+ const Object& resource,
1995
+ ResourceString resourceName,
1996
+ size_t maxQueueSize,
1997
+ size_t initialThreadCount,
1998
+ ContextType* context,
1999
+ Finalizer finalizeCallback);
2000
+
2001
+ // This API may only be called from the main thread.
2002
+ template <typename ResourceString, typename ContextType,
2003
+ typename Finalizer, typename FinalizerDataType>
2004
+ static ThreadSafeFunction New(napi_env env,
2005
+ const Function& callback,
2006
+ const Object& resource,
2007
+ ResourceString resourceName,
2008
+ size_t maxQueueSize,
2009
+ size_t initialThreadCount,
2010
+ ContextType* context,
2011
+ Finalizer finalizeCallback,
2012
+ FinalizerDataType* data);
2013
+
2014
+ ThreadSafeFunction();
2015
+ ThreadSafeFunction(napi_threadsafe_function tsFunctionValue);
2016
+
2017
+ operator napi_threadsafe_function() const;
2018
+
2019
+ // This API may be called from any thread.
2020
+ napi_status BlockingCall() const;
2021
+
2022
+ // This API may be called from any thread.
2023
+ template <typename Callback>
2024
+ napi_status BlockingCall(Callback callback) const;
2025
+
2026
+ // This API may be called from any thread.
2027
+ template <typename DataType, typename Callback>
2028
+ napi_status BlockingCall(DataType* data, Callback callback) const;
2029
+
2030
+ // This API may be called from any thread.
2031
+ napi_status NonBlockingCall() const;
2032
+
2033
+ // This API may be called from any thread.
2034
+ template <typename Callback>
2035
+ napi_status NonBlockingCall(Callback callback) const;
2036
+
2037
+ // This API may be called from any thread.
2038
+ template <typename DataType, typename Callback>
2039
+ napi_status NonBlockingCall(DataType* data, Callback callback) const;
2040
+
2041
+ // This API may only be called from the main thread.
2042
+ void Ref(napi_env env) const;
2043
+
2044
+ // This API may only be called from the main thread.
2045
+ void Unref(napi_env env) const;
2046
+
2047
+ // This API may be called from any thread.
2048
+ napi_status Acquire() const;
2049
+
2050
+ // This API may be called from any thread.
2051
+ napi_status Release();
2052
+
2053
+ // This API may be called from any thread.
2054
+ napi_status Abort();
2055
+
2056
+ struct ConvertibleContext
2057
+ {
2058
+ template <class T>
2059
+ operator T*() { return static_cast<T*>(context); }
2060
+ void* context;
2061
+ };
2062
+
2063
+ // This API may be called from any thread.
2064
+ ConvertibleContext GetContext() const;
2065
+
2066
+ private:
2067
+ using CallbackWrapper = std::function<void(Napi::Env, Napi::Function)>;
2068
+
2069
+ template <typename ResourceString, typename ContextType,
2070
+ typename Finalizer, typename FinalizerDataType>
2071
+ static ThreadSafeFunction New(napi_env env,
2072
+ const Function& callback,
2073
+ const Object& resource,
2074
+ ResourceString resourceName,
2075
+ size_t maxQueueSize,
2076
+ size_t initialThreadCount,
2077
+ ContextType* context,
2078
+ Finalizer finalizeCallback,
2079
+ FinalizerDataType* data,
2080
+ napi_finalize wrapper);
2081
+
2082
+ napi_status CallInternal(CallbackWrapper* callbackWrapper,
2083
+ napi_threadsafe_function_call_mode mode) const;
2084
+
2085
+ static void CallJS(napi_env env,
2086
+ napi_value jsCallback,
2087
+ void* context,
2088
+ void* data);
2089
+
2090
+ napi_threadsafe_function _tsfn;
2091
+ };
2092
+
2093
+ template<class T>
2094
+ class AsyncProgressWorker : public AsyncWorker {
2095
+ public:
2096
+ virtual ~AsyncProgressWorker();
2097
+
2098
+ class ExecutionProgress {
2099
+ friend class AsyncProgressWorker;
2100
+ public:
2101
+ void Signal() const;
2102
+ void Send(const T* data, size_t count) const;
2103
+ private:
2104
+ explicit ExecutionProgress(AsyncProgressWorker* worker) : _worker(worker) {}
2105
+ AsyncProgressWorker* const _worker;
2106
+ };
2107
+
2108
+ 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);
2124
+
2125
+ // Optional callback of Napi::ThreadSafeFunction only available after NAPI_VERSION 4.
2126
+ // Refs: https://github.com/nodejs/node/pull/27791
2127
+ #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);
2134
+ #endif
2135
+
2136
+ virtual void Execute(const ExecutionProgress& progress) = 0;
2137
+ virtual void OnProgress(const T* data, size_t count) = 0;
2138
+
2139
+ private:
2140
+ static void WorkProgress_(Napi::Env env, Napi::Function jsCallback, void* data);
2141
+
2142
+ void Execute() override;
2143
+ void Signal() const;
2144
+ void SendProgress_(const T* data, size_t count);
2145
+
2146
+ std::mutex _mutex;
2147
+ T* _asyncdata;
2148
+ size_t _asyncsize;
2149
+ ThreadSafeFunction _tsfn;
2150
+ };
2151
+ #endif
2152
+
1823
2153
  // Memory management.
1824
2154
  class MemoryManagement {
1825
2155
  public: