• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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-inl.h"
24 #include "env-inl.h"
25 #include "util-inl.h"
26 
27 namespace node {
28 
29 using v8::Context;
30 using v8::FunctionCallbackInfo;
31 using v8::FunctionTemplate;
32 using v8::HandleScope;
33 using v8::Local;
34 using v8::Object;
35 using v8::Value;
36 
37 
Ref(const FunctionCallbackInfo<Value> & args)38 void HandleWrap::Ref(const FunctionCallbackInfo<Value>& args) {
39   HandleWrap* wrap;
40   ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
41 
42   if (IsAlive(wrap))
43     uv_ref(wrap->GetHandle());
44 }
45 
46 
Unref(const FunctionCallbackInfo<Value> & args)47 void HandleWrap::Unref(const FunctionCallbackInfo<Value>& args) {
48   HandleWrap* wrap;
49   ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
50 
51   if (IsAlive(wrap))
52     uv_unref(wrap->GetHandle());
53 }
54 
55 
HasRef(const FunctionCallbackInfo<Value> & args)56 void HandleWrap::HasRef(const FunctionCallbackInfo<Value>& args) {
57   HandleWrap* wrap;
58   ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
59   args.GetReturnValue().Set(HasRef(wrap));
60 }
61 
62 
Close(const FunctionCallbackInfo<Value> & args)63 void HandleWrap::Close(const FunctionCallbackInfo<Value>& args) {
64   HandleWrap* wrap;
65   ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
66 
67   wrap->Close(args[0]);
68 }
69 
Close(Local<Value> close_callback)70 void HandleWrap::Close(Local<Value> close_callback) {
71   if (state_ != kInitialized)
72     return;
73 
74   uv_close(handle_, OnClose);
75   state_ = kClosing;
76 
77   if (!close_callback.IsEmpty() && close_callback->IsFunction() &&
78       !persistent().IsEmpty()) {
79     object()->Set(env()->context(),
80                   env()->handle_onclose_symbol(),
81                   close_callback).Check();
82   }
83 }
84 
85 
OnGCCollect()86 void HandleWrap::OnGCCollect() {
87   // When all references to a HandleWrap are lost and the object is supposed to
88   // be destroyed, we first call Close() to clean up the underlying libuv
89   // handle. The OnClose callback then acquires and destroys another reference
90   // to that object, and when that reference is lost, we perform the default
91   // action (i.e. destroying `this`).
92   if (state_ != kClosed) {
93     Close();
94   } else {
95     BaseObject::OnGCCollect();
96   }
97 }
98 
99 
IsNotIndicativeOfMemoryLeakAtExit() const100 bool HandleWrap::IsNotIndicativeOfMemoryLeakAtExit() const {
101   return IsWeakOrDetached() ||
102          !HandleWrap::HasRef(this) ||
103          !uv_is_active(GetHandle());
104 }
105 
106 
MarkAsInitialized()107 void HandleWrap::MarkAsInitialized() {
108   env()->handle_wrap_queue()->PushBack(this);
109   state_ = kInitialized;
110 }
111 
112 
MarkAsUninitialized()113 void HandleWrap::MarkAsUninitialized() {
114   handle_wrap_queue_.Remove();
115   state_ = kClosed;
116 }
117 
118 
HandleWrap(Environment * env,Local<Object> object,uv_handle_t * handle,AsyncWrap::ProviderType provider)119 HandleWrap::HandleWrap(Environment* env,
120                        Local<Object> object,
121                        uv_handle_t* handle,
122                        AsyncWrap::ProviderType provider)
123     : AsyncWrap(env, object, provider),
124       state_(kInitialized),
125       handle_(handle) {
126   handle_->data = this;
127   HandleScope scope(env->isolate());
128   CHECK(env->has_run_bootstrapping_code());
129   env->handle_wrap_queue()->PushBack(this);
130 }
131 
132 
OnClose(uv_handle_t * handle)133 void HandleWrap::OnClose(uv_handle_t* handle) {
134   CHECK_NOT_NULL(handle->data);
135   BaseObjectPtr<HandleWrap> wrap { static_cast<HandleWrap*>(handle->data) };
136   wrap->Detach();
137 
138   Environment* env = wrap->env();
139   HandleScope scope(env->isolate());
140   Context::Scope context_scope(env->context());
141 
142   CHECK_EQ(wrap->state_, kClosing);
143 
144   wrap->state_ = kClosed;
145 
146   wrap->OnClose();
147   wrap->handle_wrap_queue_.Remove();
148 
149   if (!wrap->persistent().IsEmpty() &&
150       wrap->object()->Has(env->context(), env->handle_onclose_symbol())
151       .FromMaybe(false)) {
152     wrap->MakeCallback(env->handle_onclose_symbol(), 0, nullptr);
153   }
154 }
155 
GetConstructorTemplate(Environment * env)156 Local<FunctionTemplate> HandleWrap::GetConstructorTemplate(Environment* env) {
157   Local<FunctionTemplate> tmpl = env->handle_wrap_ctor_template();
158   if (tmpl.IsEmpty()) {
159     tmpl = env->NewFunctionTemplate(nullptr);
160     tmpl->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "HandleWrap"));
161     tmpl->Inherit(AsyncWrap::GetConstructorTemplate(env));
162     env->SetProtoMethod(tmpl, "close", HandleWrap::Close);
163     env->SetProtoMethodNoSideEffect(tmpl, "hasRef", HandleWrap::HasRef);
164     env->SetProtoMethod(tmpl, "ref", HandleWrap::Ref);
165     env->SetProtoMethod(tmpl, "unref", HandleWrap::Unref);
166     env->set_handle_wrap_ctor_template(tmpl);
167   }
168   return tmpl;
169 }
170 
171 
172 }  // namespace node
173