GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../src/pipe_wrap.cc Lines: 95 97 97.9 %
Date: 2019-01-07 12:15:22 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 "node_internals.h"
31
#include "connect_wrap.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::HandleScope;
44
using v8::Int32;
45
using v8::Local;
46
using v8::Object;
47
using v8::String;
48
using v8::Value;
49
50
using AsyncHooks = Environment::AsyncHooks;
51
52
53
49
Local<Object> PipeWrap::Instantiate(Environment* env,
54
                                    AsyncWrap* parent,
55
                                    PipeWrap::SocketType type) {
56
49
  EscapableHandleScope handle_scope(env->isolate());
57
98
  AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(parent);
58
98
  CHECK_EQ(false, env->pipe_constructor_template().IsEmpty());
59
  Local<Function> constructor = env->pipe_constructor_template()
60
147
                                    ->GetFunction(env->context())
61
98
                                    .ToLocalChecked();
62
49
  CHECK_EQ(false, constructor.IsEmpty());
63
49
  Local<Value> type_value = Int32::New(env->isolate(), type);
64
  Local<Object> instance =
65
147
      constructor->NewInstance(env->context(), 1, &type_value).ToLocalChecked();
66
49
  return handle_scope.Escape(instance);
67
}
68
69
70
3381
void PipeWrap::Initialize(Local<Object> target,
71
                          Local<Value> unused,
72
                          Local<Context> context,
73
                          void* priv) {
74
3381
  Environment* env = Environment::GetCurrent(context);
75
76
3381
  Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
77
3381
  Local<String> pipeString = FIXED_ONE_BYTE_STRING(env->isolate(), "Pipe");
78
3381
  t->SetClassName(pipeString);
79
6762
  t->InstanceTemplate()->SetInternalFieldCount(1);
80
81
6762
  t->Inherit(LibuvStreamWrap::GetConstructorTemplate(env));
82
83
3381
  env->SetProtoMethod(t, "bind", Bind);
84
3381
  env->SetProtoMethod(t, "listen", Listen);
85
3381
  env->SetProtoMethod(t, "connect", Connect);
86
3381
  env->SetProtoMethod(t, "open", Open);
87
88
#ifdef _WIN32
89
  env->SetProtoMethod(t, "setPendingInstances", SetPendingInstances);
90
#endif
91
92
3381
  env->SetProtoMethod(t, "fchmod", Fchmod);
93
94
  target->Set(env->context(),
95
              pipeString,
96
16905
              t->GetFunction(env->context()).ToLocalChecked()).FromJust();
97
3381
  env->set_pipe_constructor_template(t);
98
99
  // Create FunctionTemplate for PipeConnectWrap.
100
3381
  auto cwt = BaseObject::MakeLazilyInitializedJSTemplate(env);
101
6762
  cwt->Inherit(AsyncWrap::GetConstructorTemplate(env));
102
  Local<String> wrapString =
103
3381
      FIXED_ONE_BYTE_STRING(env->isolate(), "PipeConnectWrap");
104
3381
  cwt->SetClassName(wrapString);
105
  target->Set(env->context(),
106
              wrapString,
107
16905
              cwt->GetFunction(env->context()).ToLocalChecked()).FromJust();
108
109
  // Define constants
110
3381
  Local<Object> constants = Object::New(env->isolate());
111
13524
  NODE_DEFINE_CONSTANT(constants, SOCKET);
112
13524
  NODE_DEFINE_CONSTANT(constants, SERVER);
113
13524
  NODE_DEFINE_CONSTANT(constants, IPC);
114
13524
  NODE_DEFINE_CONSTANT(constants, UV_READABLE);
115
13524
  NODE_DEFINE_CONSTANT(constants, UV_WRITABLE);
116
  target->Set(context,
117
              env->constants_string(),
118
10143
              constants).FromJust();
119
3381
}
120
121
122
3814
void PipeWrap::New(const FunctionCallbackInfo<Value>& args) {
123
  // This constructor should not be exposed to public javascript.
124
  // Therefore we assert that we are not trying to call this as a
125
  // normal function.
126
3814
  CHECK(args.IsConstructCall());
127
7628
  CHECK(args[0]->IsInt32());
128
3814
  Environment* env = Environment::GetCurrent(args);
129
130
11442
  int type_value = args[0].As<Int32>()->Value();
131
3814
  PipeWrap::SocketType type = static_cast<PipeWrap::SocketType>(type_value);
132
133
  bool ipc;
134
  ProviderType provider;
135

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