Browse Source

Add process.loop() process.unloop()!!!

Move the event loop calls into javascript.
Makes life so much easier.
v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
dc093ef833
  1. 55
      src/node.cc
  2. 9
      src/node.js

55
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();

9
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

Loading…
Cancel
Save