From dc093ef833561cfa8d85fbd649f62d2045d4e61f Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 23 Nov 2009 00:59:36 +0100 Subject: [PATCH] Add process.loop() process.unloop()!!! Move the event loop calls into javascript. Makes life so much easier. --- src/node.cc | 55 ++++++++++++++++++++--------------------------------- src/node.js | 9 +++++++++ 2 files changed, 30 insertions(+), 34 deletions(-) diff --git a/src/node.cc b/src/node.cc index dcb1110035..76f5eac28e 100644 --- a/src/node.cc +++ b/src/node.cc @@ -244,6 +244,25 @@ static Handle ByteLength(const Arguments& args) { return scope.Close(length); } +static Handle Loop(const Arguments& args) { + HandleScope scope; + ev_loop(EV_DEFAULT_UC_ 0); + return Undefined(); +} + +static Handle Unloop(const Arguments& args) { + HandleScope scope; + int how = EVUNLOOP_ONE; + if (args[0]->IsString()) { + String::Utf8Value how_s(args[0]->ToString()); + if (0 == strcmp(*how_s, "all")) { + how = EVUNLOOP_ALL; + } + } + ev_unloop(EV_DEFAULT_ how); + return Undefined(); +} + static Handle Chdir(const Arguments& args) { HandleScope scope; @@ -691,6 +710,8 @@ static Local Load(int argc, char *argv[]) { process->Set(String::NewSymbol("pid"), Integer::New(getpid())); // define various internal methods + NODE_SET_METHOD(process, "loop", Loop); + NODE_SET_METHOD(process, "unloop", Unloop); NODE_SET_METHOD(process, "compile", Compile); NODE_SET_METHOD(process, "_byteLength", ByteLength); NODE_SET_METHOD(process, "reallyExit", Exit); @@ -740,30 +761,6 @@ static Local Load(int argc, char *argv[]) { ExecuteNativeJS("node.js", native_node); } -static void EmitExitEvent() { - HandleScope scope; - - // Get the 'emit' function from 'process' - Local emit_v = process->Get(String::NewSymbol("emit")); - if (!emit_v->IsFunction()) { - // could not emit exit event so exit - exit(10); - } - // Cast - Local emit = Local::Cast(emit_v); - - TryCatch try_catch; - - // Arguments for the emit('exit') - Local argv[2] = { String::New("exit"), Integer::New(0) }; - // Emit! - emit->Call(process, 2, argv); - - if (try_catch.HasCaught()) { - node::FatalException(try_catch); - } -} - static void PrintHelp() { printf("Usage: node [options] [--] script.js [arguments] \n" " -v, --version print node's version\n" @@ -887,16 +884,6 @@ int main(int argc, char *argv[]) { // so your next reading stop should be node::Load()! node::Load(argc, argv); - // All our arguments are loaded. We've evaluated all of the scripts. We - // might even have created TCP servers. Now we enter the main event loop. - // If there are no watchers on the loop (except for the ones that were - // ev_unref'd) then this function exits. As long as there are active - // watchers, it blocks. - ev_loop(EV_DEFAULT_UC_ 0); // main event loop - - // Once we've dropped out, emit the 'exit' event from 'process' - node::EmitExitEvent(); - #ifndef NDEBUG // Clean up. context.Dispose(); diff --git a/src/node.js b/src/node.js index dcc8e66059..73645f0a94 100644 --- a/src/node.js +++ b/src/node.js @@ -721,4 +721,13 @@ process.mainModule = createModule("."); var loadPromise = new process.Promise(); process.mainModule.load(process.ARGV[1], loadPromise); +// All our arguments are loaded. We've evaluated all of the scripts. We +// might even have created TCP servers. Now we enter the main eventloop. If +// there are no watchers on the loop (except for the ones that were +// ev_unref'd) then this function exits. As long as there are active +// watchers, it blocks. +process.loop(); + +process.emit("exit"); + }()); // end annonymous namespace