diff --git a/src/node.js b/src/node.js index ec4dc7ba48..b09c9b0807 100644 --- a/src/node.js +++ b/src/node.js @@ -20,14 +20,14 @@ node.tcp.createConnection = function (port, host) { // Timers function setTimeout (callback, after) { - var timer = new node.Timer(); + var timer = new node.Timer(); timer.addListener("timeout", callback); timer.start(after, 0); return timer; } function setInterval (callback, repeat) { - var timer = new node.Timer(); + var timer = new node.Timer(); timer.addListener("timeout", callback); timer.start(repeat, repeat); return timer; @@ -96,7 +96,7 @@ node.Module.prototype.load = function (callback) { self.loadPromise = loadPromise; var cat_promise = node.cat(self.filename, "utf8"); - + cat_promise.addErrback(function () { node.stdio.writeError("Error reading " + self.filename + "\n"); loadPromise.emitError(); @@ -124,13 +124,15 @@ node.Module.prototype.load = function (callback) { self.onLoad = self.target.__onLoad; self.onExit = self.target.__onExit; + if (self.onLoad || self.onExit) { + node.stdio.writeError( "(node) onLoad is depreciated it will be " + + "removed in the future. Don't want it to " + + "leave? Discuss on mailing list.\n" + ); + } self.waitChildrenLoad(function () { if (self.onLoad) { - node.stdio.writeError( "(node) onLoad is depreciated it will be " - + "removed in the future. Don't want it to " - + "leave? Discuss on mailing list.\n" - ); self.onLoad(); } self.loaded = true; @@ -140,7 +142,7 @@ node.Module.prototype.load = function (callback) { }; node.Module.prototype.newChild = function (path, target) { - var child = new node.Module({ + var child = new node.Module({ target: target, path: path, base_directory: node.path.dirname(this.filename), @@ -152,7 +154,7 @@ node.Module.prototype.newChild = function (path, target) { }; node.Module.prototype.waitChildrenLoad = function (callback) { - var nloaded = 0; + var nloaded = 0; var children = this.children; for (var i = 0; i < children.length; i++) { var child = children[i]; @@ -175,7 +177,7 @@ node.Module.prototype.exitChildren = function (callback) { for (var i = 0; i < children.length; i++) { children[i].exit(function () { nexited += 1; - if (nexited == children.length && callback) callback(); + if (nexited == children.length && callback) callback(); }); } }; @@ -197,14 +199,15 @@ node.Module.prototype.exit = function (callback) { (function () { // Load the root module--the command line argument. var root_module = new node.Module({ - path: node.path.filename(ARGV[1]), + path: node.path.filename(ARGV[1]), base_directory: node.path.dirname(ARGV[1]), - target: this + target: this }); root_module.load(); node.exit = function (code) { root_module.exit(function () { + process.emit("exit"); node.reallyExit(code); }); }; diff --git a/test/mjsunit/fixtures/a.js b/test/mjsunit/fixtures/a.js index 2d23232805..e94ac175fc 100644 --- a/test/mjsunit/fixtures/a.js +++ b/test/mjsunit/fixtures/a.js @@ -13,6 +13,6 @@ exports.D = function () { return c.D(); }; -function onExit () { +process.addListener("exit", function () { string = "A done"; -} +}); diff --git a/test/mjsunit/fixtures/b/c.js b/test/mjsunit/fixtures/b/c.js index be070e2d0c..4959fbc08c 100644 --- a/test/mjsunit/fixtures/b/c.js +++ b/test/mjsunit/fixtures/b/c.js @@ -10,6 +10,6 @@ exports.D = function () { return d.D(); }; -function onExit () { +process.addListener("exit", function () { string = "C done"; -} +}); diff --git a/test/mjsunit/fixtures/b/d.js b/test/mjsunit/fixtures/b/d.js index 3258850dbe..447f912395 100644 --- a/test/mjsunit/fixtures/b/d.js +++ b/test/mjsunit/fixtures/b/d.js @@ -4,7 +4,7 @@ exports.D = function () { return string; }; -function onExit () { +process.addListener("exit", function () { string = "D done"; -} +}); diff --git a/test/mjsunit/test-event-emitter-add-listeners.js b/test/mjsunit/test-event-emitter-add-listeners.js index f3d88e26aa..1b05b265a8 100644 --- a/test/mjsunit/test-event-emitter-add-listeners.js +++ b/test/mjsunit/test-event-emitter-add-listeners.js @@ -21,9 +21,9 @@ puts("start"); e.emit("hello", ["a", "b"]); -function onExit () { +process.addListener("exit", function () { assertArrayEquals(["hello"], events_new_listener_emited); assertEquals(1, times_hello_emited); -} +}); diff --git a/test/mjsunit/test-file-cat-noexist.js b/test/mjsunit/test-file-cat-noexist.js index e36a21810f..a98eb8e05d 100644 --- a/test/mjsunit/test-file-cat-noexist.js +++ b/test/mjsunit/test-file-cat-noexist.js @@ -16,6 +16,6 @@ promise.addErrback(function () { got_error = true; }); -function onExit () { +process.addListener("exit", function () { assertTrue(got_error); -} +}); diff --git a/test/mjsunit/test-fs-stat.js b/test/mjsunit/test-fs-stat.js index 4e01d97d1a..72c7d62e6e 100644 --- a/test/mjsunit/test-fs-stat.js +++ b/test/mjsunit/test-fs-stat.js @@ -16,9 +16,9 @@ promise.addErrback(function () { got_error = true; }); -function onExit () { +process.addListener("exit", function () { assertTrue(got_success); assertFalse(got_error); assertTrue(stats.mtime instanceof Date); -} +}); diff --git a/test/mjsunit/test-http-cat.js b/test/mjsunit/test-http-cat.js index e219c1ce75..10ce313b1a 100644 --- a/test/mjsunit/test-http-cat.js +++ b/test/mjsunit/test-http-cat.js @@ -28,7 +28,7 @@ node.http.cat("http://localhost:12312/", "utf8").addErrback(function () { bad_server_got_error = true; }); -function onExit () { +process.addListener("exit", function () { assertTrue(got_good_server_content); assertTrue(bad_server_got_error); -} +}); diff --git a/test/mjsunit/test-http-client-race.js b/test/mjsunit/test-http-client-race.js index fc3744ef02..af83623695 100644 --- a/test/mjsunit/test-http-client-race.js +++ b/test/mjsunit/test-http-client-race.js @@ -35,7 +35,7 @@ client.get("/1").finish(function (res1) { }); }); -function onExit () { +process.addListener("exit", function () { assertEquals(body1_s, body1); assertEquals(body2_s, body2); -} +}); diff --git a/test/mjsunit/test-http-client-upload.js b/test/mjsunit/test-http-client-upload.js index 2ad0610fd5..82bbbfc40f 100644 --- a/test/mjsunit/test-http-client-upload.js +++ b/test/mjsunit/test-http-client-upload.js @@ -43,8 +43,8 @@ req.finish(function(res) { }); }); -function onExit () { +process.addListener("exit", function () { assertEquals("1\n2\n3\n", sent_body); assertTrue(server_req_complete); assertTrue(client_res_complete); -} +}); diff --git a/test/mjsunit/test-http-proxy.js b/test/mjsunit/test-http-proxy.js index 3511487097..2b2527e7d3 100644 --- a/test/mjsunit/test-http-proxy.js +++ b/test/mjsunit/test-http-proxy.js @@ -47,6 +47,6 @@ req.finish(function (res) { }); }); -function onExit () { +process.addListener("exit", function () { assertEquals(body, "hello world\n"); -} +}); diff --git a/test/mjsunit/test-http-server.js b/test/mjsunit/test-http-server.js index 9074b1b446..6d0aa28b03 100644 --- a/test/mjsunit/test-http-server.js +++ b/test/mjsunit/test-http-server.js @@ -59,7 +59,7 @@ c.addListener("close", function () { assertEquals(c.readyState, "closed"); }); -function onExit () { +process.addListener("exit", function () { assertEquals(2, request_number); assertEquals(2, requests_sent); @@ -70,4 +70,4 @@ function onExit () { assertTrue(quit.exec(server_response) != null); assertTrue(client_got_eof); -} +}); diff --git a/test/mjsunit/test-http.js b/test/mjsunit/test-http.js index e92a410121..76584593e0 100644 --- a/test/mjsunit/test-http.js +++ b/test/mjsunit/test-http.js @@ -56,7 +56,7 @@ setTimeout(function () { }); }, 1); -function onExit () { +process.addListener("exit", function () { node.debug("responses_recvd: " + responses_recvd); assertEquals(2, responses_recvd); @@ -65,5 +65,5 @@ function onExit () { assertEquals("The path was /hello", body0); assertEquals("The path was /world", body1); -} +}); diff --git a/test/mjsunit/test-module-loading.js b/test/mjsunit/test-module-loading.js index 661ea12b82..342b45014e 100644 --- a/test/mjsunit/test-module-loading.js +++ b/test/mjsunit/test-module-loading.js @@ -20,7 +20,7 @@ assertEquals("D", d.D()); assertInstanceof(d2.D, Function); assertEquals("D", d2.D()); -function onExit () { +process.addListener("exit", function () { assertInstanceof(a.A, Function); assertEquals("A done", a.A()); @@ -35,4 +35,4 @@ function onExit () { assertInstanceof(d2.D, Function); assertEquals("D done", d2.D()); -} +}); diff --git a/test/mjsunit/test-node-cat.js b/test/mjsunit/test-node-cat.js index b6d7eb546c..2633b332fa 100644 --- a/test/mjsunit/test-node-cat.js +++ b/test/mjsunit/test-node-cat.js @@ -45,7 +45,7 @@ promise.addErrback(function () { errors += 1; }); -function onExit () { +process.addListener("exit", function () { assertEquals(2, successes); assertEquals(0, errors); -} +}); diff --git a/test/mjsunit/test-process-buffering.js b/test/mjsunit/test-process-buffering.js index 5d7ca4e56f..9928f1e923 100644 --- a/test/mjsunit/test-process-buffering.js +++ b/test/mjsunit/test-process-buffering.js @@ -24,6 +24,6 @@ pwd(function (result) { assertEquals("\n", result[result.length-1]); }); -function onExit () { +process.addListener("exit", function () { assertTrue(pwd_called); -} +}); diff --git a/test/mjsunit/test-process-kill.js b/test/mjsunit/test-process-kill.js index 4f6fe23b97..6283a07fe0 100644 --- a/test/mjsunit/test-process-kill.js +++ b/test/mjsunit/test-process-kill.js @@ -10,6 +10,6 @@ cat.addListener("exit", function (status) { exit_status = status; }); cat.kill(); -function onExit () { +process.addListener("exit", function () { assertTrue(exit_status > 0); -} +}); diff --git a/test/mjsunit/test-process-simple.js b/test/mjsunit/test-process-simple.js index 42c130b898..9ccf3c11ac 100644 --- a/test/mjsunit/test-process-simple.js +++ b/test/mjsunit/test-process-simple.js @@ -28,7 +28,7 @@ cat.write("hello"); cat.write(" "); cat.write("world"); -function onExit () { +process.addListener("exit", function () { assertEquals(0, exit_status); assertEquals("hello world", response); -} +}); diff --git a/test/mjsunit/test-process-spawn-loop.js b/test/mjsunit/test-process-spawn-loop.js index f9583f7d9b..dabdcffb1a 100644 --- a/test/mjsunit/test-process-spawn-loop.js +++ b/test/mjsunit/test-process-spawn-loop.js @@ -22,6 +22,6 @@ function spawn (i) { spawn(0); -function onExit () { +process.addListener("exit", function () { assertTrue(finished); -} +}); diff --git a/test/mjsunit/test-promise-wait.js b/test/mjsunit/test-promise-wait.js index 1ef75c15f9..5a41277aa2 100644 --- a/test/mjsunit/test-promise-wait.js +++ b/test/mjsunit/test-promise-wait.js @@ -73,10 +73,10 @@ assertArrayEquals(["a","b","c"], ret4); assertTrue(p4_done); -function onExit() { +process.addListener("exit", function () { assertTrue(p1_done); assertTrue(p2_done); assertTrue(p3_done); assertTrue(p4_done); assertTrue(p5_done); -} +}); diff --git a/test/mjsunit/test-tcp-many-clients.js b/test/mjsunit/test-tcp-many-clients.js index ee168065b7..3cc528a532 100644 --- a/test/mjsunit/test-tcp-many-clients.js +++ b/test/mjsunit/test-tcp-many-clients.js @@ -62,7 +62,7 @@ for (var i = 0; i < concurrency; i++) { }); } -function onExit () { +process.addListener("exit", function () { assertEquals(connections_per_client * concurrency, total_connections); puts("\nokay!"); -} +}); diff --git a/test/mjsunit/test-tcp-pingpong-delay.js b/test/mjsunit/test-tcp-pingpong-delay.js index cadd615537..bd258fc741 100644 --- a/test/mjsunit/test-tcp-pingpong-delay.js +++ b/test/mjsunit/test-tcp-pingpong-delay.js @@ -85,6 +85,6 @@ function pingPongTest (port, host, on_complete) { pingPongTest(21988); -function onExit () { +process.addListener("exit", function () { assertEquals(1, tests_run); -} +}); diff --git a/test/mjsunit/test-tcp-pingpong.js b/test/mjsunit/test-tcp-pingpong.js index 20e3236840..a0a07bdb12 100644 --- a/test/mjsunit/test-tcp-pingpong.js +++ b/test/mjsunit/test-tcp-pingpong.js @@ -82,6 +82,6 @@ pingPongTest(20989, "localhost"); pingPongTest(20988, null); pingPongTest(20997, "::1"); -function onExit () { +process.addListener("exit", function () { assertEquals(3, tests_run); -} +}); diff --git a/test/mjsunit/test-tcp-raw.js b/test/mjsunit/test-tcp-raw.js index d33132b769..202f3a3fd7 100644 --- a/test/mjsunit/test-tcp-raw.js +++ b/test/mjsunit/test-tcp-raw.js @@ -36,10 +36,10 @@ c.addListener("close", function () { echoServer.close(); }); -function onExit () { +process.addListener("exit", function () { var expected = []; for (var i = 0; i < 256; i++) { expected.push(i); } assertEquals(expected, recv); -} +}); diff --git a/test/mjsunit/test-tcp-reconnect.js b/test/mjsunit/test-tcp-reconnect.js index 6b40cc96c8..3a294dc8b4 100644 --- a/test/mjsunit/test-tcp-reconnect.js +++ b/test/mjsunit/test-tcp-reconnect.js @@ -46,7 +46,7 @@ client.addListener("close", function (had_error) { server.close(); }); -function onExit () { +process.addListener("exit", function () { assertEquals(N+1, disconnect_count); assertEquals(N+1, client_recv_count); -} +}); diff --git a/test/mjsunit/test-tcp-throttle-kernel-buffer.js b/test/mjsunit/test-tcp-throttle-kernel-buffer.js index 1cca5584e8..44e6dd29a4 100644 --- a/test/mjsunit/test-tcp-throttle-kernel-buffer.js +++ b/test/mjsunit/test-tcp-throttle-kernel-buffer.js @@ -49,7 +49,7 @@ client.addListener("eof", function () { client.close(); }); -function onExit () { +process.addListener("exit", function () { assertEquals(N, chars_recved); assertTrue(npauses > 2); -} +}); diff --git a/test/mjsunit/test-tcp-throttle.js b/test/mjsunit/test-tcp-throttle.js index 22853d8bf3..4fbe8cfbd7 100644 --- a/test/mjsunit/test-tcp-throttle.js +++ b/test/mjsunit/test-tcp-throttle.js @@ -61,6 +61,6 @@ client.addListener("eof", function () { client.close(); }); -function onExit () { +process.addListener("exit", function () { assertEquals(N, recv.length); -} +}); diff --git a/test/mjsunit/test-timers.js b/test/mjsunit/test-timers.js index 6b2737467a..50e7b260c5 100644 --- a/test/mjsunit/test-timers.js +++ b/test/mjsunit/test-timers.js @@ -35,7 +35,7 @@ setInterval(function () { clearInterval(this); }, 1000); -function onExit () { +process.addListener("exit", function () { assertTrue(setTimeout_called); assertEquals(3, interval_count); -} +}); diff --git a/website/api.txt b/website/api.txt index 2727e3d775..c1b767aba1 100644 --- a/website/api.txt +++ b/website/api.txt @@ -258,12 +258,12 @@ puts("The area of a cirlce of radius 4 is " + area(4)); Functions +require_async()+ and +include_async()+ also exist. -==== +onExit()+ +==== +process.addListener("exit", function () { })+ -When the program exits a callback +onExit()+ will be called for each module -(children first). +When the program exits a special object called +process+ will emit an ++"exit"+ event. -The +onExit()+ callback cannot perform I/O since the process is going to +The +"exit"+ event cannot perform I/O since the process is going to forcably 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: @@ -276,13 +276,13 @@ setTimeout(function () { timer_executed = true }, 1000); -function onExit () { +process.addListener("exit", function () { assertTrue(timer_executed); -} +}); ---------------------------------------- -Just to reiterate: +onExit()+, is not the place to close files or shutdown -servers. The process will exit before they get performed. +Just to reiterate: the +"exit"+ event, is not the place to close files or +shutdown servers. The process will exit before they get performed.