GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../src/pipe_wrap.cc Lines: 75 76 98.7 %
Date: 2017-06-14 Branches: 12 24 50.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 "pipe_wrap.h"
23
24
#include "async-wrap.h"
25
#include "connection_wrap.h"
26
#include "env.h"
27
#include "env-inl.h"
28
#include "handle_wrap.h"
29
#include "node.h"
30
#include "node_buffer.h"
31
#include "node_wrap.h"
32
#include "connect_wrap.h"
33
#include "stream_wrap.h"
34
#include "util-inl.h"
35
#include "util.h"
36
37
namespace node {
38
39
using v8::Context;
40
using v8::EscapableHandleScope;
41
using v8::Function;
42
using v8::FunctionCallbackInfo;
43
using v8::FunctionTemplate;
44
using v8::HandleScope;
45
using v8::Local;
46
using v8::Object;
47
using v8::Value;
48
49
using AsyncHooks = Environment::AsyncHooks;
50
51
52
35
Local<Object> PipeWrap::Instantiate(Environment* env, AsyncWrap* parent) {
53
35
  EscapableHandleScope handle_scope(env->isolate());
54
70
  AsyncHooks::InitScope init_scope(env, parent->get_id());
55
70
  CHECK_EQ(false, env->pipe_constructor_template().IsEmpty());
56
70
  Local<Function> constructor = env->pipe_constructor_template()->GetFunction();
57
35
  CHECK_EQ(false, constructor.IsEmpty());
58
  Local<Object> instance =
59
105
      constructor->NewInstance(env->context()).ToLocalChecked();
60
35
  return handle_scope.Escape(instance);
61
}
62
63
64
2319
void PipeWrap::Initialize(Local<Object> target,
65
                          Local<Value> unused,
66
                          Local<Context> context) {
67
2319
  Environment* env = Environment::GetCurrent(context);
68
69
2319
  Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
70
4638
  t->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Pipe"));
71
4638
  t->InstanceTemplate()->SetInternalFieldCount(1);
72
73
2319
  env->SetProtoMethod(t, "getAsyncId", AsyncWrap::GetAsyncId);
74
75
2319
  env->SetProtoMethod(t, "close", HandleWrap::Close);
76
2319
  env->SetProtoMethod(t, "unref", HandleWrap::Unref);
77
2319
  env->SetProtoMethod(t, "ref", HandleWrap::Ref);
78
2319
  env->SetProtoMethod(t, "hasRef", HandleWrap::HasRef);
79
80
#ifdef _WIN32
81
  StreamWrap::AddMethods(env, t);
82
#else
83
2319
  StreamWrap::AddMethods(env, t, StreamBase::kFlagHasWritev);
84
#endif
85
86
2319
  env->SetProtoMethod(t, "bind", Bind);
87
2319
  env->SetProtoMethod(t, "listen", Listen);
88
2319
  env->SetProtoMethod(t, "connect", Connect);
89
2319
  env->SetProtoMethod(t, "open", Open);
90
91
#ifdef _WIN32
92
  env->SetProtoMethod(t, "setPendingInstances", SetPendingInstances);
93
#endif
94
95
6957
  target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Pipe"), t->GetFunction());
96
2319
  env->set_pipe_constructor_template(t);
97
98
  // Create FunctionTemplate for PipeConnectWrap.
99
2397
  auto constructor = [](const FunctionCallbackInfo<Value>& args) {
100
39
    CHECK(args.IsConstructCall());
101
39
    ClearWrap(args.This());
102
2397
  };
103
2319
  auto cwt = FunctionTemplate::New(env->isolate(), constructor);
104
4638
  cwt->InstanceTemplate()->SetInternalFieldCount(1);
105
2319
  env->SetProtoMethod(cwt, "getAsyncId", AsyncWrap::GetAsyncId);
106
4638
  cwt->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "PipeConnectWrap"));
107
  target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "PipeConnectWrap"),
108
6957
              cwt->GetFunction());
109
2319
}
110
111
112
3465
void PipeWrap::New(const FunctionCallbackInfo<Value>& args) {
113
  // This constructor should not be exposed to public javascript.
114
  // Therefore we assert that we are not trying to call this as a
115
  // normal function.
116
3465
  CHECK(args.IsConstructCall());
117
3465
  Environment* env = Environment::GetCurrent(args);
118
10395
  new PipeWrap(env, args.This(), args[0]->IsTrue());
119
3465
}
120
121
122
3465
PipeWrap::PipeWrap(Environment* env,
123
                   Local<Object> object,
124
                   bool ipc)
125
    : ConnectionWrap(env,
126
                     object,
127
3465
                     AsyncWrap::PROVIDER_PIPEWRAP) {
128
3465
  int r = uv_pipe_init(env->event_loop(), &handle_, ipc);
129
3465
  CHECK_EQ(r, 0);  // How do we proxy this error up to javascript?
130
                   // Suggestion: uv_pipe_init() returns void.
131
3465
  UpdateWriteQueueSize();
132
3465
}
133
134
135
42
void PipeWrap::Bind(const FunctionCallbackInfo<Value>& args) {
136
  PipeWrap* wrap;
137
84
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
138
42
  node::Utf8Value name(args.GetIsolate(), args[0]);
139
42
  int err = uv_pipe_bind(&wrap->handle_, *name);
140
84
  args.GetReturnValue().Set(err);
141
}
142
143
144
#ifdef _WIN32
145
void PipeWrap::SetPendingInstances(const FunctionCallbackInfo<Value>& args) {
146
  PipeWrap* wrap;
147
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
148
  int instances = args[0]->Int32Value();
149
  uv_pipe_pending_instances(&wrap->handle_, instances);
150
}
151
#endif
152
153
154
35
void PipeWrap::Listen(const FunctionCallbackInfo<Value>& args) {
155
  PipeWrap* wrap;
156
70
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
157
70
  int backlog = args[0]->Int32Value();
158
  int err = uv_listen(reinterpret_cast<uv_stream_t*>(&wrap->handle_),
159
                      backlog,
160
35
                      OnConnection);
161
70
  args.GetReturnValue().Set(err);
162
}
163
164
165
1180
void PipeWrap::Open(const FunctionCallbackInfo<Value>& args) {
166
1180
  Environment* env = Environment::GetCurrent(args);
167
168
  PipeWrap* wrap;
169
2360
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
170
171
2360
  int fd = args[0]->Int32Value();
172
173
1180
  int err = uv_pipe_open(&wrap->handle_, fd);
174
175
1180
  if (err != 0)
176
    env->isolate()->ThrowException(UVException(err, "uv_pipe_open"));
177
}
178
179
180
39
void PipeWrap::Connect(const FunctionCallbackInfo<Value>& args) {
181
39
  Environment* env = Environment::GetCurrent(args);
182
183
  PipeWrap* wrap;
184
78
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
185
186
78
  CHECK(args[0]->IsObject());
187
117
  CHECK(args[1]->IsString());
188
189
78
  Local<Object> req_wrap_obj = args[0].As<Object>();
190
39
  node::Utf8Value name(env->isolate(), args[1]);
191
192
  ConnectWrap* req_wrap =
193
39
      new ConnectWrap(env, req_wrap_obj, AsyncWrap::PROVIDER_PIPECONNECTWRAP);
194
  uv_pipe_connect(req_wrap->req(),
195
                  &wrap->handle_,
196
39
                  *name,
197
39
                  AfterConnect);
198
39
  req_wrap->Dispatched();
199
200
78
  args.GetReturnValue().Set(0);  // uv_pipe_connect() doesn't return errors.
201
}
202
203
204
}  // namespace node
205
206
2441
NODE_MODULE_CONTEXT_AWARE_BUILTIN(pipe_wrap, node::PipeWrap::Initialize)