Node.js  v8.x
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine
pipe_wrap.cc
Go to the documentation of this file.
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.h"
27 #include "env-inl.h"
28 #include "handle_wrap.h"
29 #include "node.h"
30 #include "node_buffer.h"
31 #include "node_wrap.h"
32 #include "connect_wrap.h"
33 #include "stream_wrap.h"
34 #include "util-inl.h"
35 #include "util.h"
36 
37 namespace node {
38 
39 using v8::Context;
40 using v8::EscapableHandleScope;
41 using v8::Function;
42 using v8::FunctionCallbackInfo;
43 using v8::FunctionTemplate;
44 using v8::HandleScope;
45 using v8::Local;
46 using v8::Object;
47 using v8::String;
48 using v8::Value;
49 
51 
52 
53 Local<Object> PipeWrap::Instantiate(Environment* env, AsyncWrap* parent) {
54  EscapableHandleScope handle_scope(env->isolate());
55  AsyncHooks::InitScope init_scope(env, parent->get_id());
56  CHECK_EQ(false, env->pipe_constructor_template().IsEmpty());
57  Local<Function> constructor = env->pipe_constructor_template()->GetFunction();
58  CHECK_EQ(false, constructor.IsEmpty());
59  Local<Object> instance =
60  constructor->NewInstance(env->context()).ToLocalChecked();
61  return handle_scope.Escape(instance);
62 }
63 
64 
65 void PipeWrap::Initialize(Local<Object> target,
66  Local<Value> unused,
67  Local<Context> context) {
68  Environment* env = Environment::GetCurrent(context);
69 
70  Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
71  Local<String> pipeString = FIXED_ONE_BYTE_STRING(env->isolate(), "Pipe");
72  t->SetClassName(pipeString);
73  t->InstanceTemplate()->SetInternalFieldCount(1);
74 
75  AsyncWrap::AddWrapMethods(env, t);
76 
77  env->SetProtoMethod(t, "close", HandleWrap::Close);
78  env->SetProtoMethod(t, "unref", HandleWrap::Unref);
79  env->SetProtoMethod(t, "ref", HandleWrap::Ref);
80  env->SetProtoMethod(t, "hasRef", HandleWrap::HasRef);
81 
82 #ifdef _WIN32
83  StreamWrap::AddMethods(env, t);
84 #else
85  StreamWrap::AddMethods(env, t, StreamBase::kFlagHasWritev);
86 #endif
87 
88  env->SetProtoMethod(t, "bind", Bind);
89  env->SetProtoMethod(t, "listen", Listen);
90  env->SetProtoMethod(t, "connect", Connect);
91  env->SetProtoMethod(t, "open", Open);
92 
93 #ifdef _WIN32
94  env->SetProtoMethod(t, "setPendingInstances", SetPendingInstances);
95 #endif
96 
97  target->Set(pipeString, t->GetFunction());
98  env->set_pipe_constructor_template(t);
99 
100  // Create FunctionTemplate for PipeConnectWrap.
101  auto constructor = [](const FunctionCallbackInfo<Value>& args) {
102  CHECK(args.IsConstructCall());
103  ClearWrap(args.This());
104  };
105  auto cwt = FunctionTemplate::New(env->isolate(), constructor);
106  cwt->InstanceTemplate()->SetInternalFieldCount(1);
107  AsyncWrap::AddWrapMethods(env, cwt);
108  Local<String> wrapString =
109  FIXED_ONE_BYTE_STRING(env->isolate(), "PipeConnectWrap");
110  cwt->SetClassName(wrapString);
111  target->Set(wrapString, cwt->GetFunction());
112 }
113 
114 
115 void PipeWrap::New(const FunctionCallbackInfo<Value>& args) {
116  // This constructor should not be exposed to public javascript.
117  // Therefore we assert that we are not trying to call this as a
118  // normal function.
119  CHECK(args.IsConstructCall());
120  Environment* env = Environment::GetCurrent(args);
121  new PipeWrap(env, args.This(), args[0]->IsTrue());
122 }
123 
124 
125 PipeWrap::PipeWrap(Environment* env,
126  Local<Object> object,
127  bool ipc)
128  : ConnectionWrap(env,
129  object,
130  AsyncWrap::PROVIDER_PIPEWRAP) {
131  int r = uv_pipe_init(env->event_loop(), &handle_, ipc);
132  CHECK_EQ(r, 0); // How do we proxy this error up to javascript?
133  // Suggestion: uv_pipe_init() returns void.
134  UpdateWriteQueueSize();
135 }
136 
137 
138 void PipeWrap::Bind(const FunctionCallbackInfo<Value>& args) {
139  PipeWrap* wrap;
140  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
141  node::Utf8Value name(args.GetIsolate(), args[0]);
142  int err = uv_pipe_bind(&wrap->handle_, *name);
143  args.GetReturnValue().Set(err);
144 }
145 
146 
147 #ifdef _WIN32
148 void PipeWrap::SetPendingInstances(const FunctionCallbackInfo<Value>& args) {
149  PipeWrap* wrap;
150  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
151  int instances = args[0]->Int32Value();
152  uv_pipe_pending_instances(&wrap->handle_, instances);
153 }
154 #endif
155 
156 
157 void PipeWrap::Listen(const FunctionCallbackInfo<Value>& args) {
158  PipeWrap* wrap;
159  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
160  int backlog = args[0]->Int32Value();
161  int err = uv_listen(reinterpret_cast<uv_stream_t*>(&wrap->handle_),
162  backlog,
163  OnConnection);
164  args.GetReturnValue().Set(err);
165 }
166 
167 
168 void PipeWrap::Open(const FunctionCallbackInfo<Value>& args) {
169  Environment* env = Environment::GetCurrent(args);
170 
171  PipeWrap* wrap;
172  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
173 
174  int fd = args[0]->Int32Value();
175 
176  int err = uv_pipe_open(&wrap->handle_, fd);
177 
178  if (err != 0)
179  env->isolate()->ThrowException(UVException(err, "uv_pipe_open"));
180 }
181 
182 
183 void PipeWrap::Connect(const FunctionCallbackInfo<Value>& args) {
184  Environment* env = Environment::GetCurrent(args);
185 
186  PipeWrap* wrap;
187  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
188 
189  CHECK(args[0]->IsObject());
190  CHECK(args[1]->IsString());
191 
192  Local<Object> req_wrap_obj = args[0].As<Object>();
193  node::Utf8Value name(env->isolate(), args[1]);
194 
195  ConnectWrap* req_wrap =
196  new ConnectWrap(env, req_wrap_obj, AsyncWrap::PROVIDER_PIPECONNECTWRAP);
197  uv_pipe_connect(req_wrap->req(),
198  &wrap->handle_,
199  *name,
200  AfterConnect);
201  req_wrap->Dispatched();
202 
203  args.GetReturnValue().Set(0); // uv_pipe_connect() doesn't return errors.
204 }
205 
206 
207 } // namespace node
208 
NODE_MODULE_CONTEXT_AWARE_BUILTIN(inspector, node::inspector::Agent::InitInspector)
QueryWrap * wrap
Definition: cares_wrap.cc:478
void Initialize(Local< Object > target, Local< Value > unused, Local< Context > context, void *priv)
Definition: node_http2.cc:1172
void Open(const FunctionCallbackInfo< Value > &args)
dtrace t
Definition: v8ustack.d:582
MaybeLocal< Object > New(Isolate *isolate, Local< String > string, enum encoding enc)
Definition: node_buffer.cc:241
node::Environment::AsyncHooks AsyncHooks
Definition: async-wrap.cc:60
Local< Value > UVException(Isolate *isolate, int errorno, const char *syscall, const char *msg, const char *path)
Definition: node.cc:940