Skip to content

Commit f01b241

Browse files
committed
add wrapper for uv_spawn
process.binding('process_wrap')
1 parent 874260b commit f01b241

6 files changed

Lines changed: 431 additions & 119 deletions

File tree

src/node_extensions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ NODE_EXT_LIST_ITEM(node_tcp_wrap)
4747
NODE_EXT_LIST_ITEM(node_pipe_wrap)
4848
NODE_EXT_LIST_ITEM(node_cares_wrap)
4949
NODE_EXT_LIST_ITEM(node_stdio_wrap)
50+
NODE_EXT_LIST_ITEM(node_process_wrap)
5051

5152
NODE_EXT_LIST_END
5253

src/pipe_wrap.cc

Lines changed: 131 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <req_wrap.h>
44
#include <handle_wrap.h>
55
#include <stream_wrap.h>
6+
#include <pipe_wrap.h>
67

78
#define UNWRAP \
89
assert(!args.Holder().IsEmpty()); \
@@ -37,176 +38,187 @@ Persistent<Function> pipeConstructor;
3738
typedef class ReqWrap<uv_connect_t> ConnectWrap;
3839

3940

40-
class PipeWrap : StreamWrap {
41-
public:
41+
uv_pipe_t* PipeWrap::UVHandle() {
42+
return &handle_;
43+
}
4244

43-
static void Initialize(Handle<Object> target) {
44-
StreamWrap::Initialize(target);
4545

46-
HandleScope scope;
46+
PipeWrap* PipeWrap::Unwrap(Local<Object> obj) {
47+
assert(!obj.IsEmpty());
48+
assert(obj->InternalFieldCount() > 0);
49+
return static_cast<PipeWrap*>(obj->GetPointerFromInternalField(0));
50+
}
4751

48-
Local<FunctionTemplate> t = FunctionTemplate::New(New);
49-
t->SetClassName(String::NewSymbol("Pipe"));
5052

51-
t->InstanceTemplate()->SetInternalFieldCount(1);
53+
void PipeWrap::Initialize(Handle<Object> target) {
54+
StreamWrap::Initialize(target);
5255

53-
NODE_SET_PROTOTYPE_METHOD(t, "close", HandleWrap::Close);
56+
HandleScope scope;
5457

55-
NODE_SET_PROTOTYPE_METHOD(t, "readStart", StreamWrap::ReadStart);
56-
NODE_SET_PROTOTYPE_METHOD(t, "readStop", StreamWrap::ReadStop);
57-
NODE_SET_PROTOTYPE_METHOD(t, "write", StreamWrap::Write);
58-
NODE_SET_PROTOTYPE_METHOD(t, "shutdown", StreamWrap::Shutdown);
58+
Local<FunctionTemplate> t = FunctionTemplate::New(New);
59+
t->SetClassName(String::NewSymbol("Pipe"));
5960

60-
NODE_SET_PROTOTYPE_METHOD(t, "bind", Bind);
61-
NODE_SET_PROTOTYPE_METHOD(t, "listen", Listen);
62-
NODE_SET_PROTOTYPE_METHOD(t, "connect", Connect);
61+
t->InstanceTemplate()->SetInternalFieldCount(1);
6362

64-
pipeConstructor = Persistent<Function>::New(t->GetFunction());
63+
NODE_SET_PROTOTYPE_METHOD(t, "close", HandleWrap::Close);
6564

66-
target->Set(String::NewSymbol("Pipe"), pipeConstructor);
67-
}
65+
NODE_SET_PROTOTYPE_METHOD(t, "readStart", StreamWrap::ReadStart);
66+
NODE_SET_PROTOTYPE_METHOD(t, "readStop", StreamWrap::ReadStop);
67+
NODE_SET_PROTOTYPE_METHOD(t, "write", StreamWrap::Write);
68+
NODE_SET_PROTOTYPE_METHOD(t, "shutdown", StreamWrap::Shutdown);
6869

69-
private:
70-
static Handle<Value> New(const Arguments& args) {
71-
// This constructor should not be exposed to public javascript.
72-
// Therefore we assert that we are not trying to call this as a
73-
// normal function.
74-
assert(args.IsConstructCall());
70+
NODE_SET_PROTOTYPE_METHOD(t, "bind", Bind);
71+
NODE_SET_PROTOTYPE_METHOD(t, "listen", Listen);
72+
NODE_SET_PROTOTYPE_METHOD(t, "connect", Connect);
7573

76-
HandleScope scope;
77-
PipeWrap* wrap = new PipeWrap(args.This());
78-
assert(wrap);
74+
pipeConstructor = Persistent<Function>::New(t->GetFunction());
7975

80-
return scope.Close(args.This());
81-
}
76+
target->Set(String::NewSymbol("Pipe"), pipeConstructor);
77+
}
8278

83-
PipeWrap(Handle<Object> object) : StreamWrap(object,
84-
(uv_stream_t*) &handle_) {
85-
int r = uv_pipe_init(&handle_);
86-
assert(r == 0); // How do we proxy this error up to javascript?
87-
// Suggestion: uv_pipe_init() returns void.
88-
handle_.data = reinterpret_cast<void*>(this);
89-
UpdateWriteQueueSize();
90-
}
9179

92-
static Handle<Value> Bind(const Arguments& args) {
93-
HandleScope scope;
80+
Handle<Value> PipeWrap::New(const Arguments& args) {
81+
// This constructor should not be exposed to public javascript.
82+
// Therefore we assert that we are not trying to call this as a
83+
// normal function.
84+
assert(args.IsConstructCall());
9485

95-
UNWRAP
86+
HandleScope scope;
87+
PipeWrap* wrap = new PipeWrap(args.This());
88+
assert(wrap);
9689

97-
String::AsciiValue name(args[0]->ToString());
90+
return scope.Close(args.This());
91+
}
9892

99-
int r = uv_pipe_bind(&wrap->handle_, *name);
10093

101-
// Error starting the pipe.
102-
if (r) SetErrno(uv_last_error().code);
94+
PipeWrap::PipeWrap(Handle<Object> object) : StreamWrap(object,
95+
(uv_stream_t*) &handle_) {
96+
int r = uv_pipe_init(&handle_);
97+
assert(r == 0); // How do we proxy this error up to javascript?
98+
// Suggestion: uv_pipe_init() returns void.
99+
handle_.data = reinterpret_cast<void*>(this);
100+
UpdateWriteQueueSize();
101+
}
103102

104-
return scope.Close(Integer::New(r));
105-
}
106103

107-
static Handle<Value> Listen(const Arguments& args) {
108-
HandleScope scope;
104+
Handle<Value> PipeWrap::Bind(const Arguments& args) {
105+
HandleScope scope;
109106

110-
UNWRAP
107+
UNWRAP
111108

112-
int backlog = args[0]->Int32Value();
109+
String::AsciiValue name(args[0]->ToString());
113110

114-
int r = uv_listen((uv_stream_t*)&wrap->handle_, backlog, OnConnection);
111+
int r = uv_pipe_bind(&wrap->handle_, *name);
115112

116-
// Error starting the pipe.
117-
if (r) SetErrno(uv_last_error().code);
113+
// Error starting the pipe.
114+
if (r) SetErrno(uv_last_error().code);
118115

119-
return scope.Close(Integer::New(r));
120-
}
116+
return scope.Close(Integer::New(r));
117+
}
121118

122-
// TODO maybe share with TCPWrap?
123-
static void OnConnection(uv_stream_t* handle, int status) {
124-
HandleScope scope;
125119

126-
PipeWrap* wrap = static_cast<PipeWrap*>(handle->data);
127-
assert(&wrap->handle_ == (uv_pipe_t*)handle);
120+
Handle<Value> PipeWrap::Listen(const Arguments& args) {
121+
HandleScope scope;
128122

129-
// We should not be getting this callback if someone as already called
130-
// uv_close() on the handle.
131-
assert(wrap->object_.IsEmpty() == false);
123+
UNWRAP
132124

133-
if (status != 0) {
134-
// TODO Handle server error (set errno and call onconnection with NULL)
135-
assert(0);
136-
return;
137-
}
125+
int backlog = args[0]->Int32Value();
138126

139-
// Instanciate the client javascript object and handle.
140-
Local<Object> client_obj = pipeConstructor->NewInstance();
127+
int r = uv_listen((uv_stream_t*)&wrap->handle_, backlog, OnConnection);
141128

142-
// Unwrap the client javascript object.
143-
assert(client_obj->InternalFieldCount() > 0);
144-
PipeWrap* client_wrap =
145-
static_cast<PipeWrap*>(client_obj->GetPointerFromInternalField(0));
129+
// Error starting the pipe.
130+
if (r) SetErrno(uv_last_error().code);
146131

147-
int r = uv_accept(handle, (uv_stream_t*)&client_wrap->handle_);
132+
return scope.Close(Integer::New(r));
133+
}
148134

149-
// uv_accept should always work.
150-
assert(r == 0);
151135

152-
// Successful accept. Call the onconnection callback in JavaScript land.
153-
Local<Value> argv[1] = { client_obj };
154-
MakeCallback(wrap->object_, "onconnection", 1, argv);
136+
// TODO maybe share with TCPWrap?
137+
void PipeWrap::OnConnection(uv_stream_t* handle, int status) {
138+
HandleScope scope;
139+
140+
PipeWrap* wrap = static_cast<PipeWrap*>(handle->data);
141+
assert(&wrap->handle_ == (uv_pipe_t*)handle);
142+
143+
// We should not be getting this callback if someone as already called
144+
// uv_close() on the handle.
145+
assert(wrap->object_.IsEmpty() == false);
146+
147+
if (status != 0) {
148+
// TODO Handle server error (set errno and call onconnection with NULL)
149+
assert(0);
150+
return;
155151
}
156152

157-
// TODO Maybe share this with TCPWrap?
158-
static void AfterConnect(uv_connect_t* req, int status) {
159-
ConnectWrap* req_wrap = (ConnectWrap*) req->data;
160-
PipeWrap* wrap = (PipeWrap*) req->handle->data;
153+
// Instanciate the client javascript object and handle.
154+
Local<Object> client_obj = pipeConstructor->NewInstance();
161155

162-
HandleScope scope;
156+
// Unwrap the client javascript object.
157+
assert(client_obj->InternalFieldCount() > 0);
158+
PipeWrap* client_wrap =
159+
static_cast<PipeWrap*>(client_obj->GetPointerFromInternalField(0));
163160

164-
// The wrap and request objects should still be there.
165-
assert(req_wrap->object_.IsEmpty() == false);
166-
assert(wrap->object_.IsEmpty() == false);
161+
int r = uv_accept(handle, (uv_stream_t*)&client_wrap->handle_);
167162

168-
if (status) {
169-
SetErrno(uv_last_error().code);
170-
}
163+
// uv_accept should always work.
164+
assert(r == 0);
171165

172-
Local<Value> argv[3] = {
173-
Integer::New(status),
174-
Local<Value>::New(wrap->object_),
175-
Local<Value>::New(req_wrap->object_)
176-
};
166+
// Successful accept. Call the onconnection callback in JavaScript land.
167+
Local<Value> argv[1] = { client_obj };
168+
MakeCallback(wrap->object_, "onconnection", 1, argv);
169+
}
177170

178-
MakeCallback(req_wrap->object_, "oncomplete", 3, argv);
171+
// TODO Maybe share this with TCPWrap?
172+
void PipeWrap::AfterConnect(uv_connect_t* req, int status) {
173+
ConnectWrap* req_wrap = (ConnectWrap*) req->data;
174+
PipeWrap* wrap = (PipeWrap*) req->handle->data;
179175

180-
delete req_wrap;
176+
HandleScope scope;
177+
178+
// The wrap and request objects should still be there.
179+
assert(req_wrap->object_.IsEmpty() == false);
180+
assert(wrap->object_.IsEmpty() == false);
181+
182+
if (status) {
183+
SetErrno(uv_last_error().code);
181184
}
182185

183-
static Handle<Value> Connect(const Arguments& args) {
184-
HandleScope scope;
186+
Local<Value> argv[3] = {
187+
Integer::New(status),
188+
Local<Value>::New(wrap->object_),
189+
Local<Value>::New(req_wrap->object_)
190+
};
185191

186-
UNWRAP
192+
MakeCallback(req_wrap->object_, "oncomplete", 3, argv);
187193

188-
String::AsciiValue name(args[0]->ToString());
194+
delete req_wrap;
195+
}
189196

190-
ConnectWrap* req_wrap = new ConnectWrap();
191197

192-
int r = uv_pipe_connect(&req_wrap->req_,
193-
&wrap->handle_,
194-
*name,
195-
AfterConnect);
198+
Handle<Value> PipeWrap::Connect(const Arguments& args) {
199+
HandleScope scope;
196200

197-
req_wrap->Dispatched();
201+
UNWRAP
198202

199-
if (r) {
200-
SetErrno(uv_last_error().code);
201-
delete req_wrap;
202-
return scope.Close(v8::Null());
203-
} else {
204-
return scope.Close(req_wrap->object_);
205-
}
206-
}
203+
String::AsciiValue name(args[0]->ToString());
204+
205+
ConnectWrap* req_wrap = new ConnectWrap();
207206

208-
uv_pipe_t handle_;
209-
};
207+
int r = uv_pipe_connect(&req_wrap->req_,
208+
&wrap->handle_,
209+
*name,
210+
AfterConnect);
211+
212+
req_wrap->Dispatched();
213+
214+
if (r) {
215+
SetErrno(uv_last_error().code);
216+
delete req_wrap;
217+
return scope.Close(v8::Null());
218+
} else {
219+
return scope.Close(req_wrap->object_);
220+
}
221+
}
210222

211223

212224
} // namespace node

src/pipe_wrap.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef PIPE_WRAP_H_
2+
#define PIPE_WRAP_H_
3+
#include <stream_wrap.h>
4+
5+
namespace node {
6+
7+
class PipeWrap : StreamWrap {
8+
public:
9+
uv_pipe_t* UVHandle();
10+
11+
static PipeWrap* Unwrap(v8::Local<v8::Object> obj);
12+
static void Initialize(v8::Handle<v8::Object> target);
13+
14+
private:
15+
PipeWrap(v8::Handle<v8::Object> object);
16+
17+
static v8::Handle<v8::Value> New(const v8::Arguments& args);
18+
static v8::Handle<v8::Value> Bind(const v8::Arguments& args);
19+
static v8::Handle<v8::Value> Listen(const v8::Arguments& args);
20+
static v8::Handle<v8::Value> Connect(const v8::Arguments& args);
21+
22+
static void OnConnection(uv_stream_t* handle, int status);
23+
static void AfterConnect(uv_connect_t* req, int status);
24+
25+
uv_pipe_t handle_;
26+
};
27+
28+
29+
} // namespace node
30+
31+
32+
#endif // PIPE_WRAP_H_

0 commit comments

Comments
 (0)