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-30 04:11:56 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
51
MaybeLocal<Object> PipeWrap::Instantiate(Environment* env,
49
                                         AsyncWrap* parent,
50
                                         PipeWrap::SocketType type) {
51
51
  EscapableHandleScope handle_scope(env->isolate());
52
102
  AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(parent);
53
102
  CHECK_EQ(false, env->pipe_constructor_template().IsEmpty());
54
102
  Local<Function> constructor = env->pipe_constructor_template()
55
153
                                    ->GetFunction(env->context())
56
51
                                    .ToLocalChecked();
57
51
  CHECK_EQ(false, constructor.IsEmpty());
58
51
  Local<Value> type_value = Int32::New(env->isolate(), type);
59
  return handle_scope.EscapeMaybe(
60
153
      constructor->NewInstance(env->context(), 1, &type_value));
61
}
62
63
64
4618
void PipeWrap::Initialize(Local<Object> target,
65
                          Local<Value> unused,
66
                          Local<Context> context,
67
                          void* priv) {
68
4618
  Environment* env = Environment::GetCurrent(context);
69
70
4618
  Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
71
9235
  t->InstanceTemplate()
72
4618
    ->SetInternalFieldCount(StreamBase::kInternalFieldCount);
73
74
9236
  t->Inherit(LibuvStreamWrap::GetConstructorTemplate(env));
75
76
4618
  env->SetProtoMethod(t, "bind", Bind);
77
4618
  env->SetProtoMethod(t, "listen", Listen);
78
4618
  env->SetProtoMethod(t, "connect", Connect);
79
4618
  env->SetProtoMethod(t, "open", Open);
80
81
#ifdef _WIN32
82
  env->SetProtoMethod(t, "setPendingInstances", SetPendingInstances);
83
#endif
84
85
4618
  env->SetProtoMethod(t, "fchmod", Fchmod);
86
87
4618
  env->SetConstructorFunction(target, "Pipe", t);
88
4618
  env->set_pipe_constructor_template(t);
89
90
  // Create FunctionTemplate for PipeConnectWrap.
91
4618
  auto cwt = BaseObject::MakeLazilyInitializedJSTemplate(env);
92
9236
  cwt->Inherit(AsyncWrap::GetConstructorTemplate(env));
93
4618
  env->SetConstructorFunction(target, "PipeConnectWrap", cwt);
94
95
  // Define constants
96
4618
  Local<Object> constants = Object::New(env->isolate());
97
18472
  NODE_DEFINE_CONSTANT(constants, SOCKET);
98
4618
  NODE_DEFINE_CONSTANT(constants, SERVER);
99
13854
  NODE_DEFINE_CONSTANT(constants, IPC);
100
18472
  NODE_DEFINE_CONSTANT(constants, UV_READABLE);
101
41562
  NODE_DEFINE_CONSTANT(constants, UV_WRITABLE);
102
46180
  target->Set(context,
103
27708
              env->constants_string(),
104
36944
              constants).Check();
105
13854
}
106
107
108
5372
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
5372
  CHECK(args.IsConstructCall());
113
10744
  CHECK(args[0]->IsInt32());
114
5372
  Environment* env = Environment::GetCurrent(args);
115
116
16116
  int type_value = args[0].As<Int32>()->Value();
117
5372
  PipeWrap::SocketType type = static_cast<PipeWrap::SocketType>(type_value);
118
119
  bool ipc;
120
  ProviderType provider;
121

5372
  switch (type) {
122
    case SOCKET:
123
4443
      provider = PROVIDER_PIPEWRAP;
124
4443
      ipc = false;
125
4443
      break;
126
    case SERVER:
127
48
      provider = PROVIDER_PIPESERVERWRAP;
128
48
      ipc = false;
129
48
      break;
130
    case IPC:
131
881
      provider = PROVIDER_PIPEWRAP;
132
881
      ipc = true;
133
881
      break;
134
    default:
135
      UNREACHABLE();
136
  }
137
138
5372
  new PipeWrap(env, args.This(), provider, ipc);
139
5372
}
140
141
142
5372
PipeWrap::PipeWrap(Environment* env,
143
                   Local<Object> object,
144
                   ProviderType provider,
145
5372
                   bool ipc)
146
5372
    : ConnectionWrap(env, object, provider) {
147
5372
  int r = uv_pipe_init(env->event_loop(), &handle_, ipc);
148
5372
  CHECK_EQ(r, 0);  // How do we proxy this error up to javascript?
149
                   // Suggestion: uv_pipe_init() returns void.
150
5372
}
151
152
153
53
void PipeWrap::Bind(const FunctionCallbackInfo<Value>& args) {
154
  PipeWrap* wrap;
155
53
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
156
106
  node::Utf8Value name(args.GetIsolate(), args[0]);
157
53
  int err = uv_pipe_bind(&wrap->handle_, *name);
158
106
  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
45
void PipeWrap::Listen(const FunctionCallbackInfo<Value>& args) {
184
  PipeWrap* wrap;
185
45
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
186
45
  Environment* env = wrap->env();
187
  int backlog;
188
180
  if (!args[0]->Int32Value(env->context()).To(&backlog)) return;
189
45
  int err = uv_listen(reinterpret_cast<uv_stream_t*>(&wrap->handle_),
190
                      backlog,
191
45
                      OnConnection);
192
90
  args.GetReturnValue().Set(err);
193
}
194
195
196
1438
void PipeWrap::Open(const FunctionCallbackInfo<Value>& args) {
197
1438
  Environment* env = Environment::GetCurrent(args);
198
199
  PipeWrap* wrap;
200
1438
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
201
202
  int fd;
203
5752
  if (!args[0]->Int32Value(env->context()).To(&fd)) return;
204
205
1438
  int err = uv_pipe_open(&wrap->handle_, fd);
206
1438
  wrap->set_fd(fd);
207
208
1438
  if (err != 0)
209
    env->ThrowUVException(err, "uv_pipe_open");
210
}
211
212
213
56
void PipeWrap::Connect(const FunctionCallbackInfo<Value>& args) {
214
56
  Environment* env = Environment::GetCurrent(args);
215
216
  PipeWrap* wrap;
217
56
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
218
219
112
  CHECK(args[0]->IsObject());
220
168
  CHECK(args[1]->IsString());
221
222
112
  Local<Object> req_wrap_obj = args[0].As<Object>();
223
112
  node::Utf8Value name(env->isolate(), args[1]);
224
225
  ConnectWrap* req_wrap =
226
56
      new ConnectWrap(env, req_wrap_obj, AsyncWrap::PROVIDER_PIPECONNECTWRAP);
227
112
  req_wrap->Dispatch(uv_pipe_connect,
228
56
                     &wrap->handle_,
229
                     *name,
230
56
                     AfterConnect);
231
232
112
  args.GetReturnValue().Set(0);  // uv_pipe_connect() doesn't return errors.
233
}
234
235
236
}  // namespace node
237
238

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