mirror of
https://github.com/nodejs/node.git
synced 2024-11-29 07:00:59 +01:00
Use ObjectWrap base class for File, Socket, Server.
This commit is contained in:
parent
cf1c58063e
commit
064c8f0252
42
src/file.cc
42
src/file.cc
@ -30,7 +30,7 @@ public:
|
||||
static Handle<Value> StrError (const Arguments& args);
|
||||
};
|
||||
|
||||
class File {
|
||||
class File : node::ObjectWrap {
|
||||
public:
|
||||
File (Handle<Object> handle);
|
||||
~File ();
|
||||
@ -50,11 +50,8 @@ public:
|
||||
static int AfterRead (eio_req *req);
|
||||
|
||||
private:
|
||||
static File* Unwrap (Handle<Object> handle);
|
||||
bool HasUtf8Encoding (void);
|
||||
int GetFD (void);
|
||||
static void MakeWeak (Persistent<Value> _, void *data);
|
||||
Persistent<Object> handle_;
|
||||
};
|
||||
|
||||
static void
|
||||
@ -206,32 +203,15 @@ FileSystem::StrError (const Arguments& args)
|
||||
///////////////////// FILE /////////////////////
|
||||
|
||||
File::File (Handle<Object> handle)
|
||||
: ObjectWrap(handle)
|
||||
{
|
||||
HandleScope scope;
|
||||
handle_ = Persistent<Object>::New(handle);
|
||||
|
||||
InitActionQueue(handle);
|
||||
|
||||
Handle<External> 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<Object> handle)
|
||||
{
|
||||
HandleScope scope;
|
||||
Handle<External> field = Handle<External>::Cast(handle->GetInternalField(0));
|
||||
File* file = static_cast<File*>(field->Value());
|
||||
return file;
|
||||
; // XXX call close?
|
||||
}
|
||||
|
||||
bool
|
||||
@ -247,20 +227,12 @@ File::GetFD (void)
|
||||
int fd = fd_value->IntegerValue();
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
File::MakeWeak (Persistent<Value> _, void *data)
|
||||
{
|
||||
File *file = static_cast<File*> (data);
|
||||
delete file;
|
||||
}
|
||||
|
||||
Handle<Value>
|
||||
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();
|
||||
|
||||
|
79
src/net.cc
79
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<Object> handle, int backlog);
|
||||
~Server ();
|
||||
@ -43,13 +43,10 @@ public:
|
||||
|
||||
private:
|
||||
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_;
|
||||
Persistent<Object> handle_;
|
||||
};
|
||||
|
||||
class Socket {
|
||||
class Socket : node::ObjectWrap {
|
||||
public:
|
||||
Socket (Handle<Object> 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<Object> handle);
|
||||
static void MakeWeak (Persistent<Value> _, void *data);
|
||||
|
||||
enum {UTF8, RAW} encoding_;
|
||||
oi_socket socket_;
|
||||
Persistent<Object> handle_;
|
||||
|
||||
char *host_;
|
||||
char *port_;
|
||||
@ -88,25 +81,20 @@ private:
|
||||
};
|
||||
|
||||
Server::Server (Handle<Object> 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<Object>::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<Value>
|
||||
@ -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<Value>
|
||||
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<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>
|
||||
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>
|
||||
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<Value>
|
||||
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<Value> _, void *data)
|
||||
{
|
||||
Socket *s = static_cast<Socket*> (data);
|
||||
delete s;
|
||||
}
|
||||
|
||||
Socket::Socket(Handle<Object> 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<Object> handle, double timeout)
|
||||
socket_.on_timeout = Socket::OnTimeout;
|
||||
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.
|
||||
host_ = NULL;
|
||||
port_ = NULL;
|
||||
@ -409,25 +362,21 @@ Socket::Socket(Handle<Object> 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<Value>
|
||||
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
|
||||
|
||||
|
37
src/node.cc
37
src/node.cc
@ -21,6 +21,43 @@ using namespace std;
|
||||
|
||||
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.
|
||||
const char*
|
||||
ToCString(const v8::String::Utf8Value& value)
|
||||
|
15
src/node.h
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_SET_METHOD(obj, name, callback) \
|
||||
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};
|
||||
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<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
|
||||
#endif // node_h
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user