nan_new.h 8.58 KB
Newer Older
1 2 3
/*********************************************************************
 * NAN - Native Abstractions for Node.js
 *
4
 * Copyright (c) 2017 NAN contributors
5
 *
6
 * MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
7 8 9 10 11
 ********************************************************************/

#ifndef NAN_NEW_H_
#define NAN_NEW_H_

12
namespace imp {  // scnr
13 14

// TODO(agnat): Generalize
15
template <typename T> v8::Local<T> To(v8::Local<v8::Integer> i);
16 17 18 19

template <>
inline
v8::Local<v8::Integer>
20 21 22
To<v8::Integer>(v8::Local<v8::Integer> i) {
  return Nan::To<v8::Integer>(i).ToLocalChecked();
}
23 24 25 26

template <>
inline
v8::Local<v8::Int32>
27 28 29
To<v8::Int32>(v8::Local<v8::Integer> i) {
  return Nan::To<v8::Int32>(i).ToLocalChecked();
}
30 31 32 33

template <>
inline
v8::Local<v8::Uint32>
34 35 36
To<v8::Uint32>(v8::Local<v8::Integer> i) {
  return Nan::To<v8::Uint32>(i).ToLocalChecked();
}
37

38 39 40 41 42 43 44
template <typename T> struct FactoryBase {
  typedef v8::Local<T> return_t;
};

template <typename T> struct MaybeFactoryBase {
  typedef MaybeLocal<T> return_t;
};
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

template <typename T> struct Factory;

template <>
struct Factory<v8::Array> : FactoryBase<v8::Array> {
  static inline return_t New();
  static inline return_t New(int length);
};

template <>
struct Factory<v8::Boolean> : FactoryBase<v8::Boolean> {
  static inline return_t New(bool value);
};

template <>
struct Factory<v8::BooleanObject> : FactoryBase<v8::BooleanObject> {
  static inline return_t New(bool value);
};

template <>
struct Factory<v8::Context> : FactoryBase<v8::Context> {
  static inline
  return_t
  New( v8::ExtensionConfiguration* extensions = NULL
69 70
     , v8::Local<v8::ObjectTemplate> tmpl = v8::Local<v8::ObjectTemplate>()
     , v8::Local<v8::Value> obj = v8::Local<v8::Value>());
71 72 73
};

template <>
74
struct Factory<v8::Date> : MaybeFactoryBase<v8::Date> {
75 76 77 78 79 80 81 82 83 84 85 86
  static inline return_t New(double value);
};

template <>
struct Factory<v8::External> : FactoryBase<v8::External> {
  static inline return_t New(void *value);
};

template <>
struct Factory<v8::Function> : FactoryBase<v8::Function> {
  static inline
  return_t
87 88
  New( FunctionCallback callback
     , v8::Local<v8::Value> data = v8::Local<v8::Value>());
89 90 91 92 93 94
};

template <>
struct Factory<v8::FunctionTemplate> : FactoryBase<v8::FunctionTemplate> {
  static inline
  return_t
95 96 97
  New( FunctionCallback callback = NULL
     , v8::Local<v8::Value> data = v8::Local<v8::Value>()
     , v8::Local<v8::Signature> signature = v8::Local<v8::Signature>());
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
};

template <>
struct Factory<v8::Number> : FactoryBase<v8::Number> {
  static inline return_t New(double value);
};

template <>
struct Factory<v8::NumberObject> : FactoryBase<v8::NumberObject> {
  static inline return_t New(double value);
};

template <typename T>
struct IntegerFactory : FactoryBase<T> {
  typedef typename FactoryBase<T>::return_t return_t;
  static inline return_t New(int32_t value);
  static inline return_t New(uint32_t value);
};

template <>
struct Factory<v8::Integer> : IntegerFactory<v8::Integer> {};

template <>
struct Factory<v8::Int32> : IntegerFactory<v8::Int32> {};

template <>
struct Factory<v8::Uint32> : FactoryBase<v8::Uint32> {
  static inline return_t New(int32_t value);
  static inline return_t New(uint32_t value);
};

template <>
struct Factory<v8::Object> : FactoryBase<v8::Object> {
  static inline return_t New();
};

template <>
struct Factory<v8::ObjectTemplate> : FactoryBase<v8::ObjectTemplate> {
  static inline return_t New();
};

template <>
140
struct Factory<v8::RegExp> : MaybeFactoryBase<v8::RegExp> {
141
  static inline return_t New(
142
      v8::Local<v8::String> pattern, v8::RegExp::Flags flags);
143 144 145
};

template <>
146
struct Factory<v8::Script> : MaybeFactoryBase<v8::Script> {
147 148 149 150 151 152 153
  static inline return_t New( v8::Local<v8::String> source);
  static inline return_t New( v8::Local<v8::String> source
                            , v8::ScriptOrigin const& origin);
};

template <>
struct Factory<v8::Signature> : FactoryBase<v8::Signature> {
154
  typedef v8::Local<v8::FunctionTemplate> FTH;
155 156 157 158
  static inline return_t New(FTH receiver = FTH());
};

template <>
159
struct Factory<v8::String> : MaybeFactoryBase<v8::String> {
160 161 162 163 164 165
  static inline return_t New();
  static inline return_t New(const char *value, int length = -1);
  static inline return_t New(const uint16_t *value, int length = -1);
  static inline return_t New(std::string const& value);

  static inline return_t New(v8::String::ExternalStringResource * value);
166
  static inline return_t New(ExternalOneByteStringResource * value);
167 168 169 170
};

template <>
struct Factory<v8::StringObject> : FactoryBase<v8::StringObject> {
171
  static inline return_t New(v8::Local<v8::String> value);
172 173
};

174
}  // end of namespace imp
175 176 177

#if (NODE_MODULE_VERSION >= 12)

178
namespace imp {
179 180

template <>
181
struct Factory<v8::UnboundScript> : MaybeFactoryBase<v8::UnboundScript> {
182 183 184 185 186
  static inline return_t New( v8::Local<v8::String> source);
  static inline return_t New( v8::Local<v8::String> source
                            , v8::ScriptOrigin const& origin);
};

187
}  // end of namespace imp
188 189 190 191 192 193 194 195 196 197 198 199

# include "nan_implementation_12_inl.h"

#else  // NODE_MODULE_VERSION >= 12

# include "nan_implementation_pre_12_inl.h"

#endif

//=== API ======================================================================

template <typename T>
200 201 202
typename imp::Factory<T>::return_t
New() {
  return imp::Factory<T>::New();
203 204 205
}

template <typename T, typename A0>
206 207 208
typename imp::Factory<T>::return_t
New(A0 arg0) {
  return imp::Factory<T>::New(arg0);
209 210 211
}

template <typename T, typename A0, typename A1>
212 213 214
typename imp::Factory<T>::return_t
New(A0 arg0, A1 arg1) {
  return imp::Factory<T>::New(arg0, arg1);
215 216 217
}

template <typename T, typename A0, typename A1, typename A2>
218 219 220
typename imp::Factory<T>::return_t
New(A0 arg0, A1 arg1, A2 arg2) {
  return imp::Factory<T>::New(arg0, arg1, arg2);
221 222 223
}

template <typename T, typename A0, typename A1, typename A2, typename A3>
224 225 226
typename imp::Factory<T>::return_t
New(A0 arg0, A1 arg1, A2 arg2, A3 arg3) {
  return imp::Factory<T>::New(arg0, arg1, arg2, arg3);
227 228 229 230
}

// Note(agnat): When passing overloaded function pointers to template functions
// as generic arguments the compiler needs help in picking the right overload.
231
// These two functions handle New<Function> and New<FunctionTemplate> with
232 233 234 235
// all argument variations.

// v8::Function and v8::FunctionTemplate with one or two arguments
template <typename T>
236 237 238 239
typename imp::Factory<T>::return_t
New( FunctionCallback callback
      , v8::Local<v8::Value> data = v8::Local<v8::Value>()) {
    return imp::Factory<T>::New(callback, data);
240 241 242 243
}

// v8::Function and v8::FunctionTemplate with three arguments
template <typename T, typename A2>
244 245 246
typename imp::Factory<T>::return_t
New( FunctionCallback callback
      , v8::Local<v8::Value> data = v8::Local<v8::Value>()
247
      , A2 a2 = A2()) {
248
    return imp::Factory<T>::New(callback, data, a2);
249 250 251 252
}

// Convenience

253 254 255
#if NODE_MODULE_VERSION < IOJS_3_0_MODULE_VERSION
template <typename T> inline v8::Local<T> New(v8::Handle<T> h);
#endif
256

257 258 259 260 261 262 263 264
#if NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION
template <typename T, typename M>
    inline v8::Local<T> New(v8::Persistent<T, M> const& p);
#else
template <typename T> inline v8::Local<T> New(v8::Persistent<T> const& p);
#endif
template <typename T, typename M>
inline v8::Local<T> New(Persistent<T, M> const& p);
265 266
template <typename T>
inline v8::Local<T> New(Global<T> const& p);
267 268

inline
269 270 271
imp::Factory<v8::Boolean>::return_t
New(bool value) {
  return New<v8::Boolean>(value);
272 273 274
}

inline
275 276 277
imp::Factory<v8::Int32>::return_t
New(int32_t value) {
  return New<v8::Int32>(value);
278 279 280
}

inline
281 282 283
imp::Factory<v8::Uint32>::return_t
New(uint32_t value) {
  return New<v8::Uint32>(value);
284 285 286
}

inline
287 288 289
imp::Factory<v8::Number>::return_t
New(double value) {
  return New<v8::Number>(value);
290 291 292
}

inline
293 294 295
imp::Factory<v8::String>::return_t
New(std::string const& value) {  // NOLINT(build/include_what_you_use)
  return New<v8::String>(value);
296 297 298
}

inline
299 300 301
imp::Factory<v8::String>::return_t
New(const char * value, int length) {
  return New<v8::String>(value, length);
302 303
}

304 305 306 307 308 309
inline
imp::Factory<v8::String>::return_t
New(const uint16_t * value, int length) {
  return New<v8::String>(value, length);
}

310
inline
311 312 313
imp::Factory<v8::String>::return_t
New(const char * value) {
  return New<v8::String>(value);
314 315 316
}

inline
317 318 319
imp::Factory<v8::String>::return_t
New(const uint16_t * value) {
  return New<v8::String>(value);
320 321 322
}

inline
323 324 325
imp::Factory<v8::String>::return_t
New(v8::String::ExternalStringResource * value) {
  return New<v8::String>(value);
326 327 328
}

inline
329 330 331
imp::Factory<v8::String>::return_t
New(ExternalOneByteStringResource * value) {
  return New<v8::String>(value);
332 333 334
}

inline
335 336 337
imp::Factory<v8::RegExp>::return_t
New(v8::Local<v8::String> pattern, v8::RegExp::Flags flags) {
  return New<v8::RegExp>(pattern, flags);
338 339 340
}

#endif  // NAN_NEW_H_