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: 96 98 98.0 %
Date: 2019-09-16 22:31:04 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::Isolate;
45
using v8::Local;
46
using v8::MaybeLocal;
47
using v8::Object;
48
using v8::String;
49
using v8::Value;
50
51
51
MaybeLocal<Object> PipeWrap::Instantiate(Environment* env,
52
                                         AsyncWrap* parent,
53
                                         PipeWrap::SocketType type) {
54
51
  EscapableHandleScope handle_scope(env->isolate());
55
102
  AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(parent);
56
102
  CHECK_EQ(false, env->pipe_constructor_template().IsEmpty());
57
  Local<Function> constructor = env->pipe_constructor_template()
58
153
                                    ->GetFunction(env->context())
59
102
                                    .ToLocalChecked();
60
51
  CHECK_EQ(false, constructor.IsEmpty());
61
51
  Local<Value> type_value = Int32::New(env->isolate(), type);
62
  return handle_scope.EscapeMaybe(
63
153
      constructor->NewInstance(env->context(), 1, &type_value));
64
}
65
66
67
4718
void PipeWrap::Initialize(Local<Object> target,
68
                          Local<Value> unused,
69
                          Local<Context> context,
70
                          void* priv) {
71
4718
  Environment* env = Environment::GetCurrent(context);
72
73
4718
  Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
74
4718
  Local<String> pipeString = FIXED_ONE_BYTE_STRING(env->isolate(), "Pipe");
75
4718
  t->SetClassName(pipeString);
76
4718
  t->InstanceTemplate()
77
9436
    ->SetInternalFieldCount(StreamBase::kStreamBaseFieldCount);
78
79
9436
  t->Inherit(LibuvStreamWrap::GetConstructorTemplate(env));
80
81
4718
  env->SetProtoMethod(t, "bind", Bind);
82
4718
  env->SetProtoMethod(t, "listen", Listen);
83
4718
  env->SetProtoMethod(t, "connect", Connect);
84
4718
  env->SetProtoMethod(t, "open", Open);
85
86
#ifdef _WIN32
87
  env->SetProtoMethod(t, "setPendingInstances", SetPendingInstances);
88
#endif
89
90
4718
  env->SetProtoMethod(t, "fchmod", Fchmod);
91
92
  target->Set(env->context(),
93
              pipeString,
94
23590
              t->GetFunction(env->context()).ToLocalChecked()).Check();
95
4718
  env->set_pipe_constructor_template(t);
96
97
  // Create FunctionTemplate for PipeConnectWrap.
98
4718
  auto cwt = BaseObject::MakeLazilyInitializedJSTemplate(env);
99
9436
  cwt->Inherit(AsyncWrap::GetConstructorTemplate(env));
100
  Local<String> wrapString =
101
4718
      FIXED_ONE_BYTE_STRING(env->isolate(), "PipeConnectWrap");
102
4718
  cwt->SetClassName(wrapString);
103
  target->Set(env->context(),
104
              wrapString,
105
23590
              cwt->GetFunction(env->context()).ToLocalChecked()).Check();
106
107
  // Define constants
108
4718
  Local<Object> constants = Object::New(env->isolate());
109
18872
  NODE_DEFINE_CONSTANT(constants, SOCKET);
110
18872
  NODE_DEFINE_CONSTANT(constants, SERVER);
111
18872
  NODE_DEFINE_CONSTANT(constants, IPC);
112
18872
  NODE_DEFINE_CONSTANT(constants, UV_READABLE);
113
18872
  NODE_DEFINE_CONSTANT(constants, UV_WRITABLE);
114
  target->Set(context,
115
              env->constants_string(),
116
14154
              constants).Check();
117
4718
}
118
119
120
6244
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
6244
  CHECK(args.IsConstructCall());
125
12488
  CHECK(args[0]->IsInt32());
126
6244
  Environment* env = Environment::GetCurrent(args);
127
128
18732
  int type_value = args[0].As<Int32>()->Value();
129
6244
  PipeWrap::SocketType type = static_cast<PipeWrap::SocketType>(type_value);
130
131
  bool ipc;
132
  ProviderType provider;
133

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