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 77 100.0 %
Date: 2020-09-06 22:14:11 Branches: 34 40 85.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
22713
void HandleWrap::Ref(const FunctionCallbackInfo<Value>& args) {
40
  HandleWrap* wrap;
41
22713
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
42
43
22712
  if (IsAlive(wrap))
44
22711
    uv_ref(wrap->GetHandle());
45
}
46
47
48
56464
void HandleWrap::Unref(const FunctionCallbackInfo<Value>& args) {
49
  HandleWrap* wrap;
50
56464
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
51
52
56286
  if (IsAlive(wrap))
53
56284
    uv_unref(wrap->GetHandle());
54
}
55
56
57
33
void HandleWrap::HasRef(const FunctionCallbackInfo<Value>& args) {
58
  HandleWrap* wrap;
59
33
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
60
93
  args.GetReturnValue().Set(HasRef(wrap));
61
}
62
63
64
27049
void HandleWrap::Close(const FunctionCallbackInfo<Value>& args) {
65
  HandleWrap* wrap;
66
27049
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
67
68
54096
  wrap->Close(args[0]);
69
}
70
71
51328
void HandleWrap::Close(Local<Value> close_callback) {
72
51328
  if (state_ != kInitialized)
73
404
    return;
74
75
50924
  uv_close(handle_, OnClose);
76
50924
  state_ = kClosing;
77
78


90176
  if (!close_callback.IsEmpty() && close_callback->IsFunction() &&
79
25212
      !persistent().IsEmpty()) {
80
37818
    object()->Set(env()->context(),
81
                  env()->handle_onclose_symbol(),
82
50424
                  close_callback).Check();
83
  }
84
}
85
86
87
2
void HandleWrap::OnGCCollect() {
88
4
  Close();
89
2
}
90
91
92
1604
void HandleWrap::MarkAsInitialized() {
93
1604
  env()->handle_wrap_queue()->PushBack(this);
94
1604
  state_ = kInitialized;
95
1604
}
96
97
98
1638
void HandleWrap::MarkAsUninitialized() {
99
1638
  handle_wrap_queue_.Remove();
100
1638
  state_ = kClosed;
101
1638
}
102
103
104
51334
HandleWrap::HandleWrap(Environment* env,
105
                       Local<Object> object,
106
                       uv_handle_t* handle,
107
51334
                       AsyncWrap::ProviderType provider)
108
    : AsyncWrap(env, object, provider),
109
      state_(kInitialized),
110
51334
      handle_(handle) {
111
51334
  handle_->data = this;
112
102668
  HandleScope scope(env->isolate());
113
51334
  CHECK(env->has_run_bootstrapping_code());
114
51334
  env->handle_wrap_queue()->PushBack(this);
115
51334
}
116
117
118
50861
void HandleWrap::OnClose(uv_handle_t* handle) {
119
50861
  CHECK_NOT_NULL(handle->data);
120
101722
  BaseObjectPtr<HandleWrap> wrap { static_cast<HandleWrap*>(handle->data) };
121
50863
  wrap->Detach();
122
123
50862
  Environment* env = wrap->env();
124
101723
  HandleScope scope(env->isolate());
125
50860
  Context::Scope context_scope(env->context());
126
127
50860
  CHECK_EQ(wrap->state_, kClosing);
128
129
50861
  wrap->state_ = kClosed;
130
131
50860
  wrap->OnClose();
132
50860
  wrap->handle_wrap_queue_.Remove();
133
134

203438
  if (!wrap->persistent().IsEmpty() &&
135
203439
      wrap->object()->Has(env->context(), env->handle_onclose_symbol())
136
152575
      .FromMaybe(false)) {
137
45381
    wrap->MakeCallback(env->handle_onclose_symbol(), 0, nullptr);
138
  }
139
50861
}
140
141
17492
Local<FunctionTemplate> HandleWrap::GetConstructorTemplate(Environment* env) {
142
17492
  Local<FunctionTemplate> tmpl = env->handle_wrap_ctor_template();
143
17492
  if (tmpl.IsEmpty()) {
144
453
    tmpl = env->NewFunctionTemplate(nullptr);
145
906
    tmpl->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "HandleWrap"));
146
906
    tmpl->Inherit(AsyncWrap::GetConstructorTemplate(env));
147
453
    env->SetProtoMethod(tmpl, "close", HandleWrap::Close);
148
453
    env->SetProtoMethodNoSideEffect(tmpl, "hasRef", HandleWrap::HasRef);
149
453
    env->SetProtoMethod(tmpl, "ref", HandleWrap::Ref);
150
453
    env->SetProtoMethod(tmpl, "unref", HandleWrap::Unref);
151
453
    env->set_handle_wrap_ctor_template(tmpl);
152
  }
153
17492
  return tmpl;
154
}
155
156
4403
void HandleWrap::RegisterExternalReferences(
157
    ExternalReferenceRegistry* registry) {
158
4403
  registry->Register(HandleWrap::Close);
159
4403
  registry->Register(HandleWrap::HasRef);
160
4403
  registry->Register(HandleWrap::Ref);
161
4403
  registry->Register(HandleWrap::Unref);
162
4403
}
163
164
}  // namespace node
165
166

17822
NODE_MODULE_EXTERNAL_REFERENCE(handle_wrap,
167
                               node::HandleWrap::RegisterExternalReferences)