/********************************************************************* * NAN - Native Abstractions for Node.js * * Copyright (c) 2015 NAN contributors * * MIT License ********************************************************************/ #ifndef NAN_IMPLEMENTATION_12_INL_H_ #define NAN_IMPLEMENTATION_12_INL_H_ //============================================================================== // node v0.11 implementation //============================================================================== namespace imp { //=== Array ==================================================================== Factory::return_t Factory::New() { return v8::Array::New(v8::Isolate::GetCurrent()); } Factory::return_t Factory::New(int length) { return v8::Array::New(v8::Isolate::GetCurrent(), length); } //=== Boolean ================================================================== Factory::return_t Factory::New(bool value) { return v8::Boolean::New(v8::Isolate::GetCurrent(), value); } //=== 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::Local tmpl , v8::Local obj) { return v8::Context::New(v8::Isolate::GetCurrent(), extensions, tmpl, obj); } //=== Date ===================================================================== #if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \ (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3)) Factory::return_t Factory::New(double value) { v8::Local ret; if (v8::Date::New(GetCurrentContext(), value). ToLocal(reinterpret_cast*>(&ret))) { return v8::MaybeLocal(ret); } else { return v8::MaybeLocal(ret); } } #else Factory::return_t Factory::New(double value) { return Factory::return_t( v8::Date::New(v8::Isolate::GetCurrent(), value).As()); } #endif //=== External ================================================================= Factory::return_t Factory::New(void * value) { return v8::External::New(v8::Isolate::GetCurrent(), value); } //=== Function ================================================================= Factory::return_t Factory::New( FunctionCallback callback , v8::Local data) { v8::Isolate *isolate = v8::Isolate::GetCurrent(); v8::EscapableHandleScope scope(isolate); v8::Local tpl = v8::ObjectTemplate::New(isolate); tpl->SetInternalFieldCount(imp::kFunctionFieldCount); v8::Local obj = NewInstance(tpl).ToLocalChecked(); obj->SetInternalField( imp::kFunctionIndex , v8::External::New(isolate, reinterpret_cast(callback))); v8::Local val = v8::Local::New(isolate, data); if (!val.IsEmpty()) { obj->SetInternalField(imp::kDataIndex, val); } return scope.Escape(v8::Function::New( isolate , imp::FunctionCallbackWrapper , obj)); } //=== Function Template ======================================================== Factory::return_t Factory::New( FunctionCallback callback , v8::Local data , v8::Local signature) { v8::Isolate *isolate = v8::Isolate::GetCurrent(); if (callback) { v8::EscapableHandleScope scope(isolate); v8::Local tpl = v8::ObjectTemplate::New(isolate); tpl->SetInternalFieldCount(imp::kFunctionFieldCount); v8::Local obj = NewInstance(tpl).ToLocalChecked(); obj->SetInternalField( imp::kFunctionIndex , v8::External::New(isolate, reinterpret_cast(callback))); v8::Local val = v8::Local::New(isolate, data); if (!val.IsEmpty()) { obj->SetInternalField(imp::kDataIndex, val); } return scope.Escape(v8::FunctionTemplate::New( isolate , imp::FunctionCallbackWrapper , obj , signature)); } else { return v8::FunctionTemplate::New(isolate, 0, data, signature); } } //=== Number =================================================================== Factory::return_t Factory::New(double value) { return v8::Number::New(v8::Isolate::GetCurrent(), value); } //=== Number Object ============================================================ Factory::return_t Factory::New(double value) { return v8::NumberObject::New( v8::Isolate::GetCurrent() , value).As(); } //=== Integer, Int32 and Uint32 ================================================ template typename IntegerFactory::return_t IntegerFactory::New(int32_t value) { return To(T::New(v8::Isolate::GetCurrent(), value)); } template typename IntegerFactory::return_t IntegerFactory::New(uint32_t value) { return To(T::NewFromUnsigned(v8::Isolate::GetCurrent(), value)); } Factory::return_t Factory::New(int32_t value) { return To( v8::Uint32::NewFromUnsigned(v8::Isolate::GetCurrent(), value)); } Factory::return_t Factory::New(uint32_t value) { return To( v8::Uint32::NewFromUnsigned(v8::Isolate::GetCurrent(), value)); } //=== Object =================================================================== Factory::return_t Factory::New() { return v8::Object::New(v8::Isolate::GetCurrent()); } //=== Object Template ========================================================== Factory::return_t Factory::New() { return v8::ObjectTemplate::New(v8::Isolate::GetCurrent()); } //=== RegExp =================================================================== #if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \ (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3)) Factory::return_t Factory::New( v8::Local pattern , v8::RegExp::Flags flags) { return v8::RegExp::New(GetCurrentContext(), pattern, flags); } #else Factory::return_t Factory::New( v8::Local pattern , v8::RegExp::Flags flags) { return Factory::return_t(v8::RegExp::New(pattern, flags)); } #endif //=== Script =================================================================== #if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \ (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3)) Factory::return_t Factory::New( v8::Local source) { v8::ScriptCompiler::Source src(source); return v8::ScriptCompiler::Compile(GetCurrentContext(), &src); } Factory::return_t Factory::New( v8::Local source , v8::ScriptOrigin const& origin) { v8::ScriptCompiler::Source src(source, origin); return v8::ScriptCompiler::Compile(GetCurrentContext(), &src); } #else Factory::return_t Factory::New( v8::Local source) { v8::ScriptCompiler::Source src(source); return Factory::return_t( v8::ScriptCompiler::Compile(v8::Isolate::GetCurrent(), &src)); } Factory::return_t Factory::New( v8::Local source , v8::ScriptOrigin const& origin) { v8::ScriptCompiler::Source src(source, origin); return Factory::return_t( v8::ScriptCompiler::Compile(v8::Isolate::GetCurrent(), &src)); } #endif //=== Signature ================================================================ Factory::return_t Factory::New(Factory::FTH receiver) { return v8::Signature::New(v8::Isolate::GetCurrent(), receiver); } //=== String =================================================================== Factory::return_t Factory::New() { return Factory::return_t( v8::String::Empty(v8::Isolate::GetCurrent())); } #if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \ (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3)) Factory::return_t Factory::New(const char * value, int length) { return v8::String::NewFromUtf8( v8::Isolate::GetCurrent(), value, v8::NewStringType::kNormal, length); } Factory::return_t Factory::New(std::string const& value) { assert(value.size() <= INT_MAX && "string too long"); return v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), value.data(), v8::NewStringType::kNormal, static_cast(value.size())); } Factory::return_t Factory::New(const uint16_t * value, int length) { return v8::String::NewFromTwoByte(v8::Isolate::GetCurrent(), value, v8::NewStringType::kNormal, length); } Factory::return_t Factory::New(v8::String::ExternalStringResource * value) { return v8::String::NewExternalTwoByte(v8::Isolate::GetCurrent(), value); } Factory::return_t Factory::New(ExternalOneByteStringResource * value) { return v8::String::NewExternalOneByte(v8::Isolate::GetCurrent(), value); } #else Factory::return_t Factory::New(const char * value, int length) { return Factory::return_t( v8::String::NewFromUtf8( v8::Isolate::GetCurrent() , value , v8::String::kNormalString , length)); } Factory::return_t Factory::New( std::string const& value) /* NOLINT(build/include_what_you_use) */ { assert(value.size() <= INT_MAX && "string too long"); return Factory::return_t( v8::String::NewFromUtf8( v8::Isolate::GetCurrent() , value.data() , v8::String::kNormalString , static_cast(value.size()))); } Factory::return_t Factory::New(const uint16_t * value, int length) { return Factory::return_t( v8::String::NewFromTwoByte( v8::Isolate::GetCurrent() , value , v8::String::kNormalString , length)); } Factory::return_t Factory::New(v8::String::ExternalStringResource * value) { return Factory::return_t( v8::String::NewExternal(v8::Isolate::GetCurrent(), value)); } Factory::return_t Factory::New(ExternalOneByteStringResource * value) { return Factory::return_t( v8::String::NewExternal(v8::Isolate::GetCurrent(), value)); } #endif //=== String Object ============================================================ Factory::return_t Factory::New(v8::Local value) { return v8::StringObject::New(value).As(); } //=== Unbound Script =========================================================== #if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \ (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3)) Factory::return_t Factory::New(v8::Local source) { v8::ScriptCompiler::Source src(source); return v8::ScriptCompiler::CompileUnboundScript( v8::Isolate::GetCurrent(), &src); } Factory::return_t Factory::New( v8::Local source , v8::ScriptOrigin const& origin) { v8::ScriptCompiler::Source src(source, origin); return v8::ScriptCompiler::CompileUnboundScript( v8::Isolate::GetCurrent(), &src); } #else Factory::return_t Factory::New(v8::Local source) { v8::ScriptCompiler::Source src(source); return Factory::return_t( v8::ScriptCompiler::CompileUnbound(v8::Isolate::GetCurrent(), &src)); } Factory::return_t Factory::New( v8::Local source , v8::ScriptOrigin const& origin) { v8::ScriptCompiler::Source src(source, origin); return Factory::return_t( v8::ScriptCompiler::CompileUnbound(v8::Isolate::GetCurrent(), &src)); } #endif } // end of namespace imp //=== Presistents and Handles ================================================== #if NODE_MODULE_VERSION < IOJS_3_0_MODULE_VERSION template inline v8::Local New(v8::Handle h) { return v8::Local::New(v8::Isolate::GetCurrent(), h); } #endif template inline v8::Local New(v8::Persistent const& p) { return v8::Local::New(v8::Isolate::GetCurrent(), p); } template inline v8::Local New(Persistent const& p) { return v8::Local::New(v8::Isolate::GetCurrent(), p); } #endif // NAN_IMPLEMENTATION_12_INL_H_