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-06-14 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.h"
24
#include "async-wrap-inl.h"
25
#include "env.h"
26
#include "env-inl.h"
27
#include "util.h"
28
#include "util-inl.h"
29
#include "node.h"
30
31
namespace node {
32
33
using v8::Context;
34
using v8::FunctionCallbackInfo;
35
using v8::HandleScope;
36
using v8::Local;
37
using v8::Object;
38
using v8::Value;
39
40
41
248
void HandleWrap::Ref(const FunctionCallbackInfo<Value>& args) {
42
  HandleWrap* wrap;
43
496
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
44
45
247
  if (IsAlive(wrap))
46
247
    uv_ref(wrap->GetHandle());
47
}
48
49
50
1264
void HandleWrap::Unref(const FunctionCallbackInfo<Value>& args) {
51
  HandleWrap* wrap;
52
2528
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
53
54
1264
  if (IsAlive(wrap))
55
1264
    uv_unref(wrap->GetHandle());
56
}
57
58
59
27
void HandleWrap::HasRef(const FunctionCallbackInfo<Value>& args) {
60
  HandleWrap* wrap;
61
54
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
62
81
  args.GetReturnValue().Set(HasRef(wrap));
63
}
64
65
66
9789
void HandleWrap::Close(const FunctionCallbackInfo<Value>& args) {
67
9789
  Environment* env = Environment::GetCurrent(args);
68
69
  HandleWrap* wrap;
70
10277
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
71
72
  // Guard against uninitialized handle or double close.
73
9787
  if (!IsAlive(wrap))
74
    return;
75
76
9787
  if (wrap->state_ != kInitialized)
77
486
    return;
78
79
18602
  CHECK_EQ(false, wrap->persistent().IsEmpty());
80
9301
  uv_close(wrap->handle_, OnClose);
81
9301
  wrap->state_ = kClosing;
82
83
18602
  if (args[0]->IsFunction()) {
84
17952
    wrap->object()->Set(env->onclose_string(), args[0]);
85
5984
    wrap->state_ = kClosingWithCallback;
86
  }
87
}
88
89
90
11694
HandleWrap::HandleWrap(Environment* env,
91
                       Local<Object> object,
92
                       uv_handle_t* handle,
93
                       AsyncWrap::ProviderType provider)
94
    : AsyncWrap(env, object, provider),
95
      state_(kInitialized),
96
11694
      handle_(handle) {
97
11694
  handle_->data = this;
98
11694
  HandleScope scope(env->isolate());
99
11694
  Wrap(object, this);
100
11694
  env->handle_wrap_queue()->PushBack(this);
101
11694
}
102
103
104
18460
HandleWrap::~HandleWrap() {
105
18460
  CHECK(persistent().IsEmpty());
106
9230
}
107
108
109
9233
void HandleWrap::OnClose(uv_handle_t* handle) {
110
9233
  HandleWrap* wrap = static_cast<HandleWrap*>(handle->data);
111
9233
  Environment* env = wrap->env();
112
9233
  HandleScope scope(env->isolate());
113
9233
  Context::Scope context_scope(env->context());
114
115
  // The wrap object should still be there.
116
18466
  CHECK_EQ(wrap->persistent().IsEmpty(), false);
117

9233
  CHECK(wrap->state_ >= kClosing && wrap->state_ <= kClosingWithCallback);
118
119
9233
  const bool have_close_callback = (wrap->state_ == kClosingWithCallback);
120
9233
  wrap->state_ = kClosed;
121
122
9233
  if (have_close_callback)
123
5953
    wrap->MakeCallback(env->onclose_string(), 0, nullptr);
124
125
9230
  ClearWrap(wrap->object());
126
9230
  wrap->persistent().Reset();
127
18460
  delete wrap;
128
9230
}
129
130
131
}  // namespace node