src: deduplicate CHECK_EQ/CHECK_NE macros
[platform/upstream/nodejs.git] / src / handle_wrap.cc
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 #include "handle_wrap.h"
23 #include "async-wrap.h"
24 #include "async-wrap-inl.h"
25 #include "env.h"
26 #include "env-inl.h"
27 #include "util.h"
28 #include "util-inl.h"
29 #include "node.h"
30 #include "queue.h"
31
32 namespace node {
33
34 using v8::Context;
35 using v8::FunctionCallbackInfo;
36 using v8::Handle;
37 using v8::HandleScope;
38 using v8::Local;
39 using v8::Object;
40 using v8::Value;
41
42 // defined in node.cc
43 extern QUEUE handle_wrap_queue;
44
45
46 void HandleWrap::Ref(const FunctionCallbackInfo<Value>& args) {
47   Environment* env = Environment::GetCurrent(args.GetIsolate());
48   HandleScope scope(env->isolate());
49
50   HandleWrap* wrap = Unwrap<HandleWrap>(args.This());
51
52   if (wrap != NULL && wrap->handle__ != NULL) {
53     uv_ref(wrap->handle__);
54     wrap->flags_ &= ~kUnref;
55   }
56 }
57
58
59 void HandleWrap::Unref(const FunctionCallbackInfo<Value>& args) {
60   Environment* env = Environment::GetCurrent(args.GetIsolate());
61   HandleScope scope(env->isolate());
62
63   HandleWrap* wrap = Unwrap<HandleWrap>(args.This());
64
65   if (wrap != NULL && wrap->handle__ != NULL) {
66     uv_unref(wrap->handle__);
67     wrap->flags_ |= kUnref;
68   }
69 }
70
71
72 void HandleWrap::Close(const FunctionCallbackInfo<Value>& args) {
73   Environment* env = Environment::GetCurrent(args.GetIsolate());
74   HandleScope scope(env->isolate());
75
76   HandleWrap* wrap = Unwrap<HandleWrap>(args.This());
77
78   // guard against uninitialized handle or double close
79   if (wrap == NULL || wrap->handle__ == NULL)
80     return;
81
82   assert(!wrap->persistent().IsEmpty());
83   uv_close(wrap->handle__, OnClose);
84   wrap->handle__ = NULL;
85
86   if (args[0]->IsFunction()) {
87     wrap->object()->Set(env->close_string(), args[0]);
88     wrap->flags_ |= kCloseCallback;
89   }
90 }
91
92
93 HandleWrap::HandleWrap(Environment* env,
94                        Handle<Object> object,
95                        uv_handle_t* handle,
96                        AsyncWrap::ProviderType provider)
97     : AsyncWrap(env, object, provider),
98       flags_(0),
99       handle__(handle) {
100   handle__->data = this;
101   HandleScope scope(env->isolate());
102   Wrap<HandleWrap>(object, this);
103   QUEUE_INSERT_TAIL(&handle_wrap_queue, &handle_wrap_queue_);
104 }
105
106
107 HandleWrap::~HandleWrap() {
108   assert(persistent().IsEmpty());
109   QUEUE_REMOVE(&handle_wrap_queue_);
110 }
111
112
113 void HandleWrap::OnClose(uv_handle_t* handle) {
114   HandleWrap* wrap = static_cast<HandleWrap*>(handle->data);
115   Environment* env = wrap->env();
116   HandleScope scope(env->isolate());
117
118   // The wrap object should still be there.
119   assert(wrap->persistent().IsEmpty() == false);
120
121   // But the handle pointer should be gone.
122   assert(wrap->handle__ == NULL);
123
124   HandleScope handle_scope(env->isolate());
125   Context::Scope context_scope(env->context());
126   Local<Object> object = wrap->object();
127
128   if (wrap->flags_ & kCloseCallback) {
129     wrap->MakeCallback(env->close_string(), 0, NULL);
130   }
131
132   object->SetAlignedPointerInInternalField(0, NULL);
133   wrap->persistent().Reset();
134   delete wrap;
135 }
136
137
138 }  // namespace node