GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../src/handle_wrap.cc Lines: 48 49 98.0 %
Date: 2017-11-19 Branches: 23 36 63.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
#include "node.h"
27
28
namespace node {
29
30
using v8::Context;
31
using v8::FunctionCallbackInfo;
32
using v8::HandleScope;
33
using v8::Local;
34
using v8::Object;
35
using v8::Value;
36
37
38
335
void HandleWrap::Ref(const FunctionCallbackInfo<Value>& args) {
39
  HandleWrap* wrap;
40
670
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
41
42
334
  if (IsAlive(wrap))
43
334
    uv_ref(wrap->GetHandle());
44
}
45
46
47
1825
void HandleWrap::Unref(const FunctionCallbackInfo<Value>& args) {
48
  HandleWrap* wrap;
49
3650
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
50
51
1825
  if (IsAlive(wrap))
52
1825
    uv_unref(wrap->GetHandle());
53
}
54
55
56
27
void HandleWrap::HasRef(const FunctionCallbackInfo<Value>& args) {
57
  HandleWrap* wrap;
58
54
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
59
81
  args.GetReturnValue().Set(HasRef(wrap));
60
}
61
62
63
11556
void HandleWrap::Close(const FunctionCallbackInfo<Value>& args) {
64
11556
  Environment* env = Environment::GetCurrent(args);
65
66
  HandleWrap* wrap;
67
12261
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
68
69
  // Guard against uninitialized handle or double close.
70
11554
  if (!IsAlive(wrap))
71
    return;
72
73
11554
  if (wrap->state_ != kInitialized)
74
703
    return;
75
76
21702
  CHECK_EQ(false, wrap->persistent().IsEmpty());
77
10851
  uv_close(wrap->handle_, OnClose);
78
10851
  wrap->state_ = kClosing;
79
80
21702
  if (args[0]->IsFunction()) {
81
20190
    wrap->object()->Set(env->onclose_string(), args[0]);
82
6730
    wrap->state_ = kClosingWithCallback;
83
  }
84
}
85
86
87
13971
HandleWrap::HandleWrap(Environment* env,
88
                       Local<Object> object,
89
                       uv_handle_t* handle,
90
                       AsyncWrap::ProviderType provider)
91
    : AsyncWrap(env, object, provider),
92
      state_(kInitialized),
93
13971
      handle_(handle) {
94
13971
  handle_->data = this;
95
13971
  HandleScope scope(env->isolate());
96
13971
  Wrap(object, this);
97
13971
  env->handle_wrap_queue()->PushBack(this);
98
13971
}
99
100
101
21552
HandleWrap::~HandleWrap() {
102
21552
  CHECK(persistent().IsEmpty());
103
10776
}
104
105
106
10779
void HandleWrap::OnClose(uv_handle_t* handle) {
107
10779
  HandleWrap* wrap = static_cast<HandleWrap*>(handle->data);
108
10779
  Environment* env = wrap->env();
109
10779
  HandleScope scope(env->isolate());
110
10779
  Context::Scope context_scope(env->context());
111
112
  // The wrap object should still be there.
113
21558
  CHECK_EQ(wrap->persistent().IsEmpty(), false);
114

10779
  CHECK(wrap->state_ >= kClosing && wrap->state_ <= kClosingWithCallback);
115
116
10779
  const bool have_close_callback = (wrap->state_ == kClosingWithCallback);
117
10779
  wrap->state_ = kClosed;
118
119
10779
  if (have_close_callback)
120
6700
    wrap->MakeCallback(env->onclose_string(), 0, nullptr);
121
122
10776
  ClearWrap(wrap->object());
123
10776
  wrap->persistent().Reset();
124
21552
  delete wrap;
125
10776
}
126
127
128
}  // namespace node