/********************************************************************* * NAN - Native Abstractions for Node.js * * Copyright (c) 2015 NAN contributors * * MIT License ********************************************************************/ #ifndef NAN_IMPLEMENTATION_PRE_12_INL_H_ #define NAN_IMPLEMENTATION_PRE_12_INL_H_ #include #if defined(_MSC_VER) # pragma warning( push ) # pragma warning( disable : 4530 ) # include # include # pragma warning( pop ) #else # include # include #endif //============================================================================== // node v0.10 implementation //============================================================================== namespace NanIntern { //=== Array ==================================================================== Factory::return_t Factory::New() { return v8::Array::New(); } Factory::return_t Factory::New(int length) { return v8::Array::New(length); } //=== Boolean ================================================================== Factory::return_t Factory::New(bool value) { return v8::Boolean::New(value)->ToBoolean(); } //=== Boolean Object =========================================================== Factory::return_t Factory::New(bool value) { return v8::BooleanObject::New(value).As(); } //=== Context ================================================================== Factory::return_t Factory::New( v8::ExtensionConfiguration* extensions , v8::Handle tmpl , v8::Handle obj) { v8::Persistent ctx = v8::Context::New(extensions, tmpl, obj); v8::Local lctx = v8::Local::New(ctx); ctx.Dispose(); return lctx; } //=== Date ===================================================================== Factory::return_t Factory::New(double value) { return v8::Date::New(value).As(); } //=== External ================================================================= Factory::return_t Factory::New(void * value) { return v8::External::New(value); } //=== Function ================================================================= Factory::return_t Factory::New( NanFunctionCallback callback , v8::Handle data) { return Factory::New( callback , data , v8::Handle() )->GetFunction(); } //=== FunctionTemplate ========================================================= Factory::return_t Factory::New( NanFunctionCallback callback , v8::Handle data , v8::Handle signature) { // Note(agnat): Emulate length argument here. Unfortunately, I couldn't find // a way. Have at it though... return v8::FunctionTemplate::New( callback , data , signature); } //=== Number =================================================================== Factory::return_t Factory::New(double value) { return v8::Number::New(value); } //=== Number Object ============================================================ Factory::return_t Factory::New(double value) { return v8::NumberObject::New(value).As(); } //=== Integer, Int32 and Uint32 ================================================ template typename IntegerFactory::return_t IntegerFactory::New(int32_t value) { return To(T::New(value)); } template typename IntegerFactory::return_t IntegerFactory::New(uint32_t value) { return To(T::NewFromUnsigned(value)); } Factory::return_t Factory::New(int32_t value) { return To(v8::Uint32::NewFromUnsigned(value)); } Factory::return_t Factory::New(uint32_t value) { return To(v8::Uint32::NewFromUnsigned(value)); } //=== Object =================================================================== Factory::return_t Factory::New() { return v8::Object::New(); } //=== Object Template ========================================================== Factory::return_t Factory::New() { return v8::ObjectTemplate::New(); } //=== RegExp =================================================================== Factory::return_t Factory::New( v8::Handle pattern , v8::RegExp::Flags flags) { return v8::RegExp::New(pattern, flags); } //=== Script =================================================================== Factory::return_t Factory::New( v8::Local source) { return v8::Script::New(source); } Factory::return_t Factory::New( v8::Local source , v8::ScriptOrigin const& origin) { return v8::Script::New(source, const_cast(&origin)); } //=== Signature ================================================================ Factory::return_t Factory::New(Factory::FTH receiver) { return v8::Signature::New(receiver); } //=== String =================================================================== Factory::return_t Factory::New() { return v8::String::Empty(); } Factory::return_t Factory::New(const char * value, int length) { return v8::String::New(value, length); } Factory::return_t Factory::New(std::string const& value) { assert(value.size() <= INT_MAX && "string too long"); return v8::String::New( value.data(), static_cast(value.size())); } inline void widenString(std::vector *ws, const uint8_t *s, int l = -1) { size_t len = static_cast(l); if (l < 0) { len = strlen(reinterpret_cast(s)); } assert(len <= INT_MAX && "string too long"); ws->resize(len); std::copy(s, s + len, ws->begin()); } Factory::return_t Factory::New(const uint16_t * value, int length) { return v8::String::New(value, length); } Factory::return_t Factory::New(const uint8_t * value, int length) { std::vector wideString; widenString(&wideString, value, length); if (wideString.size() == 0) { return v8::String::Empty(); } else { return v8::String::New(&wideString.front() , static_cast(wideString.size())); } } Factory::return_t Factory::New(v8::String::ExternalStringResource * value) { return v8::String::NewExternal(value); } Factory::return_t Factory::New(v8::String::ExternalAsciiStringResource * value) { return v8::String::NewExternal(value); } //=== String Object ============================================================ Factory::return_t Factory::New(v8::Handle value) { return v8::StringObject::New(value).As(); } } // end of namespace NanIntern //=== Presistents and Handles ================================================== template inline v8::Local NanNew(v8::Handle h) { return v8::Local::New(h); } template inline v8::Local NanNew(v8::Persistent const& p) { return v8::Local::New(p); } #endif // NAN_IMPLEMENTATION_PRE_12_INL_H_