@@ -13,15 +13,12 @@ class ObjectWrap {
1313 }
1414
1515 virtual ~ObjectWrap ( ) {
16- if (!handle_.IsEmpty ()) {
17- assert (handle_.IsNearDeath ());
18- handle_->SetInternalField (0 , v8::Undefined ());
19- handle_.Dispose ();
20- handle_.Clear ();
21- }
16+ assert (handle_.IsNearDeath ());
17+ handle_->SetInternalField (0 , v8::Undefined ());
18+ handle_.Dispose ();
19+ handle_.Clear ();
2220 }
2321
24- protected:
2522 template <class T >
2623 static inline T* Unwrap (v8::Handle<v8::Object> handle)
2724 {
@@ -31,6 +28,9 @@ class ObjectWrap {
3128 handle->GetInternalField (0 ))->Value ());
3229 }
3330
31+ v8::Persistent<v8::Object> handle_; // ro
32+
33+ protected:
3434 inline void Wrap (v8::Handle<v8::Object> handle)
3535 {
3636 assert (handle_.IsEmpty ());
@@ -51,8 +51,8 @@ class ObjectWrap {
5151 */
5252 virtual void Ref () {
5353 assert (!handle_.IsEmpty ());
54- assert (handle_.IsWeak ());
5554 refs_++;
55+ handle_.ClearWeak ();
5656 }
5757
5858 /* Unref() marks an object as detached from the event loop. This is its
@@ -66,25 +66,20 @@ class ObjectWrap {
6666 */
6767 virtual void Unref () {
6868 assert (!handle_.IsEmpty ());
69- assert (handle_.IsWeak ());
69+ assert (! handle_.IsWeak ());
7070 assert (refs_ > 0 );
71- refs_--;
72- if (refs_ == 0 && handle_.IsNearDeath ()) delete this ;
71+ if (--refs_ == 0 ) { MakeWeak (); }
7372 }
7473
75- v8::Persistent<v8::Object> handle_; // ro
7674 int refs_; // ro
7775
7876 private:
7977 static void WeakCallback (v8::Persistent<v8::Value> value, void *data)
8078 {
8179 ObjectWrap *obj = static_cast <ObjectWrap*>(data);
8280 assert (value == obj->handle_ );
83- if (obj->refs_ == 0 ) {
84- delete obj;
85- } else {
86- obj->MakeWeak ();
87- }
81+ assert (!obj->refs_ );
82+ if (value.IsNearDeath ()) delete obj;
8883 }
8984};
9085
0 commit comments