Browse Source

Use ObjectWrap base class for File, Socket, Server.

v0.7.4-release
Ryan 16 years ago
parent
commit
064c8f0252
  1. 42
      src/file.cc
  2. 79
      src/net.cc
  3. 37
      src/node.cc
  4. 15
      src/node.h

42
src/file.cc

@ -30,7 +30,7 @@ public:
static Handle<Value> StrError (const Arguments& args); static Handle<Value> StrError (const Arguments& args);
}; };
class File { class File : node::ObjectWrap {
public: public:
File (Handle<Object> handle); File (Handle<Object> handle);
~File (); ~File ();
@ -50,11 +50,8 @@ public:
static int AfterRead (eio_req *req); static int AfterRead (eio_req *req);
private: private:
static File* Unwrap (Handle<Object> handle);
bool HasUtf8Encoding (void); bool HasUtf8Encoding (void);
int GetFD (void); int GetFD (void);
static void MakeWeak (Persistent<Value> _, void *data);
Persistent<Object> handle_;
}; };
static void static void
@ -206,32 +203,15 @@ FileSystem::StrError (const Arguments& args)
///////////////////// FILE ///////////////////// ///////////////////// FILE /////////////////////
File::File (Handle<Object> handle) File::File (Handle<Object> handle)
: ObjectWrap(handle)
{ {
HandleScope scope; HandleScope scope;
handle_ = Persistent<Object>::New(handle);
InitActionQueue(handle); InitActionQueue(handle);
Handle<External> external = External::New(this);
handle_->SetInternalField(0, external);
handle_.MakeWeak(this, File::MakeWeak);
} }
File::~File () File::~File ()
{ {
// XXX call close? ; // XXX call close?
handle_->SetInternalField(0, Undefined());
handle_.Dispose();
handle_.Clear();
}
File*
File::Unwrap (Handle<Object> handle)
{
HandleScope scope;
Handle<External> field = Handle<External>::Cast(handle->GetInternalField(0));
File* file = static_cast<File*>(field->Value());
return file;
} }
bool bool
@ -247,20 +227,12 @@ File::GetFD (void)
int fd = fd_value->IntegerValue(); int fd = fd_value->IntegerValue();
} }
void
File::MakeWeak (Persistent<Value> _, void *data)
{
File *file = static_cast<File*> (data);
delete file;
}
Handle<Value> Handle<Value>
File::Close (const Arguments& args) File::Close (const Arguments& args)
{ {
HandleScope scope; HandleScope scope;
File *file = File::Unwrap(args.Holder()); File *file = NODE_UNWRAP(File, args.Holder());
int fd = file->GetFD(); int fd = file->GetFD();
@ -296,7 +268,7 @@ File::Open (const Arguments& args)
HandleScope scope; 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 // make sure that we don't already have a pending open
if (file->handle_->Has(FD_SYMBOL)) { if (file->handle_->Has(FD_SYMBOL)) {
@ -357,7 +329,7 @@ File::Write (const Arguments& args)
HandleScope scope; HandleScope scope;
File *file = File::Unwrap(args.Holder()); File *file = NODE_UNWRAP(File, args.Holder());
char *buf = NULL; char *buf = NULL;
size_t length = 0; size_t length = 0;
@ -427,7 +399,7 @@ File::Read (const Arguments& args)
if (!args[1]->IsNumber()) return Undefined(); if (!args[1]->IsNumber()) return Undefined();
HandleScope scope; HandleScope scope;
File *file = File::Unwrap(args.Holder()); File *file = NODE_UNWRAP(File, args.Holder());
size_t length = args[0]->IntegerValue(); size_t length = args[0]->IntegerValue();
off_t pos = args[1]->IntegerValue(); off_t pos = args[1]->IntegerValue();

79
src/net.cc

@ -32,7 +32,7 @@ static const struct addrinfo tcp_hints =
/* ai_next */ , NULL /* ai_next */ , NULL
}; };
class Server { class Server : node::ObjectWrap {
public: public:
Server (Handle<Object> handle, int backlog); Server (Handle<Object> handle, int backlog);
~Server (); ~Server ();
@ -43,13 +43,10 @@ public:
private: private:
static oi_socket* OnConnection (oi_server *, struct sockaddr *, socklen_t); static oi_socket* OnConnection (oi_server *, struct sockaddr *, socklen_t);
static Server* Unwrap (Handle<Object> handle);
static void MakeWeak (Persistent<Value> _, void *data);
oi_server server_; oi_server server_;
Persistent<Object> handle_;
}; };
class Socket { class Socket : node::ObjectWrap {
public: public:
Socket (Handle<Object> handle, double timeout); Socket (Handle<Object> handle, double timeout);
~Socket (); ~Socket ();
@ -74,12 +71,8 @@ private:
static int Resolve (eio_req *req); static int Resolve (eio_req *req);
static int AfterResolve (eio_req *req); static int AfterResolve (eio_req *req);
static Socket* Unwrap (Handle<Object> handle);
static void MakeWeak (Persistent<Value> _, void *data);
enum {UTF8, RAW} encoding_; enum {UTF8, RAW} encoding_;
oi_socket socket_; oi_socket socket_;
Persistent<Object> handle_;
char *host_; char *host_;
char *port_; char *port_;
@ -88,25 +81,20 @@ private:
}; };
Server::Server (Handle<Object> handle, int backlog) Server::Server (Handle<Object> handle, int backlog)
: ObjectWrap(handle)
{ {
//HandleScope scope;
oi_server_init(&server_, backlog); oi_server_init(&server_, backlog);
server_.on_connection = Server::OnConnection; server_.on_connection = Server::OnConnection;
// server_.on_error = Server::OnError; // server_.on_error = Server::OnError;
server_.data = this; server_.data = this;
HandleScope scope;
handle_ = Persistent<Object>::New(handle);
handle_->SetInternalField(0, External::New(this));
handle_.MakeWeak(this, Server::MakeWeak);
} }
Server::~Server () Server::~Server ()
{ {
HandleScope scope; //HandleScope scope;
oi_server_close(&server_); oi_server_close(&server_);
oi_server_detach(&server_); oi_server_detach(&server_);
handle_.Dispose();
handle_.Clear(); // necessary?
} }
Handle<Value> Handle<Value>
@ -131,7 +119,7 @@ Server::ListenTCP (const Arguments& args)
if (args.Length() < 2) return Undefined(); if (args.Length() < 2) return Undefined();
HandleScope scope; HandleScope scope;
Server *server = Server::Unwrap(args.Holder()); Server *server = NODE_UNWRAP(Server, args.Holder());
String::AsciiValue port(args[0]); String::AsciiValue port(args[0]);
@ -170,7 +158,7 @@ Handle<Value>
Server::Close (const Arguments& args) Server::Close (const Arguments& args)
{ {
HandleScope scope; HandleScope scope;
Server *server = Server::Unwrap(args.Holder()); Server *server = NODE_UNWRAP(Server, args.Holder());
oi_server_close(&server->server_); oi_server_close(&server->server_);
return Undefined(); return Undefined();
} }
@ -198,22 +186,6 @@ Server::OnConnection (oi_server *s, struct sockaddr *remote_addr, socklen_t remo
return &socket->socket_; return &socket->socket_;
} }
Server*
Server::Unwrap (Handle<Object> handle)
{
HandleScope scope;
Handle<External> field = Handle<External>::Cast(handle->GetInternalField(0));
Server* server = static_cast<Server*>(field->Value());
return server;
}
void
Server::MakeWeak (Persistent<Value> _, void *data)
{
Server *s = static_cast<Server*> (data);
delete s;
}
Handle<Value> Handle<Value>
Socket::New(const Arguments& args) Socket::New(const Arguments& args)
{ {
@ -270,15 +242,6 @@ Socket::SetEncoding (Handle<Value> encoding_value)
} }
} }
Socket*
Socket::Unwrap (Handle<Object> handle)
{
HandleScope scope;
Handle<External> field = Handle<External>::Cast(handle->GetInternalField(0));
Socket* socket = static_cast<Socket*>(field->Value());
return socket;
}
Handle<Value> Handle<Value>
Socket::ConnectTCP (const Arguments& args) Socket::ConnectTCP (const Arguments& args)
{ {
@ -286,7 +249,7 @@ Socket::ConnectTCP (const Arguments& args)
return Undefined(); return Undefined();
HandleScope scope; HandleScope scope;
Socket *socket = Socket::Unwrap(args.Holder()); Socket *socket = NODE_UNWRAP(Socket, args.Holder());
String::AsciiValue port(args[0]); String::AsciiValue port(args[0]);
socket->port_ = strdup(*port); socket->port_ = strdup(*port);
@ -374,20 +337,15 @@ Handle<Value>
Socket::Close (const Arguments& args) Socket::Close (const Arguments& args)
{ {
HandleScope scope; HandleScope scope;
Socket *socket = Socket::Unwrap(args.Holder()); Socket *socket = NODE_UNWRAP(Socket, args.Holder());
oi_socket_close(&socket->socket_); oi_socket_close(&socket->socket_);
return Undefined(); return Undefined();
} }
void
Socket::MakeWeak (Persistent<Value> _, void *data)
{
Socket *s = static_cast<Socket*> (data);
delete s;
}
Socket::Socket(Handle<Object> handle, double timeout) Socket::Socket(Handle<Object> handle, double timeout)
: ObjectWrap(handle)
{ {
//HandleScope scope;
oi_socket_init(&socket_, timeout); oi_socket_init(&socket_, timeout);
socket_.on_connect = Socket::OnConnect; socket_.on_connect = Socket::OnConnect;
socket_.on_read = Socket::OnRead; socket_.on_read = Socket::OnRead;
@ -397,11 +355,6 @@ Socket::Socket(Handle<Object> handle, double timeout)
socket_.on_timeout = Socket::OnTimeout; socket_.on_timeout = Socket::OnTimeout;
socket_.data = this; socket_.data = this;
HandleScope scope;
handle_ = Persistent<Object>::New(handle);
handle_->SetInternalField(0, External::New(this));
handle_.MakeWeak(this, Socket::MakeWeak);
encoding_ = UTF8; // default encoding. encoding_ = UTF8; // default encoding.
host_ = NULL; host_ = NULL;
port_ = NULL; port_ = NULL;
@ -409,25 +362,21 @@ Socket::Socket(Handle<Object> handle, double timeout)
Socket::~Socket () Socket::~Socket ()
{ {
HandleScope scope;
oi_socket_close(&socket_); oi_socket_close(&socket_);
oi_socket_detach(&socket_); oi_socket_detach(&socket_);
free(host_); free(host_);
free(port_); free(port_);
handle_->SetInternalField(0, Undefined()); //HandleScope scope;
handle_->Delete(String::NewSymbol("write")); handle_->Delete(String::NewSymbol("write"));
handle_->Delete(String::NewSymbol("close")); handle_->Delete(String::NewSymbol("close"));
handle_.Dispose();
handle_.Clear(); // necessary?
} }
Handle<Value> Handle<Value>
Socket::SetEncoding (const Arguments& args) Socket::SetEncoding (const Arguments& args)
{ {
HandleScope scope; HandleScope scope;
Socket *socket = Socket::Unwrap(args.Holder()); Socket *socket = NODE_UNWRAP(Socket, args.Holder());
socket->SetEncoding(args[0]); socket->SetEncoding(args[0]);
return Undefined(); return Undefined();
} }
@ -437,7 +386,7 @@ Socket::Write (const Arguments& args)
{ {
HandleScope scope; HandleScope scope;
Socket *socket = Socket::Unwrap(args.Holder()); Socket *socket = NODE_UNWRAP(Socket, args.Holder());
// TODO support a callback using buf->on_release // TODO support a callback using buf->on_release

37
src/node.cc

@ -21,6 +21,43 @@ using namespace std;
static int exit_code = 0; static int exit_code = 0;
ObjectWrap::ObjectWrap (Handle<Object> handle)
{
v8::HandleScope scope;
handle_ = v8::Persistent<v8::Object>::New(handle);
v8::Handle<v8::External> 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<v8::Object> handle)
{
v8::HandleScope scope;
v8::Handle<v8::External> field =
v8::Handle<v8::External>::Cast(handle->GetInternalField(0));
return field->Value();
}
void
ObjectWrap::MakeWeak (Persistent<Value> _, void *data)
{
ObjectWrap *w = static_cast<ObjectWrap*> (data);
delete w;
}
// Extracts a C string from a V8 Utf8Value. // Extracts a C string from a V8 Utf8Value.
const char* const char*
ToCString(const v8::String::Utf8Value& value) ToCString(const v8::String::Utf8Value& value)

15
src/node.h

@ -11,12 +11,25 @@ namespace node {
#define NODE_METHOD(name) v8::Handle<v8::Value> name (const v8::Arguments& args) #define NODE_METHOD(name) v8::Handle<v8::Value> name (const v8::Arguments& args)
#define NODE_SET_METHOD(obj, name, callback) \ #define NODE_SET_METHOD(obj, name, callback) \
obj->Set(NODE_SYMBOL(name), v8::FunctionTemplate::New(callback)->GetFunction()) obj->Set(NODE_SYMBOL(name), v8::FunctionTemplate::New(callback)->GetFunction())
#define NODE_UNWRAP(type, value) static_cast<type*>(node::ObjectWrap::Unwrap(value))
enum encoding {UTF8, RAW}; enum encoding {UTF8, RAW};
void fatal_exception (v8::TryCatch &try_catch); void fatal_exception (v8::TryCatch &try_catch);
void exit (int code); void exit (int code);
void eio_warmup (void); // call this before creating a new eio event. void eio_warmup (void); // call this before creating a new eio event.
class ObjectWrap {
public:
ObjectWrap (v8::Handle<v8::Object> handle);
~ObjectWrap ( );
protected:
static void* Unwrap (v8::Handle<v8::Object> handle);
v8::Persistent<v8::Object> handle_;
private:
static void MakeWeak (v8::Persistent<v8::Value> _, void *data);
};
} // namespace node } // namespace node
#endif // node_h #endif // node_h

Loading…
Cancel
Save