1 // Copyright Joyent, Inc. and other Node contributors.
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
22 #ifndef SRC_NODE_OBJECT_WRAP_H_
23 #define SRC_NODE_OBJECT_WRAP_H_
38 virtual ~ObjectWrap() {
39 if (persistent().IsEmpty())
41 assert(persistent().IsNearDeath());
42 persistent().ClearWeak();
48 static inline T* Unwrap(v8::Handle<v8::Object> handle) {
49 assert(!handle.IsEmpty());
50 assert(handle->InternalFieldCount() > 0);
51 // Cast to ObjectWrap before casting to T. A direct cast from void
52 // to T won't work right when T has more than one base class.
53 void* ptr = handle->GetAlignedPointerFromInternalField(0);
54 ObjectWrap* wrap = static_cast<ObjectWrap*>(ptr);
55 return static_cast<T*>(wrap);
59 inline v8::Local<v8::Object> handle() {
60 return handle(v8::Isolate::GetCurrent());
64 inline v8::Local<v8::Object> handle(v8::Isolate* isolate) {
65 return v8::Local<v8::Object>::New(isolate, persistent());
69 inline v8::Persistent<v8::Object>& persistent() {
75 inline void Wrap(v8::Handle<v8::Object> handle) {
76 assert(persistent().IsEmpty());
77 assert(handle->InternalFieldCount() > 0);
78 handle->SetAlignedPointerInInternalField(0, this);
79 persistent().Reset(v8::Isolate::GetCurrent(), handle);
84 inline void MakeWeak(void) {
85 persistent().SetWeak(this, WeakCallback);
86 persistent().MarkIndependent();
89 /* Ref() marks the object as being attached to an event loop.
90 * Refed objects will not be garbage collected, even if
91 * all references are lost.
94 assert(!persistent().IsEmpty());
95 persistent().ClearWeak();
99 /* Unref() marks an object as detached from the event loop. This is its
100 * default state. When an object with a "weak" reference changes from
101 * attached to detached state it will be freed. Be careful not to access
102 * the object after making this call as it might be gone!
103 * (A "weak reference" means an object that only has a
104 * persistant handle.)
106 * DO NOT CALL THIS FROM DESTRUCTOR
108 virtual void Unref() {
109 assert(!persistent().IsEmpty());
110 assert(!persistent().IsWeak());
119 static void WeakCallback(
120 const v8::WeakCallbackData<v8::Object, ObjectWrap>& data) {
121 v8::Isolate* isolate = data.GetIsolate();
122 v8::HandleScope scope(isolate);
123 ObjectWrap* wrap = data.GetParameter();
124 assert(wrap->refs_ == 0);
125 assert(wrap->handle_.IsNearDeath());
127 data.GetValue() == v8::Local<v8::Object>::New(isolate, wrap->handle_));
128 wrap->handle_.Reset();
132 v8::Persistent<v8::Object> handle_;
137 #endif // SRC_NODE_OBJECT_WRAP_H_