Add pid accessor
authorRyan <ry@tinyclouds.org>
Sun, 21 Jun 2009 11:29:15 +0000 (13:29 +0200)
committerRyan <ry@tinyclouds.org>
Sun, 21 Jun 2009 11:29:15 +0000 (13:29 +0200)
src/process.cc
src/process.h

index bba44dd..1c83c53 100644 (file)
@@ -14,6 +14,7 @@ using namespace node;
 #define ON_ERROR_SYMBOL   String::NewSymbol("onError")
 #define ON_OUTPUT_SYMBOL  String::NewSymbol("onOutput")
 #define ON_EXIT_SYMBOL    String::NewSymbol("onExit")
+#define PID_SYMBOL        String::NewSymbol("pid")
 
 Persistent<FunctionTemplate> Process::constructor_template;
 
@@ -29,6 +30,9 @@ Process::Initialize (Handle<Object> target)
   NODE_SET_PROTOTYPE_METHOD(constructor_template, "write", Process::Write);
   NODE_SET_PROTOTYPE_METHOD(constructor_template, "close", Process::Close);
 
+  constructor_template->PrototypeTemplate()->SetAccessor(PID_SYMBOL,
+                                                         PIDGetter);
+
   target->Set(String::NewSymbol("Process"), constructor_template->GetFunction());
 }
 
@@ -52,6 +56,20 @@ Process::New (const Arguments& args)
   return args.This();
 }
 
+Handle<Value>
+Process::PIDGetter (Local<String> _, const AccessorInfo& info)
+{
+  Process *process = NODE_UNWRAP(Process, info.This());
+  assert(process);
+
+  HandleScope scope;
+
+  if (process->pid_ == 0) return Null();
+
+  Local<Integer> pid = Integer::New(process->pid_);
+  return scope.Close(pid);
+}
+
 static void
 free_buf (oi_buf *b)
 {
@@ -189,6 +207,14 @@ Process::~Process ()
 void
 Process::Shutdown () 
 {
+  // Clear the out_stream
+  while (!oi_queue_empty(&out_stream_)) {
+    oi_queue *q = oi_queue_last(&out_stream_);
+    oi_buf *buf = (oi_buf*) oi_queue_data(q, oi_buf, queue);
+    oi_queue_remove(q);
+    if (buf->release) buf->release(buf);
+  }
+
   if (stdout_pipe_[0] >= 0) close(stdout_pipe_[0]);
   if (stdout_pipe_[1] >= 0) close(stdout_pipe_[1]);
 
@@ -260,8 +286,6 @@ Process::Spawn (const char *command)
       return -4;
 
     case 0: // Child.
-      //printf("child process!\n");
-
       close(stdout_pipe_[0]); // close read end 
       dup2(stdout_pipe_[1], STDOUT_FILENO);
 
@@ -271,8 +295,6 @@ Process::Spawn (const char *command)
       close(stdin_pipe_[1]); // close write end 
       dup2(stdin_pipe_[0],  STDIN_FILENO);
 
-      //printf("child process!\n");
-
       execl("/bin/sh", "sh", "-c", command, (char *)NULL);
       //execl(_PATH_BSHELL, "sh", "-c", program, (char *)NULL);
       _exit(127);
index faee88e..b48ba97 100644 (file)
@@ -18,6 +18,7 @@ class Process : ObjectWrap {
   static v8::Handle<v8::Value> New (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> PIDGetter (v8::Local<v8::String> _, const v8::AccessorInfo& info);
 
   Process(v8::Handle<v8::Object> handle);
   ~Process();