/********************************************************************* * NAN - Native Abstractions for Node.js * * Copyright (c) 2015 NAN contributors * * MIT License ********************************************************************/ #ifndef NAN_NEW_H_ #define NAN_NEW_H_ #if defined(_MSC_VER) # pragma warning( push ) # pragma warning( disable : 4530 ) # include # pragma warning( pop ) #else # include #endif namespace NanIntern { // scnr // TODO(agnat): Generalize template v8::Local To(v8::Handle i); template <> inline v8::Local To(v8::Handle i) { return i->ToInteger(); } template <> inline v8::Local To(v8::Handle i) { return i->ToInt32(); } template <> inline v8::Local To(v8::Handle i) { return i->ToUint32(); } template struct FactoryBase { typedef v8::Local return_t; }; template struct Factory; template <> struct Factory : FactoryBase { static inline return_t New(); static inline return_t New(int length); }; template <> struct Factory : FactoryBase { static inline return_t New(bool value); }; template <> struct Factory : FactoryBase { static inline return_t New(bool value); }; template <> struct Factory : FactoryBase { static inline return_t New( v8::ExtensionConfiguration* extensions = NULL , v8::Handle tmpl = v8::Handle() , v8::Handle obj = v8::Handle()); }; template <> struct Factory : FactoryBase { static inline return_t New(double value); }; template <> struct Factory : FactoryBase { static inline return_t New(void *value); }; template <> struct Factory : FactoryBase { static inline return_t New( NanFunctionCallback callback , v8::Handle data = v8::Handle()); }; template <> struct Factory : FactoryBase { static inline return_t New( NanFunctionCallback callback = NULL , v8::Handle data = v8::Handle() , v8::Handle signature = v8::Handle()); }; template <> struct Factory : FactoryBase { static inline return_t New(double value); }; template <> struct Factory : FactoryBase { static inline return_t New(double value); }; template struct IntegerFactory : FactoryBase { typedef typename FactoryBase::return_t return_t; static inline return_t New(int32_t value); static inline return_t New(uint32_t value); }; template <> struct Factory : IntegerFactory {}; template <> struct Factory : IntegerFactory {}; template <> struct Factory : FactoryBase { static inline return_t New(int32_t value); static inline return_t New(uint32_t value); }; template <> struct Factory : FactoryBase { static inline return_t New(); }; template <> struct Factory : FactoryBase { static inline return_t New(); }; template <> struct Factory : FactoryBase { static inline return_t New( v8::Handle pattern, v8::RegExp::Flags flags); }; template <> struct Factory : FactoryBase { static inline return_t New( v8::Local source); static inline return_t New( v8::Local source , v8::ScriptOrigin const& origin); }; template <> struct Factory : FactoryBase { typedef v8::Handle FTH; static inline return_t New(FTH receiver = FTH()); }; template <> struct Factory : FactoryBase { 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(NanExternalOneByteStringResource * value); // TODO(agnat): Deprecate. static inline return_t New(const uint8_t * value, int length = -1); }; template <> struct Factory : FactoryBase { static inline return_t New(v8::Handle value); }; } // end of namespace NanIntern #if (NODE_MODULE_VERSION >= 12) namespace NanIntern { template <> struct Factory : FactoryBase { static inline return_t New( v8::Local source); static inline return_t New( v8::Local source , v8::ScriptOrigin const& origin); }; } // end of namespace NanIntern # include "nan_implementation_12_inl.h" #else // NODE_MODULE_VERSION >= 12 # include "nan_implementation_pre_12_inl.h" #endif //=== API ====================================================================== template typename NanIntern::Factory::return_t NanNew() { return NanIntern::Factory::New(); } template typename NanIntern::Factory::return_t NanNew(A0 arg0) { return NanIntern::Factory::New(arg0); } template typename NanIntern::Factory::return_t NanNew(A0 arg0, A1 arg1) { return NanIntern::Factory::New(arg0, arg1); } template typename NanIntern::Factory::return_t NanNew(A0 arg0, A1 arg1, A2 arg2) { return NanIntern::Factory::New(arg0, arg1, arg2); } template typename NanIntern::Factory::return_t NanNew(A0 arg0, A1 arg1, A2 arg2, A3 arg3) { return NanIntern::Factory::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 NanNew and NanNew with // all argument variations. // v8::Function and v8::FunctionTemplate with one or two arguments template typename NanIntern::Factory::return_t NanNew( NanFunctionCallback callback , v8::Handle data = v8::Handle()) { return NanIntern::Factory::New(callback, data); } // v8::Function and v8::FunctionTemplate with three arguments template typename NanIntern::Factory::return_t NanNew( NanFunctionCallback callback , v8::Handle data = v8::Handle() , A2 a2 = A2()) { return NanIntern::Factory::New(callback, data, a2); } // Convenience template inline v8::Local NanNew(v8::Handle h); template inline v8::Local NanNew(v8::Persistent const& p); inline NanIntern::Factory::return_t NanNew(bool value) { return NanNew(value); } inline NanIntern::Factory::return_t NanNew(int32_t value) { return NanNew(value); } inline NanIntern::Factory::return_t NanNew(uint32_t value) { return NanNew(value); } inline NanIntern::Factory::return_t NanNew(double value) { return NanNew(value); } inline NanIntern::Factory::return_t NanNew(std::string const& value) { return NanNew(value); } inline NanIntern::Factory::return_t NanNew(const char * value, int length) { return NanNew(value, length); } inline NanIntern::Factory::return_t NanNew(const char * value) { return NanNew(value); } inline NanIntern::Factory::return_t NanNew(const uint8_t * value) { return NanNew(value); } inline NanIntern::Factory::return_t NanNew(const uint16_t * value) { return NanNew(value); } inline NanIntern::Factory::return_t NanNew(v8::String::ExternalStringResource * value) { return NanNew(value); } inline NanIntern::Factory::return_t NanNew(NanExternalOneByteStringResource * value) { return NanNew(value); } inline NanIntern::Factory::return_t NanNew(v8::Handle pattern, v8::RegExp::Flags flags) { return NanNew(pattern, flags); } #endif // NAN_NEW_H_