Browse Source

process: abstract out HandleToStream

Originally contributed by @tjfontaine, but modified to be faster and
more generic.
v0.11.3-release
Fedor Indutny 12 years ago
parent
commit
6978e998ee
  1. 1
      node.gyp
  2. 5
      src/node.cc
  3. 68
      src/node_wrap.h
  4. 5
      src/pipe_wrap.cc
  5. 2
      src/pipe_wrap.h
  6. 22
      src/process_wrap.cc
  7. 2
      src/tcp_wrap.cc
  8. 3
      src/tty_wrap.cc
  9. 2
      src/tty_wrap.h

1
node.gyp

@ -129,6 +129,7 @@
'src/node_string.h',
'src/node_version.h',
'src/node_watchdog.h',
'src/node_wrap.h',
'src/pipe_wrap.h',
'src/queue.h',
'src/tty_wrap.h',

5
src/node.cc

@ -123,6 +123,11 @@ static Persistent<String> enter_symbol;
static Persistent<String> exit_symbol;
static Persistent<String> disposed_symbol;
// Essential for node_wrap.h
Persistent<FunctionTemplate> pipeConstructorTmpl;
Persistent<FunctionTemplate> ttyConstructorTmpl;
Persistent<FunctionTemplate> tcpConstructorTmpl;
Persistent<FunctionTemplate> udpConstructorTmpl;
static bool print_eval = false;
static bool force_repl = false;

68
src/node_wrap.h

@ -0,0 +1,68 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
#ifndef NODE_WRAP_H
#define NODE_WRAP_H
#include "pipe_wrap.h"
#include "tty_wrap.h"
#include "tcp_wrap.h"
#include "udp_wrap.h"
#include "v8.h"
#include "uv.h"
namespace node {
extern v8::Persistent<v8::FunctionTemplate> pipeConstructorTmpl;
extern v8::Persistent<v8::FunctionTemplate> ttyConstructorTmpl;
extern v8::Persistent<v8::FunctionTemplate> tcpConstructorTmpl;
#define WITH_GENERIC_STREAM(obj, BODY) \
do { \
if (!tcpConstructorTmpl.IsEmpty() && \
tcpConstructorTmpl->HasInstance(obj)) { \
PipeWrap* wrap = PipeWrap::Unwrap(obj); \
BODY \
} else if (!ttyConstructorTmpl.IsEmpty() && \
ttyConstructorTmpl->HasInstance(obj)) { \
TTYWrap* wrap = TTYWrap::Unwrap(obj); \
BODY \
} else if (!pipeConstructorTmpl.IsEmpty() && \
pipeConstructorTmpl->HasInstance(obj)) { \
TCPWrap* wrap = TCPWrap::Unwrap(obj); \
BODY \
} \
} while (0)
inline uv_stream_t* HandleToStream(v8::Local<v8::Object> obj) {
v8::HandleScope scope(node_isolate);
WITH_GENERIC_STREAM(obj, {
return reinterpret_cast<uv_stream_t*>(wrap->UVHandle());
});
return NULL;
}
}
#endif

5
src/pipe_wrap.cc

@ -25,6 +25,7 @@
#include "handle_wrap.h"
#include "stream_wrap.h"
#include "pipe_wrap.h"
#include "node_wrap.h"
namespace node {
@ -44,7 +45,8 @@ using v8::String;
using v8::TryCatch;
using v8::Value;
Persistent<Function> pipeConstructor;
extern Persistent<FunctionTemplate> pipeConstructorTmpl;
static Persistent<Function> pipeConstructor;
static Persistent<String> onconnection_sym;
static Persistent<String> oncomplete_sym;
@ -114,6 +116,7 @@ void PipeWrap::Initialize(Handle<Object> target) {
NODE_SET_PROTOTYPE_METHOD(t, "setPendingInstances", SetPendingInstances);
#endif
pipeConstructorTmpl = Persistent<FunctionTemplate>::New(node_isolate, t);
pipeConstructor = Persistent<Function>::New(node_isolate, t->GetFunction());
target->Set(String::NewSymbol("Pipe"), pipeConstructor);

2
src/pipe_wrap.h

@ -25,7 +25,7 @@
namespace node {
class PipeWrap : StreamWrap {
class PipeWrap : public StreamWrap {
public:
uv_pipe_t* UVHandle();

22
src/process_wrap.cc

@ -21,10 +21,7 @@
#include "node.h"
#include "handle_wrap.h"
#include "pipe_wrap.h"
#include "tty_wrap.h"
#include "tcp_wrap.h"
#include "udp_wrap.h"
#include "node_wrap.h"
#include <string.h>
#include <stdlib.h>
@ -116,21 +113,8 @@ class ProcessWrap : public HandleWrap {
PipeWrap::Unwrap(stdio
->Get(String::NewSymbol("handle")).As<Object>())->UVHandle());
} else if (type->Equals(String::NewSymbol("wrap"))) {
uv_stream_t* stream = NULL;
Local<Value> wrapType = stdio->Get(String::NewSymbol("wrapType"));
if (wrapType->Equals(String::NewSymbol("pipe"))) {
stream = reinterpret_cast<uv_stream_t*>(PipeWrap::Unwrap(stdio
->Get(String::NewSymbol("handle")).As<Object>())->UVHandle());
} else if (wrapType->Equals(String::NewSymbol("tty"))) {
stream = reinterpret_cast<uv_stream_t*>(TTYWrap::Unwrap(stdio
->Get(String::NewSymbol("handle")).As<Object>())->UVHandle());
} else if (wrapType->Equals(String::NewSymbol("tcp"))) {
stream = reinterpret_cast<uv_stream_t*>(TCPWrap::Unwrap(stdio
->Get(String::NewSymbol("handle")).As<Object>())->UVHandle());
} else if (wrapType->Equals(String::NewSymbol("udp"))) {
stream = reinterpret_cast<uv_stream_t*>(UDPWrap::Unwrap(stdio
->Get(String::NewSymbol("handle")).As<Object>())->UVHandle());
}
uv_stream_t* stream = HandleToStream(
stdio->Get(String::NewSymbol("handle")).As<Object>());
assert(stream != NULL);
options->stdio[i].flags = UV_INHERIT_STREAM;

2
src/tcp_wrap.cc

@ -25,6 +25,7 @@
#include "handle_wrap.h"
#include "stream_wrap.h"
#include "tcp_wrap.h"
#include "node_wrap.h"
#include <stdlib.h>
@ -120,6 +121,7 @@ void TCPWrap::Initialize(Handle<Object> target) {
NODE_SET_PROTOTYPE_METHOD(t, "setSimultaneousAccepts", SetSimultaneousAccepts);
#endif
tcpConstructorTmpl = Persistent<FunctionTemplate>::New(node_isolate, t);
tcpConstructor = Persistent<Function>::New(node_isolate, t->GetFunction());
onconnection_sym = NODE_PSYMBOL("onconnection");

3
src/tty_wrap.cc

@ -25,6 +25,7 @@
#include "handle_wrap.h"
#include "stream_wrap.h"
#include "tty_wrap.h"
#include "node_wrap.h"
namespace node {
@ -81,6 +82,8 @@ void TTYWrap::Initialize(Handle<Object> target) {
NODE_SET_METHOD(target, "isTTY", IsTTY);
NODE_SET_METHOD(target, "guessHandleType", GuessHandleType);
ttyConstructorTmpl = Persistent<FunctionTemplate>::New(node_isolate, t);
target->Set(String::NewSymbol("TTY"), t->GetFunction());
}

2
src/tty_wrap.h

@ -34,7 +34,7 @@ using v8::Value;
using v8::Arguments;
class TTYWrap : StreamWrap {
class TTYWrap : public StreamWrap {
public:
static void Initialize(Handle<Object> target);
static TTYWrap* Unwrap(Local<Object> obj);

Loading…
Cancel
Save