diff --git a/.gitignore b/.gitignore index 16d04ca6b7..f6cfcf60de 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ tags .lock-wscript Makefile *.pyc +website/api.html diff --git a/src/node.js b/src/node.js index 76f411138f..a4faaf0a9f 100644 --- a/src/node.js +++ b/src/node.js @@ -5,6 +5,12 @@ node.tcp.createServer = function (on_connection, options) { return server; }; +node.createProcess = function (command) { + var process = new node.Process(); + process.spawn(command); + return process; +}; + // Timers function setTimeout (callback, after) { diff --git a/src/process.cc b/src/process.cc index ee9de6e4dd..85f2db7eb3 100644 --- a/src/process.cc +++ b/src/process.cc @@ -34,6 +34,7 @@ Process::Initialize (Handle target) 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); @@ -47,21 +48,32 @@ Process::Initialize (Handle target) Handle Process::New (const Arguments& args) { - if (args.Length() == 0) return Undefined(); - HandleScope scope; - String::Utf8Value command(args[0]->ToString()); - Process *p = new Process(args.Holder()); ObjectWrap::InformV8ofAllocation(p); - int r = p->Spawn(*command); + return args.This(); +} + +Handle +Process::Spawn (const Arguments& args) +{ + if (args.Length() == 0 || !args[0]->IsString()) { + return ThrowException(String::New("Bad argument.")); + } + + HandleScope scope; + Process *process = NODE_UNWRAP(Process, args.Holder()); + + String::Utf8Value command(args[0]->ToString()); + + int r = process->Spawn(*command); if (r != 0) { return ThrowException(String::New("Error spawning")); } - return args.This(); + return Undefined(); } Handle diff --git a/src/process.h b/src/process.h index 951cf4d22c..932d439fb4 100644 --- a/src/process.h +++ b/src/process.h @@ -17,6 +17,7 @@ class Process : EventEmitter { protected: static v8::Persistent constructor_template; static v8::Handle New (const v8::Arguments& args); + static v8::Handle Spawn (const v8::Arguments& args); static v8::Handle Write (const v8::Arguments& args); static v8::Handle Close (const v8::Arguments& args); static v8::Handle Kill (const v8::Arguments& args); diff --git a/test/mjsunit/test-process-buffering.js b/test/mjsunit/test-process-buffering.js index 307458703d..2e48ad7b06 100644 --- a/test/mjsunit/test-process-buffering.js +++ b/test/mjsunit/test-process-buffering.js @@ -4,7 +4,7 @@ var pwd_called = false; function pwd (callback) { var output = ""; - var process = new node.Process("pwd"); + var process = node.createProcess("pwd"); process.addListener("output", function (s) { if (s) output += s; }); diff --git a/test/mjsunit/test-process-kill.js b/test/mjsunit/test-process-kill.js index b6af123e8d..b3e5ffc228 100644 --- a/test/mjsunit/test-process-kill.js +++ b/test/mjsunit/test-process-kill.js @@ -3,7 +3,7 @@ include("mjsunit.js"); var exit_status = -1; function onLoad () { - var cat = new node.Process("cat"); + var cat = node.createProcess("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 2a0682d538..6f1b8f41c1 100644 --- a/test/mjsunit/test-process-simple.js +++ b/test/mjsunit/test-process-simple.js @@ -1,6 +1,6 @@ include("mjsunit.js"); -var cat = new node.Process("cat"); +var cat = node.createProcess("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 902dbef3a2..db900730c5 100644 --- a/test/mjsunit/test-process-spawn-loop.js +++ b/test/mjsunit/test-process-spawn-loop.js @@ -4,7 +4,7 @@ var N = 40; var finished = false; function spawn (i) { - var p = new node.Process('python -c "print 500 * 1024 * \'C\'"'); + var p = node.createProcess('python -c "print 500 * 1024 * \'C\'"'); var output = ""; p.addListener("output", function(chunk) { diff --git a/website/api.txt b/website/api.txt index 7ca8c61b03..19c1c2100f 100644 --- a/website/api.txt +++ b/website/api.txt @@ -324,7 +324,6 @@ Node provides a tridirectional +popen(3)+ facility through the class ==== +node.Process+ -.Events [cols="1,2,10",options="header"] |========================================================= |Event |Parameters |Notes @@ -348,11 +347,11 @@ that the +"output"+ and +"error"+ callbacks will no longer be made. |========================================================= -+new node.Process(command)+:: ++node.createProcess(command)+:: Launches a new process with the given +command+. For example: + ---------------------------------------- -var ls = new node.Process("ls -lh /usr"); +var ls = node.createProcess("ls -lh /usr"); ls.addListener("output", function (data) { puts(data); });