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: 63 63 100.0 %
Date: 2019-07-27 22:37:30 Branches: 27 30 90.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 "util-inl.h"
26
#include "node.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
21185
void HandleWrap::Ref(const FunctionCallbackInfo<Value>& args) {
40
  HandleWrap* wrap;
41
42370
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
42
43
21184
  if (IsAlive(wrap))
44
21183
    uv_ref(wrap->GetHandle());
45
}
46
47
48
53425
void HandleWrap::Unref(const FunctionCallbackInfo<Value>& args) {
49
  HandleWrap* wrap;
50
106850
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
51
52
53335
  if (IsAlive(wrap))
53
53333
    uv_unref(wrap->GetHandle());
54
}
55
56
57
30
void HandleWrap::HasRef(const FunctionCallbackInfo<Value>& args) {
58
  HandleWrap* wrap;
59
60
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
60
84
  args.GetReturnValue().Set(HasRef(wrap));
61
}
62
63
64
43983
void HandleWrap::Close(const FunctionCallbackInfo<Value>& args) {
65
  HandleWrap* wrap;
66
87966
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
67
68
87964
  wrap->Close(args[0]);
69
}
70
71
67458
void HandleWrap::Close(Local<Value> close_callback) {
72
67458
  if (state_ != kInitialized)
73
68543
    return;
74
75
132746
  CHECK_EQ(false, persistent().IsEmpty());
76
66373
  uv_close(handle_, OnClose);
77
66373
  state_ = kClosing;
78
79

109464
  if (!close_callback.IsEmpty() && close_callback->IsFunction()) {
80
28192
    object()->Set(env()->context(),
81
                  env()->handle_onclose_symbol(),
82
140960
                  close_callback).Check();
83
  }
84
}
85
86
87
2094
void HandleWrap::MarkAsInitialized() {
88
2094
  env()->handle_wrap_queue()->PushBack(this);
89
2094
  state_ = kInitialized;
90
2094
}
91
92
93
2124
void HandleWrap::MarkAsUninitialized() {
94
2124
  handle_wrap_queue_.Remove();
95
2124
  state_ = kClosed;
96
2124
}
97
98
99
66798
HandleWrap::HandleWrap(Environment* env,
100
                       Local<Object> object,
101
                       uv_handle_t* handle,
102
                       AsyncWrap::ProviderType provider)
103
    : AsyncWrap(env, object, provider),
104
      state_(kInitialized),
105
66798
      handle_(handle) {
106
66798
  handle_->data = this;
107
66798
  HandleScope scope(env->isolate());
108
66798
  env->handle_wrap_queue()->PushBack(this);
109
66798
}
110
111
112
66254
void HandleWrap::OnClose(uv_handle_t* handle) {
113
66254
  std::unique_ptr<HandleWrap> wrap { static_cast<HandleWrap*>(handle->data) };
114
66253
  Environment* env = wrap->env();
115
132505
  HandleScope scope(env->isolate());
116
66255
  Context::Scope context_scope(env->context());
117
118
  // The wrap object should still be there.
119
132509
  CHECK_EQ(wrap->persistent().IsEmpty(), false);
120
66255
  CHECK_EQ(wrap->state_, kClosing);
121
122
66255
  wrap->state_ = kClosed;
123
124
66255
  wrap->OnClose();
125
126
397530
  if (wrap->object()->Has(env->context(), env->handle_onclose_symbol())
127
265020
      .FromMaybe(false)) {
128
59199
    wrap->MakeCallback(env->handle_onclose_symbol(), 0, nullptr);
129
66252
  }
130
66251
}
131
132
21608
Local<FunctionTemplate> HandleWrap::GetConstructorTemplate(Environment* env) {
133
21608
  Local<FunctionTemplate> tmpl = env->handle_wrap_ctor_template();
134
21608
  if (tmpl.IsEmpty()) {
135
4965
    tmpl = env->NewFunctionTemplate(nullptr);
136
9930
    tmpl->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "HandleWrap"));
137
9930
    tmpl->Inherit(AsyncWrap::GetConstructorTemplate(env));
138
4965
    env->SetProtoMethod(tmpl, "close", HandleWrap::Close);
139
4965
    env->SetProtoMethodNoSideEffect(tmpl, "hasRef", HandleWrap::HasRef);
140
4965
    env->SetProtoMethod(tmpl, "ref", HandleWrap::Ref);
141
4965
    env->SetProtoMethod(tmpl, "unref", HandleWrap::Unref);
142
4965
    env->set_handle_wrap_ctor_template(tmpl);
143
  }
144
21608
  return tmpl;
145
}
146
147
148
}  // namespace node