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

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

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