From ace550d6b20a62ca1101153a06bccb4ba1484a14 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 16 Apr 2014 11:58:17 +0800 Subject: [PATCH] Simplify Wrappable API. --- native_mate/constructor.h | 45 +++++++++++++++------------- native_mate/wrappable.cc | 19 +++++++----- native_mate/wrappable.h | 63 +++++++++++++++++---------------------- 3 files changed, 63 insertions(+), 64 deletions(-) diff --git a/native_mate/constructor.h b/native_mate/constructor.h index 36c396f6f278..d54cb518dce0 100644 --- a/native_mate/constructor.h +++ b/native_mate/constructor.h @@ -11,23 +11,23 @@ namespace mate { -class WrappableBase; +class Wrappable; namespace internal { // This set of templates invokes a base::Callback by converting the Arguments // into native types. It relies on the function_template.h to provide helper // templates. -inline WrappableBase* InvokeFactory( +inline Wrappable* InvokeFactory( Arguments* args, - const base::Callback& callback) { + const base::Callback& callback) { return callback.Run(); }; template -inline WrappableBase* InvokeFactory( +inline Wrappable* InvokeFactory( Arguments* args, - const base::Callback& callback) { + const base::Callback& callback) { typename CallbackParamTraits::LocalType a1; if (!GetNextArgument(args, 0, false, &a1)) return NULL; @@ -35,9 +35,9 @@ inline WrappableBase* InvokeFactory( }; template -inline WrappableBase* InvokeFactory( +inline Wrappable* InvokeFactory( Arguments* args, - const base::Callback& callback) { + const base::Callback& callback) { typename CallbackParamTraits::LocalType a1; typename CallbackParamTraits::LocalType a2; if (!GetNextArgument(args, 0, true, &a1) || @@ -47,9 +47,9 @@ inline WrappableBase* InvokeFactory( }; template -inline WrappableBase* InvokeFactory( +inline Wrappable* InvokeFactory( Arguments* args, - const base::Callback& callback) { + const base::Callback& callback) { typename CallbackParamTraits::LocalType a1; typename CallbackParamTraits::LocalType a2; typename CallbackParamTraits::LocalType a3; @@ -61,9 +61,9 @@ inline WrappableBase* InvokeFactory( }; template -inline WrappableBase* InvokeFactory( +inline Wrappable* InvokeFactory( Arguments* args, - const base::Callback& callback) { + const base::Callback& callback) { typename CallbackParamTraits::LocalType a1; typename CallbackParamTraits::LocalType a2; typename CallbackParamTraits::LocalType a3; @@ -77,9 +77,9 @@ inline WrappableBase* InvokeFactory( }; template -inline WrappableBase* InvokeFactory( +inline Wrappable* InvokeFactory( Arguments* args, - const base::Callback& callback) { + const base::Callback& callback) { typename CallbackParamTraits::LocalType a1; typename CallbackParamTraits::LocalType a2; typename CallbackParamTraits::LocalType a3; @@ -96,9 +96,9 @@ inline WrappableBase* InvokeFactory( template -inline WrappableBase* InvokeFactory( +inline Wrappable* InvokeFactory( Arguments* args, - const base::Callback& callback) { + const base::Callback& callback) { typename CallbackParamTraits::LocalType a1; typename CallbackParamTraits::LocalType a2; typename CallbackParamTraits::LocalType a3; @@ -146,7 +146,7 @@ class Constructor { static void New(const WrappableFactoryFunction& factory, v8::Isolate* isolate, Arguments* args) { - WrappableBase* object = internal::InvokeFactory(args, factory); + Wrappable* object = internal::InvokeFactory(args, factory); if (object) object->Wrap(isolate, args->GetThis()); else @@ -163,16 +163,19 @@ class Constructor { template -WrappableBase* NewOperatorFactory() { +Wrappable* NewOperatorFactory() { return new T; } -template -v8::Local CreateConstructor( +template +v8::Local CreateConstructor( v8::Isolate* isolate, const base::StringPiece& name, - const base::Callback& constructor) { - return Constructor(name).GetFunctionTemplate(isolate, constructor); + const base::Callback& callback) { + v8::Local constructor = + Constructor(name).GetFunctionTemplate(isolate, callback); + T::BuildPrototype(isolate, constructor->PrototypeTemplate()); + return constructor->GetFunction(); } } // namespace mate diff --git a/native_mate/wrappable.cc b/native_mate/wrappable.cc index d5bc55f34936..ed7b49f3508c 100644 --- a/native_mate/wrappable.cc +++ b/native_mate/wrappable.cc @@ -9,32 +9,37 @@ namespace mate { -WrappableBase::WrappableBase() { +Wrappable::Wrappable() { } -WrappableBase::~WrappableBase() { +Wrappable::~Wrappable() { MATE_PERSISTENT_RESET(wrapper_); } -void WrappableBase::Wrap(v8::Isolate* isolate, v8::Handle wrapper) { +void Wrappable::Wrap(v8::Isolate* isolate, v8::Handle wrapper) { MATE_SET_INTERNAL_FIELD_POINTER(wrapper, 0, this); MATE_PERSISTENT_ASSIGN(v8::Object, isolate, wrapper_, wrapper); MATE_PERSISTENT_SET_WEAK(wrapper_, this, WeakCallback); } -ObjectTemplateBuilder WrappableBase::GetObjectTemplateBuilder( +// static +void Wrappable::BuildPrototype(v8::Isolate* isolate, + v8::Handle prototype) { +} + +ObjectTemplateBuilder Wrappable::GetObjectTemplateBuilder( v8::Isolate* isolate) { return ObjectTemplateBuilder(isolate); } // static -MATE_WEAK_CALLBACK(WrappableBase::WeakCallback, v8::Object, WrappableBase) { - MATE_WEAK_CALLBACK_INIT(WrappableBase); +MATE_WEAK_CALLBACK(Wrappable::WeakCallback, v8::Object, Wrappable) { + MATE_WEAK_CALLBACK_INIT(Wrappable); MATE_PERSISTENT_RESET(self->wrapper_); delete self; } -v8::Handle WrappableBase::GetWrapperImpl(v8::Isolate* isolate) { +v8::Handle Wrappable::GetWrapper(v8::Isolate* isolate) { if (!wrapper_.IsEmpty()) { return MATE_PERSISTENT_TO_LOCAL(v8::Object, isolate, wrapper_); } diff --git a/native_mate/wrappable.h b/native_mate/wrappable.h index 9cc8141c4991..9db85496d6d5 100644 --- a/native_mate/wrappable.h +++ b/native_mate/wrappable.h @@ -18,22 +18,24 @@ void* FromV8Impl(v8::Isolate* isolate, v8::Handle val); } // namespace internal -// WrappableBase is a base class for C++ objects that have corresponding v8 wrapper -// objects. To retain a WrappableBase object on the stack, use a mate::Handle. +// Wrappable is a base class for C++ objects that have corresponding v8 wrapper +// objects. To retain a Wrappable object on the stack, use a gin::Handle. // // USAGE: // // my_class.h -// class MyClass : WrappableBase { +// class MyClass : Wrappable { // public: // // Optional, only required if non-empty template should be used. -// virtual mate::ObjectTemplateBuilder GetObjectTemplateBuilder( +// virtual gin::ObjectTemplateBuilder GetObjectTemplateBuilder( // v8::Isolate* isolate); // ... // }; // -// mate::ObjectTemplateBuilder MyClass::GetObjectTemplateBuilder( +// // my_class.cc +// gin::ObjectTemplateBuilder MyClass::GetObjectTemplateBuilder( // v8::Isolate* isolate) { -// return WrappableBase::GetObjectTemplateBuilder(isolate).SetValue("foobar", 42); +// return Wrappable::GetObjectTemplateBuilder(isolate) +// .SetValue("foobar", 42); // } // // Subclasses should also typically have private constructors and expose a @@ -42,60 +44,49 @@ void* FromV8Impl(v8::Isolate* isolate, v8::Handle val); // wrapper for the object. If clients fail to create a wrapper for a wrappable // object, the object will leak because we use the weak callback from the // wrapper as the signal to delete the wrapped object. -template -class Wrappable; - class ObjectTemplateBuilder; -// Non-template base class to share code between templates instances. -class WrappableBase { +class Wrappable { public: + // Retrieve (or create) the v8 wrapper object cooresponding to this object. + // If the type is created via the Constructor, then the GetWrapper would + // return the constructed object, otherwise it would try to create a new + // object constructed by GetObjectTemplateBuilder. + v8::Handle GetWrapper(v8::Isolate* isolate); + + // Bind the C++ class to the JS wrapper. void Wrap(v8::Isolate* isolate, v8::Handle wrapper); + // The user should define T::BuildPrototype if they want to use Constructor + // to build a constructor function for this type. + static void BuildPrototype(v8::Isolate* isolate, + v8::Handle prototype); + protected: - WrappableBase(); - virtual ~WrappableBase(); + Wrappable(); + virtual ~Wrappable(); virtual ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate); - v8::Handle GetWrapperImpl(v8::Isolate* isolate); - private: - static MATE_WEAK_CALLBACK(WeakCallback, v8::Object, WrappableBase); + static MATE_WEAK_CALLBACK(WeakCallback, v8::Object, Wrappable); v8::Persistent wrapper_; // Weak - DISALLOW_COPY_AND_ASSIGN(WrappableBase); -}; - - -template -class Wrappable : public WrappableBase { - public: - // Retrieve (or create) the v8 wrapper object cooresponding to this object. - v8::Handle GetWrapper(v8::Isolate* isolate) { - return GetWrapperImpl(isolate); - } - - protected: - Wrappable() {} - virtual ~Wrappable() {} - - private: DISALLOW_COPY_AND_ASSIGN(Wrappable); }; -// This converter handles any subclass of WrappableBase. +// This converter handles any subclass of Wrappable. template struct Converter::value>::type> { + is_convertible::value>::type> { static v8::Handle ToV8(v8::Isolate* isolate, T* val) { return val->GetWrapper(isolate); } static bool FromV8(v8::Isolate* isolate, v8::Handle val, T** out) { - *out = static_cast(static_cast( + *out = static_cast(static_cast( internal::FromV8Impl(isolate, val))); return *out != NULL; }