node-addon-api 3.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.
package/napi.h CHANGED
@@ -1623,10 +1623,6 @@ namespace Napi {
1623
1623
  operator const napi_property_descriptor&() const;
1624
1624
 
1625
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);
1630
1626
  napi_property_descriptor _desc;
1631
1627
  };
1632
1628
 
@@ -1647,6 +1643,114 @@ namespace Napi {
1647
1643
  napi_property_descriptor _desc;
1648
1644
  };
1649
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
+
1650
1754
  /// Base class to be extended by C++ classes exposed to JavaScript; each C++ class instance gets
1651
1755
  /// "wrapped" by a JavaScript object that is managed by this class.
1652
1756
  ///
@@ -1673,7 +1777,7 @@ namespace Napi {
1673
1777
  /// Napi::Value DoSomething(const Napi::CallbackInfo& info);
1674
1778
  /// }
1675
1779
  template <typename T>
1676
- class ObjectWrap : public Reference<Object> {
1780
+ class ObjectWrap : public InstanceWrap<T>, public Reference<Object> {
1677
1781
  public:
1678
1782
  ObjectWrap(const CallbackInfo& callbackInfo);
1679
1783
  virtual ~ObjectWrap();
@@ -1685,10 +1789,6 @@ namespace Napi {
1685
1789
  typedef Napi::Value (*StaticMethodCallback)(const CallbackInfo& info);
1686
1790
  typedef Napi::Value (*StaticGetterCallback)(const CallbackInfo& info);
1687
1791
  typedef void (*StaticSetterCallback)(const CallbackInfo& info, const Napi::Value& value);
1688
- typedef void (T::*InstanceVoidMethodCallback)(const CallbackInfo& info);
1689
- typedef Napi::Value (T::*InstanceMethodCallback)(const CallbackInfo& info);
1690
- typedef Napi::Value (T::*InstanceGetterCallback)(const CallbackInfo& info);
1691
- typedef void (T::*InstanceSetterCallback)(const CallbackInfo& info, const Napi::Value& value);
1692
1792
 
1693
1793
  typedef ClassPropertyDescriptor<T> PropertyDescriptor;
1694
1794
 
@@ -1750,68 +1850,12 @@ namespace Napi {
1750
1850
  static PropertyDescriptor StaticAccessor(Symbol name,
1751
1851
  napi_property_attributes attributes = napi_default,
1752
1852
  void* data = nullptr);
1753
- static PropertyDescriptor InstanceMethod(const char* utf8name,
1754
- InstanceVoidMethodCallback method,
1755
- napi_property_attributes attributes = napi_default,
1756
- void* data = nullptr);
1757
- static PropertyDescriptor InstanceMethod(const char* utf8name,
1758
- InstanceMethodCallback method,
1759
- napi_property_attributes attributes = napi_default,
1760
- void* data = nullptr);
1761
- static PropertyDescriptor InstanceMethod(Symbol name,
1762
- InstanceVoidMethodCallback method,
1763
- napi_property_attributes attributes = napi_default,
1764
- void* data = nullptr);
1765
- static PropertyDescriptor InstanceMethod(Symbol name,
1766
- InstanceMethodCallback method,
1767
- napi_property_attributes attributes = napi_default,
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);
1785
- static PropertyDescriptor InstanceAccessor(const char* utf8name,
1786
- InstanceGetterCallback getter,
1787
- InstanceSetterCallback setter,
1788
- napi_property_attributes attributes = napi_default,
1789
- void* data = nullptr);
1790
- static PropertyDescriptor InstanceAccessor(Symbol name,
1791
- InstanceGetterCallback getter,
1792
- InstanceSetterCallback setter,
1793
- napi_property_attributes attributes = napi_default,
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);
1803
1853
  static PropertyDescriptor StaticValue(const char* utf8name,
1804
1854
  Napi::Value value,
1805
1855
  napi_property_attributes attributes = napi_default);
1806
1856
  static PropertyDescriptor StaticValue(Symbol name,
1807
1857
  Napi::Value value,
1808
1858
  napi_property_attributes attributes = napi_default);
1809
- static PropertyDescriptor InstanceValue(const char* utf8name,
1810
- Napi::Value value,
1811
- napi_property_attributes attributes = napi_default);
1812
- static PropertyDescriptor InstanceValue(Symbol name,
1813
- Napi::Value value,
1814
- napi_property_attributes attributes = napi_default);
1815
1859
  virtual void Finalize(Napi::Env env);
1816
1860
 
1817
1861
  private:
@@ -1822,10 +1866,6 @@ namespace Napi {
1822
1866
  static napi_value StaticMethodCallbackWrapper(napi_env env, napi_callback_info info);
1823
1867
  static napi_value StaticGetterCallbackWrapper(napi_env env, napi_callback_info info);
1824
1868
  static napi_value StaticSetterCallbackWrapper(napi_env env, napi_callback_info info);
1825
- static napi_value InstanceVoidMethodCallbackWrapper(napi_env env, napi_callback_info info);
1826
- static napi_value InstanceMethodCallbackWrapper(napi_env env, napi_callback_info info);
1827
- static napi_value InstanceGetterCallbackWrapper(napi_env env, napi_callback_info info);
1828
- static napi_value InstanceSetterCallbackWrapper(napi_env env, napi_callback_info info);
1829
1869
  static void FinalizeCallback(napi_env env, void* data, void* hint);
1830
1870
  static Function DefineClass(Napi::Env env,
1831
1871
  const char* utf8name,
@@ -1833,66 +1873,22 @@ namespace Napi {
1833
1873
  const napi_property_descriptor* props,
1834
1874
  void* data = nullptr);
1835
1875
 
1836
- template <typename TCallback>
1837
- struct MethodCallbackData {
1838
- TCallback callback;
1839
- void* data;
1840
- };
1841
- typedef MethodCallbackData<StaticVoidMethodCallback> StaticVoidMethodCallbackData;
1842
- typedef MethodCallbackData<StaticMethodCallback> StaticMethodCallbackData;
1843
- typedef MethodCallbackData<InstanceVoidMethodCallback> InstanceVoidMethodCallbackData;
1844
- typedef MethodCallbackData<InstanceMethodCallback> InstanceMethodCallbackData;
1845
-
1846
- template <typename TGetterCallback, typename TSetterCallback>
1847
- struct AccessorCallbackData {
1848
- TGetterCallback getterCallback;
1849
- TSetterCallback setterCallback;
1850
- void* data;
1851
- };
1852
- typedef AccessorCallbackData<StaticGetterCallback, StaticSetterCallback>
1853
- StaticAccessorCallbackData;
1854
- typedef AccessorCallbackData<InstanceGetterCallback, InstanceSetterCallback>
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;
1876
+ typedef MethodCallbackData<T, StaticVoidMethodCallback> StaticVoidMethodCallbackData;
1877
+ typedef MethodCallbackData<T, StaticMethodCallback> StaticMethodCallbackData;
1865
1878
 
1866
- template <InstanceMethodCallback method>
1867
- static napi_value WrappedMethod(napi_env env, napi_callback_info info) noexcept;
1879
+ typedef AccessorCallbackData<T,
1880
+ StaticGetterCallback,
1881
+ StaticSetterCallback> StaticAccessorCallbackData;
1868
1882
 
1869
1883
  template <StaticSetterCallback method>
1870
1884
  static napi_value WrappedMethod(napi_env env, napi_callback_info info) noexcept;
1871
1885
 
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; }
1886
+ template <StaticSetterCallback setter> struct StaticSetterTag {};
1883
1887
 
1884
1888
  template <StaticSetterCallback setter>
1885
1889
  static napi_callback WrapStaticSetter(StaticSetterTag<setter>) noexcept { return &This::WrappedMethod<setter>; }
1886
1890
  static napi_callback WrapStaticSetter(StaticSetterTag<nullptr>) noexcept { return nullptr; }
1887
1891
 
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
1892
  bool _construction_failed = true;
1897
1893
  };
1898
1894
 
@@ -2420,6 +2416,25 @@ namespace Napi {
2420
2416
  static const napi_node_version* GetNodeVersion(Env env);
2421
2417
  };
2422
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
+
2423
2438
  } // namespace Napi
2424
2439
 
2425
2440
  // Inline implementations of all the above class methods are included here.
package/package.json CHANGED
@@ -139,6 +139,10 @@
139
139
  "name": "Kevin Eady",
140
140
  "url": "https://github.com/KevinEady"
141
141
  },
142
+ {
143
+ "name": "Koki Nishihara",
144
+ "url": "https://github.com/Nishikoh"
145
+ },
142
146
  {
143
147
  "name": "Konstantin Tarkus",
144
148
  "url": "https://github.com/koistya"
@@ -195,6 +199,10 @@
195
199
  "name": "Nurbol Alpysbayev",
196
200
  "url": "https://github.com/anurbol"
197
201
  },
202
+ {
203
+ "name": "pacop",
204
+ "url": "https://github.com/pacop"
205
+ },
198
206
  {
199
207
  "name": "Philipp Renoth",
200
208
  "url": "https://github.com/DaAitch"
@@ -239,6 +247,10 @@
239
247
  "name": "Tux3",
240
248
  "url": "https://github.com/tux3"
241
249
  },
250
+ {
251
+ "name": "Vlad Velmisov",
252
+ "url": "https://github.com/Velmisov"
253
+ },
242
254
  {
243
255
  "name": "Yohei Kishimoto",
244
256
  "url": "https://github.com/morokosi"
@@ -252,6 +264,8 @@
252
264
  "description": "Node.js API (N-API)",
253
265
  "devDependencies": {
254
266
  "benchmark": "^2.1.4",
267
+ "fs-extra": "^9.0.1",
268
+ "bindings": "^1.5.0",
255
269
  "safe-buffer": "^5.1.1"
256
270
  },
257
271
  "directories": {},
@@ -288,5 +302,5 @@
288
302
  "dev:incremental": "node test",
289
303
  "doc": "doxygen doc/Doxyfile"
290
304
  },
291
- "version": "3.0.1"
305
+ "version": "3.0.2"
292
306
  }
@@ -22,8 +22,8 @@ if (disable != "--disable" && dir != "--disable") {
22
22
  [ /[ ]*"nan": *"[^"]+"(,|)[\n\r]/g, '' ]
23
23
  ],
24
24
  'binding.gyp': [
25
- [ /([ ]*)'include_dirs': \[/g, '$1\'include_dirs\': [\n$1 \'<!@(node -p "require(\\\'node-addon-api\\\').include")\',' ],
26
- [ /([ ]*)"include_dirs": \[/g, '$1"include_dirs": [\n$1 "<!@(node -p \\"require(\'node-addon-api\').include\\")",' ],
25
+ [ /([ ]*)'include_dirs': \[/g, '$1\'include_dirs\': [\n$1 \'<!(node -p "require(\\\'node-addon-api\\\').include_dir")\',' ],
26
+ [ /([ ]*)"include_dirs": \[/g, '$1"include_dirs": [\n$1 "<!(node -p \\"require(\'node-addon-api\').include_dir\\")",' ],
27
27
  [ /[ ]*("|')<!\(node -e ("|'|\\"|\\')require\(("|'|\\"|\\')nan("|'|\\"|\\')\)("|'|\\"|\\')\)("|')(,|)[\r\n]/g, '' ],
28
28
  [ /([ ]*)("|')target_name("|'): ("|')(.+?)("|'),/g, '$1$2target_name$2: $4$5$6,\n $2cflags!$2: [ $2-fno-exceptions$2 ],\n $2cflags_cc!$2: [ $2-fno-exceptions$2 ],\n $2xcode_settings$2: { $2GCC_ENABLE_CPP_EXCEPTIONS$2: $2YES$2,\n $2CLANG_CXX_LIBRARY$2: $2libc++$2,\n $2MACOSX_DEPLOYMENT_TARGET$2: $210.7$2,\n },\n $2msvs_settings$2: {\n $2VCCLCompilerTool$2: { $2ExceptionHandling$2: 1 },\n },' ],
29
29
  ]
@@ -35,8 +35,8 @@ if (disable != "--disable" && dir != "--disable") {
35
35
  [ /[ ]*"nan": *"[^"]+"(,|)[\n\r]/g, '' ]
36
36
  ],
37
37
  'binding.gyp': [
38
- [ /([ ]*)'include_dirs': \[/g, '$1\'include_dirs\': [\n$1 \'<!@(node -p "require(\\\'node-addon-api\\\').include")\',' ],
39
- [ /([ ]*)"include_dirs": \[/g, '$1"include_dirs": [\n$1 "<!@(node -p \'require(\\\"node-addon-api\\\").include\')",' ],
38
+ [ /([ ]*)'include_dirs': \[/g, '$1\'include_dirs\': [\n$1 \'<!(node -p "require(\\\'node-addon-api\\\').include_dir")\',' ],
39
+ [ /([ ]*)"include_dirs": \[/g, '$1"include_dirs": [\n$1 "<!(node -p \'require(\\\"node-addon-api\\\").include_dir\')",' ],
40
40
  [ /[ ]*("|')<!\(node -e ("|'|\\"|\\')require\(("|'|\\"|\\')nan("|'|\\"|\\')\)("|'|\\"|\\')\)("|')(,|)[\r\n]/g, '' ],
41
41
  [ /([ ]*)("|')target_name("|'): ("|')(.+?)("|'),/g, '$1$2target_name$2: $4$5$6,\n $2cflags!$2: [ $2-fno-exceptions$2 ],\n $2cflags_cc!$2: [ $2-fno-exceptions$2 ],\n $2defines$2: [ $2NAPI_DISABLE_CPP_EXCEPTIONS$2 ],\n $2conditions$2: [\n [\'OS==\"win\"\', { $2defines$2: [ $2_HAS_EXCEPTIONS=1$2 ] }]\n ]' ],
42
42
  ]