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: 2020-12-12 04:11:07 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
22821
void HandleWrap::Ref(const FunctionCallbackInfo<Value>& args) {
40
  HandleWrap* wrap;
41
22821
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
42
43
22820
  if (IsAlive(wrap))
44
22819
    uv_ref(wrap->GetHandle());
45
}
46
47
48
56534
void HandleWrap::Unref(const FunctionCallbackInfo<Value>& args) {
49
  HandleWrap* wrap;
50
56534
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
51
52
56312
  if (IsAlive(wrap))
53
56310
    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
28222
void HandleWrap::Close(const FunctionCallbackInfo<Value>& args) {
65
  HandleWrap* wrap;
66
28222
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
67
68
56442
  wrap->Close(args[0]);
69
}
70
71
52535
void HandleWrap::Close(Local<Value> close_callback) {
72
52535
  if (state_ != kInitialized)
73
484
    return;
74
75
52051
  uv_close(handle_, OnClose);
76
52050
  state_ = kClosing;
77
78


93159
  if (!close_callback.IsEmpty() && close_callback->IsFunction() &&
79
26740
      !persistent().IsEmpty()) {
80
40110
    object()->Set(env()->context(),
81
                  env()->handle_onclose_symbol(),
82
53480
                  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
1702
void HandleWrap::MarkAsInitialized() {
100
1702
  env()->handle_wrap_queue()->PushBack(this);
101
1702
  state_ = kInitialized;
102
1702
}
103
104
105
1736
void HandleWrap::MarkAsUninitialized() {
106
1736
  handle_wrap_queue_.Remove();
107
1736
  state_ = kClosed;
108
1736
}
109
110
111
52400
HandleWrap::HandleWrap(Environment* env,
112
                       Local<Object> object,
113
                       uv_handle_t* handle,
114
52400
                       AsyncWrap::ProviderType provider)
115
    : AsyncWrap(env, object, provider),
116
      state_(kInitialized),
117
52400
      handle_(handle) {
118
52400
  handle_->data = this;
119
104800
  HandleScope scope(env->isolate());
120
52400
  CHECK(env->has_run_bootstrapping_code());
121
52400
  env->handle_wrap_queue()->PushBack(this);
122
52400
}
123
124
125
51997
void HandleWrap::OnClose(uv_handle_t* handle) {
126
51997
  CHECK_NOT_NULL(handle->data);
127
103992
  BaseObjectPtr<HandleWrap> wrap { static_cast<HandleWrap*>(handle->data) };
128
51996
  wrap->Detach();
129
130
51996
  Environment* env = wrap->env();
131
103989
  HandleScope scope(env->isolate());
132
51996
  Context::Scope context_scope(env->context());
133
134
51997
  CHECK_EQ(wrap->state_, kClosing);
135
136
51996
  wrap->state_ = kClosed;
137
138
51996
  wrap->OnClose();
139
51998
  wrap->handle_wrap_queue_.Remove();
140
141

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

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