Simplify Wrappable API.

This commit is contained in:
Cheng Zhao 2014-04-16 11:58:17 +08:00
parent ac5be7dad7
commit ace550d6b2
3 changed files with 63 additions and 64 deletions

View file

@ -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<WrappableBase*()>& callback) {
const base::Callback<Wrappable*()>& callback) {
return callback.Run();
};
template<typename P1>
inline WrappableBase* InvokeFactory(
inline Wrappable* InvokeFactory(
Arguments* args,
const base::Callback<WrappableBase*(P1)>& callback) {
const base::Callback<Wrappable*(P1)>& callback) {
typename CallbackParamTraits<P1>::LocalType a1;
if (!GetNextArgument(args, 0, false, &a1))
return NULL;
@ -35,9 +35,9 @@ inline WrappableBase* InvokeFactory(
};
template<typename P1, typename P2>
inline WrappableBase* InvokeFactory(
inline Wrappable* InvokeFactory(
Arguments* args,
const base::Callback<WrappableBase*(P1, P2)>& callback) {
const base::Callback<Wrappable*(P1, P2)>& callback) {
typename CallbackParamTraits<P1>::LocalType a1;
typename CallbackParamTraits<P2>::LocalType a2;
if (!GetNextArgument(args, 0, true, &a1) ||
@ -47,9 +47,9 @@ inline WrappableBase* InvokeFactory(
};
template<typename P1, typename P2, typename P3>
inline WrappableBase* InvokeFactory(
inline Wrappable* InvokeFactory(
Arguments* args,
const base::Callback<WrappableBase*(P1, P2, P3)>& callback) {
const base::Callback<Wrappable*(P1, P2, P3)>& callback) {
typename CallbackParamTraits<P1>::LocalType a1;
typename CallbackParamTraits<P2>::LocalType a2;
typename CallbackParamTraits<P3>::LocalType a3;
@ -61,9 +61,9 @@ inline WrappableBase* InvokeFactory(
};
template<typename P1, typename P2, typename P3, typename P4>
inline WrappableBase* InvokeFactory(
inline Wrappable* InvokeFactory(
Arguments* args,
const base::Callback<WrappableBase*(P1, P2, P3, P4)>& callback) {
const base::Callback<Wrappable*(P1, P2, P3, P4)>& callback) {
typename CallbackParamTraits<P1>::LocalType a1;
typename CallbackParamTraits<P2>::LocalType a2;
typename CallbackParamTraits<P3>::LocalType a3;
@ -77,9 +77,9 @@ inline WrappableBase* InvokeFactory(
};
template<typename P1, typename P2, typename P3, typename P4, typename P5>
inline WrappableBase* InvokeFactory(
inline Wrappable* InvokeFactory(
Arguments* args,
const base::Callback<WrappableBase*(P1, P2, P3, P4, P5)>& callback) {
const base::Callback<Wrappable*(P1, P2, P3, P4, P5)>& callback) {
typename CallbackParamTraits<P1>::LocalType a1;
typename CallbackParamTraits<P2>::LocalType a2;
typename CallbackParamTraits<P3>::LocalType a3;
@ -96,9 +96,9 @@ inline WrappableBase* InvokeFactory(
template<typename P1, typename P2, typename P3, typename P4, typename P5,
typename P6>
inline WrappableBase* InvokeFactory(
inline Wrappable* InvokeFactory(
Arguments* args,
const base::Callback<WrappableBase*(P1, P2, P3, P4, P5, P6)>& callback) {
const base::Callback<Wrappable*(P1, P2, P3, P4, P5, P6)>& callback) {
typename CallbackParamTraits<P1>::LocalType a1;
typename CallbackParamTraits<P2>::LocalType a2;
typename CallbackParamTraits<P3>::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<typename T>
WrappableBase* NewOperatorFactory() {
Wrappable* NewOperatorFactory() {
return new T;
}
template<typename Sig>
v8::Local<v8::FunctionTemplate> CreateConstructor(
template<typename T, typename Sig>
v8::Local<v8::Function> CreateConstructor(
v8::Isolate* isolate,
const base::StringPiece& name,
const base::Callback<Sig>& constructor) {
return Constructor<Sig>(name).GetFunctionTemplate(isolate, constructor);
const base::Callback<Sig>& callback) {
v8::Local<v8::FunctionTemplate> constructor =
Constructor<Sig>(name).GetFunctionTemplate(isolate, callback);
T::BuildPrototype(isolate, constructor->PrototypeTemplate());
return constructor->GetFunction();
}
} // namespace mate

View file

@ -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<v8::Object> wrapper) {
void Wrappable::Wrap(v8::Isolate* isolate, v8::Handle<v8::Object> 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<v8::ObjectTemplate> 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<v8::Object> WrappableBase::GetWrapperImpl(v8::Isolate* isolate) {
v8::Handle<v8::Object> Wrappable::GetWrapper(v8::Isolate* isolate) {
if (!wrapper_.IsEmpty()) {
return MATE_PERSISTENT_TO_LOCAL(v8::Object, isolate, wrapper_);
}

View file

@ -18,22 +18,24 @@ void* FromV8Impl(v8::Isolate* isolate, v8::Handle<v8::Value> 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<MyClass> {
// 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<MyClass>::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<v8::Value> 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<typename T>
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<v8::Object> GetWrapper(v8::Isolate* isolate);
// Bind the C++ class to the JS wrapper.
void Wrap(v8::Isolate* isolate, v8::Handle<v8::Object> 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<v8::ObjectTemplate> prototype);
protected:
WrappableBase();
virtual ~WrappableBase();
Wrappable();
virtual ~Wrappable();
virtual ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate);
v8::Handle<v8::Object> GetWrapperImpl(v8::Isolate* isolate);
private:
static MATE_WEAK_CALLBACK(WeakCallback, v8::Object, WrappableBase);
static MATE_WEAK_CALLBACK(WeakCallback, v8::Object, Wrappable);
v8::Persistent<v8::Object> wrapper_; // Weak
DISALLOW_COPY_AND_ASSIGN(WrappableBase);
};
template<typename T>
class Wrappable : public WrappableBase {
public:
// Retrieve (or create) the v8 wrapper object cooresponding to this object.
v8::Handle<v8::Object> 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<typename T>
struct Converter<T*, typename enable_if<
is_convertible<T*, WrappableBase*>::value>::type> {
is_convertible<T*, Wrappable*>::value>::type> {
static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, T* val) {
return val->GetWrapper(isolate);
}
static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val, T** out) {
*out = static_cast<T*>(static_cast<WrappableBase*>(
*out = static_cast<T*>(static_cast<Wrappable*>(
internal::FromV8Impl(isolate, val)));
return *out != NULL;
}