Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Templatize ObjectWrap::Unwrap. Remove NODE_UNWRAP macro.
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Jul 10, 2009
1 parent 1fc4dce commit 22c3a1e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/http.cc
Expand Up @@ -256,6 +256,6 @@ Connection*
HTTPServer::UnwrapConnection (Local<Object> connection) HTTPServer::UnwrapConnection (Local<Object> connection)
{ {
HandleScope scope; HandleScope scope;
return NODE_UNWRAP(HTTPConnection, connection); return ObjectWrap::Unwrap<HTTPConnection>(connection);
} }


20 changes: 10 additions & 10 deletions src/net.cc
Expand Up @@ -77,7 +77,7 @@ Connection::Initialize (v8::Handle<v8::Object> target)
Handle<Value> Handle<Value>
Connection::ReadyStateGetter (Local<String> property, const AccessorInfo& info) Connection::ReadyStateGetter (Local<String> property, const AccessorInfo& info)
{ {
Connection *connection = NODE_UNWRAP(Connection, info.This()); Connection *connection = ObjectWrap::Unwrap<Connection>(info.This());
if (!connection) return Handle<Value>(); if (!connection) return Handle<Value>();


HandleScope scope; HandleScope scope;
Expand Down Expand Up @@ -159,7 +159,7 @@ Connection::ReadyState (void)
Handle<Value> Handle<Value>
Connection::Connect (const Arguments& args) Connection::Connect (const Arguments& args)
{ {
Connection *connection = NODE_UNWRAP(Connection, args.Holder()); Connection *connection = ObjectWrap::Unwrap<Connection>(args.Holder());
if (!connection) return Handle<Value>(); if (!connection) return Handle<Value>();


HandleScope scope; HandleScope scope;
Expand Down Expand Up @@ -295,7 +295,7 @@ Connection::SetEncoding (const Arguments& args)
{ {
HandleScope scope; HandleScope scope;


Connection *connection = NODE_UNWRAP(Connection, args.This()); Connection *connection = ObjectWrap::Unwrap<Connection>(args.This());
if (!connection) return Handle<Value>(); if (!connection) return Handle<Value>();


if (!args[0]->IsString()) { if (!args[0]->IsString()) {
Expand Down Expand Up @@ -323,7 +323,7 @@ Handle<Value>
Connection::Close (const Arguments& args) Connection::Close (const Arguments& args)
{ {
HandleScope scope; HandleScope scope;
Connection *connection = NODE_UNWRAP(Connection, args.Holder()); Connection *connection = ObjectWrap::Unwrap<Connection>(args.Holder());
if (!connection) return Handle<Value>(); if (!connection) return Handle<Value>();


connection->Close(); connection->Close();
Expand All @@ -334,7 +334,7 @@ Handle<Value>
Connection::FullClose (const Arguments& args) Connection::FullClose (const Arguments& args)
{ {
HandleScope scope; HandleScope scope;
Connection *connection = NODE_UNWRAP(Connection, args.Holder()); Connection *connection = ObjectWrap::Unwrap<Connection>(args.Holder());
if (!connection) return Handle<Value>(); if (!connection) return Handle<Value>();


connection->FullClose(); connection->FullClose();
Expand All @@ -345,7 +345,7 @@ Handle<Value>
Connection::ForceClose (const Arguments& args) Connection::ForceClose (const Arguments& args)
{ {
HandleScope scope; HandleScope scope;
Connection *connection = NODE_UNWRAP(Connection, args.Holder()); Connection *connection = ObjectWrap::Unwrap<Connection>(args.Holder());
if (!connection) return Handle<Value>(); if (!connection) return Handle<Value>();


connection->ForceClose(); connection->ForceClose();
Expand All @@ -358,7 +358,7 @@ Handle<Value>
Connection::Send (const Arguments& args) Connection::Send (const Arguments& args)
{ {
HandleScope scope; HandleScope scope;
Connection *connection = NODE_UNWRAP(Connection, args.Holder()); Connection *connection = ObjectWrap::Unwrap<Connection>(args.Holder());
if (!connection) return Handle<Value>(); if (!connection) return Handle<Value>();


if ( connection->ReadyState() != OPEN if ( connection->ReadyState() != OPEN
Expand Down Expand Up @@ -513,7 +513,7 @@ Connection*
Server::UnwrapConnection (Local<Object> connection) Server::UnwrapConnection (Local<Object> connection)
{ {
HandleScope scope; HandleScope scope;
return NODE_UNWRAP(Connection, connection); return ObjectWrap::Unwrap<Connection>(connection);
} }


Connection* Connection*
Expand Down Expand Up @@ -566,7 +566,7 @@ Server::New (const Arguments& args)
Handle<Value> Handle<Value>
Server::Listen (const Arguments& args) Server::Listen (const Arguments& args)
{ {
Server *server = NODE_UNWRAP(Server, args.Holder()); Server *server = ObjectWrap::Unwrap<Server>(args.Holder());
if (!server) return Handle<Value>(); if (!server) return Handle<Value>();


if (args.Length() == 0) if (args.Length() == 0)
Expand Down Expand Up @@ -603,7 +603,7 @@ Server::Listen (const Arguments& args)
Handle<Value> Handle<Value>
Server::Close (const Arguments& args) Server::Close (const Arguments& args)
{ {
Server *server = NODE_UNWRAP(Server, args.Holder()); Server *server = ObjectWrap::Unwrap<Server>(args.Holder());
if (!server) return Handle<Value>(); if (!server) return Handle<Value>();


server->Close(); server->Close();
Expand Down
13 changes: 6 additions & 7 deletions src/object_wrap.h
Expand Up @@ -6,10 +6,8 @@


namespace node { namespace node {


#define NODE_UNWRAP(type, value) static_cast<type*>(node::ObjectWrap::Unwrap(value))

class ObjectWrap { class ObjectWrap {
public: public:
ObjectWrap ( ) { ObjectWrap ( ) {
weak_ = false; weak_ = false;
attached_ = false; attached_ = false;
Expand All @@ -24,12 +22,13 @@ class ObjectWrap {
} }


protected: protected:
static inline void* Unwrap (v8::Handle<v8::Object> handle) template <class T>
static inline T* Unwrap (v8::Handle<v8::Object> handle)
{ {
assert(!handle.IsEmpty()); assert(!handle.IsEmpty());
assert(handle->InternalFieldCount() == 1); assert(handle->InternalFieldCount() > 0);
return v8::Handle<v8::External>::Cast( return static_cast<T*>(v8::Handle<v8::External>::Cast(
handle->GetInternalField(0))->Value(); handle->GetInternalField(0))->Value());
} }


inline void Wrap(v8::Handle<v8::Object> handle) inline void Wrap(v8::Handle<v8::Object> handle)
Expand Down
10 changes: 5 additions & 5 deletions src/process.cc
Expand Up @@ -64,7 +64,7 @@ Process::Spawn (const Arguments& args)
} }


HandleScope scope; HandleScope scope;
Process *process = NODE_UNWRAP(Process, args.Holder()); Process *process = ObjectWrap::Unwrap<Process>(args.Holder());


String::Utf8Value command(args[0]->ToString()); String::Utf8Value command(args[0]->ToString());


Expand All @@ -80,7 +80,7 @@ Handle<Value>
Process::PIDGetter (Local<String> property, const AccessorInfo& info) Process::PIDGetter (Local<String> property, const AccessorInfo& info)
{ {
HandleScope scope; HandleScope scope;
Process *process = NODE_UNWRAP(Process, info.This()); Process *process = ObjectWrap::Unwrap<Process>(info.This());


assert(process); assert(process);
assert(property == PID_SYMBOL); assert(property == PID_SYMBOL);
Expand All @@ -95,7 +95,7 @@ Handle<Value>
Process::Write (const Arguments& args) Process::Write (const Arguments& args)
{ {
HandleScope scope; HandleScope scope;
Process *process = NODE_UNWRAP(Process, args.Holder()); Process *process = ObjectWrap::Unwrap<Process>(args.Holder());
assert(process); assert(process);


// XXX // XXX
Expand Down Expand Up @@ -146,7 +146,7 @@ Handle<Value>
Process::Kill (const Arguments& args) Process::Kill (const Arguments& args)
{ {
HandleScope scope; HandleScope scope;
Process *process = NODE_UNWRAP(Process, args.Holder()); Process *process = ObjectWrap::Unwrap<Process>(args.Holder());
assert(process); assert(process);


int sig = SIGTERM; int sig = SIGTERM;
Expand All @@ -163,7 +163,7 @@ Handle<Value>
Process::Close (const Arguments& args) Process::Close (const Arguments& args)
{ {
HandleScope scope; HandleScope scope;
Process *process = NODE_UNWRAP(Process, args.Holder()); Process *process = ObjectWrap::Unwrap<Process>(args.Holder());
assert(process); assert(process);
return process->Close() == 0 ? True() : False(); return process->Close() == 0 ? True() : False();
} }
Expand Down
8 changes: 4 additions & 4 deletions src/timer.cc
Expand Up @@ -32,7 +32,7 @@ Handle<Value>
Timer::RepeatGetter (Local<String> property, const AccessorInfo& info) Timer::RepeatGetter (Local<String> property, const AccessorInfo& info)
{ {
HandleScope scope; HandleScope scope;
Timer *timer = NODE_UNWRAP(Timer, info.This()); Timer *timer = ObjectWrap::Unwrap<Timer>(info.This());


assert(timer); assert(timer);
assert (property == REPEAT_SYMBOL); assert (property == REPEAT_SYMBOL);
Expand All @@ -46,7 +46,7 @@ void
Timer::RepeatSetter (Local<String> property, Local<Value> value, const AccessorInfo& info) Timer::RepeatSetter (Local<String> property, Local<Value> value, const AccessorInfo& info)
{ {
HandleScope scope; HandleScope scope;
Timer *timer = NODE_UNWRAP(Timer, info.This()); Timer *timer = ObjectWrap::Unwrap<Timer>(info.This());


assert(timer); assert(timer);
assert(property == REPEAT_SYMBOL); assert(property == REPEAT_SYMBOL);
Expand Down Expand Up @@ -90,7 +90,7 @@ Timer::New (const Arguments& args)
Handle<Value> Handle<Value>
Timer::Start (const Arguments& args) Timer::Start (const Arguments& args)
{ {
Timer *timer = NODE_UNWRAP(Timer, args.Holder()); Timer *timer = ObjectWrap::Unwrap<Timer>(args.Holder());
HandleScope scope; HandleScope scope;


if (args.Length() != 2) if (args.Length() != 2)
Expand All @@ -111,7 +111,7 @@ Timer::Start (const Arguments& args)
Handle<Value> Handle<Value>
Timer::Stop (const Arguments& args) Timer::Stop (const Arguments& args)
{ {
Timer *timer = NODE_UNWRAP(Timer, args.Holder()); Timer *timer = ObjectWrap::Unwrap<Timer>(args.Holder());
ev_timer_stop(EV_DEFAULT_UC_ &timer->watcher_); ev_timer_stop(EV_DEFAULT_UC_ &timer->watcher_);
timer->Detach(); timer->Detach();
return Undefined(); return Undefined();
Expand Down

0 comments on commit 22c3a1e

Please sign in to comment.