Browse Source

Use EventEmitter for node.Process

v0.7.4-release
Ryan 16 years ago
parent
commit
80bf451e6e
  1. 43
      src/process.cc
  2. 3
      src/process.h

43
src/process.cc

@ -31,6 +31,7 @@ Process::Initialize (Handle<Object> target)
Local<FunctionTemplate> t = FunctionTemplate::New(Process::New);
constructor_template = Persistent<FunctionTemplate>::New(t);
constructor_template->Inherit(EventEmitter::constructor_template);
constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "write", Process::Write);
@ -155,7 +156,7 @@ Process::Close (const Arguments& args)
}
Process::Process (Handle<Object> handle)
: ObjectWrap(handle)
: EventEmitter(handle)
{
ev_init(&stdout_watcher_, Process::OnOutput);
stdout_watcher_.data = this;
@ -327,12 +328,6 @@ Process::OnOutput (EV_P_ ev_io *watcher, int revents)
assert(fd >= 0);
HandleScope scope;
Handle<Value> callback_v =
process->handle_->Get(is_stdout ? ON_OUTPUT_SYMBOL : ON_ERROR_SYMBOL);
Handle<Function> callback;
if (callback_v->IsFunction()) {
callback = Handle<Function>::Cast(callback_v);
}
Handle<Value> argv[1];
for (;;) {
@ -354,22 +349,15 @@ Process::OnOutput (EV_P_ ev_io *watcher, int revents)
break;
}
if (!callback.IsEmpty()) {
if (r == 0) {
argv[0] = Null();
} else {
// TODO multiple encodings
argv[0] = String::New((const char*)buf, r);
}
TryCatch try_catch;
callback->Call(process->handle_, 1, argv);
if (try_catch.HasCaught()) {
FatalException(try_catch);
return;
}
if (r == 0) {
argv[0] = Null();
} else {
// TODO multiple encodings
argv[0] = String::New((const char*)buf, r);
}
process->Emit(is_stdout ? "Output" : "Error", 1, argv);
if (r == 0) {
ev_io_stop(EV_DEFAULT_UC_ watcher);
close(fd);
@ -489,18 +477,9 @@ int
Process::MaybeShutdown (void)
{
if (STDOUT_CLOSED && STDERR_CLOSED && got_chld_) {
// Call onExit
HandleScope scope;
Handle<Value> callback_v = handle_->Get(ON_EXIT_SYMBOL);
if (callback_v->IsFunction()) {
Handle<Function> callback = Handle<Function>::Cast(callback_v);
TryCatch try_catch;
Handle<Value> argv[1] = { Integer::New(exit_code_) };
callback->Call(handle_, 1, argv);
if (try_catch.HasCaught()) FatalException(try_catch);
}
Handle<Value> argv[1] = { Integer::New(exit_code_) };
Emit("Exit", 1, argv);
Shutdown();
Detach();
}

3
src/process.h

@ -2,13 +2,14 @@
#define node_process_h
#include "node.h"
#include "events.h"
#include <v8.h>
#include <ev.h>
#include <oi_socket.h>
namespace node {
class Process : ObjectWrap {
class Process : EventEmitter {
public:
static void Initialize (v8::Handle<v8::Object> target);
virtual size_t size (void) { return sizeof(Process); }

Loading…
Cancel
Save