diff options
Diffstat (limited to 'src/node_object_wrap.h')
-rw-r--r-- | src/node_object_wrap.h | 75 |
1 files changed, 41 insertions, 34 deletions
diff --git a/src/node_object_wrap.h b/src/node_object_wrap.h index 19f536dfe9..990c82b1db 100644 --- a/src/node_object_wrap.h +++ b/src/node_object_wrap.h @@ -38,49 +38,56 @@ namespace node { class NODE_EXTERN ObjectWrap { - public: - ObjectWrap ( ) { +public: + ObjectWrap() { refs_ = 0; } - virtual ~ObjectWrap ( ) { - if (!handle_.IsEmpty()) { - v8::Isolate* isolate = v8::Isolate::GetCurrent(); - assert(handle_.IsNearDeath(isolate)); - handle_.ClearWeak(isolate); - handle_->SetAlignedPointerInInternalField(0, 0); - handle_.Dispose(isolate); - handle_.Clear(); - } + virtual ~ObjectWrap() { + if (persistent().IsEmpty()) return; + assert(persistent().IsNearDeath()); + persistent().ClearWeak(); + persistent().Dispose(); } template <class T> - static inline T* Unwrap (v8::Handle<v8::Object> handle) { + static inline T* Unwrap(v8::Handle<v8::Object> handle) { assert(!handle.IsEmpty()); assert(handle->InternalFieldCount() > 0); return static_cast<T*>(handle->GetAlignedPointerFromInternalField(0)); } - v8::Persistent<v8::Object> handle_; // ro + inline v8::Local<v8::Object> handle() { + return handle(v8::Isolate::GetCurrent()); + } + + + inline v8::Local<v8::Object> handle(v8::Isolate* isolate) { + return v8::Local<v8::Object>::New(isolate, persistent()); + } + - protected: - inline void Wrap (v8::Handle<v8::Object> handle) { - assert(handle_.IsEmpty()); + inline v8::Persistent<v8::Object>& persistent() { + return handle_; + } + + +protected: + inline void Wrap(v8::Handle<v8::Object> handle) { + assert(persistent().IsEmpty()); assert(handle->InternalFieldCount() > 0); - v8::Isolate* isolate = v8::Isolate::GetCurrent(); - handle_ = v8::Persistent<v8::Object>::New(isolate, handle); - handle_->SetAlignedPointerInInternalField(0, this); + handle->SetAlignedPointerInInternalField(0, this); + persistent().Reset(v8::Isolate::GetCurrent(), handle); MakeWeak(); } - inline void MakeWeak (void) { - v8::Isolate* isolate = v8::Isolate::GetCurrent(); - handle_.MakeWeak(isolate, this, WeakCallback); - handle_.MarkIndependent(isolate); + inline void MakeWeak(void) { + persistent().MakeWeak(this, WeakCallback); + persistent().MarkIndependent(); } /* Ref() marks the object as being attached to an event loop. @@ -88,9 +95,9 @@ class NODE_EXTERN ObjectWrap { * all references are lost. */ virtual void Ref() { - assert(!handle_.IsEmpty()); + assert(!persistent().IsEmpty()); + persistent().ClearWeak(); refs_++; - handle_.ClearWeak(v8::Isolate::GetCurrent()); } /* Unref() marks an object as detached from the event loop. This is its @@ -103,26 +110,26 @@ class NODE_EXTERN ObjectWrap { * DO NOT CALL THIS FROM DESTRUCTOR */ virtual void Unref() { - assert(!handle_.IsEmpty()); - assert(!handle_.IsWeak(v8::Isolate::GetCurrent())); + assert(!persistent().IsEmpty()); + assert(!persistent().IsWeak()); assert(refs_ > 0); - if (--refs_ == 0) { MakeWeak(); } + if (--refs_ == 0) MakeWeak(); } + int refs_; // ro - int refs_; // ro - - - private: +private: static void WeakCallback(v8::Isolate* isolate, v8::Persistent<v8::Object>* pobj, ObjectWrap* wrap) { v8::HandleScope scope(isolate); assert(wrap->refs_ == 0); - assert(*pobj == wrap->handle_); - assert((*pobj).IsNearDeath(isolate)); + assert(*pobj == wrap->persistent()); + assert((*pobj).IsNearDeath()); delete wrap; } + + v8::Persistent<v8::Object> handle_; }; } // namespace node |