GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: handle_wrap.cc Lines: 80 84 95.2 %
Date: 2022-12-31 04:22:30 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::Isolate;
35
using v8::Local;
36
using v8::Object;
37
using v8::Value;
38
39
40
24878
void HandleWrap::Ref(const FunctionCallbackInfo<Value>& args) {
41
  HandleWrap* wrap;
42
24878
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
43
44
24877
  if (IsAlive(wrap))
45
24876
    uv_ref(wrap->GetHandle());
46
}
47
48
49
64018
void HandleWrap::Unref(const FunctionCallbackInfo<Value>& args) {
50
  HandleWrap* wrap;
51
64018
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
52
53
62096
  if (IsAlive(wrap))
54
62094
    uv_unref(wrap->GetHandle());
55
}
56
57
58
63
void HandleWrap::HasRef(const FunctionCallbackInfo<Value>& args) {
59
  HandleWrap* wrap;
60
63
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
61
120
  args.GetReturnValue().Set(HasRef(wrap));
62
}
63
64
65
29556
void HandleWrap::Close(const FunctionCallbackInfo<Value>& args) {
66
  HandleWrap* wrap;
67
29556
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
68
69
59110
  wrap->Close(args[0]);
70
}
71
72
57450
void HandleWrap::Close(Local<Value> close_callback) {
73
57450
  if (state_ != kInitialized)
74
557
    return;
75
76
56893
  uv_close(handle_, OnClose);
77
56893
  state_ = kClosing;
78
79

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

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