GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/handle_wrap.cc Lines: 77 81 95.1 %
Date: 2021-06-01 04:11:54 Branches: 34 46 73.9 %

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
22995
void HandleWrap::Ref(const FunctionCallbackInfo<Value>& args) {
40
  HandleWrap* wrap;
41
22995
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
42
43
22994
  if (IsAlive(wrap))
44
22993
    uv_ref(wrap->GetHandle());
45
}
46
47
48
58130
void HandleWrap::Unref(const FunctionCallbackInfo<Value>& args) {
49
  HandleWrap* wrap;
50
58130
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
51
52
57143
  if (IsAlive(wrap))
53
57141
    uv_unref(wrap->GetHandle());
54
}
55
56
57
35
void HandleWrap::HasRef(const FunctionCallbackInfo<Value>& args) {
58
  HandleWrap* wrap;
59
35
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
60
99
  args.GetReturnValue().Set(HasRef(wrap));
61
}
62
63
64
29204
void HandleWrap::Close(const FunctionCallbackInfo<Value>& args) {
65
  HandleWrap* wrap;
66
29204
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
67
68
58406
  wrap->Close(args[0]);
69
}
70
71
53651
void HandleWrap::Close(Local<Value> close_callback) {
72
53651
  if (state_ != kInitialized)
73
496
    return;
74
75
53155
  uv_close(handle_, OnClose);
76
53154
  state_ = kClosing;
77
78


95988
  if (!close_callback.IsEmpty() && close_callback->IsFunction() &&
79
28250
      !persistent().IsEmpty()) {
80
42375
    object()->Set(env()->context(),
81
                  env()->handle_onclose_symbol(),
82
56500
                  close_callback).Check();
83
  }
84
}
85
86
87
2
void HandleWrap::OnGCCollect() {
88
4
  Close();
89
2
}
90
91
92
bool HandleWrap::IsNotIndicativeOfMemoryLeakAtExit() const {
93
  return IsWeakOrDetached() ||
94
         !HandleWrap::HasRef(this) ||
95
         !uv_is_active(GetHandle());
96
}
97
98
99
1808
void HandleWrap::MarkAsInitialized() {
100
1808
  env()->handle_wrap_queue()->PushBack(this);
101
1808
  state_ = kInitialized;
102
1808
}
103
104
105
1830
void HandleWrap::MarkAsUninitialized() {
106
1830
  handle_wrap_queue_.Remove();
107
1830
  state_ = kClosed;
108
1830
}
109
110
111
53693
HandleWrap::HandleWrap(Environment* env,
112
                       Local<Object> object,
113
                       uv_handle_t* handle,
114
53693
                       AsyncWrap::ProviderType provider)
115
    : AsyncWrap(env, object, provider),
116
      state_(kInitialized),
117
53693
      handle_(handle) {
118
53693
  handle_->data = this;
119
107386
  HandleScope scope(env->isolate());
120
53693
  CHECK(env->has_run_bootstrapping_code());
121
53693
  env->handle_wrap_queue()->PushBack(this);
122
53693
}
123
124
125
52958
void HandleWrap::OnClose(uv_handle_t* handle) {
126
52958
  CHECK_NOT_NULL(handle->data);
127
105912
  BaseObjectPtr<HandleWrap> wrap { static_cast<HandleWrap*>(handle->data) };
128
52958
  wrap->Detach();
129
130
52954
  Environment* env = wrap->env();
131
105907
  HandleScope scope(env->isolate());
132
52954
  Context::Scope context_scope(env->context());
133
134
52961
  CHECK_EQ(wrap->state_, kClosing);
135
136
52961
  wrap->state_ = kClosed;
137
138
52961
  wrap->OnClose();
139
52964
  wrap->handle_wrap_queue_.Remove();
140
141

211850
  if (!wrap->persistent().IsEmpty() &&
142
211843
      wrap->object()->Has(env->context(), env->handle_onclose_symbol())
143
158886
      .FromMaybe(false)) {
144
47040
    wrap->MakeCallback(env->handle_onclose_symbol(), 0, nullptr);
145
  }
146
52960
}
147
148
19122
Local<FunctionTemplate> HandleWrap::GetConstructorTemplate(Environment* env) {
149
19122
  Local<FunctionTemplate> tmpl = env->handle_wrap_ctor_template();
150
19122
  if (tmpl.IsEmpty()) {
151
464
    tmpl = env->NewFunctionTemplate(nullptr);
152
928
    tmpl->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "HandleWrap"));
153
928
    tmpl->Inherit(AsyncWrap::GetConstructorTemplate(env));
154
464
    env->SetProtoMethod(tmpl, "close", HandleWrap::Close);
155
464
    env->SetProtoMethodNoSideEffect(tmpl, "hasRef", HandleWrap::HasRef);
156
464
    env->SetProtoMethod(tmpl, "ref", HandleWrap::Ref);
157
464
    env->SetProtoMethod(tmpl, "unref", HandleWrap::Unref);
158
464
    env->set_handle_wrap_ctor_template(tmpl);
159
  }
160
19122
  return tmpl;
161
}
162
163
4774
void HandleWrap::RegisterExternalReferences(
164
    ExternalReferenceRegistry* registry) {
165
4774
  registry->Register(HandleWrap::Close);
166
4774
  registry->Register(HandleWrap::HasRef);
167
4774
  registry->Register(HandleWrap::Ref);
168
4774
  registry->Register(HandleWrap::Unref);
169
4774
}
170
171
}  // namespace node
172
173

19294
NODE_MODULE_EXTERNAL_REFERENCE(handle_wrap,
174
                               node::HandleWrap::RegisterExternalReferences)