GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: handle_wrap.cc Lines: 79 83 95.2 %
Date: 2022-07-10 04:16:23 Branches: 38 50 76.0 %

Line Branch Exec Source
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 "node_external_reference.h"
26
#include "util-inl.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
27303
void HandleWrap::Ref(const FunctionCallbackInfo<Value>& args) {
40
  HandleWrap* wrap;
41
27303
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
42
43
27302
  if (IsAlive(wrap))
44
27301
    uv_ref(wrap->GetHandle());
45
}
46
47
48
69289
void HandleWrap::Unref(const FunctionCallbackInfo<Value>& args) {
49
  HandleWrap* wrap;
50
69289
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
51
52
65871
  if (IsAlive(wrap))
53
65869
    uv_unref(wrap->GetHandle());
54
}
55
56
57
63
void HandleWrap::HasRef(const FunctionCallbackInfo<Value>& args) {
58
  HandleWrap* wrap;
59
63
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
60
120
  args.GetReturnValue().Set(HasRef(wrap));
61
}
62
63
64
28380
void HandleWrap::Close(const FunctionCallbackInfo<Value>& args) {
65
  HandleWrap* wrap;
66
28380
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
67
68
56758
  wrap->Close(args[0]);
69
}
70
71
58171
void HandleWrap::Close(Local<Value> close_callback) {
72
58171
  if (state_ != kInitialized)
73
507
    return;
74
75
57664
  uv_close(handle_, OnClose);
76
57664
  state_ = kClosing;
77
78

98554
  if (!close_callback.IsEmpty() && close_callback->IsFunction() &&
79
13009
      !persistent().IsEmpty()) {
80
13009
    object()->Set(env()->context(),
81
                  env()->handle_onclose_symbol(),
82
39027
                  close_callback).Check();
83
  }
84
}
85
86
87
57546
void HandleWrap::OnGCCollect() {
88
  // When all references to a HandleWrap are lost and the object is supposed to
89
  // be destroyed, we first call Close() to clean up the underlying libuv
90
  // handle. The OnClose callback then acquires and destroys another reference
91
  // to that object, and when that reference is lost, we perform the default
92
  // action (i.e. destroying `this`).
93
57546
  if (state_ != kClosed) {
94
4
    Close();
95
  } else {
96
57544
    BaseObject::OnGCCollect();
97
  }
98
57546
}
99
100
101
bool HandleWrap::IsNotIndicativeOfMemoryLeakAtExit() const {
102
  return IsWeakOrDetached() ||
103
         !HandleWrap::HasRef(this) ||
104
         !uv_is_active(GetHandle());
105
}
106
107
108
1894
void HandleWrap::MarkAsInitialized() {
109
1894
  env()->handle_wrap_queue()->PushBack(this);
110
1894
  state_ = kInitialized;
111
1894
}
112
113
114
1916
void HandleWrap::MarkAsUninitialized() {
115
1916
  handle_wrap_queue_.Remove();
116
1916
  state_ = kClosed;
117
1916
}
118
119
120
58282
HandleWrap::HandleWrap(Environment* env,
121
                       Local<Object> object,
122
                       uv_handle_t* handle,
123
58282
                       AsyncWrap::ProviderType provider)
124
    : AsyncWrap(env, object, provider),
125
      state_(kInitialized),
126
58282
      handle_(handle) {
127
58282
  handle_->data = this;
128
116564
  HandleScope scope(env->isolate());
129
58282
  CHECK(env->has_run_bootstrapping_code());
130
58282
  env->handle_wrap_queue()->PushBack(this);
131
58282
}
132
133
134
57547
void HandleWrap::OnClose(uv_handle_t* handle) {
135
57547
  CHECK_NOT_NULL(handle->data);
136
115091
  BaseObjectPtr<HandleWrap> wrap { static_cast<HandleWrap*>(handle->data) };
137
57547
  wrap->Detach();
138
139
57547
  Environment* env = wrap->env();
140
115091
  HandleScope scope(env->isolate());
141
57547
  Context::Scope context_scope(env->context());
142
143
57547
  CHECK_EQ(wrap->state_, kClosing);
144
145
57547
  wrap->state_ = kClosed;
146
147
57547
  wrap->OnClose();
148
57547
  wrap->handle_wrap_queue_.Remove();
149
150
115092
  if (!wrap->persistent().IsEmpty() &&
151
172635
      wrap->object()->Has(env->context(), env->handle_onclose_symbol())
152

172637
      .FromMaybe(false)) {
153
50148
    wrap->MakeCallback(env->handle_onclose_symbol(), 0, nullptr);
154
  }
155
57544
}
156
157
20892
Local<FunctionTemplate> HandleWrap::GetConstructorTemplate(Environment* env) {
158
20892
  Local<FunctionTemplate> tmpl = env->handle_wrap_ctor_template();
159
20892
  if (tmpl.IsEmpty()) {
160
1292
    tmpl = env->NewFunctionTemplate(nullptr);
161
1292
    tmpl->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "HandleWrap"));
162
1292
    tmpl->Inherit(AsyncWrap::GetConstructorTemplate(env));
163
1292
    env->SetProtoMethod(tmpl, "close", HandleWrap::Close);
164
1292
    env->SetProtoMethodNoSideEffect(tmpl, "hasRef", HandleWrap::HasRef);
165
1292
    env->SetProtoMethod(tmpl, "ref", HandleWrap::Ref);
166
1292
    env->SetProtoMethod(tmpl, "unref", HandleWrap::Unref);
167
1292
    env->set_handle_wrap_ctor_template(tmpl);
168
  }
169
20892
  return tmpl;
170
}
171
172
5242
void HandleWrap::RegisterExternalReferences(
173
    ExternalReferenceRegistry* registry) {
174
5242
  registry->Register(HandleWrap::Close);
175
5242
  registry->Register(HandleWrap::HasRef);
176
5242
  registry->Register(HandleWrap::Ref);
177
5242
  registry->Register(HandleWrap::Unref);
178
5242
}
179
180
}  // namespace node
181
182
5242
NODE_MODULE_EXTERNAL_REFERENCE(handle_wrap,
183
                               node::HandleWrap::RegisterExternalReferences)