• 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   Close();
88 }
89 
90 
MarkAsInitialized()91 void HandleWrap::MarkAsInitialized() {
92   env()->handle_wrap_queue()->PushBack(this);
93   state_ = kInitialized;
94 }
95 
96 
MarkAsUninitialized()97 void HandleWrap::MarkAsUninitialized() {
98   handle_wrap_queue_.Remove();
99   state_ = kClosed;
100 }
101 
102 
HandleWrap(Environment * env,Local<Object> object,uv_handle_t * handle,AsyncWrap::ProviderType provider)103 HandleWrap::HandleWrap(Environment* env,
104                        Local<Object> object,
105                        uv_handle_t* handle,
106                        AsyncWrap::ProviderType provider)
107     : AsyncWrap(env, object, provider),
108       state_(kInitialized),
109       handle_(handle) {
110   handle_->data = this;
111   HandleScope scope(env->isolate());
112   CHECK(env->has_run_bootstrapping_code());
113   env->handle_wrap_queue()->PushBack(this);
114 }
115 
116 
OnClose(uv_handle_t * handle)117 void HandleWrap::OnClose(uv_handle_t* handle) {
118   CHECK_NOT_NULL(handle->data);
119   BaseObjectPtr<HandleWrap> wrap { static_cast<HandleWrap*>(handle->data) };
120   wrap->Detach();
121 
122   Environment* env = wrap->env();
123   HandleScope scope(env->isolate());
124   Context::Scope context_scope(env->context());
125 
126   CHECK_EQ(wrap->state_, kClosing);
127 
128   wrap->state_ = kClosed;
129 
130   wrap->OnClose();
131   wrap->handle_wrap_queue_.Remove();
132 
133   if (!wrap->persistent().IsEmpty() &&
134       wrap->object()->Has(env->context(), env->handle_onclose_symbol())
135       .FromMaybe(false)) {
136     wrap->MakeCallback(env->handle_onclose_symbol(), 0, nullptr);
137   }
138 }
139 
GetConstructorTemplate(Environment * env)140 Local<FunctionTemplate> HandleWrap::GetConstructorTemplate(Environment* env) {
141   Local<FunctionTemplate> tmpl = env->handle_wrap_ctor_template();
142   if (tmpl.IsEmpty()) {
143     tmpl = env->NewFunctionTemplate(nullptr);
144     tmpl->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "HandleWrap"));
145     tmpl->Inherit(AsyncWrap::GetConstructorTemplate(env));
146     env->SetProtoMethod(tmpl, "close", HandleWrap::Close);
147     env->SetProtoMethodNoSideEffect(tmpl, "hasRef", HandleWrap::HasRef);
148     env->SetProtoMethod(tmpl, "ref", HandleWrap::Ref);
149     env->SetProtoMethod(tmpl, "unref", HandleWrap::Unref);
150     env->set_handle_wrap_ctor_template(tmpl);
151   }
152   return tmpl;
153 }
154 
155 
156 }  // namespace node
157