Permalink
Browse files

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.
  • Loading branch information...
1 parent 2d069c5 commit 7bdd05bd66607886d633332e1397550d9b8d9b39 @bnoordhuis bnoordhuis committed Feb 13, 2013
Showing with 101 additions and 29 deletions.
  1. +19 −9 src/pipe_wrap.cc
  2. +24 −11 src/stream_wrap.cc
  3. +3 −0 src/stream_wrap.h
  4. +11 −1 src/tcp_wrap.cc
  5. +18 −8 src/tty_wrap.cc
  6. +24 −0 src/udp_wrap.cc
  7. +2 −0 src/udp_wrap.h
View
28 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<Function> pipeConstructor;
@@ -82,6 +83,15 @@ void PipeWrap::Initialize(Handle<Object> target) {
t->InstanceTemplate()->SetInternalFieldCount(1);
+ enum PropertyAttribute attributes =
+ static_cast<PropertyAttribute>(v8::ReadOnly | v8::DontDelete);
+ t->InstanceTemplate()->SetAccessor(String::New("fd"),
+ StreamWrap::GetFD,
+ NULL,
+ Handle<Value>(),
+ 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);
View
35 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<uv_shutdown_t> ShutdownWrap;
@@ -117,6 +117,19 @@ StreamWrap::StreamWrap(Handle<Object> object, uv_stream_t* stream)
}
+Handle<Value> StreamWrap::GetFD(Local<String>, 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<uv_stream_t*>(h);
View
3 src/stream_wrap.h
@@ -42,6 +42,9 @@ class StreamWrap : public HandleWrap {
static void Initialize(v8::Handle<v8::Object> target);
+ static v8::Handle<v8::Value> GetFD(v8::Local<v8::String>,
+ const v8::AccessorInfo&);
+
// JavaScript functions
static v8::Handle<v8::Value> ReadStart(const v8::Arguments& args);
static v8::Handle<v8::Value> ReadStop(const v8::Arguments& args);
View
12 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<Object> target) {
t->InstanceTemplate()->SetInternalFieldCount(1);
+ enum PropertyAttribute attributes =
+ static_cast<PropertyAttribute>(v8::ReadOnly | v8::DontDelete);
+ t->InstanceTemplate()->SetAccessor(String::New("fd"),
+ StreamWrap::GetFD,
+ NULL,
+ Handle<Value>(),
+ v8::DEFAULT,
+ attributes);
+
NODE_SET_PROTOTYPE_METHOD(t, "close", HandleWrap::Close);
NODE_SET_PROTOTYPE_METHOD(t, "ref", HandleWrap::Ref);
View
26 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<Object> target) {
@@ -54,6 +55,15 @@ void TTYWrap::Initialize(Handle<Object> target) {
t->InstanceTemplate()->SetInternalFieldCount(1);
+ enum PropertyAttribute attributes =
+ static_cast<PropertyAttribute>(v8::ReadOnly | v8::DontDelete);
+ t->InstanceTemplate()->SetAccessor(String::New("fd"),
+ StreamWrap::GetFD,
+ NULL,
+ Handle<Value>(),
+ v8::DEFAULT,
+ attributes);
+
NODE_SET_PROTOTYPE_METHOD(t, "close", HandleWrap::Close);
NODE_SET_PROTOTYPE_METHOD(t, "unref", HandleWrap::Unref);
View
24 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<Object> target) {
t->InstanceTemplate()->SetInternalFieldCount(1);
t->SetClassName(String::NewSymbol("UDP"));
+ enum PropertyAttribute attributes =
+ static_cast<PropertyAttribute>(v8::ReadOnly | v8::DontDelete);
+ t->InstanceTemplate()->SetAccessor(String::New("fd"),
+ UDPWrap::GetFD,
+ NULL,
+ Handle<Value>(),
+ 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<Value> UDPWrap::New(const Arguments& args) {
return scope.Close(args.This());
}
+
+Handle<Value> UDPWrap::GetFD(Local<String>, 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<Value> UDPWrap::DoBind(const Arguments& args, int family) {
HandleScope scope;
int r;
View
2 src/udp_wrap.h
@@ -10,6 +10,8 @@ namespace node {
class UDPWrap: public HandleWrap {
public:
static void Initialize(v8::Handle<v8::Object> target);
+ static v8::Handle<v8::Value> GetFD(v8::Local<v8::String>,
+ const v8::AccessorInfo&);
static v8::Handle<v8::Value> New(const v8::Arguments& args);
static v8::Handle<v8::Value> Bind(const v8::Arguments& args);
static v8::Handle<v8::Value> Send(const v8::Arguments& args);

0 comments on commit 7bdd05b

Please sign in to comment.