Make Wrappable a template class
This commit is contained in:
parent
553326b006
commit
0df2d882ea
6 changed files with 114 additions and 118 deletions
|
@ -16,59 +16,59 @@
|
||||||
|
|
||||||
namespace mate {
|
namespace mate {
|
||||||
|
|
||||||
class Wrappable;
|
class WrappableBase;
|
||||||
|
|
||||||
namespace internal {
|
namespace internal {
|
||||||
|
|
||||||
// This set of templates invokes a base::Callback by converting the Arguments
|
// 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
|
// into native types. It relies on the function_template.h to provide helper
|
||||||
// templates.
|
// templates.
|
||||||
inline Wrappable* InvokeFactory(
|
inline WrappableBase* InvokeFactory(
|
||||||
Arguments* args,
|
Arguments* args,
|
||||||
const base::Callback<Wrappable*()>& callback) {
|
const base::Callback<WrappableBase*()>& callback) {
|
||||||
return callback.Run();
|
return callback.Run();
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename P1>
|
template<typename P1>
|
||||||
inline Wrappable* InvokeFactory(
|
inline WrappableBase* InvokeFactory(
|
||||||
Arguments* args,
|
Arguments* args,
|
||||||
const base::Callback<Wrappable*(P1)>& callback) {
|
const base::Callback<WrappableBase*(P1)>& callback) {
|
||||||
typename CallbackParamTraits<P1>::LocalType a1;
|
typename CallbackParamTraits<P1>::LocalType a1;
|
||||||
if (!GetNextArgument(args, 0, true, &a1))
|
if (!GetNextArgument(args, 0, true, &a1))
|
||||||
return NULL;
|
return nullptr;
|
||||||
return callback.Run(a1);
|
return callback.Run(a1);
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename P1, typename P2>
|
template<typename P1, typename P2>
|
||||||
inline Wrappable* InvokeFactory(
|
inline WrappableBase* InvokeFactory(
|
||||||
Arguments* args,
|
Arguments* args,
|
||||||
const base::Callback<Wrappable*(P1, P2)>& callback) {
|
const base::Callback<WrappableBase*(P1, P2)>& callback) {
|
||||||
typename CallbackParamTraits<P1>::LocalType a1;
|
typename CallbackParamTraits<P1>::LocalType a1;
|
||||||
typename CallbackParamTraits<P2>::LocalType a2;
|
typename CallbackParamTraits<P2>::LocalType a2;
|
||||||
if (!GetNextArgument(args, 0, true, &a1) ||
|
if (!GetNextArgument(args, 0, true, &a1) ||
|
||||||
!GetNextArgument(args, 0, false, &a2))
|
!GetNextArgument(args, 0, false, &a2))
|
||||||
return NULL;
|
return nullptr;
|
||||||
return callback.Run(a1, a2);
|
return callback.Run(a1, a2);
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename P1, typename P2, typename P3>
|
template<typename P1, typename P2, typename P3>
|
||||||
inline Wrappable* InvokeFactory(
|
inline WrappableBase* InvokeFactory(
|
||||||
Arguments* args,
|
Arguments* args,
|
||||||
const base::Callback<Wrappable*(P1, P2, P3)>& callback) {
|
const base::Callback<WrappableBase*(P1, P2, P3)>& callback) {
|
||||||
typename CallbackParamTraits<P1>::LocalType a1;
|
typename CallbackParamTraits<P1>::LocalType a1;
|
||||||
typename CallbackParamTraits<P2>::LocalType a2;
|
typename CallbackParamTraits<P2>::LocalType a2;
|
||||||
typename CallbackParamTraits<P3>::LocalType a3;
|
typename CallbackParamTraits<P3>::LocalType a3;
|
||||||
if (!GetNextArgument(args, 0, true, &a1) ||
|
if (!GetNextArgument(args, 0, true, &a1) ||
|
||||||
!GetNextArgument(args, 0, false, &a2) ||
|
!GetNextArgument(args, 0, false, &a2) ||
|
||||||
!GetNextArgument(args, 0, false, &a3))
|
!GetNextArgument(args, 0, false, &a3))
|
||||||
return NULL;
|
return nullptr;
|
||||||
return callback.Run(a1, a2, a3);
|
return callback.Run(a1, a2, a3);
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename P1, typename P2, typename P3, typename P4>
|
template<typename P1, typename P2, typename P3, typename P4>
|
||||||
inline Wrappable* InvokeFactory(
|
inline WrappableBase* InvokeFactory(
|
||||||
Arguments* args,
|
Arguments* args,
|
||||||
const base::Callback<Wrappable*(P1, P2, P3, P4)>& callback) {
|
const base::Callback<WrappableBase*(P1, P2, P3, P4)>& callback) {
|
||||||
typename CallbackParamTraits<P1>::LocalType a1;
|
typename CallbackParamTraits<P1>::LocalType a1;
|
||||||
typename CallbackParamTraits<P2>::LocalType a2;
|
typename CallbackParamTraits<P2>::LocalType a2;
|
||||||
typename CallbackParamTraits<P3>::LocalType a3;
|
typename CallbackParamTraits<P3>::LocalType a3;
|
||||||
|
@ -77,14 +77,14 @@ inline Wrappable* InvokeFactory(
|
||||||
!GetNextArgument(args, 0, false, &a2) ||
|
!GetNextArgument(args, 0, false, &a2) ||
|
||||||
!GetNextArgument(args, 0, false, &a3) ||
|
!GetNextArgument(args, 0, false, &a3) ||
|
||||||
!GetNextArgument(args, 0, false, &a4))
|
!GetNextArgument(args, 0, false, &a4))
|
||||||
return NULL;
|
return nullptr;
|
||||||
return callback.Run(a1, a2, a3, a4);
|
return callback.Run(a1, a2, a3, a4);
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename P1, typename P2, typename P3, typename P4, typename P5>
|
template<typename P1, typename P2, typename P3, typename P4, typename P5>
|
||||||
inline Wrappable* InvokeFactory(
|
inline WrappableBase* InvokeFactory(
|
||||||
Arguments* args,
|
Arguments* args,
|
||||||
const base::Callback<Wrappable*(P1, P2, P3, P4, P5)>& callback) {
|
const base::Callback<WrappableBase*(P1, P2, P3, P4, P5)>& callback) {
|
||||||
typename CallbackParamTraits<P1>::LocalType a1;
|
typename CallbackParamTraits<P1>::LocalType a1;
|
||||||
typename CallbackParamTraits<P2>::LocalType a2;
|
typename CallbackParamTraits<P2>::LocalType a2;
|
||||||
typename CallbackParamTraits<P3>::LocalType a3;
|
typename CallbackParamTraits<P3>::LocalType a3;
|
||||||
|
@ -95,15 +95,15 @@ inline Wrappable* InvokeFactory(
|
||||||
!GetNextArgument(args, 0, false, &a3) ||
|
!GetNextArgument(args, 0, false, &a3) ||
|
||||||
!GetNextArgument(args, 0, false, &a4) ||
|
!GetNextArgument(args, 0, false, &a4) ||
|
||||||
!GetNextArgument(args, 0, false, &a5))
|
!GetNextArgument(args, 0, false, &a5))
|
||||||
return NULL;
|
return nullptr;
|
||||||
return callback.Run(a1, a2, a3, a4, a5);
|
return callback.Run(a1, a2, a3, a4, a5);
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename P1, typename P2, typename P3, typename P4, typename P5,
|
template<typename P1, typename P2, typename P3, typename P4, typename P5,
|
||||||
typename P6>
|
typename P6>
|
||||||
inline Wrappable* InvokeFactory(
|
inline WrappableBase* InvokeFactory(
|
||||||
Arguments* args,
|
Arguments* args,
|
||||||
const base::Callback<Wrappable*(P1, P2, P3, P4, P5, P6)>& callback) {
|
const base::Callback<WrappableBase*(P1, P2, P3, P4, P5, P6)>& callback) {
|
||||||
typename CallbackParamTraits<P1>::LocalType a1;
|
typename CallbackParamTraits<P1>::LocalType a1;
|
||||||
typename CallbackParamTraits<P2>::LocalType a2;
|
typename CallbackParamTraits<P2>::LocalType a2;
|
||||||
typename CallbackParamTraits<P3>::LocalType a3;
|
typename CallbackParamTraits<P3>::LocalType a3;
|
||||||
|
@ -116,7 +116,7 @@ inline Wrappable* InvokeFactory(
|
||||||
!GetNextArgument(args, 0, false, &a4) ||
|
!GetNextArgument(args, 0, false, &a4) ||
|
||||||
!GetNextArgument(args, 0, false, &a5) ||
|
!GetNextArgument(args, 0, false, &a5) ||
|
||||||
!GetNextArgument(args, 0, false, &a6))
|
!GetNextArgument(args, 0, false, &a6))
|
||||||
return NULL;
|
return nullptr;
|
||||||
return callback.Run(a1, a2, a3, a4, a5, a6);
|
return callback.Run(a1, a2, a3, a4, a5, a6);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -156,7 +156,7 @@ class Constructor {
|
||||||
MATE_METHOD_RETURN_UNDEFINED();
|
MATE_METHOD_RETURN_UNDEFINED();
|
||||||
}
|
}
|
||||||
|
|
||||||
Wrappable* object;
|
WrappableBase* object;
|
||||||
{
|
{
|
||||||
// Don't continue if the constructor throws an exception.
|
// Don't continue if the constructor throws an exception.
|
||||||
v8::TryCatch try_catch;
|
v8::TryCatch try_catch;
|
||||||
|
@ -168,7 +168,7 @@ class Constructor {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (object)
|
if (object)
|
||||||
object->Wrap(isolate, args->GetThis());
|
object->InitWith(isolate, args->GetThis());
|
||||||
else
|
else
|
||||||
args->ThrowError();
|
args->ThrowError();
|
||||||
|
|
||||||
|
@ -176,14 +176,14 @@ class Constructor {
|
||||||
}
|
}
|
||||||
|
|
||||||
base::StringPiece name_;
|
base::StringPiece name_;
|
||||||
v8::UniquePersistent<v8::FunctionTemplate> constructor_;
|
v8::Global<v8::FunctionTemplate> constructor_;
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(Constructor);
|
DISALLOW_COPY_AND_ASSIGN(Constructor);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
Wrappable* NewOperatorFactory() {
|
WrappableBase* NewOperatorFactory() {
|
||||||
return new T;
|
return new T;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,7 @@ $var MAX_ARITY = 6
|
||||||
|
|
||||||
namespace mate {
|
namespace mate {
|
||||||
|
|
||||||
class Wrappable;
|
class WrappableBase;
|
||||||
|
|
||||||
namespace internal {
|
namespace internal {
|
||||||
|
|
||||||
|
@ -37,9 +37,9 @@ $if ARITY == 0 [[
|
||||||
template<$for ARG , [[typename P$(ARG)]]>
|
template<$for ARG , [[typename P$(ARG)]]>
|
||||||
]]
|
]]
|
||||||
|
|
||||||
inline Wrappable* InvokeFactory(
|
inline WrappableBase* InvokeFactory(
|
||||||
Arguments* args,
|
Arguments* args,
|
||||||
const base::Callback<Wrappable*($for ARG , [[P$(ARG)]])>& callback) {
|
const base::Callback<WrappableBase*($for ARG , [[P$(ARG)]])>& callback) {
|
||||||
$if ARITY != 0 [[
|
$if ARITY != 0 [[
|
||||||
|
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ $for ARG [[ typename CallbackParamTraits<P$(ARG)>::LocalType a$(ARG);
|
||||||
]]
|
]]
|
||||||
if ($for ARG ||
|
if ($for ARG ||
|
||||||
[[!GetNextArgument(args, 0, $if ARG == 1 [[true]] $else [[false]], &a$(ARG))]])
|
[[!GetNextArgument(args, 0, $if ARG == 1 [[true]] $else [[false]], &a$(ARG))]])
|
||||||
return NULL;
|
return nullptr;
|
||||||
]]
|
]]
|
||||||
|
|
||||||
return callback.Run($for ARG , [[a$(ARG)]]);
|
return callback.Run($for ARG , [[a$(ARG)]]);
|
||||||
|
@ -92,7 +92,7 @@ class Constructor {
|
||||||
MATE_METHOD_RETURN_UNDEFINED();
|
MATE_METHOD_RETURN_UNDEFINED();
|
||||||
}
|
}
|
||||||
|
|
||||||
Wrappable* object;
|
WrappableBase* object;
|
||||||
{
|
{
|
||||||
// Don't continue if the constructor throws an exception.
|
// Don't continue if the constructor throws an exception.
|
||||||
v8::TryCatch try_catch;
|
v8::TryCatch try_catch;
|
||||||
|
@ -104,7 +104,7 @@ class Constructor {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (object)
|
if (object)
|
||||||
object->Wrap(isolate, args->GetThis());
|
object->InitWith(isolate, args->GetThis());
|
||||||
else
|
else
|
||||||
args->ThrowError();
|
args->ThrowError();
|
||||||
|
|
||||||
|
@ -112,14 +112,14 @@ class Constructor {
|
||||||
}
|
}
|
||||||
|
|
||||||
base::StringPiece name_;
|
base::StringPiece name_;
|
||||||
v8::UniquePersistent<v8::FunctionTemplate> constructor_;
|
v8::Global<v8::FunctionTemplate> constructor_;
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(Constructor);
|
DISALLOW_COPY_AND_ASSIGN(Constructor);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
Wrappable* NewOperatorFactory() {
|
WrappableBase* NewOperatorFactory() {
|
||||||
return new T;
|
return new T;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,8 @@ namespace internal {
|
||||||
struct Destroyable {
|
struct Destroyable {
|
||||||
static void Destroy(Arguments* args) {
|
static void Destroy(Arguments* args) {
|
||||||
v8::Local<v8::Object> holder = args->GetHolder();
|
v8::Local<v8::Object> holder = args->GetHolder();
|
||||||
delete static_cast<Wrappable*>(holder->GetAlignedPointerFromInternalField(0));
|
delete static_cast<WrappableBase*>(
|
||||||
|
holder->GetAlignedPointerFromInternalField(0));
|
||||||
holder->SetAlignedPointerInInternalField(0, nullptr);
|
holder->SetAlignedPointerInInternalField(0, nullptr);
|
||||||
}
|
}
|
||||||
static bool IsDestroyed(Arguments* args) {
|
static bool IsDestroyed(Arguments* args) {
|
||||||
|
|
|
@ -60,7 +60,7 @@ struct Converter<mate::Handle<T> > {
|
||||||
// without having to write out the type of the object explicitly.
|
// without having to write out the type of the object explicitly.
|
||||||
template<typename T>
|
template<typename T>
|
||||||
mate::Handle<T> CreateHandle(v8::Isolate* isolate, T* object) {
|
mate::Handle<T> CreateHandle(v8::Isolate* isolate, T* object) {
|
||||||
return mate::Handle<T>(object->GetWrapper(isolate), object);
|
return mate::Handle<T>(object->GetWrapper(), object);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace mate
|
} // namespace mate
|
||||||
|
|
|
@ -10,21 +10,25 @@
|
||||||
|
|
||||||
namespace mate {
|
namespace mate {
|
||||||
|
|
||||||
Wrappable::Wrappable() : isolate_(NULL) {
|
WrappableBase::WrappableBase()
|
||||||
|
: isolate_(nullptr) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Wrappable::~Wrappable() {
|
WrappableBase::~WrappableBase() {
|
||||||
if (!wrapper_.IsEmpty())
|
if (!wrapper_.IsEmpty())
|
||||||
GetWrapper(isolate())->SetAlignedPointerInInternalField(0, nullptr);
|
GetWrapper()->SetAlignedPointerInInternalField(0, nullptr);
|
||||||
wrapper_.Reset();
|
wrapper_.Reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Wrappable::Wrap(v8::Isolate* isolate, v8::Local<v8::Object> wrapper) {
|
v8::Local<v8::Object> WrappableBase::GetWrapper() {
|
||||||
if (!wrapper_.IsEmpty())
|
CHECK(!wrapper_.IsEmpty());
|
||||||
return;
|
return v8::Local<v8::Object>::New(isolate_, wrapper_);
|
||||||
|
}
|
||||||
|
|
||||||
|
void WrappableBase::InitWith(v8::Isolate* isolate,
|
||||||
|
v8::Local<v8::Object> wrapper) {
|
||||||
|
CHECK(wrapper_.IsEmpty());
|
||||||
isolate_ = isolate;
|
isolate_ = isolate;
|
||||||
|
|
||||||
wrapper->SetAlignedPointerInInternalField(0, this);
|
wrapper->SetAlignedPointerInInternalField(0, this);
|
||||||
wrapper_.Reset(isolate, wrapper);
|
wrapper_.Reset(isolate, wrapper);
|
||||||
wrapper_.SetWeak(this, FirstWeakCallback, v8::WeakCallbackType::kParameter);
|
wrapper_.SetWeak(this, FirstWeakCallback, v8::WeakCallbackType::kParameter);
|
||||||
|
@ -38,57 +42,29 @@ void Wrappable::Wrap(v8::Isolate* isolate, v8::Local<v8::Object> wrapper) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// static
|
// static
|
||||||
void Wrappable::BuildPrototype(v8::Isolate* isolate,
|
void WrappableBase::FirstWeakCallback(
|
||||||
v8::Local<v8::ObjectTemplate> prototype) {
|
const v8::WeakCallbackInfo<WrappableBase>& data) {
|
||||||
}
|
WrappableBase* wrappable = data.GetParameter();
|
||||||
|
|
||||||
ObjectTemplateBuilder Wrappable::GetObjectTemplateBuilder(
|
|
||||||
v8::Isolate* isolate) {
|
|
||||||
return ObjectTemplateBuilder(isolate);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Wrappable::FirstWeakCallback(const v8::WeakCallbackInfo<Wrappable>& data) {
|
|
||||||
Wrappable* wrappable = data.GetParameter();
|
|
||||||
wrappable->wrapper_.Reset();
|
wrappable->wrapper_.Reset();
|
||||||
data.SetSecondPassCallback(SecondWeakCallback);
|
data.SetSecondPassCallback(SecondWeakCallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Wrappable::SecondWeakCallback(
|
// static
|
||||||
const v8::WeakCallbackInfo<Wrappable>& data) {
|
void WrappableBase::SecondWeakCallback(
|
||||||
Wrappable* wrappable = data.GetParameter();
|
const v8::WeakCallbackInfo<WrappableBase>& data) {
|
||||||
|
WrappableBase* wrappable = data.GetParameter();
|
||||||
delete wrappable;
|
delete wrappable;
|
||||||
}
|
}
|
||||||
|
|
||||||
v8::Local<v8::Object> Wrappable::GetWrapper(v8::Isolate* isolate) {
|
|
||||||
if (!wrapper_.IsEmpty())
|
|
||||||
return MATE_PERSISTENT_TO_LOCAL(v8::Object, isolate, wrapper_);
|
|
||||||
|
|
||||||
v8::Local<v8::ObjectTemplate> templ =
|
|
||||||
GetObjectTemplateBuilder(isolate).Build();
|
|
||||||
CHECK(!templ.IsEmpty());
|
|
||||||
CHECK_EQ(1, templ->InternalFieldCount());
|
|
||||||
v8::Local<v8::Object> wrapper;
|
|
||||||
// |wrapper| may be empty in some extreme cases, e.g., when
|
|
||||||
// Object.prototype.constructor is overwritten.
|
|
||||||
if (!templ->NewInstance(isolate->GetCurrentContext()).ToLocal(&wrapper)) {
|
|
||||||
// The current wrappable object will be no longer managed by V8. Delete this
|
|
||||||
// now.
|
|
||||||
delete this;
|
|
||||||
return wrapper;
|
|
||||||
}
|
|
||||||
Wrap(isolate, wrapper);
|
|
||||||
return wrapper;
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace internal {
|
namespace internal {
|
||||||
|
|
||||||
void* FromV8Impl(v8::Isolate* isolate, v8::Local<v8::Value> val) {
|
void* FromV8Impl(v8::Isolate* isolate, v8::Local<v8::Value> val) {
|
||||||
if (!val->IsObject())
|
if (!val->IsObject())
|
||||||
return NULL;
|
return nullptr;
|
||||||
v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(val);
|
v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(val);
|
||||||
if (obj->InternalFieldCount() != 1)
|
if (obj->InternalFieldCount() != 1)
|
||||||
return NULL;
|
return nullptr;
|
||||||
return MATE_GET_INTERNAL_FIELD_POINTER(obj, 0);
|
return obj->GetAlignedPointerFromInternalField(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace internal
|
} // namespace internal
|
||||||
|
|
|
@ -27,83 +27,102 @@ void* FromV8Impl(v8::Isolate* isolate, v8::Local<v8::Value> val);
|
||||||
// // my_class.h
|
// // my_class.h
|
||||||
// class MyClass : Wrappable<MyClass> {
|
// class MyClass : Wrappable<MyClass> {
|
||||||
// public:
|
// public:
|
||||||
// // Optional, only required if non-empty template should be used.
|
|
||||||
// virtual gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
|
|
||||||
// v8::Isolate* isolate);
|
|
||||||
// ...
|
// ...
|
||||||
// };
|
// };
|
||||||
//
|
//
|
||||||
// // my_class.cc
|
|
||||||
// gin::ObjectTemplateBuilder MyClass::GetObjectTemplateBuilder(
|
|
||||||
// v8::Isolate* isolate) {
|
|
||||||
// return Wrappable<MyClass>::GetObjectTemplateBuilder(isolate)
|
|
||||||
// .SetValue("foobar", 42);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// Subclasses should also typically have private constructors and expose a
|
// Subclasses should also typically have private constructors and expose a
|
||||||
// static Create function that returns a mate::Handle. Forcing creators through
|
// static Create function that returns a mate::Handle. Forcing creators through
|
||||||
// this static Create function will enforce that clients actually create a
|
// this static Create function will enforce that clients actually create a
|
||||||
// wrapper for the object. If clients fail to create a wrapper for a wrappable
|
// 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
|
// object, the object will leak because we use the weak callback from the
|
||||||
// wrapper as the signal to delete the wrapped object.
|
// wrapper as the signal to delete the wrapped object.
|
||||||
class ObjectTemplateBuilder;
|
class WrappableBase {
|
||||||
|
|
||||||
class Wrappable {
|
|
||||||
public:
|
public:
|
||||||
// Retrieve (or create) the v8 wrapper object cooresponding to this object.
|
WrappableBase();
|
||||||
// If the type is created via the Constructor, then the GetWrapper would
|
virtual ~WrappableBase();
|
||||||
// return the constructed object, otherwise it would try to create a new
|
|
||||||
// object constructed by GetObjectTemplateBuilder.
|
// Retrieve the v8 wrapper object cooresponding to this object.
|
||||||
v8::Local<v8::Object> GetWrapper(v8::Isolate* isolate);
|
v8::Local<v8::Object> GetWrapper();
|
||||||
|
|
||||||
// Returns the Isolate this object is created in.
|
// Returns the Isolate this object is created in.
|
||||||
v8::Isolate* isolate() const { return isolate_; }
|
v8::Isolate* isolate() const { return isolate_; }
|
||||||
|
|
||||||
// Bind the C++ class to the JS wrapper.
|
// Bind the C++ class to the JS wrapper.
|
||||||
void Wrap(v8::Isolate* isolate, v8::Local<v8::Object> wrapper);
|
void InitWith(v8::Isolate* isolate, v8::Local<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::Local<v8::ObjectTemplate> prototype);
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Wrappable();
|
|
||||||
virtual ~Wrappable();
|
|
||||||
|
|
||||||
virtual ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate);
|
|
||||||
|
|
||||||
// Called after the "_init" method gets called in JavaScript.
|
// Called after the "_init" method gets called in JavaScript.
|
||||||
|
// FIXME(zcbenz): Should remove this.
|
||||||
virtual void AfterInit(v8::Isolate* isolate) {}
|
virtual void AfterInit(v8::Isolate* isolate) {}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend struct internal::Destroyable;
|
friend struct internal::Destroyable;
|
||||||
|
|
||||||
static void FirstWeakCallback(const v8::WeakCallbackInfo<Wrappable>& data);
|
static void FirstWeakCallback(
|
||||||
static void SecondWeakCallback(const v8::WeakCallbackInfo<Wrappable>& data);
|
const v8::WeakCallbackInfo<WrappableBase>& data);
|
||||||
|
static void SecondWeakCallback(
|
||||||
|
const v8::WeakCallbackInfo<WrappableBase>& data);
|
||||||
|
|
||||||
v8::Isolate* isolate_;
|
v8::Isolate* isolate_;
|
||||||
v8::UniquePersistent<v8::Object> wrapper_; // Weak
|
v8::Global<v8::Object> wrapper_; // Weak
|
||||||
|
|
||||||
|
DISALLOW_COPY_AND_ASSIGN(WrappableBase);
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
class Wrappable : public WrappableBase {
|
||||||
|
public:
|
||||||
|
Wrappable() {}
|
||||||
|
|
||||||
|
// Init the class with T::BuildPrototype.
|
||||||
|
void Init(v8::Isolate* isolate) {
|
||||||
|
// Fill the object template.
|
||||||
|
if (templ_.IsEmpty()) {
|
||||||
|
v8::Local<v8::ObjectTemplate> templ = v8::ObjectTemplate::New(isolate);
|
||||||
|
T::BuildPrototype(isolate, templ);
|
||||||
|
templ_.Reset(isolate, templ);
|
||||||
|
}
|
||||||
|
|
||||||
|
v8::Local<v8::Object> wrapper;
|
||||||
|
v8::Local<v8::ObjectTemplate> templ = v8::Local<v8::ObjectTemplate>::New(
|
||||||
|
isolate, templ_);
|
||||||
|
// |wrapper| may be empty in some extreme cases, e.g., when
|
||||||
|
// Object.prototype.constructor is overwritten.
|
||||||
|
if (!templ->NewInstance(isolate->GetCurrentContext()).ToLocal(&wrapper)) {
|
||||||
|
// The current wrappable object will be no longer managed by V8. Delete
|
||||||
|
// this now.
|
||||||
|
delete this;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
InitWith(isolate, wrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static v8::Global<v8::ObjectTemplate> templ_;
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(Wrappable);
|
DISALLOW_COPY_AND_ASSIGN(Wrappable);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// static
|
||||||
|
template<typename T>
|
||||||
|
v8::Global<v8::ObjectTemplate> Wrappable<T>::templ_;
|
||||||
|
|
||||||
// This converter handles any subclass of Wrappable.
|
// This converter handles any subclass of Wrappable.
|
||||||
template<typename T>
|
template <typename T>
|
||||||
struct Converter<T*, typename enable_if<
|
struct Converter<T*,
|
||||||
is_convertible<T*, Wrappable*>::value>::type> {
|
typename std::enable_if<
|
||||||
|
std::is_convertible<T*, WrappableBase*>::value>::type> {
|
||||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, T* val) {
|
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, T* val) {
|
||||||
if (val)
|
if (val)
|
||||||
return val->GetWrapper(isolate);
|
return val->GetWrapper();
|
||||||
else
|
else
|
||||||
return v8::Null(isolate);
|
return v8::Null(isolate);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val, T** out) {
|
static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val, T** out) {
|
||||||
*out = static_cast<T*>(static_cast<Wrappable*>(
|
*out = static_cast<T*>(static_cast<WrappableBase*>(
|
||||||
internal::FromV8Impl(isolate, val)));
|
internal::FromV8Impl(isolate, val)));
|
||||||
return *out != NULL;
|
return *out != nullptr;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue