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

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

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