stream_wrap, udp_wrap: add read-only fd property
authorBen Noordhuis <info@bnoordhuis.nl>
Wed, 13 Feb 2013 15:09:03 +0000 (16:09 +0100)
committerBen Noordhuis <info@bnoordhuis.nl>
Wed, 13 Feb 2013 15:11:20 +0000 (16:11 +0100)
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
src/stream_wrap.cc
src/stream_wrap.h
src/tcp_wrap.cc
src/tty_wrap.cc
src/udp_wrap.cc
src/udp_wrap.h

index 39897af..149c4f2 100644 (file)
 
 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);
index e6756e1..fc9218e 100644 (file)
 
 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);
index c51083c..77bd234 100644 (file)
@@ -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);
index 87a3e4d..607aad1 100644 (file)
@@ -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);
index 1751105..f1892a4 100644 (file)
 
 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);
 
index af525b3..d7d1efd 100644 (file)
@@ -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;
index d06ed32..dc4ddfb 100644 (file)
@@ -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);