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

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