Merge branch 'v0.10'
[platform/upstream/nodejs.git] / src / node_object_wrap.h
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
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:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
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.
21
22 #ifndef object_wrap_h
23 #define object_wrap_h
24
25 #include "node.h"
26 #include "v8.h"
27 #include <assert.h>
28
29 // Explicitly instantiate some template classes, so we're sure they will be
30 // present in the binary / shared object. There isn't much doubt that they will
31 // be, but MSVC tends to complain about these things.
32 #ifdef _MSC_VER
33   template class NODE_EXTERN v8::Persistent<v8::Object>;
34   template class NODE_EXTERN v8::Persistent<v8::FunctionTemplate>;
35 #endif
36
37
38 namespace node {
39
40 extern v8::Isolate* node_isolate;
41
42 class NODE_EXTERN ObjectWrap {
43  public:
44   ObjectWrap ( ) {
45     refs_ = 0;
46   }
47
48
49   virtual ~ObjectWrap ( ) {
50     if (!handle_.IsEmpty()) {
51       assert(handle_.IsNearDeath(node_isolate));
52       handle_.ClearWeak(node_isolate);
53       handle_->SetAlignedPointerInInternalField(0, 0);
54       handle_.Dispose(node_isolate);
55       handle_.Clear();
56     }
57   }
58
59
60   template <class T>
61   static inline T* Unwrap (v8::Handle<v8::Object> handle) {
62     assert(!handle.IsEmpty());
63     assert(handle->InternalFieldCount() > 0);
64     return static_cast<T*>(handle->GetAlignedPointerFromInternalField(0));
65   }
66
67
68   v8::Persistent<v8::Object> handle_; // ro
69
70  protected:
71   inline void Wrap (v8::Handle<v8::Object> handle) {
72     assert(handle_.IsEmpty());
73     assert(handle->InternalFieldCount() > 0);
74     handle_ = v8::Persistent<v8::Object>::New(node_isolate, handle);
75     handle_->SetAlignedPointerInInternalField(0, this);
76     MakeWeak();
77   }
78
79
80   inline void MakeWeak (void) {
81     handle_.MakeWeak(node_isolate, this, WeakCallback);
82     handle_.MarkIndependent(node_isolate);
83   }
84
85   /* Ref() marks the object as being attached to an event loop.
86    * Refed objects will not be garbage collected, even if
87    * all references are lost.
88    */
89   virtual void Ref() {
90     assert(!handle_.IsEmpty());
91     refs_++;
92     handle_.ClearWeak(node_isolate);
93   }
94
95   /* Unref() marks an object as detached from the event loop.  This is its
96    * default state.  When an object with a "weak" reference changes from
97    * attached to detached state it will be freed. Be careful not to access
98    * the object after making this call as it might be gone!
99    * (A "weak reference" means an object that only has a
100    * persistant handle.)
101    *
102    * DO NOT CALL THIS FROM DESTRUCTOR
103    */
104   virtual void Unref() {
105     assert(!handle_.IsEmpty());
106     assert(!handle_.IsWeak(node_isolate));
107     assert(refs_ > 0);
108     if (--refs_ == 0) { MakeWeak(); }
109   }
110
111
112   int refs_; // ro
113
114
115  private:
116   static void WeakCallback(v8::Isolate* env,
117                            v8::Persistent<v8::Value> value,
118                            void* data) {
119     v8::HandleScope scope(node_isolate);
120
121     ObjectWrap *obj = static_cast<ObjectWrap*>(data);
122     assert(value == obj->handle_);
123     assert(!obj->refs_);
124     assert(value.IsNearDeath(env));
125     delete obj;
126   }
127 };
128
129 } // namespace node
130 #endif // object_wrap_h