mirror of
https://github.com/nodejs/node.git
synced 2026-11-21 12:59:16 -05:00
add wrapper for uv_spawn
process.binding('process_wrap')
This commit is contained in:
1 parent
874260b40f
commit
f01b241057
6 files changed
+455
-143
No files matched your search
@@ -47,6 +47,7 @@ NODE_EXT_LIST_ITEM(node_tcp_wrap)
|
|||||||
NODE_EXT_LIST_ITEM(node_pipe_wrap)
|
NODE_EXT_LIST_ITEM(node_pipe_wrap)
|
||||||
NODE_EXT_LIST_ITEM(node_cares_wrap)
|
NODE_EXT_LIST_ITEM(node_cares_wrap)
|
||||||
NODE_EXT_LIST_ITEM(node_stdio_wrap)
|
NODE_EXT_LIST_ITEM(node_stdio_wrap)
|
||||||
|
NODE_EXT_LIST_ITEM(node_process_wrap)
|
||||||
|
|
||||||
NODE_EXT_LIST_END
|
NODE_EXT_LIST_END
|
||||||
|
|
||||||
+155
-143
@@ -3,6 +3,7 @@
|
|||||||
#include <req_wrap.h>
|
#include <req_wrap.h>
|
||||||
#include <handle_wrap.h>
|
#include <handle_wrap.h>
|
||||||
#include <stream_wrap.h>
|
#include <stream_wrap.h>
|
||||||
|
#include <pipe_wrap.h>
|
||||||
|
|
||||||
#define UNWRAP \
|
#define UNWRAP \
|
||||||
assert(!args.Holder().IsEmpty()); \
|
assert(!args.Holder().IsEmpty()); \
|
||||||
@@ -37,176 +38,187 @@ Persistent<Function> pipeConstructor;
|
|||||||
typedef class ReqWrap<uv_connect_t> ConnectWrap;
|
typedef class ReqWrap<uv_connect_t> ConnectWrap;
|
||||||
|
|
||||||
|
|
||||||
class PipeWrap : StreamWrap {
|
uv_pipe_t* PipeWrap::UVHandle() {
|
||||||
public:
|
return &handle_;
|
||||||
|
}
|
||||||
|
|
||||||
static void Initialize(Handle<Object> target) {
|
|
||||||
StreamWrap::Initialize(target);
|
|
||||||
|
|
||||||
HandleScope scope;
|
PipeWrap* PipeWrap::Unwrap(Local<Object> obj) {
|
||||||
|
assert(!obj.IsEmpty());
|
||||||
|
assert(obj->InternalFieldCount() > 0);
|
||||||
|
return static_cast<PipeWrap*>(obj->GetPointerFromInternalField(0));
|
||||||
|
}
|
||||||
|
|
||||||
Local<FunctionTemplate> t = FunctionTemplate::New(New);
|
|
||||||
t->SetClassName(String::NewSymbol("Pipe"));
|
|
||||||
|
|
||||||
t->InstanceTemplate()->SetInternalFieldCount(1);
|
void PipeWrap::Initialize(Handle<Object> target) {
|
||||||
|
StreamWrap::Initialize(target);
|
||||||
|
|
||||||
NODE_SET_PROTOTYPE_METHOD(t, "close", HandleWrap::Close);
|
HandleScope scope;
|
||||||
|
|
||||||
NODE_SET_PROTOTYPE_METHOD(t, "readStart", StreamWrap::ReadStart);
|
Local<FunctionTemplate> t = FunctionTemplate::New(New);
|
||||||
NODE_SET_PROTOTYPE_METHOD(t, "readStop", StreamWrap::ReadStop);
|
t->SetClassName(String::NewSymbol("Pipe"));
|
||||||
NODE_SET_PROTOTYPE_METHOD(t, "write", StreamWrap::Write);
|
|
||||||
NODE_SET_PROTOTYPE_METHOD(t, "shutdown", StreamWrap::Shutdown);
|
|
||||||
|
|
||||||
NODE_SET_PROTOTYPE_METHOD(t, "bind", Bind);
|
t->InstanceTemplate()->SetInternalFieldCount(1);
|
||||||
NODE_SET_PROTOTYPE_METHOD(t, "listen", Listen);
|
|
||||||
NODE_SET_PROTOTYPE_METHOD(t, "connect", Connect);
|
|
||||||
|
|
||||||
pipeConstructor = Persistent<Function>::New(t->GetFunction());
|
NODE_SET_PROTOTYPE_METHOD(t, "close", HandleWrap::Close);
|
||||||
|
|
||||||
target->Set(String::NewSymbol("Pipe"), pipeConstructor);
|
NODE_SET_PROTOTYPE_METHOD(t, "readStart", StreamWrap::ReadStart);
|
||||||
|
NODE_SET_PROTOTYPE_METHOD(t, "readStop", StreamWrap::ReadStop);
|
||||||
|
NODE_SET_PROTOTYPE_METHOD(t, "write", StreamWrap::Write);
|
||||||
|
NODE_SET_PROTOTYPE_METHOD(t, "shutdown", StreamWrap::Shutdown);
|
||||||
|
|
||||||
|
NODE_SET_PROTOTYPE_METHOD(t, "bind", Bind);
|
||||||
|
NODE_SET_PROTOTYPE_METHOD(t, "listen", Listen);
|
||||||
|
NODE_SET_PROTOTYPE_METHOD(t, "connect", Connect);
|
||||||
|
|
||||||
|
pipeConstructor = Persistent<Function>::New(t->GetFunction());
|
||||||
|
|
||||||
|
target->Set(String::NewSymbol("Pipe"), pipeConstructor);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Handle<Value> PipeWrap::New(const Arguments& args) {
|
||||||
|
// This constructor should not be exposed to public javascript.
|
||||||
|
// Therefore we assert that we are not trying to call this as a
|
||||||
|
// normal function.
|
||||||
|
assert(args.IsConstructCall());
|
||||||
|
|
||||||
|
HandleScope scope;
|
||||||
|
PipeWrap* wrap = new PipeWrap(args.This());
|
||||||
|
assert(wrap);
|
||||||
|
|
||||||
|
return scope.Close(args.This());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
PipeWrap::PipeWrap(Handle<Object> object) : StreamWrap(object,
|
||||||
|
(uv_stream_t*) &handle_) {
|
||||||
|
int r = uv_pipe_init(&handle_);
|
||||||
|
assert(r == 0); // How do we proxy this error up to javascript?
|
||||||
|
// Suggestion: uv_pipe_init() returns void.
|
||||||
|
handle_.data = reinterpret_cast<void*>(this);
|
||||||
|
UpdateWriteQueueSize();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Handle<Value> PipeWrap::Bind(const Arguments& args) {
|
||||||
|
HandleScope scope;
|
||||||
|
|
||||||
|
UNWRAP
|
||||||
|
|
||||||
|
String::AsciiValue name(args[0]->ToString());
|
||||||
|
|
||||||
|
int r = uv_pipe_bind(&wrap->handle_, *name);
|
||||||
|
|
||||||
|
// Error starting the pipe.
|
||||||
|
if (r) SetErrno(uv_last_error().code);
|
||||||
|
|
||||||
|
return scope.Close(Integer::New(r));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Handle<Value> PipeWrap::Listen(const Arguments& args) {
|
||||||
|
HandleScope scope;
|
||||||
|
|
||||||
|
UNWRAP
|
||||||
|
|
||||||
|
int backlog = args[0]->Int32Value();
|
||||||
|
|
||||||
|
int r = uv_listen((uv_stream_t*)&wrap->handle_, backlog, OnConnection);
|
||||||
|
|
||||||
|
// Error starting the pipe.
|
||||||
|
if (r) SetErrno(uv_last_error().code);
|
||||||
|
|
||||||
|
return scope.Close(Integer::New(r));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// TODO maybe share with TCPWrap?
|
||||||
|
void PipeWrap::OnConnection(uv_stream_t* handle, int status) {
|
||||||
|
HandleScope scope;
|
||||||
|
|
||||||
|
PipeWrap* wrap = static_cast<PipeWrap*>(handle->data);
|
||||||
|
assert(&wrap->handle_ == (uv_pipe_t*)handle);
|
||||||
|
|
||||||
|
// We should not be getting this callback if someone as already called
|
||||||
|
// uv_close() on the handle.
|
||||||
|
assert(wrap->object_.IsEmpty() == false);
|
||||||
|
|
||||||
|
if (status != 0) {
|
||||||
|
// TODO Handle server error (set errno and call onconnection with NULL)
|
||||||
|
assert(0);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
// Instanciate the client javascript object and handle.
|
||||||
static Handle<Value> New(const Arguments& args) {
|
Local<Object> client_obj = pipeConstructor->NewInstance();
|
||||||
// This constructor should not be exposed to public javascript.
|
|
||||||
// Therefore we assert that we are not trying to call this as a
|
|
||||||
// normal function.
|
|
||||||
assert(args.IsConstructCall());
|
|
||||||
|
|
||||||
HandleScope scope;
|
// Unwrap the client javascript object.
|
||||||
PipeWrap* wrap = new PipeWrap(args.This());
|
assert(client_obj->InternalFieldCount() > 0);
|
||||||
assert(wrap);
|
PipeWrap* client_wrap =
|
||||||
|
static_cast<PipeWrap*>(client_obj->GetPointerFromInternalField(0));
|
||||||
|
|
||||||
return scope.Close(args.This());
|
int r = uv_accept(handle, (uv_stream_t*)&client_wrap->handle_);
|
||||||
|
|
||||||
|
// uv_accept should always work.
|
||||||
|
assert(r == 0);
|
||||||
|
|
||||||
|
// Successful accept. Call the onconnection callback in JavaScript land.
|
||||||
|
Local<Value> argv[1] = { client_obj };
|
||||||
|
MakeCallback(wrap->object_, "onconnection", 1, argv);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO Maybe share this with TCPWrap?
|
||||||
|
void PipeWrap::AfterConnect(uv_connect_t* req, int status) {
|
||||||
|
ConnectWrap* req_wrap = (ConnectWrap*) req->data;
|
||||||
|
PipeWrap* wrap = (PipeWrap*) req->handle->data;
|
||||||
|
|
||||||
|
HandleScope scope;
|
||||||
|
|
||||||
|
// The wrap and request objects should still be there.
|
||||||
|
assert(req_wrap->object_.IsEmpty() == false);
|
||||||
|
assert(wrap->object_.IsEmpty() == false);
|
||||||
|
|
||||||
|
if (status) {
|
||||||
|
SetErrno(uv_last_error().code);
|
||||||
}
|
}
|
||||||
|
|
||||||
PipeWrap(Handle<Object> object) : StreamWrap(object,
|
Local<Value> argv[3] = {
|
||||||
(uv_stream_t*) &handle_) {
|
Integer::New(status),
|
||||||
int r = uv_pipe_init(&handle_);
|
Local<Value>::New(wrap->object_),
|
||||||
assert(r == 0); // How do we proxy this error up to javascript?
|
Local<Value>::New(req_wrap->object_)
|
||||||
// Suggestion: uv_pipe_init() returns void.
|
};
|
||||||
handle_.data = reinterpret_cast<void*>(this);
|
|
||||||
UpdateWriteQueueSize();
|
|
||||||
}
|
|
||||||
|
|
||||||
static Handle<Value> Bind(const Arguments& args) {
|
MakeCallback(req_wrap->object_, "oncomplete", 3, argv);
|
||||||
HandleScope scope;
|
|
||||||
|
|
||||||
UNWRAP
|
delete req_wrap;
|
||||||
|
}
|
||||||
|
|
||||||
String::AsciiValue name(args[0]->ToString());
|
|
||||||
|
|
||||||
int r = uv_pipe_bind(&wrap->handle_, *name);
|
Handle<Value> PipeWrap::Connect(const Arguments& args) {
|
||||||
|
HandleScope scope;
|
||||||
|
|
||||||
// Error starting the pipe.
|
UNWRAP
|
||||||
if (r) SetErrno(uv_last_error().code);
|
|
||||||
|
|
||||||
return scope.Close(Integer::New(r));
|
String::AsciiValue name(args[0]->ToString());
|
||||||
}
|
|
||||||
|
|
||||||
static Handle<Value> Listen(const Arguments& args) {
|
ConnectWrap* req_wrap = new ConnectWrap();
|
||||||
HandleScope scope;
|
|
||||||
|
|
||||||
UNWRAP
|
int r = uv_pipe_connect(&req_wrap->req_,
|
||||||
|
&wrap->handle_,
|
||||||
|
*name,
|
||||||
|
AfterConnect);
|
||||||
|
|
||||||
int backlog = args[0]->Int32Value();
|
req_wrap->Dispatched();
|
||||||
|
|
||||||
int r = uv_listen((uv_stream_t*)&wrap->handle_, backlog, OnConnection);
|
|
||||||
|
|
||||||
// Error starting the pipe.
|
|
||||||
if (r) SetErrno(uv_last_error().code);
|
|
||||||
|
|
||||||
return scope.Close(Integer::New(r));
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO maybe share with TCPWrap?
|
|
||||||
static void OnConnection(uv_stream_t* handle, int status) {
|
|
||||||
HandleScope scope;
|
|
||||||
|
|
||||||
PipeWrap* wrap = static_cast<PipeWrap*>(handle->data);
|
|
||||||
assert(&wrap->handle_ == (uv_pipe_t*)handle);
|
|
||||||
|
|
||||||
// We should not be getting this callback if someone as already called
|
|
||||||
// uv_close() on the handle.
|
|
||||||
assert(wrap->object_.IsEmpty() == false);
|
|
||||||
|
|
||||||
if (status != 0) {
|
|
||||||
// TODO Handle server error (set errno and call onconnection with NULL)
|
|
||||||
assert(0);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Instanciate the client javascript object and handle.
|
|
||||||
Local<Object> client_obj = pipeConstructor->NewInstance();
|
|
||||||
|
|
||||||
// Unwrap the client javascript object.
|
|
||||||
assert(client_obj->InternalFieldCount() > 0);
|
|
||||||
PipeWrap* client_wrap =
|
|
||||||
static_cast<PipeWrap*>(client_obj->GetPointerFromInternalField(0));
|
|
||||||
|
|
||||||
int r = uv_accept(handle, (uv_stream_t*)&client_wrap->handle_);
|
|
||||||
|
|
||||||
// uv_accept should always work.
|
|
||||||
assert(r == 0);
|
|
||||||
|
|
||||||
// Successful accept. Call the onconnection callback in JavaScript land.
|
|
||||||
Local<Value> argv[1] = { client_obj };
|
|
||||||
MakeCallback(wrap->object_, "onconnection", 1, argv);
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO Maybe share this with TCPWrap?
|
|
||||||
static void AfterConnect(uv_connect_t* req, int status) {
|
|
||||||
ConnectWrap* req_wrap = (ConnectWrap*) req->data;
|
|
||||||
PipeWrap* wrap = (PipeWrap*) req->handle->data;
|
|
||||||
|
|
||||||
HandleScope scope;
|
|
||||||
|
|
||||||
// The wrap and request objects should still be there.
|
|
||||||
assert(req_wrap->object_.IsEmpty() == false);
|
|
||||||
assert(wrap->object_.IsEmpty() == false);
|
|
||||||
|
|
||||||
if (status) {
|
|
||||||
SetErrno(uv_last_error().code);
|
|
||||||
}
|
|
||||||
|
|
||||||
Local<Value> argv[3] = {
|
|
||||||
Integer::New(status),
|
|
||||||
Local<Value>::New(wrap->object_),
|
|
||||||
Local<Value>::New(req_wrap->object_)
|
|
||||||
};
|
|
||||||
|
|
||||||
MakeCallback(req_wrap->object_, "oncomplete", 3, argv);
|
|
||||||
|
|
||||||
|
if (r) {
|
||||||
|
SetErrno(uv_last_error().code);
|
||||||
delete req_wrap;
|
delete req_wrap;
|
||||||
|
return scope.Close(v8::Null());
|
||||||
|
} else {
|
||||||
|
return scope.Close(req_wrap->object_);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
static Handle<Value> Connect(const Arguments& args) {
|
|
||||||
HandleScope scope;
|
|
||||||
|
|
||||||
UNWRAP
|
|
||||||
|
|
||||||
String::AsciiValue name(args[0]->ToString());
|
|
||||||
|
|
||||||
ConnectWrap* req_wrap = new ConnectWrap();
|
|
||||||
|
|
||||||
int r = uv_pipe_connect(&req_wrap->req_,
|
|
||||||
&wrap->handle_,
|
|
||||||
*name,
|
|
||||||
AfterConnect);
|
|
||||||
|
|
||||||
req_wrap->Dispatched();
|
|
||||||
|
|
||||||
if (r) {
|
|
||||||
SetErrno(uv_last_error().code);
|
|
||||||
delete req_wrap;
|
|
||||||
return scope.Close(v8::Null());
|
|
||||||
} else {
|
|
||||||
return scope.Close(req_wrap->object_);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
uv_pipe_t handle_;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
} // namespace node
|
} // namespace node
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
#ifndef PIPE_WRAP_H_
|
||||||
|
#define PIPE_WRAP_H_
|
||||||
|
#include <stream_wrap.h>
|
||||||
|
|
||||||
|
namespace node {
|
||||||
|
|
||||||
|
class PipeWrap : StreamWrap {
|
||||||
|
public:
|
||||||
|
uv_pipe_t* UVHandle();
|
||||||
|
|
||||||
|
static PipeWrap* Unwrap(v8::Local<v8::Object> obj);
|
||||||
|
static void Initialize(v8::Handle<v8::Object> target);
|
||||||
|
|
||||||
|
private:
|
||||||
|
PipeWrap(v8::Handle<v8::Object> object);
|
||||||
|
|
||||||
|
static v8::Handle<v8::Value> New(const v8::Arguments& args);
|
||||||
|
static v8::Handle<v8::Value> Bind(const v8::Arguments& args);
|
||||||
|
static v8::Handle<v8::Value> Listen(const v8::Arguments& args);
|
||||||
|
static v8::Handle<v8::Value> Connect(const v8::Arguments& args);
|
||||||
|
|
||||||
|
static void OnConnection(uv_stream_t* handle, int status);
|
||||||
|
static void AfterConnect(uv_connect_t* req, int status);
|
||||||
|
|
||||||
|
uv_pipe_t handle_;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace node
|
||||||
|
|
||||||
|
|
||||||
|
#endif // PIPE_WRAP_H_
|
||||||
@@ -0,0 +1,203 @@
|
|||||||
|
#include <node.h>
|
||||||
|
#include <handle_wrap.h>
|
||||||
|
#include <pipe_wrap.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define UNWRAP \
|
||||||
|
assert(!args.Holder().IsEmpty()); \
|
||||||
|
assert(args.Holder()->InternalFieldCount() > 0); \
|
||||||
|
ProcessWrap* wrap = \
|
||||||
|
static_cast<ProcessWrap*>(args.Holder()->GetPointerFromInternalField(0)); \
|
||||||
|
if (!wrap) { \
|
||||||
|
SetErrno(UV_EBADF); \
|
||||||
|
return scope.Close(Integer::New(-1)); \
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace node {
|
||||||
|
|
||||||
|
using v8::Object;
|
||||||
|
using v8::Handle;
|
||||||
|
using v8::Local;
|
||||||
|
using v8::Persistent;
|
||||||
|
using v8::Value;
|
||||||
|
using v8::HandleScope;
|
||||||
|
using v8::FunctionTemplate;
|
||||||
|
using v8::String;
|
||||||
|
using v8::Array;
|
||||||
|
using v8::Function;
|
||||||
|
using v8::TryCatch;
|
||||||
|
using v8::Context;
|
||||||
|
using v8::Arguments;
|
||||||
|
using v8::Integer;
|
||||||
|
|
||||||
|
|
||||||
|
class ProcessWrap : public HandleWrap {
|
||||||
|
public:
|
||||||
|
static void Initialize(Handle<Object> target) {
|
||||||
|
HandleScope scope;
|
||||||
|
|
||||||
|
HandleWrap::Initialize(target);
|
||||||
|
|
||||||
|
Local<FunctionTemplate> constructor = FunctionTemplate::New(New);
|
||||||
|
constructor->InstanceTemplate()->SetInternalFieldCount(1);
|
||||||
|
constructor->SetClassName(String::NewSymbol("Process"));
|
||||||
|
|
||||||
|
NODE_SET_PROTOTYPE_METHOD(constructor, "close", HandleWrap::Close);
|
||||||
|
|
||||||
|
NODE_SET_PROTOTYPE_METHOD(constructor, "spawn", Spawn);
|
||||||
|
NODE_SET_PROTOTYPE_METHOD(constructor, "kill", Kill);
|
||||||
|
|
||||||
|
target->Set(String::NewSymbol("Process"), constructor->GetFunction());
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static Handle<Value> New(const Arguments& args) {
|
||||||
|
// This constructor should not be exposed to public javascript.
|
||||||
|
// Therefore we assert that we are not trying to call this as a
|
||||||
|
// normal function.
|
||||||
|
assert(args.IsConstructCall());
|
||||||
|
|
||||||
|
HandleScope scope;
|
||||||
|
ProcessWrap *wrap = new ProcessWrap(args.This());
|
||||||
|
assert(wrap);
|
||||||
|
|
||||||
|
return scope.Close(args.This());
|
||||||
|
}
|
||||||
|
|
||||||
|
ProcessWrap(Handle<Object> object) : HandleWrap(object, NULL) { }
|
||||||
|
~ProcessWrap() { }
|
||||||
|
|
||||||
|
static Handle<Value> Spawn(const Arguments& args) {
|
||||||
|
HandleScope scope;
|
||||||
|
|
||||||
|
UNWRAP
|
||||||
|
|
||||||
|
Local<Object> js_options = args[0]->ToObject();
|
||||||
|
|
||||||
|
uv_process_options_t options;
|
||||||
|
memset(&options, 0, sizeof(uv_process_options_t));
|
||||||
|
|
||||||
|
options.exit_cb = OnExit;
|
||||||
|
|
||||||
|
// TODO is this possible to do without mallocing ?
|
||||||
|
|
||||||
|
// options.file
|
||||||
|
Local<Value> file_v = js_options->Get(String::New("file"));
|
||||||
|
if (!file_v.IsEmpty() && file_v->IsString()) {
|
||||||
|
String::Utf8Value file(file_v->ToString());
|
||||||
|
options.file = strdup(*file);
|
||||||
|
}
|
||||||
|
|
||||||
|
// options.args
|
||||||
|
Local<Value> argv_v = js_options->Get(String::New("args"));
|
||||||
|
if (!argv_v.IsEmpty() && argv_v->IsArray()) {
|
||||||
|
Local<Array> js_argv = Local<Array>::Cast(argv_v);
|
||||||
|
int argc = js_argv->Length();
|
||||||
|
// Heap allocate to detect errors. +1 is for NULL.
|
||||||
|
options.args = new char*[argc + 1];
|
||||||
|
for (int i = 0; i < argc; i++) {
|
||||||
|
String::Utf8Value arg(js_argv->Get(i)->ToString());
|
||||||
|
options.args[i] = strdup(*arg);
|
||||||
|
}
|
||||||
|
options.args[argc] = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// options.cwd
|
||||||
|
Local<Value> cwd_v = js_options->Get(String::New("cwd"));
|
||||||
|
if (!cwd_v.IsEmpty() && cwd_v->IsString()) {
|
||||||
|
String::Utf8Value cwd(js_options->ToString());
|
||||||
|
options.cwd = strdup(*cwd);
|
||||||
|
}
|
||||||
|
|
||||||
|
// options.env
|
||||||
|
Local<Value> env_v = js_options->Get(String::New("env"));
|
||||||
|
if (!env_v.IsEmpty() && env_v->IsArray()) {
|
||||||
|
Local<Array> env = Local<Array>::Cast(env_v);
|
||||||
|
int envc = env->Length();
|
||||||
|
options.env = new char*[envc + 1]; // Heap allocated to detect errors.
|
||||||
|
for (int i = 0; i < envc; i++) {
|
||||||
|
String::Utf8Value pair(env->Get(i)->ToString());
|
||||||
|
options.env[i] = strdup(*pair);
|
||||||
|
}
|
||||||
|
options.env[envc] = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// options.stdin_stream
|
||||||
|
Local<Value> stdin_stream_v = js_options->Get(String::New("stdinStream"));
|
||||||
|
if (!stdin_stream_v.IsEmpty() && stdin_stream_v->IsObject()) {
|
||||||
|
PipeWrap* stdin_wrap = PipeWrap::Unwrap(stdin_stream_v->ToObject());
|
||||||
|
options.stdin_stream = stdin_wrap->UVHandle();
|
||||||
|
}
|
||||||
|
|
||||||
|
// options.stdout_stream
|
||||||
|
Local<Value> stdout_stream_v = js_options->Get(String::New("stdoutStream"));
|
||||||
|
if (!stdout_stream_v.IsEmpty() && stdout_stream_v->IsObject()) {
|
||||||
|
PipeWrap* stdout_wrap = PipeWrap::Unwrap(stdout_stream_v->ToObject());
|
||||||
|
options.stdout_stream = stdout_wrap->UVHandle();
|
||||||
|
}
|
||||||
|
|
||||||
|
// options.stderr_stream
|
||||||
|
Local<Value> stderr_stream_v = js_options->Get(String::New("stderrStream"));
|
||||||
|
if (!stderr_stream_v.IsEmpty() && stderr_stream_v->IsObject()) {
|
||||||
|
PipeWrap* stderr_wrap = PipeWrap::Unwrap(stderr_stream_v->ToObject());
|
||||||
|
options.stderr_stream = stderr_wrap->UVHandle();
|
||||||
|
}
|
||||||
|
|
||||||
|
int r = uv_spawn(&wrap->process_, options);
|
||||||
|
|
||||||
|
wrap->SetHandle((uv_handle_t*)&wrap->process_);
|
||||||
|
assert(wrap->process_.data == wrap);
|
||||||
|
|
||||||
|
if (options.args) {
|
||||||
|
for (int i = 0; options.args[i]; i++) free(options.args[i]);
|
||||||
|
delete [] options.args;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(options.cwd);
|
||||||
|
|
||||||
|
if (options.env) {
|
||||||
|
for (int i = 0; options.env[i]; i++) free(options.env[i]);
|
||||||
|
delete [] options.env;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (r) SetErrno(uv_last_error().code);
|
||||||
|
|
||||||
|
return scope.Close(Integer::New(r));
|
||||||
|
}
|
||||||
|
|
||||||
|
static Handle<Value> Kill(const Arguments& args) {
|
||||||
|
HandleScope scope;
|
||||||
|
|
||||||
|
UNWRAP
|
||||||
|
|
||||||
|
int signal = args[0]->Int32Value();
|
||||||
|
|
||||||
|
int r = uv_process_kill(&wrap->process_, signal);
|
||||||
|
|
||||||
|
if (r) SetErrno(uv_last_error().code);
|
||||||
|
|
||||||
|
return scope.Close(Integer::New(r));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void OnExit(uv_process_t* handle, int exit_status, int term_signal) {
|
||||||
|
HandleScope scope;
|
||||||
|
|
||||||
|
ProcessWrap* wrap = static_cast<ProcessWrap*>(handle->data);
|
||||||
|
assert(wrap);
|
||||||
|
assert(&wrap->process_ == handle);
|
||||||
|
|
||||||
|
Local<Value> argv[2] = {
|
||||||
|
Integer::New(exit_status),
|
||||||
|
Integer::New(term_signal)
|
||||||
|
};
|
||||||
|
|
||||||
|
MakeCallback(wrap->object_, "onexit", 2, argv);
|
||||||
|
}
|
||||||
|
|
||||||
|
uv_process_t process_;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace node
|
||||||
|
|
||||||
|
NODE_MODULE(node_process_wrap, node::ProcessWrap::Initialize);
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
// Copyright Joyent, Inc. and other Node contributors.
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
|
// copy of this software and associated documentation files (the
|
||||||
|
// "Software"), to deal in the Software without restriction, including
|
||||||
|
// without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
||||||
|
// persons to whom the Software is furnished to do so, subject to the
|
||||||
|
// following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included
|
||||||
|
// in all copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||||
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
||||||
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||||
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||||
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
||||||
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
var common = require('../common');
|
||||||
|
var assert = require('assert');
|
||||||
|
var Process = process.binding('process_wrap').Process;
|
||||||
|
var Pipe = process.binding('pipe_wrap').Pipe;
|
||||||
|
var pipe = new Pipe();
|
||||||
|
var p = new Process();
|
||||||
|
|
||||||
|
var processExited = false;
|
||||||
|
var gotPipeEOF = false;
|
||||||
|
var gotPipeData = false;
|
||||||
|
|
||||||
|
p.onexit = function() {
|
||||||
|
console.log("exit");
|
||||||
|
p.close();
|
||||||
|
pipe.readStart();
|
||||||
|
|
||||||
|
processExited = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
pipe.onread = function(b, off, len) {
|
||||||
|
assert.ok(processExited);
|
||||||
|
if (b) {
|
||||||
|
gotPipeData = true;
|
||||||
|
console.log("read %d", len);
|
||||||
|
} else {
|
||||||
|
gotPipeEOF = true;
|
||||||
|
pipe.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
p.spawn({
|
||||||
|
file: process.execPath,
|
||||||
|
args: [ process.execPath, "-v" ],
|
||||||
|
stdoutStream: pipe
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
process.on('exit', function() {
|
||||||
|
assert.ok(processExited);
|
||||||
|
assert.ok(gotPipeEOF);
|
||||||
|
assert.ok(gotPipeData);
|
||||||
|
});
|
||||||
Reference in new issue
Block a user