Factor out TryCatch in ReallyEmit
authorRyan Dahl <ry@tinyclouds.org>
Fri, 12 Mar 2010 21:45:49 +0000 (13:45 -0800)
committerRyan Dahl <ry@tinyclouds.org>
Fri, 12 Mar 2010 21:48:03 +0000 (13:48 -0800)
src/node_events.cc

index 9630aa6..d88229f 100644 (file)
@@ -43,8 +43,8 @@ static bool ReallyEmit(Handle<Object> self,
                        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);
   if (!events_v->IsObject()) return false;
   Local<Object> events = events_v->ToObject();
@@ -52,12 +52,12 @@ static bool ReallyEmit(Handle<Object> self,
   Local<Value> listeners_v = events->Get(event);
   Local<Function> listener;
 
+  TryCatch try_catch;
+
   if (listeners_v->IsFunction()) {
     // Optimized one-listener case
     Local<Function> listener = Local<Function>::Cast(listeners_v);
 
-    TryCatch try_catch;
-
     listener->Call(self, argc, argv);
 
     if (try_catch.HasCaught()) {
@@ -68,15 +68,11 @@ static bool ReallyEmit(Handle<Object> self,
   } else if (listeners_v->IsArray()) {
     Local<Array> listeners = Local<Array>::Cast(listeners_v);
 
-    for (unsigned int i = 0; i < listeners->Length(); i++) {
-      HandleScope scope;
-
-      Local<Value> listener_v = listeners->Get(Integer::New(i));
+    for (uint32_t i = 0; i < listeners->Length(); i++) {
+      Local<Value> listener_v = listeners->Get(i);
       if (!listener_v->IsFunction()) continue;
       Local<Function> listener = Local<Function>::Cast(listener_v);
 
-      TryCatch try_catch;
-
       listener->Call(self, argc, argv);
 
       if (try_catch.HasCaught()) {