diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | src/node.js | 6 | ||||
-rw-r--r-- | src/process.cc | 24 | ||||
-rw-r--r-- | src/process.h | 1 | ||||
-rw-r--r-- | test/mjsunit/test-process-buffering.js | 2 | ||||
-rw-r--r-- | test/mjsunit/test-process-kill.js | 2 | ||||
-rw-r--r-- | test/mjsunit/test-process-simple.js | 2 | ||||
-rw-r--r-- | test/mjsunit/test-process-spawn-loop.js | 2 | ||||
-rw-r--r-- | website/api.txt | 5 |
9 files changed, 32 insertions, 13 deletions
diff --git a/.gitignore b/.gitignore index 16d04ca6b..f6cfcf60d 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 76f411138..a4faaf0a9 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 ee9de6e4d..85f2db7eb 100644 --- a/src/process.cc +++ b/src/process.cc @@ -34,6 +34,7 @@ Process::Initialize (Handle<Object> 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<Object> target) Handle<Value> 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<Value> +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<Value> diff --git a/src/process.h b/src/process.h index 951cf4d22..932d439fb 100644 --- a/src/process.h +++ b/src/process.h @@ -17,6 +17,7 @@ class Process : EventEmitter { protected: static v8::Persistent<v8::FunctionTemplate> constructor_template; static v8::Handle<v8::Value> New (const v8::Arguments& args); + static v8::Handle<v8::Value> Spawn (const v8::Arguments& args); static v8::Handle<v8::Value> Write (const v8::Arguments& args); static v8::Handle<v8::Value> Close (const v8::Arguments& args); static v8::Handle<v8::Value> Kill (const v8::Arguments& args); diff --git a/test/mjsunit/test-process-buffering.js b/test/mjsunit/test-process-buffering.js index 307458703..2e48ad7b0 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 b6af123e8..b3e5ffc22 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 2a0682d53..6f1b8f41c 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 902dbef3a..db900730c 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 7ca8c61b0..19c1c2100 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); }); |