Browse Source

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.
v0.9.10-release
Ben Noordhuis 12 years ago
parent
commit
7bdd05bd66
  1. 28
      src/pipe_wrap.cc
  2. 35
      src/stream_wrap.cc
  3. 3
      src/stream_wrap.h
  4. 12
      src/tcp_wrap.cc
  5. 26
      src/tty_wrap.cc
  6. 24
      src/udp_wrap.cc
  7. 2
      src/udp_wrap.h

28
src/pipe_wrap.cc

@ -28,20 +28,21 @@
namespace node { 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::Handle;
using v8::HandleScope;
using v8::Integer;
using v8::Local; using v8::Local;
using v8::Object;
using v8::Persistent; using v8::Persistent;
using v8::Value; using v8::PropertyAttribute;
using v8::HandleScope;
using v8::FunctionTemplate;
using v8::String; using v8::String;
using v8::Function;
using v8::TryCatch; using v8::TryCatch;
using v8::Context; using v8::Value;
using v8::Arguments;
using v8::Integer;
using v8::Boolean;
Persistent<Function> pipeConstructor; Persistent<Function> pipeConstructor;
@ -82,6 +83,15 @@ void PipeWrap::Initialize(Handle<Object> target) {
t->InstanceTemplate()->SetInternalFieldCount(1); 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, "close", HandleWrap::Close);
NODE_SET_PROTOTYPE_METHOD(t, "unref", HandleWrap::Unref); NODE_SET_PROTOTYPE_METHOD(t, "unref", HandleWrap::Unref);
NODE_SET_PROTOTYPE_METHOD(t, "ref", HandleWrap::Ref); NODE_SET_PROTOTYPE_METHOD(t, "ref", HandleWrap::Ref);

35
src/stream_wrap.cc

@ -38,22 +38,22 @@
namespace node { 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::Handle;
using v8::HandleScope;
using v8::Integer;
using v8::Local; using v8::Local;
using v8::Number;
using v8::Object;
using v8::Persistent; using v8::Persistent;
using v8::Value;
using v8::HandleScope;
using v8::FunctionTemplate;
using v8::String; using v8::String;
using v8::Function;
using v8::TryCatch; using v8::TryCatch;
using v8::Context; using v8::Value;
using v8::Arguments;
using v8::Integer;
using v8::Number;
using v8::Exception;
typedef class ReqWrap<uv_shutdown_t> ShutdownWrap; 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) { void StreamWrap::SetHandle(uv_handle_t* h) {
HandleWrap::SetHandle(h); HandleWrap::SetHandle(h);
stream_ = reinterpret_cast<uv_stream_t*>(h); stream_ = reinterpret_cast<uv_stream_t*>(h);

3
src/stream_wrap.h

@ -42,6 +42,9 @@ class StreamWrap : public HandleWrap {
static void Initialize(v8::Handle<v8::Object> target); static void Initialize(v8::Handle<v8::Object> target);
static v8::Handle<v8::Value> GetFD(v8::Local<v8::String>,
const v8::AccessorInfo&);
// JavaScript functions // JavaScript functions
static v8::Handle<v8::Value> ReadStart(const v8::Arguments& args); static v8::Handle<v8::Value> ReadStart(const v8::Arguments& args);
static v8::Handle<v8::Value> ReadStop(const v8::Arguments& args); static v8::Handle<v8::Value> ReadStop(const v8::Arguments& args);

12
src/tcp_wrap.cc

@ -39,9 +39,10 @@ using v8::Handle;
using v8::HandleScope; using v8::HandleScope;
using v8::Integer; using v8::Integer;
using v8::Local; using v8::Local;
using v8::Object;
using v8::Null; using v8::Null;
using v8::Object;
using v8::Persistent; using v8::Persistent;
using v8::PropertyAttribute;
using v8::String; using v8::String;
using v8::TryCatch; using v8::TryCatch;
using v8::Undefined; using v8::Undefined;
@ -80,6 +81,15 @@ void TCPWrap::Initialize(Handle<Object> target) {
t->InstanceTemplate()->SetInternalFieldCount(1); 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, "close", HandleWrap::Close);
NODE_SET_PROTOTYPE_METHOD(t, "ref", HandleWrap::Ref); NODE_SET_PROTOTYPE_METHOD(t, "ref", HandleWrap::Ref);

26
src/tty_wrap.cc

@ -28,20 +28,21 @@
namespace node { namespace node {
using v8::Object; using v8::Arguments;
using v8::Context;
using v8::Function;
using v8::FunctionTemplate;
using v8::Handle; using v8::Handle;
using v8::HandleScope;
using v8::Integer;
using v8::Local; using v8::Local;
using v8::Object;
using v8::Persistent; using v8::Persistent;
using v8::Value; using v8::PropertyAttribute;
using v8::HandleScope;
using v8::FunctionTemplate;
using v8::String; using v8::String;
using v8::Function;
using v8::TryCatch; using v8::TryCatch;
using v8::Context;
using v8::Arguments;
using v8::Integer;
using v8::Undefined; using v8::Undefined;
using v8::Value;
void TTYWrap::Initialize(Handle<Object> target) { void TTYWrap::Initialize(Handle<Object> target) {
@ -54,6 +55,15 @@ void TTYWrap::Initialize(Handle<Object> target) {
t->InstanceTemplate()->SetInternalFieldCount(1); 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, "close", HandleWrap::Close);
NODE_SET_PROTOTYPE_METHOD(t, "unref", HandleWrap::Unref); NODE_SET_PROTOTYPE_METHOD(t, "unref", HandleWrap::Unref);

24
src/udp_wrap.cc

@ -33,6 +33,7 @@
namespace node { namespace node {
using v8::AccessorInfo;
using v8::Arguments; using v8::Arguments;
using v8::Function; using v8::Function;
using v8::FunctionTemplate; using v8::FunctionTemplate;
@ -42,6 +43,7 @@ using v8::Integer;
using v8::Local; using v8::Local;
using v8::Object; using v8::Object;
using v8::Persistent; using v8::Persistent;
using v8::PropertyAttribute;
using v8::String; using v8::String;
using v8::Value; using v8::Value;
@ -91,6 +93,15 @@ void UDPWrap::Initialize(Handle<Object> target) {
t->InstanceTemplate()->SetInternalFieldCount(1); t->InstanceTemplate()->SetInternalFieldCount(1);
t->SetClassName(String::NewSymbol("UDP")); 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, "bind", Bind);
NODE_SET_PROTOTYPE_METHOD(t, "send", Send); NODE_SET_PROTOTYPE_METHOD(t, "send", Send);
NODE_SET_PROTOTYPE_METHOD(t, "bind6", Bind6); NODE_SET_PROTOTYPE_METHOD(t, "bind6", Bind6);
@ -124,6 +135,19 @@ Handle<Value> UDPWrap::New(const Arguments& args) {
return scope.Close(args.This()); 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) { Handle<Value> UDPWrap::DoBind(const Arguments& args, int family) {
HandleScope scope; HandleScope scope;
int r; int r;

2
src/udp_wrap.h

@ -10,6 +10,8 @@ namespace node {
class UDPWrap: public HandleWrap { class UDPWrap: public HandleWrap {
public: public:
static void Initialize(v8::Handle<v8::Object> target); 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> New(const v8::Arguments& args);
static v8::Handle<v8::Value> Bind(const v8::Arguments& args); static v8::Handle<v8::Value> Bind(const v8::Arguments& args);
static v8::Handle<v8::Value> Send(const v8::Arguments& args); static v8::Handle<v8::Value> Send(const v8::Arguments& args);

Loading…
Cancel
Save