GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: pipe_wrap.cc Lines: 107 110 97.3 %
Date: 2021-10-06 04:12:32 Branches: 20 38 52.6 %

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 "connect_wrap.h"
26
#include "connection_wrap.h"
27
#include "env-inl.h"
28
#include "handle_wrap.h"
29
#include "node.h"
30
#include "node_buffer.h"
31
#include "node_external_reference.h"
32
#include "stream_base-inl.h"
33
#include "stream_wrap.h"
34
#include "util-inl.h"
35
36
namespace node {
37
38
using v8::Context;
39
using v8::EscapableHandleScope;
40
using v8::Function;
41
using v8::FunctionCallbackInfo;
42
using v8::FunctionTemplate;
43
using v8::Int32;
44
using v8::Local;
45
using v8::MaybeLocal;
46
using v8::Object;
47
using v8::Value;
48
49
51
MaybeLocal<Object> PipeWrap::Instantiate(Environment* env,
50
                                         AsyncWrap* parent,
51
                                         PipeWrap::SocketType type) {
52
51
  EscapableHandleScope handle_scope(env->isolate());
53
51
  AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(parent);
54
102
  CHECK_EQ(false, env->pipe_constructor_template().IsEmpty());
55
51
  Local<Function> constructor = env->pipe_constructor_template()
56
51
                                    ->GetFunction(env->context())
57
51
                                    .ToLocalChecked();
58
51
  CHECK_EQ(false, constructor.IsEmpty());
59
102
  Local<Value> type_value = Int32::New(env->isolate(), type);
60
  return handle_scope.EscapeMaybe(
61
102
      constructor->NewInstance(env->context(), 1, &type_value));
62
}
63
64
65
4714
void PipeWrap::Initialize(Local<Object> target,
66
                          Local<Value> unused,
67
                          Local<Context> context,
68
                          void* priv) {
69
4714
  Environment* env = Environment::GetCurrent(context);
70
71
4714
  Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
72
9428
  t->InstanceTemplate()
73
4714
    ->SetInternalFieldCount(StreamBase::kInternalFieldCount);
74
75
4714
  t->Inherit(LibuvStreamWrap::GetConstructorTemplate(env));
76
77
4714
  env->SetProtoMethod(t, "bind", Bind);
78
4714
  env->SetProtoMethod(t, "listen", Listen);
79
4714
  env->SetProtoMethod(t, "connect", Connect);
80
4714
  env->SetProtoMethod(t, "open", Open);
81
82
#ifdef _WIN32
83
  env->SetProtoMethod(t, "setPendingInstances", SetPendingInstances);
84
#endif
85
86
4714
  env->SetProtoMethod(t, "fchmod", Fchmod);
87
88
4714
  env->SetConstructorFunction(target, "Pipe", t);
89
4714
  env->set_pipe_constructor_template(t);
90
91
  // Create FunctionTemplate for PipeConnectWrap.
92
4714
  auto cwt = BaseObject::MakeLazilyInitializedJSTemplate(env);
93
4714
  cwt->Inherit(AsyncWrap::GetConstructorTemplate(env));
94
4714
  env->SetConstructorFunction(target, "PipeConnectWrap", cwt);
95
96
  // Define constants
97
4714
  Local<Object> constants = Object::New(env->isolate());
98
14142
  NODE_DEFINE_CONSTANT(constants, SOCKET);
99
14142
  NODE_DEFINE_CONSTANT(constants, SERVER);
100
14142
  NODE_DEFINE_CONSTANT(constants, IPC);
101
14142
  NODE_DEFINE_CONSTANT(constants, UV_READABLE);
102
14142
  NODE_DEFINE_CONSTANT(constants, UV_WRITABLE);
103
4714
  target->Set(context,
104
              env->constants_string(),
105
9428
              constants).Check();
106
4714
}
107
108
4853
void PipeWrap::RegisterExternalReferences(ExternalReferenceRegistry* registry) {
109
4853
  registry->Register(New);
110
4853
  registry->Register(Bind);
111
4853
  registry->Register(Listen);
112
4853
  registry->Register(Connect);
113
4853
  registry->Register(Open);
114
#ifdef _WIN32
115
  registry->Register(SetPendingInstances);
116
#endif
117
4853
  registry->Register(Fchmod);
118
4853
}
119
120
5462
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
5462
  CHECK(args.IsConstructCall());
125
5462
  CHECK(args[0]->IsInt32());
126
5462
  Environment* env = Environment::GetCurrent(args);
127
128
10924
  int type_value = args[0].As<Int32>()->Value();
129
5462
  PipeWrap::SocketType type = static_cast<PipeWrap::SocketType>(type_value);
130
131
  bool ipc;
132
  ProviderType provider;
133

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