From ac5be7dad7b0b8b3ece684a60b4e7b6df73b225f Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 16 Apr 2014 10:36:54 +0800 Subject: [PATCH] Constructor should wrap the this pointer. --- native_mate/constructor.h | 6 ++++-- native_mate/wrappable.cc | 10 +++++++--- native_mate/wrappable.h | 3 +++ 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/native_mate/constructor.h b/native_mate/constructor.h index 7e429cf62c..36c396f6f2 100644 --- a/native_mate/constructor.h +++ b/native_mate/constructor.h @@ -143,10 +143,12 @@ class Constructor { } 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); if (object) - MATE_SET_INTERNAL_FIELD_POINTER(args->GetThis(), 0, object); + object->Wrap(isolate, args->GetThis()); else args->ThrowError(); diff --git a/native_mate/wrappable.cc b/native_mate/wrappable.cc index e6dd876e96..d5bc55f349 100644 --- a/native_mate/wrappable.cc +++ b/native_mate/wrappable.cc @@ -16,6 +16,12 @@ WrappableBase::~WrappableBase() { MATE_PERSISTENT_RESET(wrapper_); } +void WrappableBase::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( v8::Isolate* isolate) { return ObjectTemplateBuilder(isolate); @@ -38,9 +44,7 @@ v8::Handle WrappableBase::GetWrapperImpl(v8::Isolate* isolate) { CHECK(!templ.IsEmpty()); CHECK_EQ(1, templ->InternalFieldCount()); v8::Handle wrapper = templ->NewInstance(); - MATE_SET_INTERNAL_FIELD_POINTER(wrapper, 0, this); - MATE_PERSISTENT_ASSIGN(v8::Object, isolate, wrapper_, wrapper); - MATE_PERSISTENT_SET_WEAK(wrapper_, this, WeakCallback); + Wrap(isolate, wrapper); return wrapper; } diff --git a/native_mate/wrappable.h b/native_mate/wrappable.h index 10391fae1d..9cc8141c49 100644 --- a/native_mate/wrappable.h +++ b/native_mate/wrappable.h @@ -49,6 +49,9 @@ class ObjectTemplateBuilder; // Non-template base class to share code between templates instances. class WrappableBase { + public: + void Wrap(v8::Isolate* isolate, v8::Handle wrapper); + protected: WrappableBase(); virtual ~WrappableBase();