From 5aadeae88893fc1d6e417c75e67169749a97f16c Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 28 Jun 2010 21:09:44 -0700 Subject: [PATCH] Simply C++ event emitter --- src/node_events.cc | 43 ++++++------------------------------------- src/node_events.h | 2 -- 2 files changed, 6 insertions(+), 39 deletions(-) diff --git a/src/node_events.cc b/src/node_events.cc index c43225a528..e95fe71289 100644 --- a/src/node_events.cc +++ b/src/node_events.cc @@ -28,9 +28,6 @@ void EventEmitter::Initialize(Local ctemplate) { constructor_template = Persistent::New(ctemplate); - Local __emit = FunctionTemplate::New(Emit); - constructor_template->PrototypeTemplate()->Set(String::NewSymbol("emit"), - __emit); constructor_template->SetClassName(String::NewSymbol("EventEmitter")); events_symbol = NODE_PSYMBOL("_events"); @@ -38,13 +35,12 @@ void EventEmitter::Initialize(Local ctemplate) { // All other prototype methods are defined in events.js } -static bool ReallyEmit(Handle self, - Handle event, - int argc, - Handle argv[]) { + +bool EventEmitter::Emit(Handle event, int argc, Handle argv[]) { + HandleScope scope; // HandleScope not needed here because only called from one of the two // functions below - Local events_v = self->Get(events_symbol); + Local events_v = handle_->Get(events_symbol); if (!events_v->IsObject()) return false; Local events = events_v->ToObject(); @@ -56,7 +52,7 @@ static bool ReallyEmit(Handle self, // Optimized one-listener case Local listener = Local::Cast(listeners_v); - listener->Call(self, argc, argv); + listener->Call(handle_, argc, argv); if (try_catch.HasCaught()) { FatalException(try_catch); @@ -71,7 +67,7 @@ static bool ReallyEmit(Handle self, if (!listener_v->IsFunction()) continue; Local listener = Local::Cast(listener_v); - listener->Call(self, argc, argv); + listener->Call(handle_, argc, argv); if (try_catch.HasCaught()) { FatalException(try_catch); @@ -86,31 +82,4 @@ static bool ReallyEmit(Handle self, return true; } -Handle EventEmitter::Emit(const Arguments& args) { - HandleScope scope; - - if (args.Length() == 0) { - return ThrowException(Exception::TypeError( - String::New("Must give event name."))); - } - - Local event = args[0]->ToString(); - - int argc = args.Length() - 1; - Local argv[argc]; - - for (int i = 0; i < argc; i++) { - argv[i] = args[i+1]; - } - - bool r = ReallyEmit(args.Holder(), event, argc, argv); - - return scope.Close(r ? True() : False()); -} - -bool EventEmitter::Emit(Handle event, int argc, Handle argv[]) { - HandleScope scope; - return ReallyEmit(handle_, event, argc, argv); -} - } // namespace node diff --git a/src/node_events.h b/src/node_events.h index 2cfcaf02f9..cb4391ac4a 100644 --- a/src/node_events.h +++ b/src/node_events.h @@ -17,8 +17,6 @@ class EventEmitter : public ObjectWrap { v8::Handle argv[]); protected: - static v8::Handle Emit(const v8::Arguments& args); - EventEmitter() : ObjectWrap () { } };