From d785f0f71bb34e0b657511c272792e9eed83028d Mon Sep 17 00:00:00 2001 From: jubianchi Date: Mon, 13 Jul 2015 14:33:42 +0200 Subject: [PATCH] Support nodejs >= 0.10 --- src/waitpid.cc | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/waitpid.cc b/src/waitpid.cc index d6c211e..cef0429 100644 --- a/src/waitpid.cc +++ b/src/waitpid.cc @@ -8,6 +8,7 @@ using namespace v8; using namespace node; +#if NODE_MINOR_VERSION==10 static Handle Waitpid(const Arguments& args) { HandleScope scope; int r, child, status; @@ -43,6 +44,43 @@ extern "C" void init(Handle target) { HandleScope scope; NODE_SET_METHOD(target, "waitpid", Waitpid); } +#endif +#if NODE_MINOR_VERSION>10 +void Waitpid(const FunctionCallbackInfo& args) { + Isolate* isolate = Isolate::GetCurrent(); + HandleScope scope(isolate); + + int r, child, status; + + if (args[0]->IsInt32()) { + child = args[0]->Int32Value(); + + do { + r = waitpid(child, &status, 0); + } while (r != -1); + + Local result = Object::New(isolate); + + if (WIFEXITED(status)) { + result->Set(String::NewFromUtf8(isolate, "exitCode"), Integer::New(isolate, WEXITSTATUS(status))); + result->Set(String::NewFromUtf8(isolate, "signalCode"), Null(isolate)); + return; + } + else if (WIFSIGNALED(status)) { + result->Set(String::NewFromUtf8(isolate, "exitCode"), Null(isolate)); + result->Set(String::NewFromUtf8(isolate, "signalCode"), Integer::New(isolate, WTERMSIG(status))); + return; + } + } + else { + isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Not an integer."))); + } +} + +extern "C" void init(Handle exports) { + NODE_SET_METHOD(exports, "waitpid", Waitpid); +} +#endif NODE_MODULE(waitpid, init)