GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: pipe_wrap.cc Lines: 107 110 97.3 %
Date: 2022-05-20 04:15:46 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
52
MaybeLocal<Object> PipeWrap::Instantiate(Environment* env,
50
                                         AsyncWrap* parent,
51
                                         PipeWrap::SocketType type) {
52
52
  EscapableHandleScope handle_scope(env->isolate());
53
52
  AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(parent);
54
104
  CHECK_EQ(false, env->pipe_constructor_template().IsEmpty());
55
52
  Local<Function> constructor = env->pipe_constructor_template()
56
52
                                    ->GetFunction(env->context())
57
52
                                    .ToLocalChecked();
58
52
  CHECK_EQ(false, constructor.IsEmpty());
59
104
  Local<Value> type_value = Int32::New(env->isolate(), type);
60
  return handle_scope.EscapeMaybe(
61
104
      constructor->NewInstance(env->context(), 1, &type_value));
62
}
63
64
65
5913
void PipeWrap::Initialize(Local<Object> target,
66
                          Local<Value> unused,
67
                          Local<Context> context,
68
                          void* priv) {
69
5913
  Environment* env = Environment::GetCurrent(context);
70
71
5913
  Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
72
11826
  t->InstanceTemplate()
73
5913
    ->SetInternalFieldCount(StreamBase::kInternalFieldCount);
74
75
5913
  t->Inherit(LibuvStreamWrap::GetConstructorTemplate(env));
76
77
5913
  env->SetProtoMethod(t, "bind", Bind);
78
5913
  env->SetProtoMethod(t, "listen", Listen);
79
5913
  env->SetProtoMethod(t, "connect", Connect);
80
5913
  env->SetProtoMethod(t, "open", Open);
81
82
#ifdef _WIN32
83
  env->SetProtoMethod(t, "setPendingInstances", SetPendingInstances);
84
#endif
85
86
5913
  env->SetProtoMethod(t, "fchmod", Fchmod);
87
88
5913
  env->SetConstructorFunction(target, "Pipe", t);
89
5913
  env->set_pipe_constructor_template(t);
90
91
  // Create FunctionTemplate for PipeConnectWrap.
92
5913
  auto cwt = BaseObject::MakeLazilyInitializedJSTemplate(env);
93
5913
  cwt->Inherit(AsyncWrap::GetConstructorTemplate(env));
94
5913
  env->SetConstructorFunction(target, "PipeConnectWrap", cwt);
95
96
  // Define constants
97
5913
  Local<Object> constants = Object::New(env->isolate());
98
17739
  NODE_DEFINE_CONSTANT(constants, SOCKET);
99
17739
  NODE_DEFINE_CONSTANT(constants, SERVER);
100
17739
  NODE_DEFINE_CONSTANT(constants, IPC);
101
17739
  NODE_DEFINE_CONSTANT(constants, UV_READABLE);
102
17739
  NODE_DEFINE_CONSTANT(constants, UV_WRITABLE);
103
5913
  target->Set(context,
104
              env->constants_string(),
105
11826
              constants).Check();
106
5913
}
107
108
5111
void PipeWrap::RegisterExternalReferences(ExternalReferenceRegistry* registry) {
109
5111
  registry->Register(New);
110
5111
  registry->Register(Bind);
111
5111
  registry->Register(Listen);
112
5111
  registry->Register(Connect);
113
5111
  registry->Register(Open);
114
#ifdef _WIN32
115
  registry->Register(SetPendingInstances);
116
#endif
117
5111
  registry->Register(Fchmod);
118
5111
}
119
120
5648
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
5648
  CHECK(args.IsConstructCall());
125
5648
  CHECK(args[0]->IsInt32());
126
5648
  Environment* env = Environment::GetCurrent(args);
127
128
11296
  int type_value = args[0].As<Int32>()->Value();
129
5648
  PipeWrap::SocketType type = static_cast<PipeWrap::SocketType>(type_value);
130
131
  bool ipc;
132
  ProviderType provider;
133

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