Skip to content

Commit 6978e99

Browse files
committed
process: abstract out HandleToStream
Originally contributed by @tjfontaine, but modified to be faster and more generic.
1 parent 0495b70 commit 6978e99

9 files changed

Lines changed: 88 additions & 22 deletions

File tree

node.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
'src/node_string.h',
130130
'src/node_version.h',
131131
'src/node_watchdog.h',
132+
'src/node_wrap.h',
132133
'src/pipe_wrap.h',
133134
'src/queue.h',
134135
'src/tty_wrap.h',

src/node.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ static Persistent<String> enter_symbol;
123123
static Persistent<String> exit_symbol;
124124
static Persistent<String> disposed_symbol;
125125

126+
// Essential for node_wrap.h
127+
Persistent<FunctionTemplate> pipeConstructorTmpl;
128+
Persistent<FunctionTemplate> ttyConstructorTmpl;
129+
Persistent<FunctionTemplate> tcpConstructorTmpl;
130+
Persistent<FunctionTemplate> udpConstructorTmpl;
126131

127132
static bool print_eval = false;
128133
static bool force_repl = false;

src/node_wrap.h

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
#ifndef NODE_WRAP_H
23+
#define NODE_WRAP_H
24+
25+
#include "pipe_wrap.h"
26+
#include "tty_wrap.h"
27+
#include "tcp_wrap.h"
28+
#include "udp_wrap.h"
29+
30+
#include "v8.h"
31+
#include "uv.h"
32+
33+
namespace node {
34+
35+
extern v8::Persistent<v8::FunctionTemplate> pipeConstructorTmpl;
36+
extern v8::Persistent<v8::FunctionTemplate> ttyConstructorTmpl;
37+
extern v8::Persistent<v8::FunctionTemplate> tcpConstructorTmpl;
38+
39+
#define WITH_GENERIC_STREAM(obj, BODY) \
40+
do { \
41+
if (!tcpConstructorTmpl.IsEmpty() && \
42+
tcpConstructorTmpl->HasInstance(obj)) { \
43+
PipeWrap* wrap = PipeWrap::Unwrap(obj); \
44+
BODY \
45+
} else if (!ttyConstructorTmpl.IsEmpty() && \
46+
ttyConstructorTmpl->HasInstance(obj)) { \
47+
TTYWrap* wrap = TTYWrap::Unwrap(obj); \
48+
BODY \
49+
} else if (!pipeConstructorTmpl.IsEmpty() && \
50+
pipeConstructorTmpl->HasInstance(obj)) { \
51+
TCPWrap* wrap = TCPWrap::Unwrap(obj); \
52+
BODY \
53+
} \
54+
} while (0)
55+
56+
inline uv_stream_t* HandleToStream(v8::Local<v8::Object> obj) {
57+
v8::HandleScope scope(node_isolate);
58+
59+
WITH_GENERIC_STREAM(obj, {
60+
return reinterpret_cast<uv_stream_t*>(wrap->UVHandle());
61+
});
62+
63+
return NULL;
64+
}
65+
66+
}
67+
68+
#endif

src/pipe_wrap.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "handle_wrap.h"
2626
#include "stream_wrap.h"
2727
#include "pipe_wrap.h"
28+
#include "node_wrap.h"
2829

2930
namespace node {
3031

@@ -44,7 +45,8 @@ using v8::String;
4445
using v8::TryCatch;
4546
using v8::Value;
4647

47-
Persistent<Function> pipeConstructor;
48+
extern Persistent<FunctionTemplate> pipeConstructorTmpl;
49+
static Persistent<Function> pipeConstructor;
4850

4951
static Persistent<String> onconnection_sym;
5052
static Persistent<String> oncomplete_sym;
@@ -114,6 +116,7 @@ void PipeWrap::Initialize(Handle<Object> target) {
114116
NODE_SET_PROTOTYPE_METHOD(t, "setPendingInstances", SetPendingInstances);
115117
#endif
116118

119+
pipeConstructorTmpl = Persistent<FunctionTemplate>::New(node_isolate, t);
117120
pipeConstructor = Persistent<Function>::New(node_isolate, t->GetFunction());
118121

119122
target->Set(String::NewSymbol("Pipe"), pipeConstructor);

src/pipe_wrap.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
namespace node {
2727

28-
class PipeWrap : StreamWrap {
28+
class PipeWrap : public StreamWrap {
2929
public:
3030
uv_pipe_t* UVHandle();
3131

src/process_wrap.cc

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@
2121

2222
#include "node.h"
2323
#include "handle_wrap.h"
24-
#include "pipe_wrap.h"
25-
#include "tty_wrap.h"
26-
#include "tcp_wrap.h"
27-
#include "udp_wrap.h"
24+
#include "node_wrap.h"
2825

2926
#include <string.h>
3027
#include <stdlib.h>
@@ -116,21 +113,8 @@ class ProcessWrap : public HandleWrap {
116113
PipeWrap::Unwrap(stdio
117114
->Get(String::NewSymbol("handle")).As<Object>())->UVHandle());
118115
} else if (type->Equals(String::NewSymbol("wrap"))) {
119-
uv_stream_t* stream = NULL;
120-
Local<Value> wrapType = stdio->Get(String::NewSymbol("wrapType"));
121-
if (wrapType->Equals(String::NewSymbol("pipe"))) {
122-
stream = reinterpret_cast<uv_stream_t*>(PipeWrap::Unwrap(stdio
123-
->Get(String::NewSymbol("handle")).As<Object>())->UVHandle());
124-
} else if (wrapType->Equals(String::NewSymbol("tty"))) {
125-
stream = reinterpret_cast<uv_stream_t*>(TTYWrap::Unwrap(stdio
126-
->Get(String::NewSymbol("handle")).As<Object>())->UVHandle());
127-
} else if (wrapType->Equals(String::NewSymbol("tcp"))) {
128-
stream = reinterpret_cast<uv_stream_t*>(TCPWrap::Unwrap(stdio
129-
->Get(String::NewSymbol("handle")).As<Object>())->UVHandle());
130-
} else if (wrapType->Equals(String::NewSymbol("udp"))) {
131-
stream = reinterpret_cast<uv_stream_t*>(UDPWrap::Unwrap(stdio
132-
->Get(String::NewSymbol("handle")).As<Object>())->UVHandle());
133-
}
116+
uv_stream_t* stream = HandleToStream(
117+
stdio->Get(String::NewSymbol("handle")).As<Object>());
134118
assert(stream != NULL);
135119

136120
options->stdio[i].flags = UV_INHERIT_STREAM;

src/tcp_wrap.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "handle_wrap.h"
2626
#include "stream_wrap.h"
2727
#include "tcp_wrap.h"
28+
#include "node_wrap.h"
2829

2930
#include <stdlib.h>
3031

@@ -120,6 +121,7 @@ void TCPWrap::Initialize(Handle<Object> target) {
120121
NODE_SET_PROTOTYPE_METHOD(t, "setSimultaneousAccepts", SetSimultaneousAccepts);
121122
#endif
122123

124+
tcpConstructorTmpl = Persistent<FunctionTemplate>::New(node_isolate, t);
123125
tcpConstructor = Persistent<Function>::New(node_isolate, t->GetFunction());
124126

125127
onconnection_sym = NODE_PSYMBOL("onconnection");

src/tty_wrap.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "handle_wrap.h"
2626
#include "stream_wrap.h"
2727
#include "tty_wrap.h"
28+
#include "node_wrap.h"
2829

2930
namespace node {
3031

@@ -81,6 +82,8 @@ void TTYWrap::Initialize(Handle<Object> target) {
8182
NODE_SET_METHOD(target, "isTTY", IsTTY);
8283
NODE_SET_METHOD(target, "guessHandleType", GuessHandleType);
8384

85+
ttyConstructorTmpl = Persistent<FunctionTemplate>::New(node_isolate, t);
86+
8487
target->Set(String::NewSymbol("TTY"), t->GetFunction());
8588
}
8689

src/tty_wrap.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ using v8::Value;
3434
using v8::Arguments;
3535

3636

37-
class TTYWrap : StreamWrap {
37+
class TTYWrap : public StreamWrap {
3838
public:
3939
static void Initialize(Handle<Object> target);
4040
static TTYWrap* Unwrap(Local<Object> obj);

0 commit comments

Comments
 (0)