react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / contextify / node_modules / nan / nan_new.h
81145 views/*********************************************************************1* NAN - Native Abstractions for Node.js2*3* Copyright (c) 2015 NAN contributors4*5* MIT License <https://github.com/rvagg/nan/blob/master/LICENSE.md>6********************************************************************/78#ifndef NAN_NEW_H_9#define NAN_NEW_H_1011#if defined(_MSC_VER)12# pragma warning( push )13# pragma warning( disable : 4530 )14# include <string>15# pragma warning( pop )16#else17# include <string>18#endif1920namespace NanIntern { // scnr2122// TODO(agnat): Generalize23template <typename T> v8::Local<T> To(v8::Handle<v8::Integer> i);2425template <>26inline27v8::Local<v8::Integer>28To<v8::Integer>(v8::Handle<v8::Integer> i) { return i->ToInteger(); }2930template <>31inline32v8::Local<v8::Int32>33To<v8::Int32>(v8::Handle<v8::Integer> i) { return i->ToInt32(); }3435template <>36inline37v8::Local<v8::Uint32>38To<v8::Uint32>(v8::Handle<v8::Integer> i) { return i->ToUint32(); }3940template <typename T> struct FactoryBase { typedef v8::Local<T> return_t; };4142template <typename T> struct Factory;4344template <>45struct Factory<v8::Array> : FactoryBase<v8::Array> {46static inline return_t New();47static inline return_t New(int length);48};4950template <>51struct Factory<v8::Boolean> : FactoryBase<v8::Boolean> {52static inline return_t New(bool value);53};5455template <>56struct Factory<v8::BooleanObject> : FactoryBase<v8::BooleanObject> {57static inline return_t New(bool value);58};5960template <>61struct Factory<v8::Context> : FactoryBase<v8::Context> {62static inline63return_t64New( v8::ExtensionConfiguration* extensions = NULL65, v8::Handle<v8::ObjectTemplate> tmpl = v8::Handle<v8::ObjectTemplate>()66, v8::Handle<v8::Value> obj = v8::Handle<v8::Value>());67};6869template <>70struct Factory<v8::Date> : FactoryBase<v8::Date> {71static inline return_t New(double value);72};7374template <>75struct Factory<v8::External> : FactoryBase<v8::External> {76static inline return_t New(void *value);77};7879template <>80struct Factory<v8::Function> : FactoryBase<v8::Function> {81static inline82return_t83New( NanFunctionCallback callback84, v8::Handle<v8::Value> data = v8::Handle<v8::Value>());85};8687template <>88struct Factory<v8::FunctionTemplate> : FactoryBase<v8::FunctionTemplate> {89static inline90return_t91New( NanFunctionCallback callback = NULL92, v8::Handle<v8::Value> data = v8::Handle<v8::Value>()93, v8::Handle<v8::Signature> signature = v8::Handle<v8::Signature>());94};9596template <>97struct Factory<v8::Number> : FactoryBase<v8::Number> {98static inline return_t New(double value);99};100101template <>102struct Factory<v8::NumberObject> : FactoryBase<v8::NumberObject> {103static inline return_t New(double value);104};105106template <typename T>107struct IntegerFactory : FactoryBase<T> {108typedef typename FactoryBase<T>::return_t return_t;109static inline return_t New(int32_t value);110static inline return_t New(uint32_t value);111};112113template <>114struct Factory<v8::Integer> : IntegerFactory<v8::Integer> {};115116template <>117struct Factory<v8::Int32> : IntegerFactory<v8::Int32> {};118119template <>120struct Factory<v8::Uint32> : FactoryBase<v8::Uint32> {121static inline return_t New(int32_t value);122static inline return_t New(uint32_t value);123};124125template <>126struct Factory<v8::Object> : FactoryBase<v8::Object> {127static inline return_t New();128};129130template <>131struct Factory<v8::ObjectTemplate> : FactoryBase<v8::ObjectTemplate> {132static inline return_t New();133};134135template <>136struct Factory<v8::RegExp> : FactoryBase<v8::RegExp> {137static inline return_t New(138v8::Handle<v8::String> pattern, v8::RegExp::Flags flags);139};140141template <>142struct Factory<v8::Script> : FactoryBase<v8::Script> {143static inline return_t New( v8::Local<v8::String> source);144static inline return_t New( v8::Local<v8::String> source145, v8::ScriptOrigin const& origin);146};147148template <>149struct Factory<v8::Signature> : FactoryBase<v8::Signature> {150typedef v8::Handle<v8::FunctionTemplate> FTH;151static inline return_t New(FTH receiver = FTH());152};153154template <>155struct Factory<v8::String> : FactoryBase<v8::String> {156static inline return_t New();157static inline return_t New(const char *value, int length = -1);158static inline return_t New(const uint16_t *value, int length = -1);159static inline return_t New(std::string const& value);160161static inline return_t New(v8::String::ExternalStringResource * value);162static inline return_t New(NanExternalOneByteStringResource * value);163164// TODO(agnat): Deprecate.165static inline return_t New(const uint8_t * value, int length = -1);166};167168template <>169struct Factory<v8::StringObject> : FactoryBase<v8::StringObject> {170static inline return_t New(v8::Handle<v8::String> value);171};172173} // end of namespace NanIntern174175#if (NODE_MODULE_VERSION >= 12)176177namespace NanIntern {178179template <>180struct Factory<v8::UnboundScript> : FactoryBase<v8::UnboundScript> {181static inline return_t New( v8::Local<v8::String> source);182static inline return_t New( v8::Local<v8::String> source183, v8::ScriptOrigin const& origin);184};185186} // end of namespace NanIntern187188# include "nan_implementation_12_inl.h"189190#else // NODE_MODULE_VERSION >= 12191192# include "nan_implementation_pre_12_inl.h"193194#endif195196//=== API ======================================================================197198template <typename T>199typename NanIntern::Factory<T>::return_t200NanNew() {201return NanIntern::Factory<T>::New();202}203204template <typename T, typename A0>205typename NanIntern::Factory<T>::return_t206NanNew(A0 arg0) {207return NanIntern::Factory<T>::New(arg0);208}209210template <typename T, typename A0, typename A1>211typename NanIntern::Factory<T>::return_t212NanNew(A0 arg0, A1 arg1) {213return NanIntern::Factory<T>::New(arg0, arg1);214}215216template <typename T, typename A0, typename A1, typename A2>217typename NanIntern::Factory<T>::return_t218NanNew(A0 arg0, A1 arg1, A2 arg2) {219return NanIntern::Factory<T>::New(arg0, arg1, arg2);220}221222template <typename T, typename A0, typename A1, typename A2, typename A3>223typename NanIntern::Factory<T>::return_t224NanNew(A0 arg0, A1 arg1, A2 arg2, A3 arg3) {225return NanIntern::Factory<T>::New(arg0, arg1, arg2, arg3);226}227228// Note(agnat): When passing overloaded function pointers to template functions229// as generic arguments the compiler needs help in picking the right overload.230// These two functions handle NanNew<Function> and NanNew<FunctionTemplate> with231// all argument variations.232233// v8::Function and v8::FunctionTemplate with one or two arguments234template <typename T>235typename NanIntern::Factory<T>::return_t236NanNew( NanFunctionCallback callback237, v8::Handle<v8::Value> data = v8::Handle<v8::Value>()) {238return NanIntern::Factory<T>::New(callback, data);239}240241// v8::Function and v8::FunctionTemplate with three arguments242template <typename T, typename A2>243typename NanIntern::Factory<T>::return_t244NanNew( NanFunctionCallback callback245, v8::Handle<v8::Value> data = v8::Handle<v8::Value>()246, A2 a2 = A2()) {247return NanIntern::Factory<T>::New(callback, data, a2);248}249250// Convenience251252template <typename T> inline v8::Local<T> NanNew(v8::Handle<T> h);253template <typename T> inline v8::Local<T> NanNew(v8::Persistent<T> const& p);254255inline256NanIntern::Factory<v8::Boolean>::return_t257NanNew(bool value) {258return NanNew<v8::Boolean>(value);259}260261inline262NanIntern::Factory<v8::Int32>::return_t263NanNew(int32_t value) {264return NanNew<v8::Int32>(value);265}266267inline268NanIntern::Factory<v8::Uint32>::return_t269NanNew(uint32_t value) {270return NanNew<v8::Uint32>(value);271}272273inline274NanIntern::Factory<v8::Number>::return_t275NanNew(double value) {276return NanNew<v8::Number>(value);277}278279inline280NanIntern::Factory<v8::String>::return_t281NanNew(std::string const& value) {282return NanNew<v8::String>(value);283}284285inline286NanIntern::Factory<v8::String>::return_t287NanNew(const char * value, int length) {288return NanNew<v8::String>(value, length);289}290291inline292NanIntern::Factory<v8::String>::return_t293NanNew(const char * value) {294return NanNew<v8::String>(value);295}296297inline298NanIntern::Factory<v8::String>::return_t299NanNew(const uint8_t * value) {300return NanNew<v8::String>(value);301}302303inline304NanIntern::Factory<v8::String>::return_t305NanNew(const uint16_t * value) {306return NanNew<v8::String>(value);307}308309inline310NanIntern::Factory<v8::String>::return_t311NanNew(v8::String::ExternalStringResource * value) {312return NanNew<v8::String>(value);313}314315inline316NanIntern::Factory<v8::String>::return_t317NanNew(NanExternalOneByteStringResource * value) {318return NanNew<v8::String>(value);319}320321inline322NanIntern::Factory<v8::RegExp>::return_t323NanNew(v8::Handle<v8::String> pattern, v8::RegExp::Flags flags) {324return NanNew<v8::RegExp>(pattern, flags);325}326327#endif // NAN_NEW_H_328329330