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-05-27 22:15:15 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
22344
void HandleWrap::Ref(const FunctionCallbackInfo<Value>& args) {
39
  HandleWrap* wrap;
40
22344
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
41
42
22343
  if (IsAlive(wrap))
43
22342
    uv_ref(wrap->GetHandle());
44
}
45
46
47
55495
void HandleWrap::Unref(const FunctionCallbackInfo<Value>& args) {
48
  HandleWrap* wrap;
49
55495
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
50
51
55330
  if (IsAlive(wrap))
52
55328
    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
26874
void HandleWrap::Close(const FunctionCallbackInfo<Value>& args) {
64
  HandleWrap* wrap;
65
26874
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
66
67
53746
  wrap->Close(args[0]);
68
}
69
70
50648
void HandleWrap::Close(Local<Value> close_callback) {
71
50648
  if (state_ != kInitialized)
72
406
    return;
73
74
50242
  uv_close(handle_, OnClose);
75
50241
  state_ = kClosing;
76
77


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

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