15 virtual ~ObjectWrap ( ) {
16 if (!handle_.IsEmpty()) {
17 assert(handle_.IsNearDeath());
18 handle_->SetInternalField(0, v8::Undefined());
25 static inline T* Unwrap (v8::Handle<v8::Object> handle)
27 assert(!handle.IsEmpty());
28 assert(handle->InternalFieldCount() > 0);
29 return static_cast<T*>(handle->GetPointerFromInternalField(0));
32 v8::Persistent<v8::Object> handle_; // ro
35 inline void Wrap (v8::Handle<v8::Object> handle)
37 assert(handle_.IsEmpty());
38 assert(handle->InternalFieldCount() > 0);
39 handle_ = v8::Persistent<v8::Object>::New(handle);
40 handle_->SetPointerInInternalField(0, this);
44 inline void MakeWeak (void)
46 handle_.MakeWeak(this, WeakCallback);
49 /* Ref() marks the object as being attached to an event loop.
50 * Refed objects will not be garbage collected, even if
51 * all references are lost.
54 assert(!handle_.IsEmpty());
59 /* Unref() marks an object as detached from the event loop. This is its
60 * default state. When an object with a "weak" reference changes from
61 * attached to detached state it will be freed. Be careful not to access
62 * the object after making this call as it might be gone!
63 * (A "weak reference" means an object that only has a
66 * DO NOT CALL THIS FROM DESTRUCTOR
68 virtual void Unref() {
69 assert(!handle_.IsEmpty());
70 assert(!handle_.IsWeak());
72 if (--refs_ == 0) { MakeWeak(); }
78 static void WeakCallback (v8::Persistent<v8::Value> value, void *data)
80 ObjectWrap *obj = static_cast<ObjectWrap*>(data);
81 assert(value == obj->handle_);
83 if (value.IsNearDeath()) delete obj;
88 #endif // object_wrap_h