123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*********************************************************************
  2. * NAN - Native Abstractions for Node.js
  3. *
  4. * Copyright (c) 2018 NAN contributors
  5. *
  6. * MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
  7. ********************************************************************/
  8. #ifndef NAN_NEW_H_
  9. #define NAN_NEW_H_
  10. namespace imp { // scnr
  11. // TODO(agnat): Generalize
  12. template <typename T> v8::Local<T> To(v8::Local<v8::Integer> i);
  13. template <>
  14. inline
  15. v8::Local<v8::Integer>
  16. To<v8::Integer>(v8::Local<v8::Integer> i) {
  17. return Nan::To<v8::Integer>(i).ToLocalChecked();
  18. }
  19. template <>
  20. inline
  21. v8::Local<v8::Int32>
  22. To<v8::Int32>(v8::Local<v8::Integer> i) {
  23. return Nan::To<v8::Int32>(i).ToLocalChecked();
  24. }
  25. template <>
  26. inline
  27. v8::Local<v8::Uint32>
  28. To<v8::Uint32>(v8::Local<v8::Integer> i) {
  29. return Nan::To<v8::Uint32>(i).ToLocalChecked();
  30. }
  31. template <typename T> struct FactoryBase {
  32. typedef v8::Local<T> return_t;
  33. };
  34. template <typename T> struct MaybeFactoryBase {
  35. typedef MaybeLocal<T> return_t;
  36. };
  37. template <typename T> struct Factory;
  38. template <>
  39. struct Factory<v8::Array> : FactoryBase<v8::Array> {
  40. static inline return_t New();
  41. static inline return_t New(int length);
  42. };
  43. template <>
  44. struct Factory<v8::Boolean> : FactoryBase<v8::Boolean> {
  45. static inline return_t New(bool value);
  46. };
  47. template <>
  48. struct Factory<v8::BooleanObject> : FactoryBase<v8::BooleanObject> {
  49. static inline return_t New(bool value);
  50. };
  51. template <>
  52. struct Factory<v8::Context> : FactoryBase<v8::Context> {
  53. static inline
  54. return_t
  55. New( v8::ExtensionConfiguration* extensions = NULL
  56. , v8::Local<v8::ObjectTemplate> tmpl = v8::Local<v8::ObjectTemplate>()
  57. , v8::Local<v8::Value> obj = v8::Local<v8::Value>());
  58. };
  59. template <>
  60. struct Factory<v8::Date> : MaybeFactoryBase<v8::Date> {
  61. static inline return_t New(double value);
  62. };
  63. template <>
  64. struct Factory<v8::External> : FactoryBase<v8::External> {
  65. static inline return_t New(void *value);
  66. };
  67. template <>
  68. struct Factory<v8::Function> : FactoryBase<v8::Function> {
  69. static inline
  70. return_t
  71. New( FunctionCallback callback
  72. , v8::Local<v8::Value> data = v8::Local<v8::Value>());
  73. };
  74. template <>
  75. struct Factory<v8::FunctionTemplate> : FactoryBase<v8::FunctionTemplate> {
  76. static inline
  77. return_t
  78. New( FunctionCallback callback = NULL
  79. , v8::Local<v8::Value> data = v8::Local<v8::Value>()
  80. , v8::Local<v8::Signature> signature = v8::Local<v8::Signature>());
  81. };
  82. template <>
  83. struct Factory<v8::Number> : FactoryBase<v8::Number> {
  84. static inline return_t New(double value);
  85. };
  86. template <>
  87. struct Factory<v8::NumberObject> : FactoryBase<v8::NumberObject> {
  88. static inline return_t New(double value);
  89. };
  90. template <typename T>
  91. struct IntegerFactory : FactoryBase<T> {
  92. typedef typename FactoryBase<T>::return_t return_t;
  93. static inline return_t New(int32_t value);
  94. static inline return_t New(uint32_t value);
  95. };
  96. template <>
  97. struct Factory<v8::Integer> : IntegerFactory<v8::Integer> {};
  98. template <>
  99. struct Factory<v8::Int32> : IntegerFactory<v8::Int32> {};
  100. template <>
  101. struct Factory<v8::Uint32> : FactoryBase<v8::Uint32> {
  102. static inline return_t New(int32_t value);
  103. static inline return_t New(uint32_t value);
  104. };
  105. template <>
  106. struct Factory<v8::Object> : FactoryBase<v8::Object> {
  107. static inline return_t New();
  108. };
  109. template <>
  110. struct Factory<v8::ObjectTemplate> : FactoryBase<v8::ObjectTemplate> {
  111. static inline return_t New();
  112. };
  113. template <>
  114. struct Factory<v8::RegExp> : MaybeFactoryBase<v8::RegExp> {
  115. static inline return_t New(
  116. v8::Local<v8::String> pattern, v8::RegExp::Flags flags);
  117. };
  118. template <>
  119. struct Factory<v8::Script> : MaybeFactoryBase<v8::Script> {
  120. static inline return_t New( v8::Local<v8::String> source);
  121. static inline return_t New( v8::Local<v8::String> source
  122. , v8::ScriptOrigin const& origin);
  123. };
  124. template <>
  125. struct Factory<v8::Signature> : FactoryBase<v8::Signature> {
  126. typedef v8::Local<v8::FunctionTemplate> FTH;
  127. static inline return_t New(FTH receiver = FTH());
  128. };
  129. template <>
  130. struct Factory<v8::String> : MaybeFactoryBase<v8::String> {
  131. static inline return_t New();
  132. static inline return_t New(const char *value, int length = -1);
  133. static inline return_t New(const uint16_t *value, int length = -1);
  134. static inline return_t New(std::string const& value);
  135. static inline return_t New(v8::String::ExternalStringResource * value);
  136. static inline return_t New(ExternalOneByteStringResource * value);
  137. };
  138. template <>
  139. struct Factory<v8::StringObject> : FactoryBase<v8::StringObject> {
  140. static inline return_t New(v8::Local<v8::String> value);
  141. };
  142. } // end of namespace imp
  143. #if (NODE_MODULE_VERSION >= 12)
  144. namespace imp {
  145. template <>
  146. struct Factory<v8::UnboundScript> : MaybeFactoryBase<v8::UnboundScript> {
  147. static inline return_t New( v8::Local<v8::String> source);
  148. static inline return_t New( v8::Local<v8::String> source
  149. , v8::ScriptOrigin const& origin);
  150. };
  151. } // end of namespace imp
  152. # include "nan_implementation_12_inl.h"
  153. #else // NODE_MODULE_VERSION >= 12
  154. # include "nan_implementation_pre_12_inl.h"
  155. #endif
  156. //=== API ======================================================================
  157. template <typename T>
  158. typename imp::Factory<T>::return_t
  159. New() {
  160. return imp::Factory<T>::New();
  161. }
  162. template <typename T, typename A0>
  163. typename imp::Factory<T>::return_t
  164. New(A0 arg0) {
  165. return imp::Factory<T>::New(arg0);
  166. }
  167. template <typename T, typename A0, typename A1>
  168. typename imp::Factory<T>::return_t
  169. New(A0 arg0, A1 arg1) {
  170. return imp::Factory<T>::New(arg0, arg1);
  171. }
  172. template <typename T, typename A0, typename A1, typename A2>
  173. typename imp::Factory<T>::return_t
  174. New(A0 arg0, A1 arg1, A2 arg2) {
  175. return imp::Factory<T>::New(arg0, arg1, arg2);
  176. }
  177. template <typename T, typename A0, typename A1, typename A2, typename A3>
  178. typename imp::Factory<T>::return_t
  179. New(A0 arg0, A1 arg1, A2 arg2, A3 arg3) {
  180. return imp::Factory<T>::New(arg0, arg1, arg2, arg3);
  181. }
  182. // Note(agnat): When passing overloaded function pointers to template functions
  183. // as generic arguments the compiler needs help in picking the right overload.
  184. // These two functions handle New<Function> and New<FunctionTemplate> with
  185. // all argument variations.
  186. // v8::Function and v8::FunctionTemplate with one or two arguments
  187. template <typename T>
  188. typename imp::Factory<T>::return_t
  189. New( FunctionCallback callback
  190. , v8::Local<v8::Value> data = v8::Local<v8::Value>()) {
  191. return imp::Factory<T>::New(callback, data);
  192. }
  193. // v8::Function and v8::FunctionTemplate with three arguments
  194. template <typename T, typename A2>
  195. typename imp::Factory<T>::return_t
  196. New( FunctionCallback callback
  197. , v8::Local<v8::Value> data = v8::Local<v8::Value>()
  198. , A2 a2 = A2()) {
  199. return imp::Factory<T>::New(callback, data, a2);
  200. }
  201. // Convenience
  202. #if NODE_MODULE_VERSION < IOJS_3_0_MODULE_VERSION
  203. template <typename T> inline v8::Local<T> New(v8::Handle<T> h);
  204. #endif
  205. #if NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION
  206. template <typename T, typename M>
  207. inline v8::Local<T> New(v8::Persistent<T, M> const& p);
  208. #else
  209. template <typename T> inline v8::Local<T> New(v8::Persistent<T> const& p);
  210. #endif
  211. template <typename T, typename M>
  212. inline v8::Local<T> New(Persistent<T, M> const& p);
  213. template <typename T>
  214. inline v8::Local<T> New(Global<T> const& p);
  215. inline
  216. imp::Factory<v8::Boolean>::return_t
  217. New(bool value) {
  218. return New<v8::Boolean>(value);
  219. }
  220. inline
  221. imp::Factory<v8::Int32>::return_t
  222. New(int32_t value) {
  223. return New<v8::Int32>(value);
  224. }
  225. inline
  226. imp::Factory<v8::Uint32>::return_t
  227. New(uint32_t value) {
  228. return New<v8::Uint32>(value);
  229. }
  230. inline
  231. imp::Factory<v8::Number>::return_t
  232. New(double value) {
  233. return New<v8::Number>(value);
  234. }
  235. inline
  236. imp::Factory<v8::String>::return_t
  237. New(std::string const& value) { // NOLINT(build/include_what_you_use)
  238. return New<v8::String>(value);
  239. }
  240. inline
  241. imp::Factory<v8::String>::return_t
  242. New(const char * value, int length) {
  243. return New<v8::String>(value, length);
  244. }
  245. inline
  246. imp::Factory<v8::String>::return_t
  247. New(const uint16_t * value, int length) {
  248. return New<v8::String>(value, length);
  249. }
  250. inline
  251. imp::Factory<v8::String>::return_t
  252. New(const char * value) {
  253. return New<v8::String>(value);
  254. }
  255. inline
  256. imp::Factory<v8::String>::return_t
  257. New(const uint16_t * value) {
  258. return New<v8::String>(value);
  259. }
  260. inline
  261. imp::Factory<v8::String>::return_t
  262. New(v8::String::ExternalStringResource * value) {
  263. return New<v8::String>(value);
  264. }
  265. inline
  266. imp::Factory<v8::String>::return_t
  267. New(ExternalOneByteStringResource * value) {
  268. return New<v8::String>(value);
  269. }
  270. inline
  271. imp::Factory<v8::RegExp>::return_t
  272. New(v8::Local<v8::String> pattern, v8::RegExp::Flags flags) {
  273. return New<v8::RegExp>(pattern, flags);
  274. }
  275. #endif // NAN_NEW_H_