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: 64 64 100.0 %
Date: 2019-02-26 22:23:30 Branches: 25 30 83.3 %

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
807
void HandleWrap::Ref(const FunctionCallbackInfo<Value>& args) {
40
  HandleWrap* wrap;
41
1614
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
42
43
806
  if (IsAlive(wrap))
44
806
    uv_ref(wrap->GetHandle());
45
}
46
47
48
2729
void HandleWrap::Unref(const FunctionCallbackInfo<Value>& args) {
49
  HandleWrap* wrap;
50
5458
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
51
52
2643
  if (IsAlive(wrap))
53
2643
    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
34685
void HandleWrap::Close(const FunctionCallbackInfo<Value>& args) {
65
  HandleWrap* wrap;
66
69370
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
67
68
69368
  wrap->Close(args[0]);
69
}
70
71
37587
void HandleWrap::Close(Local<Value> close_callback) {
72
37587
  if (state_ != kInitialized)
73
38643
    return;
74
75
73062
  CHECK_EQ(false, persistent().IsEmpty());
76
36531
  uv_close(handle_, OnClose);
77
36531
  state_ = kClosing;
78
79

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