Permalink
Browse files

Simply C++ event emitter

  • Loading branch information...
1 parent b69c6a0 commit 5aadeae88893fc1d6e417c75e67169749a97f16c @ry ry committed Jun 29, 2010
Showing with 6 additions and 39 deletions.
  1. +6 −37 src/node_events.cc
  2. +0 −2 src/node_events.h
View
@@ -28,23 +28,19 @@ void EventEmitter::Initialize(Local<FunctionTemplate> ctemplate) {
constructor_template = Persistent<FunctionTemplate>::New(ctemplate);
- Local<FunctionTemplate> __emit = FunctionTemplate::New(Emit);
- constructor_template->PrototypeTemplate()->Set(String::NewSymbol("emit"),
- __emit);
constructor_template->SetClassName(String::NewSymbol("EventEmitter"));
events_symbol = NODE_PSYMBOL("_events");
// All other prototype methods are defined in events.js
}
-static bool ReallyEmit(Handle<Object> self,
- Handle<String> event,
- int argc,
- Handle<Value> argv[]) {
+
+bool EventEmitter::Emit(Handle<String> event, int argc, Handle<Value> argv[]) {
+ HandleScope scope;
// HandleScope not needed here because only called from one of the two
// functions below
- Local<Value> events_v = self->Get(events_symbol);
+ Local<Value> events_v = handle_->Get(events_symbol);
if (!events_v->IsObject()) return false;
Local<Object> events = events_v->ToObject();
@@ -56,7 +52,7 @@ static bool ReallyEmit(Handle<Object> self,
// Optimized one-listener case
Local<Function> listener = Local<Function>::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<Object> self,
if (!listener_v->IsFunction()) continue;
Local<Function> listener = Local<Function>::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<Object> self,
return true;
}
-Handle<Value> EventEmitter::Emit(const Arguments& args) {
- HandleScope scope;
-
- if (args.Length() == 0) {
- return ThrowException(Exception::TypeError(
- String::New("Must give event name.")));
- }
-
- Local<String> event = args[0]->ToString();
-
- int argc = args.Length() - 1;
- Local<Value> 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<String> event, int argc, Handle<Value> argv[]) {
- HandleScope scope;
- return ReallyEmit(handle_, event, argc, argv);
-}
-
} // namespace node
View
@@ -17,8 +17,6 @@ class EventEmitter : public ObjectWrap {
v8::Handle<v8::Value> argv[]);
protected:
- static v8::Handle<v8::Value> Emit(const v8::Arguments& args);
-
EventEmitter() : ObjectWrap () { }
};

0 comments on commit 5aadeae

Please sign in to comment.