diff options
-rw-r--r-- | node.gyp | 1 | ||||
-rw-r--r-- | src/node.cc | 5 | ||||
-rw-r--r-- | src/node_wrap.h | 68 | ||||
-rw-r--r-- | src/pipe_wrap.cc | 5 | ||||
-rw-r--r-- | src/pipe_wrap.h | 2 | ||||
-rw-r--r-- | src/process_wrap.cc | 22 | ||||
-rw-r--r-- | src/tcp_wrap.cc | 2 | ||||
-rw-r--r-- | src/tty_wrap.cc | 3 | ||||
-rw-r--r-- | src/tty_wrap.h | 2 |
9 files changed, 88 insertions, 22 deletions
@@ -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', diff --git a/src/node.cc b/src/node.cc index ee364bd3a6..07b42f4ad3 100644 --- a/src/node.cc +++ b/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; diff --git a/src/node_wrap.h b/src/node_wrap.h new file mode 100644 index 0000000000..6b68e4e78c --- /dev/null +++ b/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 diff --git a/src/pipe_wrap.cc b/src/pipe_wrap.cc index 6449eebab9..6639b8d66c 100644 --- a/src/pipe_wrap.cc +++ b/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); diff --git a/src/pipe_wrap.h b/src/pipe_wrap.h index d5241e08f3..f1e12eb753 100644 --- a/src/pipe_wrap.h +++ b/src/pipe_wrap.h @@ -25,7 +25,7 @@ namespace node { -class PipeWrap : StreamWrap { +class PipeWrap : public StreamWrap { public: uv_pipe_t* UVHandle(); diff --git a/src/process_wrap.cc b/src/process_wrap.cc index 02984976d2..4b14464a34 100644 --- a/src/process_wrap.cc +++ b/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; diff --git a/src/tcp_wrap.cc b/src/tcp_wrap.cc index 43199637ad..e0556aa025 100644 --- a/src/tcp_wrap.cc +++ b/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"); diff --git a/src/tty_wrap.cc b/src/tty_wrap.cc index 1e70a4ff2a..f96ab0cdc3 100644 --- a/src/tty_wrap.cc +++ b/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()); } diff --git a/src/tty_wrap.h b/src/tty_wrap.h index 4a3341a660..f20fea6ad6 100644 --- a/src/tty_wrap.h +++ b/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); |