Skip to content

Commit

Permalink
Remove node.Process constructor from API
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Jun 30, 2009
1 parent a3d77ee commit d56552d
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ tags
.lock-wscript
Makefile
*.pyc
website/api.html
6 changes: 6 additions & 0 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
24 changes: 18 additions & 6 deletions src/process.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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>
Expand Down
1 change: 1 addition & 0 deletions src/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/mjsunit/test-process-buffering.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down
2 changes: 1 addition & 1 deletion test/mjsunit/test-process-kill.js
Original file line number Diff line number Diff line change
Expand Up @@ -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); });
Expand Down
2 changes: 1 addition & 1 deletion test/mjsunit/test-process-simple.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
include("mjsunit.js");

var cat = new node.Process("cat");
var cat = node.createProcess("cat");

var response = "";
var exit_status = -1;
Expand Down
2 changes: 1 addition & 1 deletion test/mjsunit/test-process-spawn-loop.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 2 additions & 3 deletions website/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
});
Expand Down

0 comments on commit d56552d

Please sign in to comment.