summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2009-11-23 00:59:36 +0100
committerRyan Dahl <ry@tinyclouds.org>2009-11-23 00:59:39 +0100
commitdc093ef833561cfa8d85fbd649f62d2045d4e61f (patch)
treefbd7547be0d69121f72b84d528a9000d22a22d7a
parent9c1034b1832ea861fdacd4788dea4686c33bacc5 (diff)
downloadnodejs-dc093ef833561cfa8d85fbd649f62d2045d4e61f.tar.gz
nodejs-dc093ef833561cfa8d85fbd649f62d2045d4e61f.tar.bz2
nodejs-dc093ef833561cfa8d85fbd649f62d2045d4e61f.zip
Add process.loop() process.unloop()!!!
Move the event loop calls into javascript. Makes life so much easier.
-rw-r--r--src/node.cc55
-rw-r--r--src/node.js9
2 files changed, 30 insertions, 34 deletions
diff --git a/src/node.cc b/src/node.cc
index dcb111003..76f5eac28 100644
--- a/src/node.cc
+++ b/src/node.cc
@@ -244,6 +244,25 @@ static Handle<Value> ByteLength(const Arguments& args) {
return scope.Close(length);
}
+static Handle<Value> Loop(const Arguments& args) {
+ HandleScope scope;
+ ev_loop(EV_DEFAULT_UC_ 0);
+ return Undefined();
+}
+
+static Handle<Value> 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<Value> Chdir(const Arguments& args) {
HandleScope scope;
@@ -691,6 +710,8 @@ static Local<Object> 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<Object> Load(int argc, char *argv[]) {
ExecuteNativeJS("node.js", native_node);
}
-static void EmitExitEvent() {
- HandleScope scope;
-
- // Get the 'emit' function from 'process'
- Local<Value> emit_v = process->Get(String::NewSymbol("emit"));
- if (!emit_v->IsFunction()) {
- // could not emit exit event so exit
- exit(10);
- }
- // Cast
- Local<Function> emit = Local<Function>::Cast(emit_v);
-
- TryCatch try_catch;
-
- // Arguments for the emit('exit')
- Local<Value> 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 dcc8e6605..73645f0a9 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