From 23c7f472d066087d8053e31e3797a91053a79d9a Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 28 Sep 2009 16:50:19 +0200 Subject: [PATCH] API: Move node.exit() to process.exit(). --- benchmark/process_loop.js | 2 +- doc/api.html | 89 ++++++++++++++++++++----------------- doc/api.txt | 67 ++++++++++++---------------- doc/api.xml | 92 +++++++++++++++++++++------------------ doc/node.1 | 83 +++++++++++++++-------------------- src/node.cc | 22 +++++++--- src/node.js | 19 +++++--- 7 files changed, 189 insertions(+), 185 deletions(-) diff --git a/benchmark/process_loop.js b/benchmark/process_loop.js index ec971f0959..2e1e35465a 100644 --- a/benchmark/process_loop.js +++ b/benchmark/process_loop.js @@ -11,7 +11,7 @@ function next (i) { }); child.addListener("exit", function (code) { - if (code != 0) node.exit(-1); + if (code != 0) process.exit(-1); next(i - 1); }); } diff --git a/doc/api.html b/doc/api.html index b396955dbd..80feec48d7 100644 --- a/doc/api.html +++ b/doc/api.html @@ -61,18 +61,10 @@ of the 16bit javascript string characters. Both are relatively fast—use them if you can. "utf8" is slower and should be avoided when possible.

Unless otherwise noted, functions are all asynchronous and do not block execution.

-

Helpers and Global Variables

+

Helpers

These objects are available to all programs.

-node.exit(code) -
-
-

-Immediately ends the process with the specified code. -

-
-
node.cwd()
@@ -81,36 +73,73 @@ Returns the current working directory of the process.

-ARGV +__filename

-An array containing the command line arguments. +The filename of the script being executed.

+
+

The process Object

+

process is the equivalent of window in browser-side javascript. It is +the global scope. process is an instance of node.EventEmitter.

+
+ ++++ + + + + + + + + + + + + + +
Event Parameters Notes

"exit"

code

Made when the process exits. + A listener on this event should not try to perform + I/O since the process will forcibly exit in less + than microsecond. However, it is a good hook to + perform constant time checks of the module’s + state (like for unit tests). +
+ The parameter code is the integer exit code + passed to process.exit().

+
+
-ENV +process.exit(code=0)

-An object containing the user environment. See environ(7). +Ends the process with the specified code. By default it exits with the +success code 0.

-__filename +process.ARGV

-The filename of the script being executed. +An array containing the command line arguments.

-process +process.ENV

-A special global object. The process object is like the window object of -browser-side javascript. +An object containing the user environment. See environ(7).

@@ -475,28 +504,6 @@ variable (which should be a list of paths, colon separated).

is run. These are currently undocumented, but do look them up in your system.

(Functions require_async() and include_async() also exist.)

-

process.addListener("exit", function () { })

-

When the program exits a special object called process will emit an -"exit" event.

-

The "exit" event cannot perform I/O since the process is going to -forcibly exit in less than microsecond. However, it is a good hook to -perform constant time checks of the module’s state. E.G. for unit tests:

-
-
-
include("asserts.js");
-
-var timer_executed = false;
-
-setTimeout(function () {
-  timer_executed = true
-}, 1000);
-
-process.addListener("exit", function () {
-  assertTrue(timer_executed);
-});
-
-

Just to reiterate: the "exit" event, is not the place to close files or -shutdown servers. The process will exit before they get performed.

Timers

@@ -1948,7 +1955,7 @@ init (Handle<Object> target) diff --git a/doc/api.txt b/doc/api.txt index 141be0dd91..4a1f7fb176 100644 --- a/doc/api.txt +++ b/doc/api.txt @@ -47,29 +47,44 @@ Unless otherwise noted, functions are all asynchronous and do not block execution. -=== Helpers and Global Variables +=== Helpers These objects are available to all programs. -+node.exit(code)+:: -Immediately ends the process with the specified code. - +node.cwd()+:: Returns the current working directory of the process. -+ARGV+ :: -An array containing the command line arguments. - -+ENV+ :: -An object containing the user environment. See environ(7). - +__filename+ :: The filename of the script being executed. -+process+ :: -A special global object. The +process+ object is like the +window+ object of -browser-side javascript. +=== The +process+ Object ++process+ is the equivalent of +window+ in browser-side javascript. It is +the global scope. +process+ is an instance of +node.EventEmitter+. + +[cols="1,2,10",options="header"] +|========================================================= +| Event | Parameters | Notes +| +"exit"+ | +code+ | Made when the process exits. + A listener on this event should not try to perform + I/O since the process will forcibly exit in less + than microsecond. However, it is a good hook to + perform constant time checks of the module's + state (like for unit tests). + + + The parameter +code+ is the integer exit code + passed to +process.exit()+. +|========================================================= + ++process.exit(code=0)+:: +Ends the process with the specified code. By default it exits with the +success code 0. + ++process.ARGV+ :: +An array containing the command line arguments. + ++process.ENV+ :: +An object containing the user environment. See environ(7). === Utilities @@ -306,32 +321,6 @@ system. (Functions +require_async()+ and +include_async()+ also exist.) -==== +process.addListener("exit", function () { })+ - -When the program exits a special object called +process+ will emit an -+"exit"+ event. - -The +"exit"+ event cannot perform I/O since the process is going to -forcibly exit in less than microsecond. However, it is a good hook to -perform constant time checks of the module's state. E.G. for unit tests: - ----------------------------------------- -include("asserts.js"); - -var timer_executed = false; - -setTimeout(function () { - timer_executed = true -}, 1000); - -process.addListener("exit", function () { - assertTrue(timer_executed); -}); ----------------------------------------- - -Just to reiterate: the +"exit"+ event, is not the place to close files or -shutdown servers. The process will exit before they get performed. - === Timers diff --git a/doc/api.xml b/doc/api.xml index 311ec9c8ae..1ce13fcd45 100644 --- a/doc/api.xml +++ b/doc/api.xml @@ -33,22 +33,12 @@ of the 16bit javascript string characters. Both are relatively fast—use them if you can. "utf8" is slower and should be avoided when possible. Unless otherwise noted, functions are all asynchronous and do not block execution. - -Helpers and Global Variables + +Helpers These objects are available to all programs. -node.exit(code) - - - -Immediately ends the process with the specified code. - - - - - node.cwd() @@ -59,42 +49,81 @@ Returns the current working directory of the process. -ARGV +__filename -An array containing the command line arguments. +The filename of the script being executed. + + + +The <literal>process</literal> Object +process is the equivalent of window in browser-side javascript. It is +the global scope. process is an instance of node.EventEmitter. + + + + + + + + Event + Parameters + Notes + + + + +"exit" +code +Made when the process exits. + A listener on this event should not try to perform + I/O since the process will forcibly exit in less + than microsecond. However, it is a good hook to + perform constant time checks of the module’s + state (like for unit tests). + + The parameter code is the integer exit code + passed to process.exit(). + + + + + -ENV +process.exit(code=0) -An object containing the user environment. See environ(7). +Ends the process with the specified code. By default it exits with the +success code 0. -__filename +process.ARGV -The filename of the script being executed. +An array containing the command line arguments. -process +process.ENV -A special global object. The process object is like the window object of -browser-side javascript. +An object containing the user environment. See environ(7). @@ -488,27 +517,6 @@ variable (which should be a list of paths, colon separated). is run. These are currently undocumented, but do look them up in your system. (Functions require_async() and include_async() also exist.) - -<literal>process.addListener("exit", function () { })</literal> -When the program exits a special object called process will emit an -"exit" event. -The "exit" event cannot perform I/O since the process is going to -forcibly exit in less than microsecond. However, it is a good hook to -perform constant time checks of the module’s state. E.G. for unit tests: -include("asserts.js"); - -var timer_executed = false; - -setTimeout(function () { - timer_executed = true -}, 1000); - -process.addListener("exit", function () { - assertTrue(timer_executed); -}); -Just to reiterate: the "exit" event, is not the place to close files or -shutdown servers. The process will exit before they get performed. - Timers diff --git a/doc/node.1 b/doc/node.1 index 1b08915ea2..836c307d62 100644 --- a/doc/node.1 +++ b/doc/node.1 @@ -42,41 +42,58 @@ Node supports 3 string encodings\. UTF\-8 ("utf8"), ASCII ("ascii"), and Binary .sp Unless otherwise noted, functions are all asynchronous and do not block execution\. .sp -.SS "Helpers and Global Variables" +.SS "Helpers" These objects are available to all programs\. .PP -node\.exit(code) -.RS 4 -Immediately ends the process with the specified code\. -.RE -.PP node\.cwd() .RS 4 Returns the current working directory of the process\. .RE .PP -ARGV +__filename .RS 4 -An array containing the command line arguments\. +The filename of the script being executed\. .RE +.SS "The process Object" +process is the equivalent of window in browser\-side javascript\. It is the global scope\. process is an instance of node\.EventEmitter\. +.sp +.TS +allbox tab(:); +ltB ltB ltBx. +T{ +Event +T}:T{ +Parameters +T}:T{ +Notes +T} +.T& +lt lt lt. +T{ +"exit" +.sp +T}:T{ +code +.sp +T}:T{ +Made when the process exits\. A listener on this event should not try to perform I/O since the process will forcibly exit in less than microsecond\. However, it is a good hook to perform constant time checks of the module\(cqs state (like for unit tests)\. The parameter code is the integer exit code passed to process\.exit()\. +.sp +T} +.TE .PP -ENV +process\.exit(code=0) .RS 4 -An object containing the user environment\. See environ(7)\. +Ends the process with the specified code\. By default it exits with the success code 0\. .RE .PP -__filename +process\.ARGV .RS 4 -The filename of the script being executed\. +An array containing the command line arguments\. .RE .PP -process +process\.ENV .RS 4 -A special global object\. The -process -object is like the -window -object of browser\-side javascript\. +An object containing the user environment\. See environ(7)\. .RE .SS "Utilities" These function are in "/utils\.js"\. Use require("/utils\.js") to access them\. @@ -418,36 +435,6 @@ Node comes with several libraries which are installed when "make install" is run .sp (Functions require_async() and include_async() also exist\.) .sp -.sp -.it 1 an-trap -.nr an-no-space-flag 1 -.nr an-break-flag 1 -.br -process.addListener("exit", function () { }) -.RS -When the program exits a special object called process will emit an "exit" event\. -.sp -The "exit" event cannot perform I/O since the process is going to forcibly exit in less than microsecond\. However, it is a good hook to perform constant time checks of the module\(cqs state\. E\.G\. for unit tests: -.sp -.sp -.RS 4 -.nf -include("asserts\.js"); - -var timer_executed = false; - -setTimeout(function () { - timer_executed = true -}, 1000); - -process\.addListener("exit", function () { - assertTrue(timer_executed); -}); -.fi -.RE -Just to reiterate: the "exit" event, is not the place to close files or shutdown servers\. The process will exit before they get performed\. -.sp -.RE .SS "Timers" .PP setTimeout(callback, delay) diff --git a/src/node.cc b/src/node.cc index e1eabe4213..f452f66028 100644 --- a/src/node.cc +++ b/src/node.cc @@ -408,15 +408,23 @@ static Local Load(int argc, char *argv[]) { return scope.Close(node_obj); } -static void CallExitHandler(Handle node_obj) { +static void CallExitHandler() { HandleScope scope; - Local exit_v = node_obj->Get(String::New("exit")); - assert(exit_v->IsFunction()); - Handle exit_f = Handle::Cast(exit_v); + Local process = Context::GetCurrent()->Global(); + Local emit_v = process->Get(String::NewSymbol("emit")); + if (!emit_v->IsFunction()) { + exit(10); // could not emit exit event so exit with error code 10. + } + Local emit = Local::Cast(emit_v); + TryCatch try_catch; - exit_f->Call(Context::GetCurrent()->Global(), 0, NULL); - if (try_catch.HasCaught()) + + Local argv[2] = { String::New("exit"), Integer::New(0) }; + emit->Call(process, 2, argv); + + if (try_catch.HasCaught()) { node::FatalException(try_catch); + } } static void PrintHelp() { @@ -494,7 +502,7 @@ int main(int argc, char *argv[]) { ev_loop(EV_DEFAULT_UC_ 0); // main event loop - node::CallExitHandler(node_obj); + node::CallExitHandler(); context.Dispose(); V8::Dispose(); diff --git a/src/node.js b/src/node.js index 917a15daa4..8922e2dfe7 100644 --- a/src/node.js +++ b/src/node.js @@ -202,7 +202,7 @@ node.Module.prototype.loadObject = function (loadPromise) { loadPromise.emitSuccess(self.target); } else { loadPromise.emitError(new Error("Error reading " + self.filename)); - node.exit(1); + process.exit(1); } }); }; @@ -213,7 +213,7 @@ node.Module.prototype.loadScript = function (loadPromise) { catPromise.addErrback(function () { loadPromise.emitError(new Error("Error reading " + self.filename)); - node.exit(1); + process.exit(1); }); catPromise.addCallback(function (content) { @@ -256,6 +256,16 @@ node.Module.prototype.waitChildrenLoad = function (callback) { if (children.length == nloaded && callback) callback(); }; + +process.exit = function (code) { + process.emit("exit"); + node.reallyExit(code); +}; + +node.exit = function (code) { + throw new Error("process.exit() has been renamed to process.exit()."); +}; + (function () { var cwd = node.cwd(); @@ -270,9 +280,4 @@ node.Module.prototype.waitChildrenLoad = function (callback) { // Load the root module--the command line argument. node.loadModule(ARGV[1], process); - - node.exit = function (code) { - process.emit("exit"); - node.reallyExit(code); - }; }());