Constructor should wrap the this pointer.

This commit is contained in:
Cheng Zhao 2014-04-16 10:36:54 +08:00
parent bdfbef41c6
commit ac5be7dad7
3 changed files with 14 additions and 5 deletions

View file

@ -143,10 +143,12 @@ class Constructor {
} }
private: private:
static void New(const WrappableFactoryFunction& factory, Arguments* args) { static void New(const WrappableFactoryFunction& factory,
v8::Isolate* isolate,
Arguments* args) {
WrappableBase* object = internal::InvokeFactory(args, factory); WrappableBase* object = internal::InvokeFactory(args, factory);
if (object) if (object)
MATE_SET_INTERNAL_FIELD_POINTER(args->GetThis(), 0, object); object->Wrap(isolate, args->GetThis());
else else
args->ThrowError(); args->ThrowError();

View file

@ -16,6 +16,12 @@ WrappableBase::~WrappableBase() {
MATE_PERSISTENT_RESET(wrapper_); MATE_PERSISTENT_RESET(wrapper_);
} }
void WrappableBase::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( ObjectTemplateBuilder WrappableBase::GetObjectTemplateBuilder(
v8::Isolate* isolate) { v8::Isolate* isolate) {
return ObjectTemplateBuilder(isolate); return ObjectTemplateBuilder(isolate);
@ -38,9 +44,7 @@ v8::Handle<v8::Object> WrappableBase::GetWrapperImpl(v8::Isolate* isolate) {
CHECK(!templ.IsEmpty()); CHECK(!templ.IsEmpty());
CHECK_EQ(1, templ->InternalFieldCount()); CHECK_EQ(1, templ->InternalFieldCount());
v8::Handle<v8::Object> wrapper = templ->NewInstance(); v8::Handle<v8::Object> wrapper = templ->NewInstance();
MATE_SET_INTERNAL_FIELD_POINTER(wrapper, 0, this); Wrap(isolate, wrapper);
MATE_PERSISTENT_ASSIGN(v8::Object, isolate, wrapper_, wrapper);
MATE_PERSISTENT_SET_WEAK(wrapper_, this, WeakCallback);
return wrapper; return wrapper;
} }

View file

@ -49,6 +49,9 @@ class ObjectTemplateBuilder;
// Non-template base class to share code between templates instances. // Non-template base class to share code between templates instances.
class WrappableBase { class WrappableBase {
public:
void Wrap(v8::Isolate* isolate, v8::Handle<v8::Object> wrapper);
protected: protected:
WrappableBase(); WrappableBase();
virtual ~WrappableBase(); virtual ~WrappableBase();