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-08-22 22:13:06 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
22708
void HandleWrap::Ref(const FunctionCallbackInfo<Value>& args) {
40
  HandleWrap* wrap;
41
22708
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
42
43
22707
  if (IsAlive(wrap))
44
22706
    uv_ref(wrap->GetHandle());
45
}
46
47
48
56462
void HandleWrap::Unref(const FunctionCallbackInfo<Value>& args) {
49
  HandleWrap* wrap;
50
56462
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
51
52
56281
  if (IsAlive(wrap))
53
56279
    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
27058
void HandleWrap::Close(const FunctionCallbackInfo<Value>& args) {
65
  HandleWrap* wrap;
66
27058
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
67
68
54114
  wrap->Close(args[0]);
69
}
70
71
51333
void HandleWrap::Close(Local<Value> close_callback) {
72
51333
  if (state_ != kInitialized)
73
404
    return;
74
75
50929
  uv_close(handle_, OnClose);
76
50925
  state_ = kClosing;
77
78


90200
  if (!close_callback.IsEmpty() && close_callback->IsFunction() &&
79
25240
      !persistent().IsEmpty()) {
80
37860
    object()->Set(env()->context(),
81
                  env()->handle_onclose_symbol(),
82
50480
                  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
51342
HandleWrap::HandleWrap(Environment* env,
105
                       Local<Object> object,
106
                       uv_handle_t* handle,
107
51342
                       AsyncWrap::ProviderType provider)
108
    : AsyncWrap(env, object, provider),
109
      state_(kInitialized),
110
51342
      handle_(handle) {
111
51342
  handle_->data = this;
112
102683
  HandleScope scope(env->isolate());
113
51342
  CHECK(env->has_run_bootstrapping_code());
114
51342
  env->handle_wrap_queue()->PushBack(this);
115
51341
}
116
117
118
50876
void HandleWrap::OnClose(uv_handle_t* handle) {
119
50876
  CHECK_NOT_NULL(handle->data);
120
101751
  BaseObjectPtr<HandleWrap> wrap { static_cast<HandleWrap*>(handle->data) };
121
50879
  wrap->Detach();
122
123
50879
  Environment* env = wrap->env();
124
101751
  HandleScope scope(env->isolate());
125
50876
  Context::Scope context_scope(env->context());
126
127
50877
  CHECK_EQ(wrap->state_, kClosing);
128
129
50877
  wrap->state_ = kClosed;
130
131
50877
  wrap->OnClose();
132
50879
  wrap->handle_wrap_queue_.Remove();
133
134

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

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