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: 70 70 100.0 %
Date: 2020-02-19 22:14:06 Branches: 32 36 88.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 "util-inl.h"
26
27
namespace node {
28
29
using v8::Context;
30
using v8::FunctionCallbackInfo;
31
using v8::FunctionTemplate;
32
using v8::HandleScope;
33
using v8::Local;
34
using v8::Object;
35
using v8::Value;
36
37
38
21319
void HandleWrap::Ref(const FunctionCallbackInfo<Value>& args) {
39
  HandleWrap* wrap;
40
21319
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
41
42
21318
  if (IsAlive(wrap))
43
21317
    uv_ref(wrap->GetHandle());
44
}
45
46
47
53232
void HandleWrap::Unref(const FunctionCallbackInfo<Value>& args) {
48
  HandleWrap* wrap;
49
53232
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
50
51
53109
  if (IsAlive(wrap))
52
53107
    uv_unref(wrap->GetHandle());
53
}
54
55
56
33
void HandleWrap::HasRef(const FunctionCallbackInfo<Value>& args) {
57
  HandleWrap* wrap;
58
33
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
59
93
  args.GetReturnValue().Set(HasRef(wrap));
60
}
61
62
63
26587
void HandleWrap::Close(const FunctionCallbackInfo<Value>& args) {
64
  HandleWrap* wrap;
65
26587
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
66
67
53172
  wrap->Close(args[0]);
68
}
69
70
49275
void HandleWrap::Close(Local<Value> close_callback) {
71
49275
  if (state_ != kInitialized)
72
396
    return;
73
74
48879
  uv_close(handle_, OnClose);
75
48880
  state_ = kClosing;
76
77


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

195162
  if (!wrap->persistent().IsEmpty() &&
134
195160
      wrap->object()->Has(env->context(), env->handle_onclose_symbol())
135
146368
      .FromMaybe(false)) {
136
43489
    wrap->MakeCallback(env->handle_onclose_symbol(), 0, nullptr);
137
  }
138
48789
}
139
140
19337
Local<FunctionTemplate> HandleWrap::GetConstructorTemplate(Environment* env) {
141
19337
  Local<FunctionTemplate> tmpl = env->handle_wrap_ctor_template();
142
19337
  if (tmpl.IsEmpty()) {
143
4377
    tmpl = env->NewFunctionTemplate(nullptr);
144
8754
    tmpl->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "HandleWrap"));
145
8754
    tmpl->Inherit(AsyncWrap::GetConstructorTemplate(env));
146
4377
    env->SetProtoMethod(tmpl, "close", HandleWrap::Close);
147
4377
    env->SetProtoMethodNoSideEffect(tmpl, "hasRef", HandleWrap::HasRef);
148
4377
    env->SetProtoMethod(tmpl, "ref", HandleWrap::Ref);
149
4377
    env->SetProtoMethod(tmpl, "unref", HandleWrap::Unref);
150
4377
    env->set_handle_wrap_ctor_template(tmpl);
151
  }
152
19337
  return tmpl;
153
}
154
155
156
}  // namespace node