|
Revision 21653, 1.8 kB
(checked in by tokuhirom, 5 years ago)
|
|
work in progress
|
| Line | |
|---|
| 1 | #include <v8.h> |
|---|
| 2 | #include <cassert> |
|---|
| 3 | #include <cstring> |
|---|
| 4 | #include <cstdio> |
|---|
| 5 | #include <errno.h> |
|---|
| 6 | #include <string> |
|---|
| 7 | #include <sstream> |
|---|
| 8 | #include "v8ext.h" |
|---|
| 9 | #include <llv8-macros.h> |
|---|
| 10 | #include <signal.h> |
|---|
| 11 | |
|---|
| 12 | using namespace v8; |
|---|
| 13 | |
|---|
| 14 | #define EXTERNAL_pid() pid_t pid = (pid_t) args.This()->GetInternalField(0)->Int32Value() |
|---|
| 15 | |
|---|
| 16 | static inline Handle<Value> throw_stderr(const char *msg) { |
|---|
| 17 | std::string buf(msg); |
|---|
| 18 | buf += " error: "; |
|---|
| 19 | buf += strerror(errno); |
|---|
| 20 | return ThrowException(String::New(buf.c_str())); |
|---|
| 21 | } |
|---|
| 22 | |
|---|
| 23 | ///////////////////////////////////////////////////////////// |
|---|
| 24 | // methods |
|---|
| 25 | // |
|---|
| 26 | |
|---|
| 27 | FUNCTION(_open) |
|---|
| 28 | ARG_BETWEEN(1, 2); |
|---|
| 29 | Handle<Array> ary = Handle<Array>::Cast( args[0] ); |
|---|
| 30 | assert(ary->Length() >= 1); |
|---|
| 31 | |
|---|
| 32 | pid_t pid = fork(); |
|---|
| 33 | if (pid > 0) { |
|---|
| 34 | // parent |
|---|
| 35 | return args.This(); |
|---|
| 36 | } else if (pid == 0) { |
|---|
| 37 | // child |
|---|
| 38 | String::Utf8Value file(ary->Get(Int32::New(0))->ToString()); |
|---|
| 39 | char ** argv = new char*[args.Length()]; |
|---|
| 40 | int i; |
|---|
| 41 | for (i=1; i<args.Length(); i++) { |
|---|
| 42 | String::Utf8Value buf(ary->Get(Int32::New(i))); |
|---|
| 43 | char *bbuf = new char [strlen(*buf)]; |
|---|
| 44 | strcmp(bbuf, *buf); |
|---|
| 45 | argv[i-1] = bbuf; |
|---|
| 46 | } |
|---|
| 47 | argv[i] = NULL; |
|---|
| 48 | int ret = execvp(*file, argv); |
|---|
| 49 | if (ret == -1) { |
|---|
| 50 | return throw_stderr("new SubProcess"); |
|---|
| 51 | } |
|---|
| 52 | assert(!"SHOULD NOT REACHE HERE"); |
|---|
| 53 | } else { |
|---|
| 54 | // error |
|---|
| 55 | return throw_stderr("new SubProcess"); |
|---|
| 56 | } |
|---|
| 57 | END |
|---|
| 58 | |
|---|
| 59 | FUNCTION(_kill) |
|---|
| 60 | ARG_COUNT(0); |
|---|
| 61 | EXTERNAL_pid(); |
|---|
| 62 | kill(pid, SIGKILL); |
|---|
| 63 | return Undefined(); |
|---|
| 64 | END |
|---|
| 65 | |
|---|
| 66 | ///////////////////////////////////////////////////////////// |
|---|
| 67 | // initialize |
|---|
| 68 | |
|---|
| 69 | MODULE() |
|---|
| 70 | { |
|---|
| 71 | CLASS_WITH_CONSTRUCTOR(_open); |
|---|
| 72 | BIND_IM("kill", _kill); |
|---|
| 73 | INTERNALCOUNT(1); |
|---|
| 74 | EXPORT_CLASS("SubProcess"); |
|---|
| 75 | } |
|---|
| 76 | ENDMODULE |
|---|
| 77 | |
|---|