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: 104 106 98.1 %
Date: 2020-08-22 22:13:06 Branches: 21 40 52.5 %

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

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

17859
NODE_MODULE_CONTEXT_AWARE_INTERNAL(pipe_wrap, node::PipeWrap::Initialize)