#include "events.h" #include #include #include #include #include #include #include #include #include /* inet_ntop */ #include /* sockaddr_in, sockaddr_in6 */ #ifndef RAMP # define RAMP(x) ((x) > 0 ? (x) : 0) #endif using namespace v8; using namespace node; Persistent EventEmitter::constructor_template; void EventEmitter::Initialize (v8::Handle target) { HandleScope scope; Local t = FunctionTemplate::New(); constructor_template = Persistent::New(t); Local __emit = FunctionTemplate::New(Emit); constructor_template->PrototypeTemplate()->Set(String::NewSymbol("emit"), __emit); // All other prototype methods are defined in events.js target->Set(String::NewSymbol("EventEmitter"), constructor_template->GetFunction()); } static bool ReallyEmit (Handle self, Handle event, int argc, Handle argv[]) { HandleScope scope; Local events_v = self->Get(String::NewSymbol("_events")); if (!events_v->IsObject()) return false; Local events = events_v->ToObject(); Local listeners_v = events->Get(event); if (!listeners_v->IsArray()) return false; Local listeners = Local::Cast(listeners_v); for (unsigned int i = 0; i < listeners->Length(); i++) { HandleScope scope; Local listener_v = listeners->Get(Integer::New(i)); if (!listener_v->IsFunction()) continue; Local listener = Local::Cast(listener_v); TryCatch try_catch; listener->Call(self, argc, argv); if (try_catch.HasCaught()) { FatalException(try_catch); return false; } } return true; } Handle EventEmitter::Emit (const Arguments& args) { HandleScope scope; Local event = args[0]->ToString(); int argc = 0; Local emit_args; if (args[1]->IsArray()) { emit_args = Local::Cast(args[1]); argc = emit_args->Length(); } Local argv[argc]; for (int i = 0; i < argc; i++) { argv[i] = emit_args->Get(Integer::New(i)); } bool r = ReallyEmit(args.Holder(), event, argc, argv); return scope.Close(r ? True() : False()); } bool EventEmitter::Emit (const char *event_s, int argc, Handle argv[]) { HandleScope scope; Local event = String::NewSymbol(event_s); return ReallyEmit(handle_, event, argc, argv); } Persistent Promise::constructor_template; void Promise::Initialize (v8::Handle target) { HandleScope scope; Local t = FunctionTemplate::New(); constructor_template = Persistent::New(t); constructor_template->Inherit(EventEmitter::constructor_template); constructor_template->InstanceTemplate()->SetInternalFieldCount(1); // All prototype methods are defined in events.js target->Set(String::NewSymbol("Promise"), constructor_template->GetFunction()); } Promise* Promise::Create (void) { HandleScope scope; Local handle = Promise::constructor_template->GetFunction()->NewInstance(); Promise *promise = new Promise(); promise->Wrap(handle); promise->Attach(); return promise; } bool Promise::EmitSuccess (int argc, v8::Handle argv[]) { bool r = Emit("success", argc, argv); Detach(); return r; } bool Promise::EmitError (int argc, v8::Handle argv[]) { bool r = Emit("error", argc, argv); Detach(); return r; } void EIOPromise::Attach (void) { ObjectWrap::Attach(); ev_ref(EV_DEFAULT_UC); } void EIOPromise::Detach (void) { ObjectWrap::Detach(); ev_unref(EV_DEFAULT_UC); } EIOPromise* EIOPromise::Create (void) { HandleScope scope; Local handle = Promise::constructor_template->GetFunction()->NewInstance(); EIOPromise *promise = new EIOPromise(); promise->Wrap(handle); promise->Attach(); return promise; }