GCC Code Coverage Report
Directory: ../src/ Exec Total Coverage
File: /home/node-core-coverage/node-core-coverage/workdir/node/out/../src/pipe_wrap.cc Lines: 78 79 98.7 %
Date: 2016-12-18 Branches: 14 26 53.8 %

Line Branch Exec Source
1
#include "pipe_wrap.h"
2
3
#include "async-wrap.h"
4
#include "connection_wrap.h"
5
#include "env.h"
6
#include "env-inl.h"
7
#include "handle_wrap.h"
8
#include "node.h"
9
#include "node_buffer.h"
10
#include "node_wrap.h"
11
#include "connect_wrap.h"
12
#include "stream_wrap.h"
13
#include "util-inl.h"
14
#include "util.h"
15
16
namespace node {
17
18
using v8::Context;
19
using v8::EscapableHandleScope;
20
using v8::External;
21
using v8::Function;
22
using v8::FunctionCallbackInfo;
23
using v8::FunctionTemplate;
24
using v8::HandleScope;
25
using v8::Local;
26
using v8::Object;
27
using v8::Value;
28
29
30
11
Local<Object> PipeWrap::Instantiate(Environment* env, AsyncWrap* parent) {
31
22
  EscapableHandleScope handle_scope(env->isolate());
32
22
  CHECK_EQ(false, env->pipe_constructor_template().IsEmpty());
33
11
  Local<Function> constructor = env->pipe_constructor_template()->GetFunction();
34
11
  CHECK_EQ(false, constructor.IsEmpty());
35
22
  Local<Value> ptr = External::New(env->isolate(), parent);
36
  Local<Object> instance =
37
22
      constructor->NewInstance(env->context(), 1, &ptr).ToLocalChecked();
38
22
  return handle_scope.Escape(instance);
39
}
40
41
42
1676
void PipeWrap::Initialize(Local<Object> target,
43
                          Local<Value> unused,
44
                          Local<Context> context) {
45
1676
  Environment* env = Environment::GetCurrent(context);
46
47
3352
  Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
48
1676
  t->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Pipe"));
49
3352
  t->InstanceTemplate()->SetInternalFieldCount(1);
50
51
1676
  env->SetProtoMethod(t, "close", HandleWrap::Close);
52
1676
  env->SetProtoMethod(t, "unref", HandleWrap::Unref);
53
1676
  env->SetProtoMethod(t, "ref", HandleWrap::Ref);
54
1676
  env->SetProtoMethod(t, "hasRef", HandleWrap::HasRef);
55
56
1676
  StreamWrap::AddMethods(env, t);
57
58
1676
  env->SetProtoMethod(t, "bind", Bind);
59
1676
  env->SetProtoMethod(t, "listen", Listen);
60
1676
  env->SetProtoMethod(t, "connect", Connect);
61
1676
  env->SetProtoMethod(t, "open", Open);
62
63
#ifdef _WIN32
64
  env->SetProtoMethod(t, "setPendingInstances", SetPendingInstances);
65
#endif
66
67
5028
  target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Pipe"), t->GetFunction());
68
1676
  env->set_pipe_constructor_template(t);
69
70
  // Create FunctionTemplate for PipeConnectWrap.
71
28
  auto constructor = [](const FunctionCallbackInfo<Value>& args) {
72
14
    CHECK(args.IsConstructCall());
73
28
  };
74
3352
  auto cwt = FunctionTemplate::New(env->isolate(), constructor);
75
3352
  cwt->InstanceTemplate()->SetInternalFieldCount(1);
76
1676
  cwt->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "PipeConnectWrap"));
77
6704
  target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "PipeConnectWrap"),
78
1676
              cwt->GetFunction());
79
1676
}
80
81
82
2061
void PipeWrap::New(const FunctionCallbackInfo<Value>& args) {
83
  // This constructor should not be exposed to public javascript.
84
  // Therefore we assert that we are not trying to call this as a
85
  // normal function.
86
2061
  CHECK(args.IsConstructCall());
87
2061
  Environment* env = Environment::GetCurrent(args);
88
2061
  if (args[0]->IsExternal()) {
89
22
    void* ptr = args[0].As<External>()->Value();
90
11
    new PipeWrap(env, args.This(), false, static_cast<AsyncWrap*>(ptr));
91
  } else {
92
4100
    new PipeWrap(env, args.This(), args[0]->IsTrue(), nullptr);
93
  }
94
2061
}
95
96
97
2061
PipeWrap::PipeWrap(Environment* env,
98
                   Local<Object> object,
99
                   bool ipc,
100
                   AsyncWrap* parent)
101
    : ConnectionWrap(env,
102
                     object,
103
                     AsyncWrap::PROVIDER_PIPEWRAP,
104
2061
                     parent) {
105
4122
  int r = uv_pipe_init(env->event_loop(), &handle_, ipc);
106
2061
  CHECK_EQ(r, 0);  // How do we proxy this error up to javascript?
107
                   // Suggestion: uv_pipe_init() returns void.
108
2061
  UpdateWriteQueueSize();
109
2061
}
110
111
112
16
void PipeWrap::Bind(const FunctionCallbackInfo<Value>& args) {
113
  PipeWrap* wrap;
114
16
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
115
48
  node::Utf8Value name(args.GetIsolate(), args[0]);
116
16
  int err = uv_pipe_bind(&wrap->handle_, *name);
117
32
  args.GetReturnValue().Set(err);
118
}
119
120
121
#ifdef _WIN32
122
void PipeWrap::SetPendingInstances(const FunctionCallbackInfo<Value>& args) {
123
  PipeWrap* wrap;
124
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
125
  int instances = args[0]->Int32Value();
126
  uv_pipe_pending_instances(&wrap->handle_, instances);
127
}
128
#endif
129
130
131
15
void PipeWrap::Listen(const FunctionCallbackInfo<Value>& args) {
132
  PipeWrap* wrap;
133
15
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
134
15
  int backlog = args[0]->Int32Value();
135
15
  int err = uv_listen(reinterpret_cast<uv_stream_t*>(&wrap->handle_),
136
                      backlog,
137
15
                      OnConnection);
138
30
  args.GetReturnValue().Set(err);
139
}
140
141
142
603
void PipeWrap::Open(const FunctionCallbackInfo<Value>& args) {
143
603
  Environment* env = Environment::GetCurrent(args);
144
145
  PipeWrap* wrap;
146
603
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
147
148
603
  int fd = args[0]->Int32Value();
149
150
603
  int err = uv_pipe_open(&wrap->handle_, fd);
151
152
603
  if (err != 0)
153
    env->isolate()->ThrowException(UVException(err, "uv_pipe_open"));
154
}
155
156
157
14
void PipeWrap::Connect(const FunctionCallbackInfo<Value>& args) {
158
14
  Environment* env = Environment::GetCurrent(args);
159
160
  PipeWrap* wrap;
161
14
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
162
163
14
  CHECK(args[0]->IsObject());
164
28
  CHECK(args[1]->IsString());
165
166
28
  Local<Object> req_wrap_obj = args[0].As<Object>();
167
28
  node::Utf8Value name(env->isolate(), args[1]);
168
169
  ConnectWrap* req_wrap =
170
14
      new ConnectWrap(env, req_wrap_obj, AsyncWrap::PROVIDER_PIPECONNECTWRAP);
171
28
  uv_pipe_connect(req_wrap->req(),
172
                  &wrap->handle_,
173
14
                  *name,
174
14
                  AfterConnect);
175
28
  req_wrap->Dispatched();
176
177
28
  args.GetReturnValue().Set(0);  // uv_pipe_connect() doesn't return errors.
178
}
179
180
181
}  // namespace node
182
183
1734
NODE_MODULE_CONTEXT_AWARE_BUILTIN(pipe_wrap, node::PipeWrap::Initialize)