From 7bdd05bd66607886d633332e1397550d9b8d9b39 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 13 Feb 2013 16:09:03 +0100 Subject: [PATCH] stream_wrap, udp_wrap: add read-only fd property Expose the file descriptor as a read-only property on the internal handle objects. Intended for debugging purposes, not part of the API proper. The property is always null on Windows. Fixes #4754. --- src/pipe_wrap.cc | 28 +++++++++++++++++++--------- src/stream_wrap.cc | 35 ++++++++++++++++++++++++----------- src/stream_wrap.h | 3 +++ src/tcp_wrap.cc | 12 +++++++++++- src/tty_wrap.cc | 26 ++++++++++++++++++-------- src/udp_wrap.cc | 24 ++++++++++++++++++++++++ src/udp_wrap.h | 2 ++ 7 files changed, 101 insertions(+), 29 deletions(-) diff --git a/src/pipe_wrap.cc b/src/pipe_wrap.cc index 39897af..149c4f2 100644 --- a/src/pipe_wrap.cc +++ b/src/pipe_wrap.cc @@ -28,20 +28,21 @@ namespace node { -using v8::Object; +using v8::Arguments; +using v8::Boolean; +using v8::Context; +using v8::Function; +using v8::FunctionTemplate; using v8::Handle; +using v8::HandleScope; +using v8::Integer; using v8::Local; +using v8::Object; using v8::Persistent; -using v8::Value; -using v8::HandleScope; -using v8::FunctionTemplate; +using v8::PropertyAttribute; using v8::String; -using v8::Function; using v8::TryCatch; -using v8::Context; -using v8::Arguments; -using v8::Integer; -using v8::Boolean; +using v8::Value; Persistent pipeConstructor; @@ -82,6 +83,15 @@ void PipeWrap::Initialize(Handle target) { t->InstanceTemplate()->SetInternalFieldCount(1); + enum PropertyAttribute attributes = + static_cast(v8::ReadOnly | v8::DontDelete); + t->InstanceTemplate()->SetAccessor(String::New("fd"), + StreamWrap::GetFD, + NULL, + Handle(), + v8::DEFAULT, + attributes); + NODE_SET_PROTOTYPE_METHOD(t, "close", HandleWrap::Close); NODE_SET_PROTOTYPE_METHOD(t, "unref", HandleWrap::Unref); NODE_SET_PROTOTYPE_METHOD(t, "ref", HandleWrap::Ref); diff --git a/src/stream_wrap.cc b/src/stream_wrap.cc index e6756e1..fc9218e 100644 --- a/src/stream_wrap.cc +++ b/src/stream_wrap.cc @@ -38,22 +38,22 @@ namespace node { -using v8::Object; +using v8::AccessorInfo; +using v8::Arguments; +using v8::Context; +using v8::Exception; +using v8::Function; +using v8::FunctionTemplate; using v8::Handle; +using v8::HandleScope; +using v8::Integer; using v8::Local; +using v8::Number; +using v8::Object; using v8::Persistent; -using v8::Value; -using v8::HandleScope; -using v8::FunctionTemplate; using v8::String; -using v8::Function; using v8::TryCatch; -using v8::Context; -using v8::Arguments; -using v8::Integer; -using v8::Number; -using v8::Exception; - +using v8::Value; typedef class ReqWrap ShutdownWrap; @@ -117,6 +117,19 @@ StreamWrap::StreamWrap(Handle object, uv_stream_t* stream) } +Handle StreamWrap::GetFD(Local, const AccessorInfo& args) { +#if defined(_WIN32) + return v8::Null(node_isolate); +#else + HandleScope scope; + UNWRAP(StreamWrap) + int fd = -1; + if (wrap != NULL && wrap->stream_ != NULL) fd = wrap->stream_->io_watcher.fd; + return scope.Close(Integer::New(fd, node_isolate)); +#endif +} + + void StreamWrap::SetHandle(uv_handle_t* h) { HandleWrap::SetHandle(h); stream_ = reinterpret_cast(h); diff --git a/src/stream_wrap.h b/src/stream_wrap.h index c51083c..77bd234 100644 --- a/src/stream_wrap.h +++ b/src/stream_wrap.h @@ -42,6 +42,9 @@ class StreamWrap : public HandleWrap { static void Initialize(v8::Handle target); + static v8::Handle GetFD(v8::Local, + const v8::AccessorInfo&); + // JavaScript functions static v8::Handle ReadStart(const v8::Arguments& args); static v8::Handle ReadStop(const v8::Arguments& args); diff --git a/src/tcp_wrap.cc b/src/tcp_wrap.cc index 87a3e4d..607aad1 100644 --- a/src/tcp_wrap.cc +++ b/src/tcp_wrap.cc @@ -39,9 +39,10 @@ using v8::Handle; using v8::HandleScope; using v8::Integer; using v8::Local; -using v8::Object; using v8::Null; +using v8::Object; using v8::Persistent; +using v8::PropertyAttribute; using v8::String; using v8::TryCatch; using v8::Undefined; @@ -80,6 +81,15 @@ void TCPWrap::Initialize(Handle target) { t->InstanceTemplate()->SetInternalFieldCount(1); + enum PropertyAttribute attributes = + static_cast(v8::ReadOnly | v8::DontDelete); + t->InstanceTemplate()->SetAccessor(String::New("fd"), + StreamWrap::GetFD, + NULL, + Handle(), + v8::DEFAULT, + attributes); + NODE_SET_PROTOTYPE_METHOD(t, "close", HandleWrap::Close); NODE_SET_PROTOTYPE_METHOD(t, "ref", HandleWrap::Ref); diff --git a/src/tty_wrap.cc b/src/tty_wrap.cc index 1751105..f1892a4 100644 --- a/src/tty_wrap.cc +++ b/src/tty_wrap.cc @@ -28,20 +28,21 @@ namespace node { -using v8::Object; +using v8::Arguments; +using v8::Context; +using v8::Function; +using v8::FunctionTemplate; using v8::Handle; +using v8::HandleScope; +using v8::Integer; using v8::Local; +using v8::Object; using v8::Persistent; -using v8::Value; -using v8::HandleScope; -using v8::FunctionTemplate; +using v8::PropertyAttribute; using v8::String; -using v8::Function; using v8::TryCatch; -using v8::Context; -using v8::Arguments; -using v8::Integer; using v8::Undefined; +using v8::Value; void TTYWrap::Initialize(Handle target) { @@ -54,6 +55,15 @@ void TTYWrap::Initialize(Handle target) { t->InstanceTemplate()->SetInternalFieldCount(1); + enum PropertyAttribute attributes = + static_cast(v8::ReadOnly | v8::DontDelete); + t->InstanceTemplate()->SetAccessor(String::New("fd"), + StreamWrap::GetFD, + NULL, + Handle(), + v8::DEFAULT, + attributes); + NODE_SET_PROTOTYPE_METHOD(t, "close", HandleWrap::Close); NODE_SET_PROTOTYPE_METHOD(t, "unref", HandleWrap::Unref); diff --git a/src/udp_wrap.cc b/src/udp_wrap.cc index af525b3..d7d1efd 100644 --- a/src/udp_wrap.cc +++ b/src/udp_wrap.cc @@ -33,6 +33,7 @@ namespace node { +using v8::AccessorInfo; using v8::Arguments; using v8::Function; using v8::FunctionTemplate; @@ -42,6 +43,7 @@ using v8::Integer; using v8::Local; using v8::Object; using v8::Persistent; +using v8::PropertyAttribute; using v8::String; using v8::Value; @@ -91,6 +93,15 @@ void UDPWrap::Initialize(Handle target) { t->InstanceTemplate()->SetInternalFieldCount(1); t->SetClassName(String::NewSymbol("UDP")); + enum PropertyAttribute attributes = + static_cast(v8::ReadOnly | v8::DontDelete); + t->InstanceTemplate()->SetAccessor(String::New("fd"), + UDPWrap::GetFD, + NULL, + Handle(), + v8::DEFAULT, + attributes); + NODE_SET_PROTOTYPE_METHOD(t, "bind", Bind); NODE_SET_PROTOTYPE_METHOD(t, "send", Send); NODE_SET_PROTOTYPE_METHOD(t, "bind6", Bind6); @@ -124,6 +135,19 @@ Handle UDPWrap::New(const Arguments& args) { return scope.Close(args.This()); } + +Handle UDPWrap::GetFD(Local, const AccessorInfo& args) { +#if defined(_WIN32) + return v8::Null(node_isolate); +#else + HandleScope scope; + UNWRAP(UDPWrap) + int fd = (wrap == NULL) ? -1 : wrap->handle_.io_watcher.fd; + return scope.Close(Integer::New(fd, node_isolate)); +#endif +} + + Handle UDPWrap::DoBind(const Arguments& args, int family) { HandleScope scope; int r; diff --git a/src/udp_wrap.h b/src/udp_wrap.h index d06ed32..dc4ddfb 100644 --- a/src/udp_wrap.h +++ b/src/udp_wrap.h @@ -10,6 +10,8 @@ namespace node { class UDPWrap: public HandleWrap { public: static void Initialize(v8::Handle target); + static v8::Handle GetFD(v8::Local, + const v8::AccessorInfo&); static v8::Handle New(const v8::Arguments& args); static v8::Handle Bind(const v8::Arguments& args); static v8::Handle Send(const v8::Arguments& args); -- 2.7.4