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 | 109 ++++++++++++++++++---------------- doc/api.txt | 67 +++++++++------------ doc/api.xml | 120 ++++++++++++++++++++------------------ doc/node.1 | 91 +++++++++++++---------------- src/node.cc | 22 ++++--- src/node.js | 19 +++--- 7 files changed, 217 insertions(+), 213 deletions(-) diff --git a/benchmark/process_loop.js b/benchmark/process_loop.js index ec971f0959b..2e1e35465a9 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 b396955dbd6..80feec48d7d 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,22 +73,6 @@ 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
@@ -104,13 +80,66 @@ An object containing the user environment. See environ(7). 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().

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

-A special global object. The process object is like the window object of -browser-side javascript. +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).

@@ -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 141be0dd911..4a1f7fb1769 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 311ec9c8ae1..1ce13fcd45f 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,26 +49,6 @@ 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 @@ -87,14 +57,73 @@ 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(). + + + + + -process +process.exit(code=0) -A special global object. The process object is like the window object of -browser-side javascript. +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). @@ -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 1b08915ea2d..836c307d62f 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 -.RS 4 -An array containing the command line arguments\. -.RE -.PP -ENV -.RS 4 -An object containing the user environment\. See environ(7)\. -.RE -.PP __filename .RS 4 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 -process +process\.exit(code=0) .RS 4 -A special global object\. The -process -object is like the -window -object of browser\-side javascript\. +Ends the process with the specified code\. By default it exits with the success code 0\. +.RE +.PP +process\.ARGV +.RS 4 +An array containing the command line arguments\. +.RE +.PP +process\.ENV +.RS 4 +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 e1eabe42131..f452f660289 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 917a15daa44..8922e2dfe71 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); - }; }());