GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/pipe_wrap.cc Lines: 94 96 97.9 %
Date: 2019-02-13 22:28:58 Branches: 19 36 52.8 %

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 "connect_wrap.h"
31
#include "stream_base-inl.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::MaybeLocal;
46
using v8::Object;
47
using v8::String;
48
using v8::Value;
49
50
using AsyncHooks = Environment::AsyncHooks;
51
52
50
MaybeLocal<Object> PipeWrap::Instantiate(Environment* env,
53
                                         AsyncWrap* parent,
54
                                         PipeWrap::SocketType type) {
55
50
  EscapableHandleScope handle_scope(env->isolate());
56
100
  AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(parent);
57
100
  CHECK_EQ(false, env->pipe_constructor_template().IsEmpty());
58
  Local<Function> constructor = env->pipe_constructor_template()
59
150
                                    ->GetFunction(env->context())
60
100
                                    .ToLocalChecked();
61
50
  CHECK_EQ(false, constructor.IsEmpty());
62
50
  Local<Value> type_value = Int32::New(env->isolate(), type);
63
  return handle_scope.EscapeMaybe(
64
150
      constructor->NewInstance(env->context(), 1, &type_value));
65
}
66
67
68
4061
void PipeWrap::Initialize(Local<Object> target,
69
                          Local<Value> unused,
70
                          Local<Context> context,
71
                          void* priv) {
72
4061
  Environment* env = Environment::GetCurrent(context);
73
74
4061
  Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
75
4061
  Local<String> pipeString = FIXED_ONE_BYTE_STRING(env->isolate(), "Pipe");
76
4061
  t->SetClassName(pipeString);
77
8122
  t->InstanceTemplate()->SetInternalFieldCount(1);
78
79
8122
  t->Inherit(LibuvStreamWrap::GetConstructorTemplate(env));
80
81
4061
  env->SetProtoMethod(t, "bind", Bind);
82
4061
  env->SetProtoMethod(t, "listen", Listen);
83
4061
  env->SetProtoMethod(t, "connect", Connect);
84
4061
  env->SetProtoMethod(t, "open", Open);
85
86
#ifdef _WIN32
87
  env->SetProtoMethod(t, "setPendingInstances", SetPendingInstances);
88
#endif
89
90
4061
  env->SetProtoMethod(t, "fchmod", Fchmod);
91
92
  target->Set(env->context(),
93
              pipeString,
94
20305
              t->GetFunction(env->context()).ToLocalChecked()).FromJust();
95
4061
  env->set_pipe_constructor_template(t);
96
97
  // Create FunctionTemplate for PipeConnectWrap.
98
4061
  auto cwt = BaseObject::MakeLazilyInitializedJSTemplate(env);
99
8122
  cwt->Inherit(AsyncWrap::GetConstructorTemplate(env));
100
  Local<String> wrapString =
101
4061
      FIXED_ONE_BYTE_STRING(env->isolate(), "PipeConnectWrap");
102
4061
  cwt->SetClassName(wrapString);
103
  target->Set(env->context(),
104
              wrapString,
105
20305
              cwt->GetFunction(env->context()).ToLocalChecked()).FromJust();
106
107
  // Define constants
108
4061
  Local<Object> constants = Object::New(env->isolate());
109
16244
  NODE_DEFINE_CONSTANT(constants, SOCKET);
110
16244
  NODE_DEFINE_CONSTANT(constants, SERVER);
111
16244
  NODE_DEFINE_CONSTANT(constants, IPC);
112
16244
  NODE_DEFINE_CONSTANT(constants, UV_READABLE);
113
16244
  NODE_DEFINE_CONSTANT(constants, UV_WRITABLE);
114
  target->Set(context,
115
              env->constants_string(),
116
12183
              constants).FromJust();
117
4061
}
118
119
120
5334
void PipeWrap::New(const FunctionCallbackInfo<Value>& args) {
121
  // This constructor should not be exposed to public javascript.
122
  // Therefore we assert that we are not trying to call this as a
123
  // normal function.
124
5334
  CHECK(args.IsConstructCall());
125
10668
  CHECK(args[0]->IsInt32());
126
5334
  Environment* env = Environment::GetCurrent(args);
127
128
16002
  int type_value = args[0].As<Int32>()->Value();
129
5334
  PipeWrap::SocketType type = static_cast<PipeWrap::SocketType>(type_value);
130
131
  bool ipc;
132
  ProviderType provider;
133

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