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.
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.
33 template class NODE_EXTERN v8::Persistent<v8::Object>;
34 template class NODE_EXTERN v8::Persistent<v8::FunctionTemplate>;
40 extern v8::Isolate* node_isolate;
42 class NODE_EXTERN ObjectWrap {
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);
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));
68 v8::Persistent<v8::Object> handle_; // ro
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);
80 inline void MakeWeak (void) {
81 handle_.MakeWeak(node_isolate, this, WeakCallback);
82 handle_.MarkIndependent(node_isolate);
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.
90 assert(!handle_.IsEmpty());
92 handle_.ClearWeak(node_isolate);
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.)
102 * DO NOT CALL THIS FROM DESTRUCTOR
104 virtual void Unref() {
105 assert(!handle_.IsEmpty());
106 assert(!handle_.IsWeak(node_isolate));
108 if (--refs_ == 0) { MakeWeak(); }
116 static void WeakCallback(v8::Isolate* env,
117 v8::Persistent<v8::Value> value,
119 v8::HandleScope scope(node_isolate);
121 ObjectWrap *obj = static_cast<ObjectWrap*>(data);
122 assert(value == obj->handle_);
124 assert(value.IsNearDeath(env));
130 #endif // object_wrap_h