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: 95 97 97.9 %
Date: 2017-12-18 Branches: 16 30 53.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 "pipe_wrap.h"
23
24
#include "async_wrap.h"
25
#include "connection_wrap.h"
26
#include "env-inl.h"
27
#include "handle_wrap.h"
28
#include "node.h"
29
#include "node_buffer.h"
30
#include "node_wrap.h"
31
#include "connect_wrap.h"
32
#include "stream_wrap.h"
33
#include "util-inl.h"
34
35
namespace node {
36
37
using v8::Context;
38
using v8::EscapableHandleScope;
39
using v8::Function;
40
using v8::FunctionCallbackInfo;
41
using v8::FunctionTemplate;
42
using v8::HandleScope;
43
using v8::Int32;
44
using v8::Local;
45
using v8::Object;
46
using v8::String;
47
using v8::Value;
48
49
using AsyncHooks = Environment::AsyncHooks;
50
51
52
51
Local<Object> PipeWrap::Instantiate(Environment* env,
53
                                    AsyncWrap* parent,
54
                                    PipeWrap::SocketType type) {
55
51
  EscapableHandleScope handle_scope(env->isolate());
56
102
  AsyncHooks::InitScope init_scope(env, parent->get_async_id());
57
102
  CHECK_EQ(false, env->pipe_constructor_template().IsEmpty());
58
102
  Local<Function> constructor = env->pipe_constructor_template()->GetFunction();
59
51
  CHECK_EQ(false, constructor.IsEmpty());
60
51
  Local<Value> type_value = Int32::New(env->isolate(), type);
61
  Local<Object> instance =
62
153
      constructor->NewInstance(env->context(), 1, &type_value).ToLocalChecked();
63
51
  return handle_scope.Escape(instance);
64
}
65
66
67
3297
void PipeWrap::Initialize(Local<Object> target,
68
                          Local<Value> unused,
69
                          Local<Context> context) {
70
3297
  Environment* env = Environment::GetCurrent(context);
71
72
3297
  Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
73
3297
  Local<String> pipeString = FIXED_ONE_BYTE_STRING(env->isolate(), "Pipe");
74
3297
  t->SetClassName(pipeString);
75
6594
  t->InstanceTemplate()->SetInternalFieldCount(1);
76
77
3297
  AsyncWrap::AddWrapMethods(env, t);
78
79
3297
  env->SetProtoMethod(t, "close", HandleWrap::Close);
80
3297
  env->SetProtoMethod(t, "unref", HandleWrap::Unref);
81
3297
  env->SetProtoMethod(t, "ref", HandleWrap::Ref);
82
3297
  env->SetProtoMethod(t, "hasRef", HandleWrap::HasRef);
83
84
#ifdef _WIN32
85
  LibuvStreamWrap::AddMethods(env, t);
86
#else
87
3297
  LibuvStreamWrap::AddMethods(env, t, StreamBase::kFlagHasWritev);
88
#endif
89
90
3297
  env->SetProtoMethod(t, "bind", Bind);
91
3297
  env->SetProtoMethod(t, "listen", Listen);
92
3297
  env->SetProtoMethod(t, "connect", Connect);
93
3297
  env->SetProtoMethod(t, "open", Open);
94
95
#ifdef _WIN32
96
  env->SetProtoMethod(t, "setPendingInstances", SetPendingInstances);
97
#endif
98
99
6594
  target->Set(pipeString, t->GetFunction());
100
3297
  env->set_pipe_constructor_template(t);
101
102
  // Create FunctionTemplate for PipeConnectWrap.
103
3405
  auto constructor = [](const FunctionCallbackInfo<Value>& args) {
104
54
    CHECK(args.IsConstructCall());
105
54
    ClearWrap(args.This());
106
3405
  };
107
3297
  auto cwt = FunctionTemplate::New(env->isolate(), constructor);
108
6594
  cwt->InstanceTemplate()->SetInternalFieldCount(1);
109
3297
  AsyncWrap::AddWrapMethods(env, cwt);
110
  Local<String> wrapString =
111
3297
      FIXED_ONE_BYTE_STRING(env->isolate(), "PipeConnectWrap");
112
3297
  cwt->SetClassName(wrapString);
113
6594
  target->Set(wrapString, cwt->GetFunction());
114
115
  // Define constants
116
3297
  Local<Object> constants = Object::New(env->isolate());
117
9891
  NODE_DEFINE_CONSTANT(constants, SOCKET);
118
9891
  NODE_DEFINE_CONSTANT(constants, SERVER);
119
9891
  NODE_DEFINE_CONSTANT(constants, IPC);
120
  target->Set(context,
121
              FIXED_ONE_BYTE_STRING(env->isolate(), "constants"),
122
9891
              constants).FromJust();
123
3297
}
124
125
126
4553
void PipeWrap::New(const FunctionCallbackInfo<Value>& args) {
127
  // This constructor should not be exposed to public javascript.
128
  // Therefore we assert that we are not trying to call this as a
129
  // normal function.
130
4553
  CHECK(args.IsConstructCall());
131
9106
  CHECK(args[0]->IsInt32());
132
4553
  Environment* env = Environment::GetCurrent(args);
133
134
13659
  int type_value = args[0].As<Int32>()->Value();
135
4553
  PipeWrap::SocketType type = static_cast<PipeWrap::SocketType>(type_value);
136
137
  bool ipc;
138
  ProviderType provider;
139

4553
  switch (type) {
140
    case SOCKET:
141
3251
      provider = PROVIDER_PIPEWRAP;
142
3251
      ipc = false;
143
3251
      break;
144
    case SERVER:
145
44
      provider = PROVIDER_PIPESERVERWRAP;
146
44
      ipc = false;
147
44
      break;
148
    case IPC:
149
1258
      provider = PROVIDER_PIPEWRAP;
150
1258
      ipc = true;
151
1258
      break;
152
    default:
153
      UNREACHABLE();
154
  }
155
156
4553
  new PipeWrap(env, args.This(), provider, ipc);
157
4553
}
158
159
160
4553
PipeWrap::PipeWrap(Environment* env,
161
                   Local<Object> object,
162
                   ProviderType provider,
163
                   bool ipc)
164
4553
    : ConnectionWrap(env, object, provider) {
165
4553
  int r = uv_pipe_init(env->event_loop(), &handle_, ipc);
166
4553
  CHECK_EQ(r, 0);  // How do we proxy this error up to javascript?
167
                   // Suggestion: uv_pipe_init() returns void.
168
4553
}
169
170
171
49
void PipeWrap::Bind(const FunctionCallbackInfo<Value>& args) {
172
  PipeWrap* wrap;
173
98
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
174
49
  node::Utf8Value name(args.GetIsolate(), args[0]);
175
49
  int err = uv_pipe_bind(&wrap->handle_, *name);
176
98
  args.GetReturnValue().Set(err);
177
}
178
179
180
#ifdef _WIN32
181
void PipeWrap::SetPendingInstances(const FunctionCallbackInfo<Value>& args) {
182
  PipeWrap* wrap;
183
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
184
  int instances = args[0]->Int32Value();
185
  uv_pipe_pending_instances(&wrap->handle_, instances);
186
}
187
#endif
188
189
190
42
void PipeWrap::Listen(const FunctionCallbackInfo<Value>& args) {
191
  PipeWrap* wrap;
192
84
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
193
84
  int backlog = args[0]->Int32Value();
194
  int err = uv_listen(reinterpret_cast<uv_stream_t*>(&wrap->handle_),
195
                      backlog,
196
42
                      OnConnection);
197
84
  args.GetReturnValue().Set(err);
198
}
199
200
201
1807
void PipeWrap::Open(const FunctionCallbackInfo<Value>& args) {
202
1807
  Environment* env = Environment::GetCurrent(args);
203
204
  PipeWrap* wrap;
205
3614
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
206
207
3614
  int fd = args[0]->Int32Value();
208
209
1807
  int err = uv_pipe_open(&wrap->handle_, fd);
210
211
1807
  if (err != 0)
212
    env->isolate()->ThrowException(UVException(err, "uv_pipe_open"));
213
}
214
215
216
54
void PipeWrap::Connect(const FunctionCallbackInfo<Value>& args) {
217
54
  Environment* env = Environment::GetCurrent(args);
218
219
  PipeWrap* wrap;
220
108
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
221
222
108
  CHECK(args[0]->IsObject());
223
162
  CHECK(args[1]->IsString());
224
225
108
  Local<Object> req_wrap_obj = args[0].As<Object>();
226
54
  node::Utf8Value name(env->isolate(), args[1]);
227
228
  ConnectWrap* req_wrap =
229
54
      new ConnectWrap(env, req_wrap_obj, AsyncWrap::PROVIDER_PIPECONNECTWRAP);
230
  uv_pipe_connect(req_wrap->req(),
231
                  &wrap->handle_,
232
54
                  *name,
233
54
                  AfterConnect);
234
54
  req_wrap->Dispatched();
235
236
108
  args.GetReturnValue().Set(0);  // uv_pipe_connect() doesn't return errors.
237
}
238
239
240
}  // namespace node
241
242
3391
NODE_BUILTIN_MODULE_CONTEXT_AWARE(pipe_wrap, node::PipeWrap::Initialize)