node-addon-api 1.6.2 → 1.7.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.
package/napi.h CHANGED
@@ -1,9 +1,10 @@
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>
7
8
  #include <string>
8
9
  #include <vector>
9
10
 
@@ -38,6 +39,64 @@ static_assert(sizeof(char16_t) == sizeof(wchar_t), "Size mismatch between char16
38
39
  #define NAPI_NOEXCEPT noexcept
39
40
  #endif
40
41
 
42
+ #ifdef NAPI_CPP_EXCEPTIONS
43
+
44
+ // When C++ exceptions are enabled, Errors are thrown directly. There is no need
45
+ // to return anything after the throw statements. The variadic parameter is an
46
+ // optional return value that is ignored.
47
+ // We need _VOID versions of the macros to avoid warnings resulting from
48
+ // leaving the NAPI_THROW_* `...` argument empty.
49
+
50
+ #define NAPI_THROW(e, ...) throw e
51
+ #define NAPI_THROW_VOID(e) throw e
52
+
53
+ #define NAPI_THROW_IF_FAILED(env, status, ...) \
54
+ if ((status) != napi_ok) throw Napi::Error::New(env);
55
+
56
+ #define NAPI_THROW_IF_FAILED_VOID(env, status) \
57
+ if ((status) != napi_ok) throw Napi::Error::New(env);
58
+
59
+ #else // NAPI_CPP_EXCEPTIONS
60
+
61
+ // When C++ exceptions are disabled, Errors are thrown as JavaScript exceptions,
62
+ // which are pending until the callback returns to JS. The variadic parameter
63
+ // is an optional return value; usually it is an empty result.
64
+ // We need _VOID versions of the macros to avoid warnings resulting from
65
+ // leaving the NAPI_THROW_* `...` argument empty.
66
+
67
+ #define NAPI_THROW(e, ...) \
68
+ do { \
69
+ (e).ThrowAsJavaScriptException(); \
70
+ return __VA_ARGS__; \
71
+ } while (0)
72
+
73
+ #define NAPI_THROW_VOID(e) \
74
+ do { \
75
+ (e).ThrowAsJavaScriptException(); \
76
+ return; \
77
+ } while (0)
78
+
79
+ #define NAPI_THROW_IF_FAILED(env, status, ...) \
80
+ if ((status) != napi_ok) { \
81
+ Napi::Error::New(env).ThrowAsJavaScriptException(); \
82
+ return __VA_ARGS__; \
83
+ }
84
+
85
+ #define NAPI_THROW_IF_FAILED_VOID(env, status) \
86
+ if ((status) != napi_ok) { \
87
+ Napi::Error::New(env).ThrowAsJavaScriptException(); \
88
+ return; \
89
+ }
90
+
91
+ #endif // NAPI_CPP_EXCEPTIONS
92
+
93
+ #define NAPI_FATAL_IF_FAILED(status, location, message) \
94
+ do { \
95
+ if ((status) != napi_ok) { \
96
+ Napi::Error::Fatal((location), (message)); \
97
+ } \
98
+ } while (0)
99
+
41
100
  ////////////////////////////////////////////////////////////////////////////////
42
101
  /// N-API C++ Wrapper Classes
43
102
  ///
@@ -601,7 +660,7 @@ namespace Napi {
601
660
  uint32_t index ///< Property / element index
602
661
  );
603
662
 
604
- Array GetPropertyNames(); ///< Get all property names
663
+ Array GetPropertyNames() const; ///< Get all property names
605
664
 
606
665
  /// Defines a property on the object.
607
666
  void DefineProperty(
@@ -633,12 +692,12 @@ namespace Napi {
633
692
  public:
634
693
  static External New(napi_env env, T* data);
635
694
 
636
- // Finalizer must implement operator() accepting a T* and returning void.
695
+ // Finalizer must implement `void operator()(Env env, T* data)`.
637
696
  template <typename Finalizer>
638
697
  static External New(napi_env env,
639
698
  T* data,
640
699
  Finalizer finalizeCallback);
641
- // Finalizer must implement operator() accepting a T* and Hint* and returning void.
700
+ // Finalizer must implement `void operator()(Env env, T* data, Hint* hint)`.
642
701
  template <typename Finalizer, typename Hint>
643
702
  static External New(napi_env env,
644
703
  T* data,
@@ -686,8 +745,7 @@ namespace Napi {
686
745
  size_t byteLength, ///< Length of the external buffer to be used by the array,
687
746
  /// in bytes
688
747
  Finalizer finalizeCallback ///< Function to be called when the array buffer is destroyed;
689
- /// must implement `operator()`, accept a `void*` (which is the
690
- /// data buffer pointer), and return `void`
748
+ /// must implement `void operator()(Env env, void* externalData)`
691
749
  );
692
750
 
693
751
  /// Creates a new ArrayBuffer instance, using an external buffer with specified byte length.
@@ -698,8 +756,7 @@ namespace Napi {
698
756
  size_t byteLength, ///< Length of the external buffer to be used by the array,
699
757
  /// in bytes
700
758
  Finalizer finalizeCallback, ///< Function to be called when the array buffer is destroyed;
701
- /// must implement `operator()`, accept a `void*` (which is the
702
- /// data buffer pointer) and `Hint*`, and return `void`
759
+ /// must implement `void operator()(Env env, void* externalData, Hint* hint)`
703
760
  Hint* finalizeHint ///< Hint (second parameter) to be passed to the finalize callback
704
761
  );
705
762
 
@@ -969,12 +1026,12 @@ namespace Napi {
969
1026
  static Buffer<T> New(napi_env env, size_t length);
970
1027
  static Buffer<T> New(napi_env env, T* data, size_t length);
971
1028
 
972
- // Finalizer must implement operator() accepting a T* and returning void.
1029
+ // Finalizer must implement `void operator()(Env env, T* data)`.
973
1030
  template <typename Finalizer>
974
1031
  static Buffer<T> New(napi_env env, T* data,
975
1032
  size_t length,
976
1033
  Finalizer finalizeCallback);
977
- // Finalizer must implement operator() accepting a T* and Hint* and returning void.
1034
+ // Finalizer must implement `void operator()(Env env, T* data, Hint* hint)`.
978
1035
  template <typename Finalizer, typename Hint>
979
1036
  static Buffer<T> New(napi_env env, T* data,
980
1037
  size_t length,
@@ -1722,6 +1779,7 @@ namespace Napi {
1722
1779
 
1723
1780
  void Queue();
1724
1781
  void Cancel();
1782
+ void SuppressDestruct();
1725
1783
 
1726
1784
  ObjectReference& Receiver();
1727
1785
  FunctionReference& Callback();
@@ -1743,9 +1801,18 @@ namespace Napi {
1743
1801
  const char* resource_name,
1744
1802
  const Object& resource);
1745
1803
 
1804
+ explicit AsyncWorker(Napi::Env env);
1805
+ explicit AsyncWorker(Napi::Env env,
1806
+ const char* resource_name);
1807
+ explicit AsyncWorker(Napi::Env env,
1808
+ const char* resource_name,
1809
+ const Object& resource);
1810
+
1746
1811
  virtual void Execute() = 0;
1747
1812
  virtual void OnOK();
1748
1813
  virtual void OnError(const Error& e);
1814
+ virtual void Destroy();
1815
+ virtual std::vector<napi_value> GetResult(Napi::Env env);
1749
1816
 
1750
1817
  void SetError(const std::string& error);
1751
1818
 
@@ -1760,7 +1827,210 @@ namespace Napi {
1760
1827
  ObjectReference _receiver;
1761
1828
  FunctionReference _callback;
1762
1829
  std::string _error;
1830
+ bool _suppress_destruct;
1831
+ };
1832
+
1833
+ #if (NAPI_VERSION > 3)
1834
+ class ThreadSafeFunction {
1835
+ public:
1836
+ // This API may only be called from the main thread.
1837
+ template <typename ResourceString>
1838
+ static ThreadSafeFunction New(napi_env env,
1839
+ const Function& callback,
1840
+ ResourceString resourceName,
1841
+ size_t maxQueueSize,
1842
+ size_t initialThreadCount);
1843
+
1844
+ // This API may only be called from the main thread.
1845
+ template <typename ResourceString, typename ContextType>
1846
+ static ThreadSafeFunction New(napi_env env,
1847
+ const Function& callback,
1848
+ ResourceString resourceName,
1849
+ size_t maxQueueSize,
1850
+ size_t initialThreadCount,
1851
+ ContextType* context);
1852
+
1853
+ // This API may only be called from the main thread.
1854
+ template <typename ResourceString, typename Finalizer>
1855
+ static ThreadSafeFunction New(napi_env env,
1856
+ const Function& callback,
1857
+ ResourceString resourceName,
1858
+ size_t maxQueueSize,
1859
+ size_t initialThreadCount,
1860
+ Finalizer finalizeCallback);
1861
+
1862
+ // This API may only be called from the main thread.
1863
+ template <typename ResourceString, typename Finalizer,
1864
+ typename FinalizerDataType>
1865
+ static ThreadSafeFunction New(napi_env env,
1866
+ const Function& callback,
1867
+ ResourceString resourceName,
1868
+ size_t maxQueueSize,
1869
+ size_t initialThreadCount,
1870
+ Finalizer finalizeCallback,
1871
+ FinalizerDataType* data);
1872
+
1873
+ // This API may only be called from the main thread.
1874
+ template <typename ResourceString, typename ContextType, typename Finalizer>
1875
+ static ThreadSafeFunction New(napi_env env,
1876
+ const Function& callback,
1877
+ ResourceString resourceName,
1878
+ size_t maxQueueSize,
1879
+ size_t initialThreadCount,
1880
+ ContextType* context,
1881
+ Finalizer finalizeCallback);
1882
+
1883
+ // This API may only be called from the main thread.
1884
+ template <typename ResourceString, typename ContextType,
1885
+ typename Finalizer, typename FinalizerDataType>
1886
+ static ThreadSafeFunction New(napi_env env,
1887
+ const Function& callback,
1888
+ ResourceString resourceName,
1889
+ size_t maxQueueSize,
1890
+ size_t initialThreadCount,
1891
+ ContextType* context,
1892
+ Finalizer finalizeCallback,
1893
+ FinalizerDataType* data);
1894
+
1895
+ // This API may only be called from the main thread.
1896
+ template <typename ResourceString>
1897
+ static ThreadSafeFunction New(napi_env env,
1898
+ const Function& callback,
1899
+ const Object& resource,
1900
+ ResourceString resourceName,
1901
+ size_t maxQueueSize,
1902
+ size_t initialThreadCount);
1903
+
1904
+ // This API may only be called from the main thread.
1905
+ template <typename ResourceString, typename ContextType>
1906
+ static ThreadSafeFunction New(napi_env env,
1907
+ const Function& callback,
1908
+ const Object& resource,
1909
+ ResourceString resourceName,
1910
+ size_t maxQueueSize,
1911
+ size_t initialThreadCount,
1912
+ ContextType* context);
1913
+
1914
+ // This API may only be called from the main thread.
1915
+ template <typename ResourceString, typename Finalizer>
1916
+ static ThreadSafeFunction New(napi_env env,
1917
+ const Function& callback,
1918
+ const Object& resource,
1919
+ ResourceString resourceName,
1920
+ size_t maxQueueSize,
1921
+ size_t initialThreadCount,
1922
+ Finalizer finalizeCallback);
1923
+
1924
+ // This API may only be called from the main thread.
1925
+ template <typename ResourceString, typename Finalizer,
1926
+ typename FinalizerDataType>
1927
+ static ThreadSafeFunction New(napi_env env,
1928
+ const Function& callback,
1929
+ const Object& resource,
1930
+ ResourceString resourceName,
1931
+ size_t maxQueueSize,
1932
+ size_t initialThreadCount,
1933
+ Finalizer finalizeCallback,
1934
+ FinalizerDataType* data);
1935
+
1936
+ // This API may only be called from the main thread.
1937
+ template <typename ResourceString, typename ContextType, typename Finalizer>
1938
+ static ThreadSafeFunction New(napi_env env,
1939
+ const Function& callback,
1940
+ const Object& resource,
1941
+ ResourceString resourceName,
1942
+ size_t maxQueueSize,
1943
+ size_t initialThreadCount,
1944
+ ContextType* context,
1945
+ Finalizer finalizeCallback);
1946
+
1947
+ // This API may only be called from the main thread.
1948
+ template <typename ResourceString, typename ContextType,
1949
+ typename Finalizer, typename FinalizerDataType>
1950
+ static ThreadSafeFunction New(napi_env env,
1951
+ const Function& callback,
1952
+ const Object& resource,
1953
+ ResourceString resourceName,
1954
+ size_t maxQueueSize,
1955
+ size_t initialThreadCount,
1956
+ ContextType* context,
1957
+ Finalizer finalizeCallback,
1958
+ FinalizerDataType* data);
1959
+
1960
+ ThreadSafeFunction();
1961
+ ThreadSafeFunction(napi_threadsafe_function tsFunctionValue);
1962
+
1963
+ ThreadSafeFunction(ThreadSafeFunction&& other);
1964
+ ThreadSafeFunction& operator=(ThreadSafeFunction&& other);
1965
+
1966
+ // This API may be called from any thread.
1967
+ napi_status BlockingCall() const;
1968
+
1969
+ // This API may be called from any thread.
1970
+ template <typename Callback>
1971
+ napi_status BlockingCall(Callback callback) const;
1972
+
1973
+ // This API may be called from any thread.
1974
+ template <typename DataType, typename Callback>
1975
+ napi_status BlockingCall(DataType* data, Callback callback) const;
1976
+
1977
+ // This API may be called from any thread.
1978
+ napi_status NonBlockingCall() const;
1979
+
1980
+ // This API may be called from any thread.
1981
+ template <typename Callback>
1982
+ napi_status NonBlockingCall(Callback callback) const;
1983
+
1984
+ // This API may be called from any thread.
1985
+ template <typename DataType, typename Callback>
1986
+ napi_status NonBlockingCall(DataType* data, Callback callback) const;
1987
+
1988
+ // This API may be called from any thread.
1989
+ napi_status Acquire() const;
1990
+
1991
+ // This API may be called from any thread.
1992
+ napi_status Release();
1993
+
1994
+ // This API may be called from any thread.
1995
+ napi_status Abort();
1996
+
1997
+ struct ConvertibleContext
1998
+ {
1999
+ template <class T>
2000
+ operator T*() { return static_cast<T*>(context); }
2001
+ void* context;
2002
+ };
2003
+
2004
+ // This API may be called from any thread.
2005
+ ConvertibleContext GetContext() const;
2006
+
2007
+ private:
2008
+ using CallbackWrapper = std::function<void(Napi::Env, Napi::Function)>;
2009
+
2010
+ template <typename ResourceString, typename ContextType,
2011
+ typename Finalizer, typename FinalizerDataType>
2012
+ static ThreadSafeFunction New(napi_env env,
2013
+ const Function& callback,
2014
+ const Object& resource,
2015
+ ResourceString resourceName,
2016
+ size_t maxQueueSize,
2017
+ size_t initialThreadCount,
2018
+ ContextType* context,
2019
+ Finalizer finalizeCallback,
2020
+ FinalizerDataType* data,
2021
+ napi_finalize wrapper);
2022
+
2023
+ napi_status CallInternal(CallbackWrapper* callbackWrapper,
2024
+ napi_threadsafe_function_call_mode mode) const;
2025
+
2026
+ static void CallJS(napi_env env,
2027
+ napi_value jsCallback,
2028
+ void* context,
2029
+ void* data);
2030
+
2031
+ std::unique_ptr<napi_threadsafe_function> _tsfn;
1763
2032
  };
2033
+ #endif
1764
2034
 
1765
2035
  // Memory management.
1766
2036
  class MemoryManagement {
package/package.json CHANGED
@@ -3,6 +3,8 @@
3
3
  "url": "https://github.com/nodejs/node-addon-api/issues"
4
4
  },
5
5
  "contributors": [
6
+ "Abhishek Kumar Singh (https://github.com/abhi11210646)",
7
+ "Alba Mendez (https://github.com/jmendeth)",
6
8
  "Andrew Petersen (https://github.com/kirbysayshi)",
7
9
  "Anisha Rohra (https://github.com/anisha-rohra)",
8
10
  "Anna Henningsen (https://github.com/addaleax)",
@@ -10,6 +12,8 @@
10
12
  "Arunesh Chandra (https://github.com/aruneshchandra)",
11
13
  "Ben Berman (https://github.com/rivertam)",
12
14
  "Benjamin Byholm (https://github.com/kkoopa)",
15
+ "Bill Gallafent (https://github.com/gallafent)",
16
+ "Bruce A. MacNaughton (https://github.com/bmacnaughton)",
13
17
  "Cory Mickelson (https://github.com/corymickelson)",
14
18
  "David Halls (https://github.com/davedoesdev)",
15
19
  "Dongjin Na (https://github.com/nadongguri)",
@@ -17,13 +21,16 @@
17
21
  "Gabriel Schulhof (https://github.com/gabrielschulhof)",
18
22
  "Gus Caplan (https://github.com/devsnek)",
19
23
  "Hitesh Kanwathirtha (https://github.com/digitalinfinity)",
24
+ "Jake Barnes (https://github.com/DuBistKomisch)",
20
25
  "Jake Yoon (https://github.com/yjaeseok)",
21
26
  "Jason Ginchereau (https://github.com/jasongin)",
22
27
  "Jim Schlight (https://github.com/jschlight)",
23
28
  "Jinho Bang (https://github.com/romandev)",
24
29
  "joshgarde (https://github.com/joshgarde)",
30
+ "Kevin Eady (https://github.com/KevinEady)",
25
31
  "Konstantin Tarkus (https://github.com/koistya)",
26
32
  "Kyle Farnung (https://github.com/kfarnung)",
33
+ "Luciano Martorella (https://github.com/lmartorella)",
27
34
  "Matteo Collina (https://github.com/mcollina)",
28
35
  "Michael Dawson (https://github.com/mhdawson)",
29
36
  "Michele Campus (https://github.com/kYroL01)",
@@ -32,9 +39,15 @@
32
39
  "Nick Soggin (https://github.com/iSkore)",
33
40
  "Philipp Renoth (https://github.com/DaAitch)",
34
41
  "Rolf Timmermans (https://github.com/rolftimmermans)",
42
+ "Ross Weir (https://github.com/ross-weir)",
43
+ "Ryuichi Okumura (https://github.com/okuryu)",
35
44
  "Sampson Gao (https://github.com/sampsongao)",
45
+ "Sam Roberts (https://github.com/sam-github)",
36
46
  "Taylor Woll (https://github.com/boingoing)",
37
- "Thomas Gentilhomme (https://github.com/fraxken)"
47
+ "Thomas Gentilhomme (https://github.com/fraxken)",
48
+ "Tobias Nießen (https://github.com/tniessen)",
49
+ "Tux3 (https://github.com/tux3)",
50
+ "Yohei Kishimoto (https://github.com/morokosi)"
38
51
  ],
39
52
  "dependencies": {},
40
53
  "description": "Node.js API (N-API)",
@@ -43,6 +56,7 @@
43
56
  },
44
57
  "directories": {},
45
58
  "homepage": "https://github.com/nodejs/node-addon-api",
59
+ "keywords": ["n-api", "napi", "addon", "native", "bindings", "c", "c++", "nan", "node-addon-api"],
46
60
  "license": "MIT",
47
61
  "main": "index.js",
48
62
  "name": "node-addon-api",
@@ -55,7 +69,11 @@
55
69
  "scripts": {
56
70
  "pretest": "node-gyp rebuild -C test",
57
71
  "test": "node test",
72
+ "predev": "node-gyp rebuild -C test --debug",
73
+ "dev": "node test",
74
+ "predev:incremental": "node-gyp configure build -C test --debug",
75
+ "dev:incremental": "node test",
58
76
  "doc": "doxygen doc/Doxyfile"
59
77
  },
60
- "version": "1.6.2"
78
+ "version": "1.7.2"
61
79
  }
package/src/node_api.cc CHANGED
@@ -2238,7 +2238,7 @@ napi_status napi_get_value_string_latin1(napi_env env,
2238
2238
  if (!buf) {
2239
2239
  CHECK_ARG(env, result);
2240
2240
  *result = val.As<v8::String>()->Length();
2241
- } else {
2241
+ } else if (bufsize != 0) {
2242
2242
  int copied = val.As<v8::String>()->WriteOneByte(
2243
2243
  reinterpret_cast<uint8_t*>(buf), 0, bufsize - 1,
2244
2244
  v8::String::NO_NULL_TERMINATION);
@@ -2247,6 +2247,8 @@ napi_status napi_get_value_string_latin1(napi_env env,
2247
2247
  if (result != nullptr) {
2248
2248
  *result = copied;
2249
2249
  }
2250
+ } else if (result != nullptr) {
2251
+ *result = 0;
2250
2252
  }
2251
2253
 
2252
2254
  return napi_clear_last_error(env);
@@ -2274,7 +2276,7 @@ napi_status napi_get_value_string_utf8(napi_env env,
2274
2276
  if (!buf) {
2275
2277
  CHECK_ARG(env, result);
2276
2278
  *result = val.As<v8::String>()->Utf8Length();
2277
- } else {
2279
+ } else if (bufsize != 0) {
2278
2280
  int copied = val.As<v8::String>()->WriteUtf8(
2279
2281
  buf, bufsize - 1, nullptr, v8::String::REPLACE_INVALID_UTF8 |
2280
2282
  v8::String::NO_NULL_TERMINATION);
@@ -2283,6 +2285,8 @@ napi_status napi_get_value_string_utf8(napi_env env,
2283
2285
  if (result != nullptr) {
2284
2286
  *result = copied;
2285
2287
  }
2288
+ } else if (result != nullptr) {
2289
+ *result = 0;
2286
2290
  }
2287
2291
 
2288
2292
  return napi_clear_last_error(env);
@@ -2311,7 +2315,7 @@ napi_status napi_get_value_string_utf16(napi_env env,
2311
2315
  CHECK_ARG(env, result);
2312
2316
  // V8 assumes UTF-16 length is the same as the number of characters.
2313
2317
  *result = val.As<v8::String>()->Length();
2314
- } else {
2318
+ } else if (bufsize != 0) {
2315
2319
  int copied = val.As<v8::String>()->Write(
2316
2320
  reinterpret_cast<uint16_t*>(buf), 0, bufsize - 1,
2317
2321
  v8::String::NO_NULL_TERMINATION);
@@ -2320,6 +2324,8 @@ napi_status napi_get_value_string_utf16(napi_env env,
2320
2324
  if (result != nullptr) {
2321
2325
  *result = copied;
2322
2326
  }
2327
+ } else if (result != nullptr) {
2328
+ *result = 0;
2323
2329
  }
2324
2330
 
2325
2331
  return napi_clear_last_error(env);
package/tools/README.md CHANGED
@@ -25,7 +25,7 @@ Here is the list of things that can be fixed easily.
25
25
 
26
26
 
27
27
  ### Major Reconstructions
28
- The implementation of `Napi::ObjectWrap` is significantly different from NAN's. `Napi::ObjectWrap` takes a pointer to the wrapped object and creates a reference to the wrapped object inside ObjectWrap constructor. `Napi::ObjectWrap` also associated wrapped object's instance methods to Javascript module instead of static methods like NAN.
28
+ The implementation of `Napi::ObjectWrap` is significantly different from NAN's. `Napi::ObjectWrap` takes a pointer to the wrapped object and creates a reference to the wrapped object inside ObjectWrap constructor. `Napi::ObjectWrap` also associates wrapped object's instance methods to Javascript module instead of static methods like NAN.
29
29
 
30
30
  So if you use Nan::ObjectWrap in your module, you will need to execute the following steps.
31
31
 
@@ -39,7 +39,7 @@ and define it as
39
39
  ...
40
40
  }
41
41
  ```
42
- This way, the `Napi::ObjectWrap` constructor will be invoked after the object has been instanciated and `Napi::ObjectWrap` can use the `this` pointer to create reference to the wrapped object.
42
+ This way, the `Napi::ObjectWrap` constructor will be invoked after the object has been instantiated and `Napi::ObjectWrap` can use the `this` pointer to create a reference to the wrapped object.
43
43
 
44
44
  2. Move your original constructor code into the new constructor. Delete your original constructor.
45
45
  3. In your class initialization function, associate native methods in the following way. The `&` character before methods is required because they are not static methods but instance methods.