From d48e9ffd019ae1827fda4bc74228253fa34e56a0 Mon Sep 17 00:00:00 2001 From: hammonda Date: Tue, 16 Oct 2012 10:44:22 +0100 Subject: [PATCH 1/6] Added type trait to determine if a type is convertible to a V8 JS type. --- src/vu8/detail/TypeTraits.hpp | 43 +++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/vu8/detail/TypeTraits.hpp diff --git a/src/vu8/detail/TypeTraits.hpp b/src/vu8/detail/TypeTraits.hpp new file mode 100644 index 0000000..6fb568b --- /dev/null +++ b/src/vu8/detail/TypeTraits.hpp @@ -0,0 +1,43 @@ +#ifndef TYPETRAITS_HPP_ +#define TYPETRAITS_HPP_ + +#include + +#include +#include +#include +#include +#include + +#include + +#include + +namespace vu8 { + +template struct remove_reference_and_const { + typedef typename boost::remove_const< + typename boost::remove_reference::type + >::type type; +}; + +template +struct is_to_v8_convertible : public boost::false_type { }; + +template +struct is_to_v8_convertible +>::type> : public boost::true_type { }; + +#define VU8_TO_V8_CONV_TYPE_TRAIT_SPEC(T,M,spec) \ +template \ +struct is_to_v8_convertible \ +>::type> : public boost::true_type { }; \ +/**/ + +VU8_TO_V8_CONV_TYPE_TRAIT_SPEC(T,T,v8::Handle); +VU8_TO_V8_CONV_TYPE_TRAIT_SPEC(T,typename remove_reference_and_const::type, std::string); +} + +#endif /* TYPETRAITS_HPP_ */ From 950da8da1a93305eb1b38a16fd98c4e619218645 Mon Sep 17 00:00:00 2001 From: hammonda Date: Tue, 16 Oct 2012 10:45:42 +0100 Subject: [PATCH 2/6] Added specialization to MemFunProto to allow return types which are wrapped c++ classes. --- src/vu8/detail/ProtoHelper.hpp | 78 ++++++++++++++++++++++------------ 1 file changed, 51 insertions(+), 27 deletions(-) diff --git a/src/vu8/detail/ProtoHelper.hpp b/src/vu8/detail/ProtoHelper.hpp index 1aa3b89..30c1fa7 100644 --- a/src/vu8/detail/ProtoHelper.hpp +++ b/src/vu8/detail/ProtoHelper.hpp @@ -1,23 +1,29 @@ #ifndef BOOST_PP_IS_ITERATING -# ifndef TSA_VU8_DETAIL_PROTO_HELPER_HPP -# define TSA_VU8_DETAIL_PROTO_HELPER_HPP -# include - -# include -# include -# include -# include - -# ifndef VU8_PROTO_MAX_SIZE -# define VU8_PROTO_MAX_SIZE VU8_PP_ITERATION_LIMIT -# endif -namespace vu8 { namespace detail { +# ifndef TSA_VU8_DETAIL_PROTO_HELPER_HPP +# define TSA_VU8_DETAIL_PROTO_HELPER_HPP +# include + +# include +# include +# include +# include +# include + +# ifndef VU8_PROTO_MAX_SIZE +# define VU8_PROTO_MAX_SIZE VU8_PP_ITERATION_LIMIT +# endif +namespace vu8 { + +template struct Class; +template struct ClassSingleton; + +namespace detail { namespace mpl = boost::mpl; template struct FunProtoBase { - typedef R return_type; + typedef R return_type; }; template @@ -27,37 +33,55 @@ template struct MemFunProto; } } -# define BOOST_PP_ITERATION_LIMITS (0, VU8_PROTO_MAX_SIZE - 1) -# define BOOST_PP_FILENAME_1 "vu8/detail/ProtoHelper.hpp" -# include BOOST_PP_ITERATE() -# endif // include guard +# define BOOST_PP_ITERATION_LIMITS (0, VU8_PROTO_MAX_SIZE - 1) +# define BOOST_PP_FILENAME_1 "vu8/detail/ProtoHelper.hpp" +# include BOOST_PP_ITERATE() +# endif // include guard #else // BOOST_PP_IS_ITERATING -# define n BOOST_PP_ITERATION() -# define VU8_PROTO_print(z, n, data) data +# define n BOOST_PP_ITERATION() +# define VU8_PROTO_print(z, n, data) data namespace vu8 { namespace detail { template struct FunProto : FunProtoBase { - typedef mpl::vector arguments; - typedef R(*function_type)(BOOST_PP_ENUM_PARAMS(n, A)); + typedef mpl::vector arguments; + typedef R(*function_type)(BOOST_PP_ENUM_PARAMS(n, A)); }; template struct MemFunProto : FunProtoBase { - typedef mpl::vector arguments; - typedef R(C::*method_type)(BOOST_PP_ENUM_PARAMS(n, A)); + typedef mpl::vector arguments; + typedef R(C::*method_type)(BOOST_PP_ENUM_PARAMS(n, A)); + typedef boost::false_type IS_RETURN_WRAPPED_CLASS; +}; + +template +struct MemFunProto ( BOOST_PP_ENUM_PARAMS(n, A) )> : FunProtoBase { + typedef mpl::vector arguments; + typedef R(C::*method_type)(BOOST_PP_ENUM_PARAMS(n, A)); + typedef boost::true_type IS_RETURN_WRAPPED_CLASS; + typedef vu8::ClassSingleton ClassSingleton; }; template struct MemFunProto : FunProtoBase { - typedef mpl::vector arguments; - typedef R(C::*method_type)(BOOST_PP_ENUM_PARAMS(n, A)) const; + typedef mpl::vector arguments; + typedef R(C::*method_type)(BOOST_PP_ENUM_PARAMS(n, A)) const; + typedef boost::false_type IS_RETURN_WRAPPED_CLASS; +}; + +template +struct MemFunProto ( BOOST_PP_ENUM_PARAMS(n, A) )> : FunProtoBase { + typedef mpl::vector arguments; + typedef R(C::*method_type)(BOOST_PP_ENUM_PARAMS(n, A)) const; + typedef boost::true_type IS_RETURN_WRAPPED_CLASS; + typedef vu8::ClassSingleton ClassSingleton; }; } } -# undef n +# undef n #endif From f907da78f40ce0dd33f0f937080432f73feaba52 Mon Sep 17 00:00:00 2001 From: hammonda Date: Tue, 16 Oct 2012 10:48:12 +0100 Subject: [PATCH 3/6] Added ForwardReturn method overload to handle returning wrapped classes. --- src/vu8/Class.hpp | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/src/vu8/Class.hpp b/src/vu8/Class.hpp index 65bae3a..e970061 100644 --- a/src/vu8/Class.hpp +++ b/src/vu8/Class.hpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -64,7 +65,7 @@ struct ClassSingletonFactory { template class ClassSingleton - : detail::LazySingleton< ClassSingleton >, + : private detail::LazySingleton< ClassSingleton >, public ClassSingletonFactory, Factory> { friend class ClassSingletonFactory, Factory>; @@ -92,22 +93,40 @@ class ClassSingleton static inline typename boost::disable_if< detail::PassDirectIf

, typename P::return_type >::type Invoke(T *obj, const v8::Arguments& args) { - return CallFromV8

(*obj, args); + return CallFromV8

(*obj, args); } template - static inline typename boost::disable_if< - boost::is_same, ValueHandle >::type - ForwardReturn(T *obj, const v8::Arguments& args) { - return ToV8(Invoke

(obj, args)); + static inline typename + boost::enable_if, + ValueHandle >::type ForwardReturn (T *obj, const v8::Arguments& args) { + return ToV8(Invoke

(obj, args)); } template - static inline typename boost::enable_if< - boost::is_same, ValueHandle >::type - ForwardReturn(T *obj, const v8::Arguments& args) { - Invoke

(obj, args); - return v8::Undefined(); + static inline typename + boost::enable_if, + ValueHandle >::type ForwardReturn (T *obj, const v8::Arguments& args) { + Invoke

(obj, args); + return v8::Undefined(); + } + + template + static inline typename + boost::enable_if::type ForwardReturn (T *obj, const v8::Arguments& args) { + typedef typename P::ClassSingleton LocalSelf; + typedef typename P::return_type ReturnType; + + v8::HandleScope scope; + ReturnType* return_value = new ReturnType(Invoke

(obj, args)); + v8::Local localObj = + LocalSelf::Instance().func_->GetFunction()->NewInstance(); + v8::Persistent persistentObj = + v8::Persistent::New(localObj); + persistentObj->SetInternalField(0, v8::External::New(return_value)); + persistentObj.MakeWeak(return_value, &LocalSelf::MadeWeak); + return scope.Close(localObj); } // every method is run inside a handle scope From 75eddebce4d0b7924b8851f39fd33c3383a4f967 Mon Sep 17 00:00:00 2001 From: hammonda Date: Tue, 16 Oct 2012 11:04:15 +0100 Subject: [PATCH 4/6] Added missing include for boost::is_void. --- src/vu8/Class.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/vu8/Class.hpp b/src/vu8/Class.hpp index e970061..dee3e39 100644 --- a/src/vu8/Class.hpp +++ b/src/vu8/Class.hpp @@ -16,6 +16,8 @@ #include #include +#include + #include #include From 6894587a8cbf4945d7425b9944940b03fa8d7c9d Mon Sep 17 00:00:00 2001 From: hammonda Date: Sat, 3 Nov 2012 09:50:12 +0000 Subject: [PATCH 5/6] Changed typename to class to be consistent with parent vu8 project. --- src/vu8/detail/TypeTraits.hpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/vu8/detail/TypeTraits.hpp b/src/vu8/detail/TypeTraits.hpp index 6fb568b..368cb56 100644 --- a/src/vu8/detail/TypeTraits.hpp +++ b/src/vu8/detail/TypeTraits.hpp @@ -15,24 +15,24 @@ namespace vu8 { -template struct remove_reference_and_const { - typedef typename boost::remove_const< - typename boost::remove_reference::type - >::type type; +template struct remove_reference_and_const { + typedef typename boost::remove_const< + typename boost::remove_reference::type + >::type type; }; -template +template struct is_to_v8_convertible : public boost::false_type { }; -template +template struct is_to_v8_convertible + boost::is_arithmetic >::type> : public boost::true_type { }; #define VU8_TO_V8_CONV_TYPE_TRAIT_SPEC(T,M,spec) \ -template \ +template \ struct is_to_v8_convertible \ + boost::is_same \ >::type> : public boost::true_type { }; \ /**/ From 678664ea8b9de4ec3c3880cfb5962253b3eb02a4 Mon Sep 17 00:00:00 2001 From: hammonda Date: Sat, 3 Nov 2012 09:52:43 +0000 Subject: [PATCH 6/6] Reformatted to be consistent with parent vu8 project. --- src/vu8/Class.hpp | 32 +++++++-------- src/vu8/detail/ProtoHelper.hpp | 74 +++++++++++++++++----------------- 2 files changed, 53 insertions(+), 53 deletions(-) diff --git a/src/vu8/Class.hpp b/src/vu8/Class.hpp index dee3e39..6042e95 100644 --- a/src/vu8/Class.hpp +++ b/src/vu8/Class.hpp @@ -95,40 +95,40 @@ class ClassSingleton static inline typename boost::disable_if< detail::PassDirectIf

, typename P::return_type >::type Invoke(T *obj, const v8::Arguments& args) { - return CallFromV8

(*obj, args); + return CallFromV8

(*obj, args); } template static inline typename boost::enable_if, ValueHandle >::type ForwardReturn (T *obj, const v8::Arguments& args) { - return ToV8(Invoke

(obj, args)); + return ToV8(Invoke

(obj, args)); } template static inline typename boost::enable_if, ValueHandle >::type ForwardReturn (T *obj, const v8::Arguments& args) { - Invoke

(obj, args); - return v8::Undefined(); + Invoke

(obj, args); + return v8::Undefined(); } template static inline typename boost::enable_if::type ForwardReturn (T *obj, const v8::Arguments& args) { - typedef typename P::ClassSingleton LocalSelf; - typedef typename P::return_type ReturnType; - - v8::HandleScope scope; - ReturnType* return_value = new ReturnType(Invoke

(obj, args)); - v8::Local localObj = - LocalSelf::Instance().func_->GetFunction()->NewInstance(); - v8::Persistent persistentObj = - v8::Persistent::New(localObj); - persistentObj->SetInternalField(0, v8::External::New(return_value)); - persistentObj.MakeWeak(return_value, &LocalSelf::MadeWeak); - return scope.Close(localObj); + typedef typename P::ClassSingleton LocalSelf; + typedef typename P::return_type ReturnType; + + v8::HandleScope scope; + ReturnType* return_value = new ReturnType(Invoke

(obj, args)); + v8::Local localObj = + LocalSelf::Instance().func_->GetFunction()->NewInstance(); + v8::Persistent persistentObj = + v8::Persistent::New(localObj); + persistentObj->SetInternalField(0, v8::External::New(return_value)); + persistentObj.MakeWeak(return_value, &LocalSelf::MadeWeak); + return scope.Close(localObj); } // every method is run inside a handle scope diff --git a/src/vu8/detail/ProtoHelper.hpp b/src/vu8/detail/ProtoHelper.hpp index 30c1fa7..89f1590 100644 --- a/src/vu8/detail/ProtoHelper.hpp +++ b/src/vu8/detail/ProtoHelper.hpp @@ -1,17 +1,17 @@ #ifndef BOOST_PP_IS_ITERATING -# ifndef TSA_VU8_DETAIL_PROTO_HELPER_HPP -# define TSA_VU8_DETAIL_PROTO_HELPER_HPP -# include - -# include -# include -# include -# include -# include - -# ifndef VU8_PROTO_MAX_SIZE -# define VU8_PROTO_MAX_SIZE VU8_PP_ITERATION_LIMIT -# endif +# ifndef TSA_VU8_DETAIL_PROTO_HELPER_HPP +# define TSA_VU8_DETAIL_PROTO_HELPER_HPP +# include + +# include +# include +# include +# include +# include + +# ifndef VU8_PROTO_MAX_SIZE +# define VU8_PROTO_MAX_SIZE VU8_PP_ITERATION_LIMIT +# endif namespace vu8 { template struct Class; @@ -23,7 +23,7 @@ namespace mpl = boost::mpl; template struct FunProtoBase { - typedef R return_type; + typedef R return_type; }; template @@ -33,55 +33,55 @@ template struct MemFunProto; } } -# define BOOST_PP_ITERATION_LIMITS (0, VU8_PROTO_MAX_SIZE - 1) -# define BOOST_PP_FILENAME_1 "vu8/detail/ProtoHelper.hpp" -# include BOOST_PP_ITERATE() -# endif // include guard +# define BOOST_PP_ITERATION_LIMITS (0, VU8_PROTO_MAX_SIZE - 1) +# define BOOST_PP_FILENAME_1 "vu8/detail/ProtoHelper.hpp" +# include BOOST_PP_ITERATE() +# endif // include guard #else // BOOST_PP_IS_ITERATING -# define n BOOST_PP_ITERATION() -# define VU8_PROTO_print(z, n, data) data +# define n BOOST_PP_ITERATION() +# define VU8_PROTO_print(z, n, data) data namespace vu8 { namespace detail { template struct FunProto : FunProtoBase { - typedef mpl::vector arguments; - typedef R(*function_type)(BOOST_PP_ENUM_PARAMS(n, A)); + typedef mpl::vector arguments; + typedef R(*function_type)(BOOST_PP_ENUM_PARAMS(n, A)); }; template struct MemFunProto : FunProtoBase { - typedef mpl::vector arguments; - typedef R(C::*method_type)(BOOST_PP_ENUM_PARAMS(n, A)); - typedef boost::false_type IS_RETURN_WRAPPED_CLASS; + typedef mpl::vector arguments; + typedef R(C::*method_type)(BOOST_PP_ENUM_PARAMS(n, A)); + typedef boost::false_type IS_RETURN_WRAPPED_CLASS; }; template struct MemFunProto ( BOOST_PP_ENUM_PARAMS(n, A) )> : FunProtoBase { - typedef mpl::vector arguments; - typedef R(C::*method_type)(BOOST_PP_ENUM_PARAMS(n, A)); - typedef boost::true_type IS_RETURN_WRAPPED_CLASS; - typedef vu8::ClassSingleton ClassSingleton; + typedef mpl::vector arguments; + typedef R(C::*method_type)(BOOST_PP_ENUM_PARAMS(n, A)); + typedef boost::true_type IS_RETURN_WRAPPED_CLASS; + typedef vu8::ClassSingleton ClassSingleton; }; template struct MemFunProto : FunProtoBase { - typedef mpl::vector arguments; - typedef R(C::*method_type)(BOOST_PP_ENUM_PARAMS(n, A)) const; - typedef boost::false_type IS_RETURN_WRAPPED_CLASS; + typedef mpl::vector arguments; + typedef R(C::*method_type)(BOOST_PP_ENUM_PARAMS(n, A)) const; + typedef boost::false_type IS_RETURN_WRAPPED_CLASS; }; template struct MemFunProto ( BOOST_PP_ENUM_PARAMS(n, A) )> : FunProtoBase { - typedef mpl::vector arguments; - typedef R(C::*method_type)(BOOST_PP_ENUM_PARAMS(n, A)) const; - typedef boost::true_type IS_RETURN_WRAPPED_CLASS; - typedef vu8::ClassSingleton ClassSingleton; + typedef mpl::vector arguments; + typedef R(C::*method_type)(BOOST_PP_ENUM_PARAMS(n, A)) const; + typedef boost::true_type IS_RETURN_WRAPPED_CLASS; + typedef vu8::ClassSingleton ClassSingleton; }; } } -# undef n +# undef n #endif