nan_new.h 8.58 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
/*********************************************************************
 * NAN - Native Abstractions for Node.js
 *
 * Copyright (c) 2018 NAN contributors
 *
 * MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
 ********************************************************************/

#ifndef NAN_NEW_H_
#define NAN_NEW_H_

namespace imp {  // scnr

// TODO(agnat): Generalize
template <typename T> v8::Local<T> To(v8::Local<v8::Integer> i);

template <>
inline
v8::Local<v8::Integer>
To<v8::Integer>(v8::Local<v8::Integer> i) {
  return Nan::To<v8::Integer>(i).ToLocalChecked();
}

template <>
inline
v8::Local<v8::Int32>
To<v8::Int32>(v8::Local<v8::Integer> i) {
  return Nan::To<v8::Int32>(i).ToLocalChecked();
}

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

template <typename T> struct FactoryBase {
  typedef v8::Local<T> return_t;
};

template <typename T> struct MaybeFactoryBase {
  typedef MaybeLocal<T> return_t;
};

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
     , v8::Local<v8::ObjectTemplate> tmpl = v8::Local<v8::ObjectTemplate>()
     , v8::Local<v8::Value> obj = v8::Local<v8::Value>());
};

template <>
struct Factory<v8::Date> : MaybeFactoryBase<v8::Date> {
  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
  New( FunctionCallback callback
     , v8::Local<v8::Value> data = v8::Local<v8::Value>());
};

template <>
struct Factory<v8::FunctionTemplate> : FactoryBase<v8::FunctionTemplate> {
  static inline
  return_t
  New( FunctionCallback callback = NULL
     , v8::Local<v8::Value> data = v8::Local<v8::Value>()
     , v8::Local<v8::Signature> signature = v8::Local<v8::Signature>());
};

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 <>
struct Factory<v8::RegExp> : MaybeFactoryBase<v8::RegExp> {
  static inline return_t New(
      v8::Local<v8::String> pattern, v8::RegExp::Flags flags);
};

template <>
struct Factory<v8::Script> : MaybeFactoryBase<v8::Script> {
  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> {
  typedef v8::Local<v8::FunctionTemplate> FTH;
  static inline return_t New(FTH receiver = FTH());
};

template <>
struct Factory<v8::String> : MaybeFactoryBase<v8::String> {
  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);
  static inline return_t New(ExternalOneByteStringResource * value);
};

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

}  // end of namespace imp

#if (NODE_MODULE_VERSION >= 12)

namespace imp {

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

}  // end of namespace imp

# include "nan_implementation_12_inl.h"

#else  // NODE_MODULE_VERSION >= 12

# include "nan_implementation_pre_12_inl.h"

#endif

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

template <typename T>
typename imp::Factory<T>::return_t
New() {
  return imp::Factory<T>::New();
}

template <typename T, typename A0>
typename imp::Factory<T>::return_t
New(A0 arg0) {
  return imp::Factory<T>::New(arg0);
}

template <typename T, typename A0, typename A1>
typename imp::Factory<T>::return_t
New(A0 arg0, A1 arg1) {
  return imp::Factory<T>::New(arg0, arg1);
}

template <typename T, typename A0, typename A1, typename A2>
typename imp::Factory<T>::return_t
New(A0 arg0, A1 arg1, A2 arg2) {
  return imp::Factory<T>::New(arg0, arg1, arg2);
}

template <typename T, typename A0, typename A1, typename A2, typename A3>
typename imp::Factory<T>::return_t
New(A0 arg0, A1 arg1, A2 arg2, A3 arg3) {
  return imp::Factory<T>::New(arg0, arg1, arg2, arg3);
}

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

// v8::Function and v8::FunctionTemplate with one or two arguments
template <typename T>
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);
}

// v8::Function and v8::FunctionTemplate with three arguments
template <typename T, typename A2>
typename imp::Factory<T>::return_t
New( FunctionCallback callback
      , v8::Local<v8::Value> data = v8::Local<v8::Value>()
      , A2 a2 = A2()) {
    return imp::Factory<T>::New(callback, data, a2);
}

// Convenience

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

#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);
template <typename T>
inline v8::Local<T> New(Global<T> const& p);

inline
imp::Factory<v8::Boolean>::return_t
New(bool value) {
  return New<v8::Boolean>(value);
}

inline
imp::Factory<v8::Int32>::return_t
New(int32_t value) {
  return New<v8::Int32>(value);
}

inline
imp::Factory<v8::Uint32>::return_t
New(uint32_t value) {
  return New<v8::Uint32>(value);
}

inline
imp::Factory<v8::Number>::return_t
New(double value) {
  return New<v8::Number>(value);
}

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

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

inline
imp::Factory<v8::String>::return_t
New(const uint16_t * value, int length) {
  return New<v8::String>(value, length);
}

inline
imp::Factory<v8::String>::return_t
New(const char * value) {
  return New<v8::String>(value);
}

inline
imp::Factory<v8::String>::return_t
New(const uint16_t * value) {
  return New<v8::String>(value);
}

inline
imp::Factory<v8::String>::return_t
New(v8::String::ExternalStringResource * value) {
  return New<v8::String>(value);
}

inline
imp::Factory<v8::String>::return_t
New(ExternalOneByteStringResource * value) {
  return New<v8::String>(value);
}

inline
imp::Factory<v8::RegExp>::return_t
New(v8::Local<v8::String> pattern, v8::RegExp::Flags flags) {
  return New<v8::RegExp>(pattern, flags);
}

#endif  // NAN_NEW_H_