From ad9d683f9fe9eabe2d797bd6f2ffddf02b0ea009 Mon Sep 17 00:00:00 2001 From: Ryan Date: Wed, 26 Aug 2009 22:11:51 +0200 Subject: [PATCH] API: rename node.Process to node.ChildProcess This is to avoid confusion with the global "process" object, especially for the instances of node.Process. --- benchmark/process_loop.js | 6 +- benchmark/run.js | 4 +- benchmark/static_http_server.js | 12 +-- src/{process.cc => child_process.cc} | 126 ++++++++++++------------ src/{process.h => child_process.h} | 12 +-- src/node.cc | 4 +- src/node.js | 12 ++- test/mjsunit/test-process-buffering.js | 6 +- test/mjsunit/test-process-kill.js | 2 +- test/mjsunit/test-process-simple.js | 2 +- test/mjsunit/test-process-spawn-loop.js | 6 +- website/api.txt | 16 +-- wscript | 2 +- 13 files changed, 106 insertions(+), 104 deletions(-) rename src/{process.cc => child_process.cc} (66%) rename src/{process.h => child_process.h} (89%) diff --git a/benchmark/process_loop.js b/benchmark/process_loop.js index eb2df746b8..67b9edd74b 100644 --- a/benchmark/process_loop.js +++ b/benchmark/process_loop.js @@ -1,13 +1,13 @@ function next (i) { if (i <= 0) return; - var process = node.createProcess("echo hello"); + var child = node.createChildProcess("echo hello"); - process.addListener("output", function (chunk) { + child.addListener("output", function (chunk) { if (chunk) print(chunk); }); - process.addListener("exit", function (code) { + child.addListener("exit", function (code) { if (code != 0) node.exit(-1); next(i - 1); }); diff --git a/benchmark/run.js b/benchmark/run.js index 4bb2f5c4db..a8a4d1715e 100644 --- a/benchmark/run.js +++ b/benchmark/run.js @@ -8,8 +8,8 @@ var benchmark_dir = node.path.dirname(__filename); function exec (script, callback) { var command = ARGV[0] + " " + node.path.join(benchmark_dir, script); var start = new Date(); - var process = node.createProcess(command); - process.addListener("exit", function (code) { + var child = node.createChildProcess(command); + child.addListener("exit", function (code) { var elapsed = new Date() - start; callback(elapsed, code); }); diff --git a/benchmark/static_http_server.js b/benchmark/static_http_server.js index debdceb336..ef465a8462 100644 --- a/benchmark/static_http_server.js +++ b/benchmark/static_http_server.js @@ -34,11 +34,9 @@ function responseListener (res) { }); } -function onLoad () { - for (var i = 0; i < concurrency; i++) { - var client = node.http.createClient(port); - client.id = i; - client.get("/").finish(responseListener); - requests++; - } +for (var i = 0; i < concurrency; i++) { + var client = node.http.createClient(port); + client.id = i; + client.get("/").finish(responseListener); + requests++; } diff --git a/src/process.cc b/src/child_process.cc similarity index 66% rename from src/process.cc rename to src/child_process.cc index d36add77ed..30b1a6878d 100644 --- a/src/process.cc +++ b/src/child_process.cc @@ -1,5 +1,5 @@ #include "node.h" -#include "process.h" +#include "child_process.h" #include #include @@ -13,65 +13,65 @@ using namespace node; #define PID_SYMBOL String::NewSymbol("pid") -Persistent Process::constructor_template; +Persistent ChildProcess::constructor_template; void -Process::Initialize (Handle target) +ChildProcess::Initialize (Handle target) { HandleScope scope; - Local t = FunctionTemplate::New(Process::New); + Local t = FunctionTemplate::New(ChildProcess::New); constructor_template = Persistent::New(t); constructor_template->Inherit(EventEmitter::constructor_template); constructor_template->InstanceTemplate()->SetInternalFieldCount(1); - NODE_SET_PROTOTYPE_METHOD(constructor_template, "spawn", Process::Spawn); - NODE_SET_PROTOTYPE_METHOD(constructor_template, "write", Process::Write); - NODE_SET_PROTOTYPE_METHOD(constructor_template, "close", Process::Close); - NODE_SET_PROTOTYPE_METHOD(constructor_template, "kill", Process::Kill); + NODE_SET_PROTOTYPE_METHOD(constructor_template, "spawn", ChildProcess::Spawn); + NODE_SET_PROTOTYPE_METHOD(constructor_template, "write", ChildProcess::Write); + NODE_SET_PROTOTYPE_METHOD(constructor_template, "close", ChildProcess::Close); + NODE_SET_PROTOTYPE_METHOD(constructor_template, "kill", ChildProcess::Kill); - target->Set(String::NewSymbol("Process"), constructor_template->GetFunction()); + target->Set(String::NewSymbol("ChildProcess"), constructor_template->GetFunction()); } Handle -Process::New (const Arguments& args) +ChildProcess::New (const Arguments& args) { HandleScope scope; - Process *p = new Process(); + ChildProcess *p = new ChildProcess(); p->Wrap(args.Holder()); return args.This(); } Handle -Process::Spawn (const Arguments& args) +ChildProcess::Spawn (const Arguments& args) { if (args.Length() == 0 || !args[0]->IsString()) { return ThrowException(String::New("Bad argument.")); } HandleScope scope; - Process *process = ObjectWrap::Unwrap(args.Holder()); + ChildProcess *child = ObjectWrap::Unwrap(args.Holder()); String::Utf8Value command(args[0]->ToString()); - int r = process->Spawn(*command); + int r = child->Spawn(*command); if (r != 0) { return ThrowException(String::New("Error spawning")); } - process->handle_->Set(PID_SYMBOL, Integer::New(process->pid_)); + child->handle_->Set(PID_SYMBOL, Integer::New(child->pid_)); return Undefined(); } Handle -Process::Write (const Arguments& args) +ChildProcess::Write (const Arguments& args) { HandleScope scope; - Process *process = ObjectWrap::Unwrap(args.Holder()); - assert(process); + ChildProcess *child = ObjectWrap::Unwrap(args.Holder()); + assert(child); ssize_t len; @@ -109,69 +109,69 @@ Process::Write (const Arguments& args) } } - return process->Write(buf, len) == 0 ? True() : False(); + return child->Write(buf, len) == 0 ? True() : False(); } Handle -Process::Kill (const Arguments& args) +ChildProcess::Kill (const Arguments& args) { HandleScope scope; - Process *process = ObjectWrap::Unwrap(args.Holder()); - assert(process); + ChildProcess *child = ObjectWrap::Unwrap(args.Holder()); + assert(child); int sig = SIGTERM; if (args[0]->IsInt32()) sig = args[0]->Int32Value(); - if (process->Kill(sig) != 0) { - return ThrowException(String::New("Process already dead")); + if (child->Kill(sig) != 0) { + return ThrowException(String::New("ChildProcess already dead")); } return Undefined(); } Handle -Process::Close (const Arguments& args) +ChildProcess::Close (const Arguments& args) { HandleScope scope; - Process *process = ObjectWrap::Unwrap(args.Holder()); - assert(process); - return process->Close() == 0 ? True() : False(); + ChildProcess *child = ObjectWrap::Unwrap(args.Holder()); + assert(child); + return child->Close() == 0 ? True() : False(); } void -Process::reader_closed (evcom_reader *r) +ChildProcess::reader_closed (evcom_reader *r) { - Process *process = static_cast (r->data); - if (r == &process->stdout_reader_) { - process->stdout_fd_ = -1; + ChildProcess *child = static_cast (r->data); + if (r == &child->stdout_reader_) { + child->stdout_fd_ = -1; } else { - assert(r == &process->stderr_reader_); - process->stderr_fd_ = -1; + assert(r == &child->stderr_reader_); + child->stderr_fd_ = -1; } evcom_reader_detach(r); - process->MaybeShutdown(); + child->MaybeShutdown(); } void -Process::stdin_closed (evcom_writer *w) +ChildProcess::stdin_closed (evcom_writer *w) { - Process *process = static_cast (w->data); - assert(w == &process->stdin_writer_); - process->stdin_fd_ = -1; + ChildProcess *child = static_cast (w->data); + assert(w == &child->stdin_writer_); + child->stdin_fd_ = -1; evcom_writer_detach(w); - process->MaybeShutdown(); + child->MaybeShutdown(); } void -Process::on_read (evcom_reader *r, const void *buf, size_t len) +ChildProcess::on_read (evcom_reader *r, const void *buf, size_t len) { - Process *process = static_cast (r->data); + ChildProcess *child = static_cast (r->data); HandleScope scope; - bool isSTDOUT = (r == &process->stdout_reader_); + bool isSTDOUT = (r == &child->stdout_reader_); Local argv[1]; - enum encoding encoding = isSTDOUT ? process->stdout_encoding_ : process->stderr_encoding_; + enum encoding encoding = isSTDOUT ? child->stdout_encoding_ : child->stderr_encoding_; if (len == 0) { argv[0] = Local::New(Null()); @@ -190,11 +190,11 @@ Process::on_read (evcom_reader *r, const void *buf, size_t len) argv[0] = String::New((const char*)buf, len); } - process->Emit(isSTDOUT ? "output" : "error", 1, argv); - process->MaybeShutdown(); + child->Emit(isSTDOUT ? "output" : "error", 1, argv); + child->MaybeShutdown(); } -Process::Process () +ChildProcess::ChildProcess () : EventEmitter() { evcom_reader_init(&stdout_reader_); @@ -211,7 +211,7 @@ Process::Process () stdin_writer_.data = this; stdin_writer_.on_close = stdin_closed; - ev_init(&child_watcher_, Process::OnCHLD); + ev_init(&child_watcher_, ChildProcess::OnCHLD); child_watcher_.data = this; stdout_fd_ = -1; @@ -227,13 +227,13 @@ Process::Process () pid_ = 0; } -Process::~Process () +ChildProcess::~ChildProcess () { Shutdown(); } void -Process::Shutdown () +ChildProcess::Shutdown () { if (stdin_fd_ >= 0) { evcom_writer_close(&stdin_writer_); @@ -269,7 +269,7 @@ SetNonBlocking (int fd) } int -Process::Spawn (const char *command) +ChildProcess::Spawn (const char *command) { assert(pid_ == 0); assert(stdout_fd_ == -1); @@ -345,25 +345,25 @@ Process::Spawn (const char *command) } void -Process::OnCHLD (EV_P_ ev_child *watcher, int revents) +ChildProcess::OnCHLD (EV_P_ ev_child *watcher, int revents) { ev_child_stop(EV_A_ watcher); - Process *process = static_cast(watcher->data); + ChildProcess *child = static_cast(watcher->data); assert(revents == EV_CHILD); - assert(process->pid_ == watcher->rpid); - assert(&process->child_watcher_ == watcher); + assert(child->pid_ == watcher->rpid); + assert(&child->child_watcher_ == watcher); - process->got_chld_ = true; - process->exit_code_ = watcher->rstatus; + child->got_chld_ = true; + child->exit_code_ = watcher->rstatus; - if (process->stdin_fd_ >= 0) evcom_writer_close(&process->stdin_writer_); + if (child->stdin_fd_ >= 0) evcom_writer_close(&child->stdin_writer_); - process->MaybeShutdown(); + child->MaybeShutdown(); } int -Process::Write (const char *str, size_t len) +ChildProcess::Write (const char *str, size_t len) { if (stdin_fd_ < 0 || got_chld_) return -1; evcom_writer_write(&stdin_writer_, str, len); @@ -371,7 +371,7 @@ Process::Write (const char *str, size_t len) } int -Process::Close (void) +ChildProcess::Close (void) { if (stdin_fd_ < 0 || got_chld_) return -1; evcom_writer_close(EV_DEFAULT_UC_ &stdin_writer_); @@ -379,14 +379,14 @@ Process::Close (void) } int -Process::Kill (int sig) +ChildProcess::Kill (int sig) { if (got_chld_ || pid_ == 0) return -1; return kill(pid_, sig); } void -Process::MaybeShutdown (void) +ChildProcess::MaybeShutdown (void) { if (stdout_fd_ < 0 && stderr_fd_ < 0 && got_chld_) { HandleScope scope; diff --git a/src/process.h b/src/child_process.h similarity index 89% rename from src/process.h rename to src/child_process.h index fd73219069..c756e45c7d 100644 --- a/src/process.h +++ b/src/child_process.h @@ -1,5 +1,5 @@ -#ifndef node_process_h -#define node_process_h +#ifndef node_child_process_h +#define node_child_process_h #include "node.h" #include "events.h" @@ -9,7 +9,7 @@ namespace node { -class Process : EventEmitter { +class ChildProcess : EventEmitter { public: static void Initialize (v8::Handle target); @@ -22,8 +22,8 @@ class Process : EventEmitter { static v8::Handle Kill (const v8::Arguments& args); static v8::Handle PIDGetter (v8::Local _, const v8::AccessorInfo& info); - Process(); - ~Process(); + ChildProcess(); + ~ChildProcess(); int Spawn (const char *command); int Write (const char *str, size_t len); @@ -59,4 +59,4 @@ class Process : EventEmitter { }; } // namespace node -#endif // node_process_h +#endif // node_child_process_h diff --git a/src/node.cc b/src/node.cc index b0eebe92fc..8f46dcf7c0 100644 --- a/src/node.cc +++ b/src/node.cc @@ -6,7 +6,7 @@ #include "file.h" #include "http.h" #include "timer.h" -#include "process.h" +#include "child_process.h" #include "constants.h" #include "node_stdio.h" @@ -213,7 +213,7 @@ Load (int argc, char *argv[]) Stdio::Initialize(node_obj); Timer::Initialize(node_obj); - Process::Initialize(node_obj); + ChildProcess::Initialize(node_obj); DefineConstants(node_obj); diff --git a/src/node.js b/src/node.js index b09c9b0807..fae638a5ff 100644 --- a/src/node.js +++ b/src/node.js @@ -5,10 +5,14 @@ node.tcp.createServer = function (on_connection, options) { return server; }; -node.createProcess = function (command) { - var process = new node.Process(); - process.spawn(command); - return process; +node.createProcess = function () { + throw "node.createProcess() has been changed to node.createChildProcess() update your code"; +}; + +node.createChildProcess = function (command) { + var child = new node.ChildProcess(); + child.spawn(command); + return child; }; node.tcp.createConnection = function (port, host) { diff --git a/test/mjsunit/test-process-buffering.js b/test/mjsunit/test-process-buffering.js index 427b340d62..df71148220 100644 --- a/test/mjsunit/test-process-buffering.js +++ b/test/mjsunit/test-process-buffering.js @@ -4,12 +4,12 @@ var pwd_called = false; function pwd (callback) { var output = ""; - var process = node.createProcess("pwd"); - process.addListener("output", function (s) { + var child = node.createChildProcess("pwd"); + child.addListener("output", function (s) { puts("stdout: " + JSON.stringify(s)); if (s) output += s; }); - process.addListener("exit", function (c) { + child.addListener("exit", function (c) { puts("exit: " + c); assertEquals(0, c); callback(output); diff --git a/test/mjsunit/test-process-kill.js b/test/mjsunit/test-process-kill.js index 6283a07fe0..beac411e76 100644 --- a/test/mjsunit/test-process-kill.js +++ b/test/mjsunit/test-process-kill.js @@ -2,7 +2,7 @@ include("mjsunit.js"); var exit_status = -1; -var cat = node.createProcess("cat"); +var cat = node.createChildProcess("cat"); cat.addListener("output", function (chunk) { assertEquals(null, chunk); }); cat.addListener("error", function (chunk) { assertEquals(null, chunk); }); diff --git a/test/mjsunit/test-process-simple.js b/test/mjsunit/test-process-simple.js index 9ccf3c11ac..de7c0c0397 100644 --- a/test/mjsunit/test-process-simple.js +++ b/test/mjsunit/test-process-simple.js @@ -1,6 +1,6 @@ include("mjsunit.js"); -var cat = node.createProcess("cat"); +var cat = node.createChildProcess("cat"); var response = ""; var exit_status = -1; diff --git a/test/mjsunit/test-process-spawn-loop.js b/test/mjsunit/test-process-spawn-loop.js index 6d837318c2..07a7e815d7 100644 --- a/test/mjsunit/test-process-spawn-loop.js +++ b/test/mjsunit/test-process-spawn-loop.js @@ -4,14 +4,14 @@ var N = 40; var finished = false; function spawn (i) { - var p = node.createProcess('python -c "print 500 * 1024 * \'C\'"'); + var child = node.createChildProcess('python -c "print 500 * 1024 * \'C\'"'); var output = ""; - p.addListener("output", function(chunk) { + child.addListener("output", function(chunk) { if (chunk) output += chunk; }); - p.addListener("exit", function () { + child.addListener("exit", function () { //puts(output); if (i < N) spawn(i+1); diff --git a/website/api.txt b/website/api.txt index 2c3459521c..ddfd8f5f3b 100644 --- a/website/api.txt +++ b/website/api.txt @@ -313,10 +313,10 @@ Stops a interval from triggering. === Child Processes Node provides a tridirectional +popen(3)+ facility through the class -+node.Process+. It is possible to stream data through the child's +stdin+, ++node.ChildProcess+. It is possible to stream data through the child's +stdin+, +stdout+, and +stderr+ in a fully non-blocking way. -==== +node.Process+ +==== +node.ChildProcess+ [cols="1,2,10",options="header"] |========================================================= @@ -341,31 +341,31 @@ that the +"output"+ and +"error"+ callbacks will no longer be made. |========================================================= -+node.createProcess(command)+:: ++node.createChildProcess(command)+:: Launches a new process with the given +command+. For example: + ---------------------------------------- -var ls = node.createProcess("ls -lh /usr"); +var ls = node.createChildProcess("ls -lh /usr"); ls.addListener("output", function (data) { puts(data); }); ---------------------------------------- -+process.pid+ :: ++child.pid+ :: The PID of the child process. -+process.write(data, encoding="ascii")+ :: ++child.write(data, encoding="ascii")+ :: Write data to the child process's +stdin+. The second argument is optional and specifies the encoding: possible values are +"utf8"+, +"ascii"+, and +"raw"+. -+process.close()+ :: ++child.close()+ :: Closes the process's +stdin+ stream. -+process.kill(signal=node.SIGTERM)+ :: ++child.kill(signal=node.SIGTERM)+ :: Send a single to the child process. If no argument is given, the process will be sent +node.SIGTERM+. The standard POSIX signals are defined under the +node+ namespace (+node.SIGINT+, +node.SIGUSR1+, ...). diff --git a/wscript b/wscript index d2afe1b665..e4f01c4cd7 100644 --- a/wscript +++ b/wscript @@ -234,7 +234,7 @@ def build(bld): src/dns.cc src/file.cc src/timer.cc - src/process.cc + src/child_process.cc src/constants.cc """ node.includes = """