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-07-19 22:14:24 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
22448
void HandleWrap::Ref(const FunctionCallbackInfo<Value>& args) {
40
  HandleWrap* wrap;
41
22448
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
42
43
22447
  if (IsAlive(wrap))
44
22446
    uv_ref(wrap->GetHandle());
45
}
46
47
48
56301
void HandleWrap::Unref(const FunctionCallbackInfo<Value>& args) {
49
  HandleWrap* wrap;
50
56301
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
51
52
56151
  if (IsAlive(wrap))
53
56147
    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
28826
void HandleWrap::Close(const FunctionCallbackInfo<Value>& args) {
65
  HandleWrap* wrap;
66
28826
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
67
68
57650
  wrap->Close(args[0]);
69
}
70
71
53322
void HandleWrap::Close(Local<Value> close_callback) {
72
53322
  if (state_ != kInitialized)
73
925
    return;
74
75
52397
  uv_close(handle_, OnClose);
76
52396
  state_ = kClosing;
77
78


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

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

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