Skip to content

Commit

Permalink
Simply C++ event emitter
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Jun 29, 2010
1 parent b69c6a0 commit 5aadeae
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 39 deletions.
43 changes: 6 additions & 37 deletions src/node_events.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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
2 changes: 0 additions & 2 deletions src/node_events.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 () { }
};

Expand Down

0 comments on commit 5aadeae

Please sign in to comment.