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-02-19 22:14:06 Branches: 19 36 52.8 %

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::HandleScope;
43
using v8::Int32;
44
using v8::Isolate;
45
using v8::Local;
46
using v8::MaybeLocal;
47
using v8::Object;
48
using v8::String;
49
using v8::Value;
50
51
50
MaybeLocal<Object> PipeWrap::Instantiate(Environment* env,
52
                                         AsyncWrap* parent,
53
                                         PipeWrap::SocketType type) {
54
50
  EscapableHandleScope handle_scope(env->isolate());
55
100
  AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(parent);
56
100
  CHECK_EQ(false, env->pipe_constructor_template().IsEmpty());
57
100
  Local<Function> constructor = env->pipe_constructor_template()
58
150
                                    ->GetFunction(env->context())
59
50
                                    .ToLocalChecked();
60
50
  CHECK_EQ(false, constructor.IsEmpty());
61
50
  Local<Value> type_value = Int32::New(env->isolate(), type);
62
  return handle_scope.EscapeMaybe(
63
150
      constructor->NewInstance(env->context(), 1, &type_value));
64
}
65
66
67
3961
void PipeWrap::Initialize(Local<Object> target,
68
                          Local<Value> unused,
69
                          Local<Context> context,
70
                          void* priv) {
71
3961
  Environment* env = Environment::GetCurrent(context);
72
73
3961
  Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
74
3961
  Local<String> pipeString = FIXED_ONE_BYTE_STRING(env->isolate(), "Pipe");
75
3961
  t->SetClassName(pipeString);
76
7922
  t->InstanceTemplate()
77
3961
    ->SetInternalFieldCount(StreamBase::kStreamBaseFieldCount);
78
79
7922
  t->Inherit(LibuvStreamWrap::GetConstructorTemplate(env));
80
81
3961
  env->SetProtoMethod(t, "bind", Bind);
82
3961
  env->SetProtoMethod(t, "listen", Listen);
83
3961
  env->SetProtoMethod(t, "connect", Connect);
84
3961
  env->SetProtoMethod(t, "open", Open);
85
86
#ifdef _WIN32
87
  env->SetProtoMethod(t, "setPendingInstances", SetPendingInstances);
88
#endif
89
90
3961
  env->SetProtoMethod(t, "fchmod", Fchmod);
91
92
7922
  target->Set(env->context(),
93
              pipeString,
94
19805
              t->GetFunction(env->context()).ToLocalChecked()).Check();
95
3961
  env->set_pipe_constructor_template(t);
96
97
  // Create FunctionTemplate for PipeConnectWrap.
98
3961
  auto cwt = BaseObject::MakeLazilyInitializedJSTemplate(env);
99
7922
  cwt->Inherit(AsyncWrap::GetConstructorTemplate(env));
100
  Local<String> wrapString =
101
3961
      FIXED_ONE_BYTE_STRING(env->isolate(), "PipeConnectWrap");
102
3961
  cwt->SetClassName(wrapString);
103
7922
  target->Set(env->context(),
104
              wrapString,
105
19805
              cwt->GetFunction(env->context()).ToLocalChecked()).Check();
106
107
  // Define constants
108
3961
  Local<Object> constants = Object::New(env->isolate());
109
15844
  NODE_DEFINE_CONSTANT(constants, SOCKET);
110
15844
  NODE_DEFINE_CONSTANT(constants, SERVER);
111
15844
  NODE_DEFINE_CONSTANT(constants, IPC);
112
15844
  NODE_DEFINE_CONSTANT(constants, UV_READABLE);
113
15844
  NODE_DEFINE_CONSTANT(constants, UV_WRITABLE);
114
7922
  target->Set(context,
115
              env->constants_string(),
116
11883
              constants).Check();
117
3961
}
118
119
120
4684
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
4684
  CHECK(args.IsConstructCall());
125
9368
  CHECK(args[0]->IsInt32());
126
4684
  Environment* env = Environment::GetCurrent(args);
127
128
14052
  int type_value = args[0].As<Int32>()->Value();
129
4684
  PipeWrap::SocketType type = static_cast<PipeWrap::SocketType>(type_value);
130
131
  bool ipc;
132
  ProviderType provider;
133

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