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

#ifndef NAN_IMPLEMENTATION_PRE_12_INL_H_
#define NAN_IMPLEMENTATION_PRE_12_INL_H_

//==============================================================================
// node v0.10 implementation
//==============================================================================

16
namespace imp {
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

//=== Array ====================================================================

Factory<v8::Array>::return_t
Factory<v8::Array>::New() {
  return v8::Array::New();
}

Factory<v8::Array>::return_t
Factory<v8::Array>::New(int length) {
  return v8::Array::New(length);
}

//=== Boolean ==================================================================

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

//=== Boolean Object ===========================================================

Factory<v8::BooleanObject>::return_t
Factory<v8::BooleanObject>::New(bool value) {
  return v8::BooleanObject::New(value).As<v8::BooleanObject>();
}

//=== Context ==================================================================

Factory<v8::Context>::return_t
Factory<v8::Context>::New( v8::ExtensionConfiguration* extensions
48 49
                         , v8::Local<v8::ObjectTemplate> tmpl
                         , v8::Local<v8::Value> obj) {
50 51 52 53 54 55 56 57 58 59
  v8::Persistent<v8::Context> ctx = v8::Context::New(extensions, tmpl, obj);
  v8::Local<v8::Context> lctx = v8::Local<v8::Context>::New(ctx);
  ctx.Dispose();
  return lctx;
}

//=== Date =====================================================================

Factory<v8::Date>::return_t
Factory<v8::Date>::New(double value) {
60
  return Factory<v8::Date>::return_t(v8::Date::New(value).As<v8::Date>());
61 62 63 64 65 66 67 68 69 70 71 72
}

//=== External =================================================================

Factory<v8::External>::return_t
Factory<v8::External>::New(void * value) {
  return v8::External::New(value);
}

//=== Function =================================================================

Factory<v8::Function>::return_t
73 74
Factory<v8::Function>::New( FunctionCallback callback
                          , v8::Local<v8::Value> data) {
75 76
  return Factory<v8::FunctionTemplate>::New( callback
                                           , data
77
                                           , v8::Local<v8::Signature>()
78 79 80 81 82 83 84
                                           )->GetFunction();
}


//=== FunctionTemplate =========================================================

Factory<v8::FunctionTemplate>::return_t
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
Factory<v8::FunctionTemplate>::New( FunctionCallback callback
                                  , v8::Local<v8::Value> data
                                  , v8::Local<v8::Signature> signature) {
  if (callback) {
    v8::HandleScope scope;

    v8::Local<v8::ObjectTemplate> tpl = v8::ObjectTemplate::New();
    tpl->SetInternalFieldCount(imp::kFunctionFieldCount);
    v8::Local<v8::Object> obj = tpl->NewInstance();

    obj->SetInternalField(
        imp::kFunctionIndex
      , v8::External::New(reinterpret_cast<void *>(callback)));

    v8::Local<v8::Value> val = v8::Local<v8::Value>::New(data);

    if (!val.IsEmpty()) {
      obj->SetInternalField(imp::kDataIndex, val);
    }

    // Note(agnat): Emulate length argument here. Unfortunately, I couldn't find
    // a way. Have at it though...
    return scope.Close(
        v8::FunctionTemplate::New(imp::FunctionCallbackWrapper
                                 , obj
                                 , signature));
  } else {
    return v8::FunctionTemplate::New(0, data, signature);
  }
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
}

//=== Number ===================================================================

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

//=== Number Object ============================================================

Factory<v8::NumberObject>::return_t
Factory<v8::NumberObject>::New(double value) {
  return v8::NumberObject::New(value).As<v8::NumberObject>();
}

//=== Integer, Int32 and Uint32 ================================================

template <typename T>
typename IntegerFactory<T>::return_t
IntegerFactory<T>::New(int32_t value) {
  return To<T>(T::New(value));
}

template <typename T>
typename IntegerFactory<T>::return_t
IntegerFactory<T>::New(uint32_t value) {
  return To<T>(T::NewFromUnsigned(value));
}

Factory<v8::Uint32>::return_t
Factory<v8::Uint32>::New(int32_t value) {
  return To<v8::Uint32>(v8::Uint32::NewFromUnsigned(value));
}

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


//=== Object ===================================================================

Factory<v8::Object>::return_t
Factory<v8::Object>::New() {
  return v8::Object::New();
}

//=== Object Template ==========================================================

Factory<v8::ObjectTemplate>::return_t
Factory<v8::ObjectTemplate>::New() {
  return v8::ObjectTemplate::New();
}

//=== RegExp ===================================================================

Factory<v8::RegExp>::return_t
Factory<v8::RegExp>::New(
173
    v8::Local<v8::String> pattern
174
  , v8::RegExp::Flags flags) {
175
  return Factory<v8::RegExp>::return_t(v8::RegExp::New(pattern, flags));
176 177 178 179 180 181
}

//=== Script ===================================================================

Factory<v8::Script>::return_t
Factory<v8::Script>::New( v8::Local<v8::String> source) {
182
  return Factory<v8::Script>::return_t(v8::Script::New(source));
183 184 185 186
}
Factory<v8::Script>::return_t
Factory<v8::Script>::New( v8::Local<v8::String> source
                        , v8::ScriptOrigin const& origin) {
187 188
  return Factory<v8::Script>::return_t(
      v8::Script::New(source, const_cast<v8::ScriptOrigin*>(&origin)));
189 190 191 192 193 194 195 196 197 198 199 200 201
}

//=== Signature ================================================================

Factory<v8::Signature>::return_t
Factory<v8::Signature>::New(Factory<v8::Signature>::FTH receiver) {
  return v8::Signature::New(receiver);
}

//=== String ===================================================================

Factory<v8::String>::return_t
Factory<v8::String>::New() {
202
  return Factory<v8::String>::return_t(v8::String::Empty());
203 204 205 206
}

Factory<v8::String>::return_t
Factory<v8::String>::New(const char * value, int length) {
207
  return Factory<v8::String>::return_t(v8::String::New(value, length));
208 209 210
}

Factory<v8::String>::return_t
211 212
Factory<v8::String>::New(
    std::string const& value) /* NOLINT(build/include_what_you_use) */ {
213
  assert(value.size() <= INT_MAX && "string too long");
214 215
  return Factory<v8::String>::return_t(
      v8::String::New( value.data(), static_cast<int>(value.size())));
216 217 218 219
}

Factory<v8::String>::return_t
Factory<v8::String>::New(const uint16_t * value, int length) {
220
  return Factory<v8::String>::return_t(v8::String::New(value, length));
221 222 223 224
}

Factory<v8::String>::return_t
Factory<v8::String>::New(v8::String::ExternalStringResource * value) {
225
  return Factory<v8::String>::return_t(v8::String::NewExternal(value));
226 227 228 229
}

Factory<v8::String>::return_t
Factory<v8::String>::New(v8::String::ExternalAsciiStringResource * value) {
230
  return Factory<v8::String>::return_t(v8::String::NewExternal(value));
231 232 233 234 235
}

//=== String Object ============================================================

Factory<v8::StringObject>::return_t
236
Factory<v8::StringObject>::New(v8::Local<v8::String> value) {
237 238 239
  return v8::StringObject::New(value).As<v8::StringObject>();
}

240
}  // end of namespace imp
241 242 243 244

//=== Presistents and Handles ==================================================

template <typename T>
245
inline v8::Local<T> New(v8::Handle<T> h) {
246 247 248 249
  return v8::Local<T>::New(h);
}

template <typename T>
250
inline v8::Local<T> New(v8::Persistent<T> const& p) {
251 252 253
  return v8::Local<T>::New(p);
}

254 255 256 257 258
template <typename T, typename M>
inline v8::Local<T> New(Persistent<T, M> const& p) {
  return v8::Local<T>::New(p.persistent);
}

259 260 261 262 263
template <typename T>
inline v8::Local<T> New(Global<T> const& p) {
  return v8::Local<T>::New(p.persistent);
}

264
#endif  // NAN_IMPLEMENTATION_PRE_12_INL_H_