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 #include "handle_wrap.h"
23 #include "async-wrap.h"
24 #include "async-wrap-inl.h"
35 using v8::FunctionCallbackInfo;
37 using v8::HandleScope;
43 extern QUEUE handle_wrap_queue;
46 void HandleWrap::Ref(const FunctionCallbackInfo<Value>& args) {
47 Environment* env = Environment::GetCurrent(args.GetIsolate());
48 HandleScope scope(env->isolate());
50 HandleWrap* wrap = Unwrap<HandleWrap>(args.This());
52 if (wrap != NULL && wrap->handle__ != NULL) {
53 uv_ref(wrap->handle__);
54 wrap->flags_ &= ~kUnref;
59 void HandleWrap::Unref(const FunctionCallbackInfo<Value>& args) {
60 Environment* env = Environment::GetCurrent(args.GetIsolate());
61 HandleScope scope(env->isolate());
63 HandleWrap* wrap = Unwrap<HandleWrap>(args.This());
65 if (wrap != NULL && wrap->handle__ != NULL) {
66 uv_unref(wrap->handle__);
67 wrap->flags_ |= kUnref;
72 void HandleWrap::Close(const FunctionCallbackInfo<Value>& args) {
73 Environment* env = Environment::GetCurrent(args.GetIsolate());
74 HandleScope scope(env->isolate());
76 HandleWrap* wrap = Unwrap<HandleWrap>(args.This());
78 // guard against uninitialized handle or double close
79 if (wrap == NULL || wrap->handle__ == NULL)
82 assert(!wrap->persistent().IsEmpty());
83 uv_close(wrap->handle__, OnClose);
84 wrap->handle__ = NULL;
86 if (args[0]->IsFunction()) {
87 wrap->object()->Set(env->close_string(), args[0]);
88 wrap->flags_ |= kCloseCallback;
93 HandleWrap::HandleWrap(Environment* env,
94 Handle<Object> object,
96 AsyncWrap::ProviderType provider)
97 : AsyncWrap(env, object, provider),
100 handle__->data = this;
101 HandleScope scope(env->isolate());
102 Wrap<HandleWrap>(object, this);
103 QUEUE_INSERT_TAIL(&handle_wrap_queue, &handle_wrap_queue_);
107 HandleWrap::~HandleWrap() {
108 assert(persistent().IsEmpty());
109 QUEUE_REMOVE(&handle_wrap_queue_);
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());
118 // The wrap object should still be there.
119 assert(wrap->persistent().IsEmpty() == false);
121 // But the handle pointer should be gone.
122 assert(wrap->handle__ == NULL);
124 HandleScope handle_scope(env->isolate());
125 Context::Scope context_scope(env->context());
126 Local<Object> object = wrap->object();
128 if (wrap->flags_ & kCloseCallback) {
129 wrap->MakeCallback(env->close_string(), 0, NULL);
132 object->SetAlignedPointerInInternalField(0, NULL);
133 wrap->persistent().Reset();