From 064c8f0252026e892cae39dcaa9d89a6b932621a Mon Sep 17 00:00:00 2001 From: Ryan Date: Wed, 29 Apr 2009 01:07:19 +0200 Subject: [PATCH] Use ObjectWrap base class for File, Socket, Server. --- src/file.cc | 42 +++++----------------------- src/net.cc | 79 ++++++++++------------------------------------------- src/node.cc | 37 +++++++++++++++++++++++++ src/node.h | 15 +++++++++- 4 files changed, 72 insertions(+), 101 deletions(-) diff --git a/src/file.cc b/src/file.cc index dc9c4a83b9..d3c9dec543 100644 --- a/src/file.cc +++ b/src/file.cc @@ -30,7 +30,7 @@ public: static Handle StrError (const Arguments& args); }; -class File { +class File : node::ObjectWrap { public: File (Handle handle); ~File (); @@ -50,11 +50,8 @@ public: static int AfterRead (eio_req *req); private: - static File* Unwrap (Handle handle); bool HasUtf8Encoding (void); int GetFD (void); - static void MakeWeak (Persistent _, void *data); - Persistent handle_; }; static void @@ -206,32 +203,15 @@ FileSystem::StrError (const Arguments& args) ///////////////////// FILE ///////////////////// File::File (Handle handle) + : ObjectWrap(handle) { HandleScope scope; - handle_ = Persistent::New(handle); - InitActionQueue(handle); - - Handle external = External::New(this); - handle_->SetInternalField(0, external); - handle_.MakeWeak(this, File::MakeWeak); } File::~File () { - // XXX call close? - handle_->SetInternalField(0, Undefined()); - handle_.Dispose(); - handle_.Clear(); -} - -File* -File::Unwrap (Handle handle) -{ - HandleScope scope; - Handle field = Handle::Cast(handle->GetInternalField(0)); - File* file = static_cast(field->Value()); - return file; + ; // XXX call close? } bool @@ -247,20 +227,12 @@ File::GetFD (void) int fd = fd_value->IntegerValue(); } - -void -File::MakeWeak (Persistent _, void *data) -{ - File *file = static_cast (data); - delete file; -} - Handle File::Close (const Arguments& args) { HandleScope scope; - File *file = File::Unwrap(args.Holder()); + File *file = NODE_UNWRAP(File, args.Holder()); int fd = file->GetFD(); @@ -296,7 +268,7 @@ File::Open (const Arguments& args) HandleScope scope; - File *file = File::Unwrap(args.Holder()); + File *file = NODE_UNWRAP(File, args.Holder()); // make sure that we don't already have a pending open if (file->handle_->Has(FD_SYMBOL)) { @@ -357,7 +329,7 @@ File::Write (const Arguments& args) HandleScope scope; - File *file = File::Unwrap(args.Holder()); + File *file = NODE_UNWRAP(File, args.Holder()); char *buf = NULL; size_t length = 0; @@ -427,7 +399,7 @@ File::Read (const Arguments& args) if (!args[1]->IsNumber()) return Undefined(); HandleScope scope; - File *file = File::Unwrap(args.Holder()); + File *file = NODE_UNWRAP(File, args.Holder()); size_t length = args[0]->IntegerValue(); off_t pos = args[1]->IntegerValue(); diff --git a/src/net.cc b/src/net.cc index 6afff74189..e7a2defaa5 100644 --- a/src/net.cc +++ b/src/net.cc @@ -32,7 +32,7 @@ static const struct addrinfo tcp_hints = /* ai_next */ , NULL }; -class Server { +class Server : node::ObjectWrap { public: Server (Handle handle, int backlog); ~Server (); @@ -43,13 +43,10 @@ public: private: static oi_socket* OnConnection (oi_server *, struct sockaddr *, socklen_t); - static Server* Unwrap (Handle handle); - static void MakeWeak (Persistent _, void *data); oi_server server_; - Persistent handle_; }; -class Socket { +class Socket : node::ObjectWrap { public: Socket (Handle handle, double timeout); ~Socket (); @@ -74,12 +71,8 @@ private: static int Resolve (eio_req *req); static int AfterResolve (eio_req *req); - static Socket* Unwrap (Handle handle); - static void MakeWeak (Persistent _, void *data); - enum {UTF8, RAW} encoding_; oi_socket socket_; - Persistent handle_; char *host_; char *port_; @@ -88,25 +81,20 @@ private: }; Server::Server (Handle handle, int backlog) + : ObjectWrap(handle) { + //HandleScope scope; oi_server_init(&server_, backlog); server_.on_connection = Server::OnConnection; // server_.on_error = Server::OnError; server_.data = this; - - HandleScope scope; - handle_ = Persistent::New(handle); - handle_->SetInternalField(0, External::New(this)); - handle_.MakeWeak(this, Server::MakeWeak); } Server::~Server () { - HandleScope scope; + //HandleScope scope; oi_server_close(&server_); oi_server_detach(&server_); - handle_.Dispose(); - handle_.Clear(); // necessary? } Handle @@ -131,7 +119,7 @@ Server::ListenTCP (const Arguments& args) if (args.Length() < 2) return Undefined(); HandleScope scope; - Server *server = Server::Unwrap(args.Holder()); + Server *server = NODE_UNWRAP(Server, args.Holder()); String::AsciiValue port(args[0]); @@ -170,7 +158,7 @@ Handle Server::Close (const Arguments& args) { HandleScope scope; - Server *server = Server::Unwrap(args.Holder()); + Server *server = NODE_UNWRAP(Server, args.Holder()); oi_server_close(&server->server_); return Undefined(); } @@ -198,22 +186,6 @@ Server::OnConnection (oi_server *s, struct sockaddr *remote_addr, socklen_t remo return &socket->socket_; } -Server* -Server::Unwrap (Handle handle) -{ - HandleScope scope; - Handle field = Handle::Cast(handle->GetInternalField(0)); - Server* server = static_cast(field->Value()); - return server; -} - -void -Server::MakeWeak (Persistent _, void *data) -{ - Server *s = static_cast (data); - delete s; -} - Handle Socket::New(const Arguments& args) { @@ -270,15 +242,6 @@ Socket::SetEncoding (Handle encoding_value) } } -Socket* -Socket::Unwrap (Handle handle) -{ - HandleScope scope; - Handle field = Handle::Cast(handle->GetInternalField(0)); - Socket* socket = static_cast(field->Value()); - return socket; -} - Handle Socket::ConnectTCP (const Arguments& args) { @@ -286,7 +249,7 @@ Socket::ConnectTCP (const Arguments& args) return Undefined(); HandleScope scope; - Socket *socket = Socket::Unwrap(args.Holder()); + Socket *socket = NODE_UNWRAP(Socket, args.Holder()); String::AsciiValue port(args[0]); socket->port_ = strdup(*port); @@ -374,20 +337,15 @@ Handle Socket::Close (const Arguments& args) { HandleScope scope; - Socket *socket = Socket::Unwrap(args.Holder()); + Socket *socket = NODE_UNWRAP(Socket, args.Holder()); oi_socket_close(&socket->socket_); return Undefined(); } -void -Socket::MakeWeak (Persistent _, void *data) -{ - Socket *s = static_cast (data); - delete s; -} - Socket::Socket(Handle handle, double timeout) + : ObjectWrap(handle) { + //HandleScope scope; oi_socket_init(&socket_, timeout); socket_.on_connect = Socket::OnConnect; socket_.on_read = Socket::OnRead; @@ -397,11 +355,6 @@ Socket::Socket(Handle handle, double timeout) socket_.on_timeout = Socket::OnTimeout; socket_.data = this; - HandleScope scope; - handle_ = Persistent::New(handle); - handle_->SetInternalField(0, External::New(this)); - handle_.MakeWeak(this, Socket::MakeWeak); - encoding_ = UTF8; // default encoding. host_ = NULL; port_ = NULL; @@ -409,25 +362,21 @@ Socket::Socket(Handle handle, double timeout) Socket::~Socket () { - HandleScope scope; oi_socket_close(&socket_); oi_socket_detach(&socket_); free(host_); free(port_); - handle_->SetInternalField(0, Undefined()); + //HandleScope scope; handle_->Delete(String::NewSymbol("write")); handle_->Delete(String::NewSymbol("close")); - - handle_.Dispose(); - handle_.Clear(); // necessary? } Handle Socket::SetEncoding (const Arguments& args) { HandleScope scope; - Socket *socket = Socket::Unwrap(args.Holder()); + Socket *socket = NODE_UNWRAP(Socket, args.Holder()); socket->SetEncoding(args[0]); return Undefined(); } @@ -437,7 +386,7 @@ Socket::Write (const Arguments& args) { HandleScope scope; - Socket *socket = Socket::Unwrap(args.Holder()); + Socket *socket = NODE_UNWRAP(Socket, args.Holder()); // TODO support a callback using buf->on_release diff --git a/src/node.cc b/src/node.cc index a77748928e..49fb08227e 100644 --- a/src/node.cc +++ b/src/node.cc @@ -21,6 +21,43 @@ using namespace std; static int exit_code = 0; +ObjectWrap::ObjectWrap (Handle handle) +{ + v8::HandleScope scope; + handle_ = v8::Persistent::New(handle); + + v8::Handle external = v8::External::New(this); + handle_->SetInternalField(0, external); + handle_.MakeWeak(this, ObjectWrap::MakeWeak); +} + +ObjectWrap::~ObjectWrap ( ) +{ + handle_->SetInternalField(0, Undefined()); + handle_.Dispose(); + handle_.Clear(); +} + +void* +ObjectWrap::Unwrap (v8::Handle handle) +{ + v8::HandleScope scope; + v8::Handle field = + v8::Handle::Cast(handle->GetInternalField(0)); + return field->Value(); +} + +void +ObjectWrap::MakeWeak (Persistent _, void *data) +{ + ObjectWrap *w = static_cast (data); + delete w; +} + + + + + // Extracts a C string from a V8 Utf8Value. const char* ToCString(const v8::String::Utf8Value& value) diff --git a/src/node.h b/src/node.h index b43d514d22..74fb60a0d7 100644 --- a/src/node.h +++ b/src/node.h @@ -11,12 +11,25 @@ namespace node { #define NODE_METHOD(name) v8::Handle name (const v8::Arguments& args) #define NODE_SET_METHOD(obj, name, callback) \ obj->Set(NODE_SYMBOL(name), v8::FunctionTemplate::New(callback)->GetFunction()) +#define NODE_UNWRAP(type, value) static_cast(node::ObjectWrap::Unwrap(value)) enum encoding {UTF8, RAW}; void fatal_exception (v8::TryCatch &try_catch); void exit (int code); void eio_warmup (void); // call this before creating a new eio event. +class ObjectWrap { +public: + ObjectWrap (v8::Handle handle); + ~ObjectWrap ( ); + +protected: + static void* Unwrap (v8::Handle handle); + v8::Persistent handle_; + +private: + static void MakeWeak (v8::Persistent _, void *data); +}; + } // namespace node #endif // node_h -