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-06-24 22:13:30 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
22406
void HandleWrap::Ref(const FunctionCallbackInfo<Value>& args) {
39
  HandleWrap* wrap;
40
22406
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
41
42
22405
  if (IsAlive(wrap))
43
22404
    uv_ref(wrap->GetHandle());
44
}
45
46
47
55698
void HandleWrap::Unref(const FunctionCallbackInfo<Value>& args) {
48
  HandleWrap* wrap;
49
55698
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
50
51
55537
  if (IsAlive(wrap))
52
55535
    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
26962
void HandleWrap::Close(const FunctionCallbackInfo<Value>& args) {
64
  HandleWrap* wrap;
65
26962
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
66
67
53922
  wrap->Close(args[0]);
68
}
69
70
50873
void HandleWrap::Close(Local<Value> close_callback) {
71
50873
  if (state_ != kInitialized)
72
405
    return;
73
74
50468
  uv_close(handle_, OnClose);
75
50469
  state_ = kClosing;
76
77


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

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