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 |
|
|
#include "node.h" |
27 |
|
|
|
28 |
|
|
namespace node { |
29 |
|
|
|
30 |
|
|
using v8::Context; |
31 |
|
|
using v8::FunctionCallbackInfo; |
32 |
|
|
using v8::FunctionTemplate; |
33 |
|
|
using v8::HandleScope; |
34 |
|
|
using v8::Local; |
35 |
|
|
using v8::Object; |
36 |
|
|
using v8::Value; |
37 |
|
|
|
38 |
|
|
|
39 |
|
21273 |
void HandleWrap::Ref(const FunctionCallbackInfo<Value>& args) { |
40 |
|
|
HandleWrap* wrap; |
41 |
✓✓ |
42546 |
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder()); |
42 |
|
|
|
43 |
✓✓ |
21272 |
if (IsAlive(wrap)) |
44 |
|
21271 |
uv_ref(wrap->GetHandle()); |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
|
48 |
|
53529 |
void HandleWrap::Unref(const FunctionCallbackInfo<Value>& args) { |
49 |
|
|
HandleWrap* wrap; |
50 |
✓✓ |
107058 |
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder()); |
51 |
|
|
|
52 |
✓✓ |
53429 |
if (IsAlive(wrap)) |
53 |
|
53427 |
uv_unref(wrap->GetHandle()); |
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
|
57 |
|
30 |
void HandleWrap::HasRef(const FunctionCallbackInfo<Value>& args) { |
58 |
|
|
HandleWrap* wrap; |
59 |
✓✓ |
60 |
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder()); |
60 |
|
84 |
args.GetReturnValue().Set(HasRef(wrap)); |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
|
64 |
|
56935 |
void HandleWrap::Close(const FunctionCallbackInfo<Value>& args) { |
65 |
|
|
HandleWrap* wrap; |
66 |
✓✓ |
113870 |
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder()); |
67 |
|
|
|
68 |
|
113868 |
wrap->Close(args[0]); |
69 |
|
|
} |
70 |
|
|
|
71 |
|
80519 |
void HandleWrap::Close(Local<Value> close_callback) { |
72 |
✓✓ |
80519 |
if (state_ != kInitialized) |
73 |
|
81622 |
return; |
74 |
|
|
|
75 |
|
79416 |
uv_close(handle_, OnClose); |
76 |
|
79416 |
state_ = kClosing; |
77 |
|
|
|
78 |
✓✓✓✓ ✓✗✓✓
|
176478 |
if (!close_callback.IsEmpty() && close_callback->IsFunction() && |
79 |
|
82058 |
!persistent().IsEmpty()) { |
80 |
|
41029 |
object()->Set(env()->context(), |
81 |
|
|
env()->handle_onclose_symbol(), |
82 |
|
205145 |
close_callback).Check(); |
83 |
|
|
} |
84 |
|
|
} |
85 |
|
|
|
86 |
|
|
|
87 |
|
2 |
void HandleWrap::MakeWeak() { |
88 |
|
2 |
persistent().SetWeak( |
89 |
|
|
this, |
90 |
|
6 |
[](const v8::WeakCallbackInfo<HandleWrap>& data) { |
91 |
|
2 |
HandleWrap* handle_wrap = data.GetParameter(); |
92 |
|
2 |
handle_wrap->persistent().Reset(); |
93 |
|
4 |
handle_wrap->Close(); |
94 |
|
12 |
}, v8::WeakCallbackType::kParameter); |
95 |
|
2 |
} |
96 |
|
|
|
97 |
|
|
|
98 |
|
2212 |
void HandleWrap::MarkAsInitialized() { |
99 |
|
2212 |
env()->handle_wrap_queue()->PushBack(this); |
100 |
|
2212 |
state_ = kInitialized; |
101 |
|
2212 |
} |
102 |
|
|
|
103 |
|
|
|
104 |
|
2242 |
void HandleWrap::MarkAsUninitialized() { |
105 |
|
2242 |
handle_wrap_queue_.Remove(); |
106 |
|
2242 |
state_ = kClosed; |
107 |
|
2242 |
} |
108 |
|
|
|
109 |
|
|
|
110 |
|
80067 |
HandleWrap::HandleWrap(Environment* env, |
111 |
|
|
Local<Object> object, |
112 |
|
|
uv_handle_t* handle, |
113 |
|
|
AsyncWrap::ProviderType provider) |
114 |
|
|
: AsyncWrap(env, object, provider), |
115 |
|
|
state_(kInitialized), |
116 |
|
80067 |
handle_(handle) { |
117 |
|
80066 |
handle_->data = this; |
118 |
|
80066 |
HandleScope scope(env->isolate()); |
119 |
|
80067 |
env->handle_wrap_queue()->PushBack(this); |
120 |
|
80067 |
} |
121 |
|
|
|
122 |
|
|
|
123 |
|
79283 |
void HandleWrap::OnClose(uv_handle_t* handle) { |
124 |
|
79283 |
std::unique_ptr<HandleWrap> wrap { static_cast<HandleWrap*>(handle->data) }; |
125 |
|
79284 |
Environment* env = wrap->env(); |
126 |
|
158564 |
HandleScope scope(env->isolate()); |
127 |
|
79284 |
Context::Scope context_scope(env->context()); |
128 |
|
|
|
129 |
✗✓ |
79284 |
CHECK_EQ(wrap->state_, kClosing); |
130 |
|
|
|
131 |
|
79284 |
wrap->state_ = kClosed; |
132 |
|
|
|
133 |
|
79283 |
wrap->OnClose(); |
134 |
|
|
|
135 |
✓✓✓✓ ✓✓ |
475700 |
if (!wrap->persistent().IsEmpty() && |
136 |
✓✓✓✓
|
317130 |
wrap->object()->Has(env->context(), env->handle_onclose_symbol()) |
137 |
✓✓✓✓
|
634260 |
.FromMaybe(false)) { |
138 |
|
72075 |
wrap->MakeCallback(env->handle_onclose_symbol(), 0, nullptr); |
139 |
|
79280 |
} |
140 |
|
79280 |
} |
141 |
|
|
|
142 |
|
22197 |
Local<FunctionTemplate> HandleWrap::GetConstructorTemplate(Environment* env) { |
143 |
|
22197 |
Local<FunctionTemplate> tmpl = env->handle_wrap_ctor_template(); |
144 |
✓✓ |
22197 |
if (tmpl.IsEmpty()) { |
145 |
|
5100 |
tmpl = env->NewFunctionTemplate(nullptr); |
146 |
|
10200 |
tmpl->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "HandleWrap")); |
147 |
|
10200 |
tmpl->Inherit(AsyncWrap::GetConstructorTemplate(env)); |
148 |
|
5100 |
env->SetProtoMethod(tmpl, "close", HandleWrap::Close); |
149 |
|
5100 |
env->SetProtoMethodNoSideEffect(tmpl, "hasRef", HandleWrap::HasRef); |
150 |
|
5100 |
env->SetProtoMethod(tmpl, "ref", HandleWrap::Ref); |
151 |
|
5100 |
env->SetProtoMethod(tmpl, "unref", HandleWrap::Unref); |
152 |
|
5100 |
env->set_handle_wrap_ctor_template(tmpl); |
153 |
|
|
} |
154 |
|
22197 |
return tmpl; |
155 |
|
|
} |
156 |
|
|
|
157 |
|
|
|
158 |
|
|
} // namespace node |