From: Ryan Date: Thu, 23 Jul 2009 16:35:03 +0000 (+0200) Subject: ObjectWrap: MakeWeak again after each Weak callback. X-Git-Tag: v0.1.1~12 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=0462b5d1ec55f601212c465059492f192f38f791;p=platform%2Fupstream%2Fnodejs.git ObjectWrap: MakeWeak again after each Weak callback. --- diff --git a/src/object_wrap.h b/src/object_wrap.h index 30a4224..904eb76 100644 --- a/src/object_wrap.h +++ b/src/object_wrap.h @@ -9,7 +9,6 @@ namespace node { class ObjectWrap { public: ObjectWrap ( ) { - weak_ = false; attached_ = 0; } @@ -32,13 +31,19 @@ class ObjectWrap { handle->GetInternalField(0))->Value()); } - inline void Wrap(v8::Handle handle) + inline void Wrap (v8::Handle handle) { assert(handle_.IsEmpty()); assert(handle->InternalFieldCount() > 0); handle_ = v8::Persistent::New(handle); handle_->SetInternalField(0, v8::External::New(this)); - handle_.MakeWeak(this, MakeWeak); + handle_->Set(v8::String::NewSymbol("nodeId"), v8::Integer::New((int32_t)this)); + MakeWeak(); + } + + inline void MakeWeak (void) + { + handle_.MakeWeak(this, WeakCallback); } /* Attach() marks the object as being attached to an event loop. @@ -47,6 +52,7 @@ class ObjectWrap { */ void Attach() { assert(!handle_.IsEmpty()); + assert(handle_.IsWeak()); attached_++; } @@ -61,21 +67,26 @@ class ObjectWrap { */ void Detach() { assert(!handle_.IsEmpty()); + assert(handle_.IsWeak()); assert(attached_ > 0); attached_--; - if (attached_ == 0 && weak_) delete this; + if (attached_ == 0 && handle_.IsNearDeath()) delete this; } v8::Persistent handle_; // ro int attached_; // ro + private: - static void MakeWeak (v8::Persistent value, void *data) { + static void WeakCallback (v8::Persistent value, void *data) + { ObjectWrap *obj = static_cast(data); assert(value == obj->handle_); - obj->weak_ = true; - if (!obj->attached_) delete obj; + if (obj->attached_ == 0) { + delete obj; + } else { + obj->MakeWeak(); + } } - bool weak_; }; } // namespace node