|
| 1 | +#include <node.h> |
| 2 | +#include <node_buffer.h> |
| 3 | +#include <req_wrap.h> |
| 4 | +#include <handle_wrap.h> |
| 5 | +#include <stream_wrap.h> |
| 6 | + |
| 7 | +namespace node { |
| 8 | + |
| 9 | +using v8::Object; |
| 10 | +using v8::Handle; |
| 11 | +using v8::Local; |
| 12 | +using v8::Persistent; |
| 13 | +using v8::Value; |
| 14 | +using v8::HandleScope; |
| 15 | +using v8::FunctionTemplate; |
| 16 | +using v8::String; |
| 17 | +using v8::Function; |
| 18 | +using v8::TryCatch; |
| 19 | +using v8::Context; |
| 20 | +using v8::Arguments; |
| 21 | +using v8::Integer; |
| 22 | +using v8::Undefined; |
| 23 | + |
| 24 | + |
| 25 | +class TTYWrap : StreamWrap { |
| 26 | + public: |
| 27 | + static void Initialize(Handle<Object> target) { |
| 28 | + StreamWrap::Initialize(target); |
| 29 | + |
| 30 | + HandleScope scope; |
| 31 | + |
| 32 | + Local<FunctionTemplate> t = FunctionTemplate::New(New); |
| 33 | + t->SetClassName(String::NewSymbol("TTY")); |
| 34 | + |
| 35 | + t->InstanceTemplate()->SetInternalFieldCount(1); |
| 36 | + |
| 37 | + NODE_SET_PROTOTYPE_METHOD(t, "close", HandleWrap::Close); |
| 38 | + |
| 39 | + NODE_SET_PROTOTYPE_METHOD(t, "readStart", StreamWrap::ReadStart); |
| 40 | + NODE_SET_PROTOTYPE_METHOD(t, "readStop", StreamWrap::ReadStop); |
| 41 | + NODE_SET_PROTOTYPE_METHOD(t, "write", StreamWrap::Write); |
| 42 | + NODE_SET_PROTOTYPE_METHOD(t, "write", StreamWrap::Write); |
| 43 | + |
| 44 | + NODE_SET_METHOD(target, "isTTY", IsTTY); |
| 45 | + |
| 46 | + target->Set(String::NewSymbol("TTY"), t->GetFunction()); |
| 47 | + } |
| 48 | + |
| 49 | + private: |
| 50 | + static Handle<Value> IsTTY(const Arguments& args) { |
| 51 | + HandleScope scope; |
| 52 | + int fd = args[0]->Int32Value(); |
| 53 | + assert(fd >= 0); |
| 54 | + return uv_is_tty(fd) ? v8::True() : v8::False(); |
| 55 | + } |
| 56 | + |
| 57 | + static Handle<Value> New(const Arguments& args) { |
| 58 | + HandleScope scope; |
| 59 | + |
| 60 | + // This constructor should not be exposed to public javascript. |
| 61 | + // Therefore we assert that we are not trying to call this as a |
| 62 | + // normal function. |
| 63 | + assert(args.IsConstructCall()); |
| 64 | + |
| 65 | + int fd = args[0]->Int32Value(); |
| 66 | + assert(fd >= 0); |
| 67 | + |
| 68 | + TTYWrap* wrap = new TTYWrap(args.This(), fd); |
| 69 | + assert(wrap); |
| 70 | + wrap->UpdateWriteQueueSize(); |
| 71 | + |
| 72 | + return scope.Close(args.This()); |
| 73 | + } |
| 74 | + |
| 75 | + TTYWrap(Handle<Object> object, int fd) |
| 76 | + : StreamWrap(object, (uv_stream_t*)&handle_) { |
| 77 | + uv_tty_init(uv_default_loop(), &handle_, fd); |
| 78 | + } |
| 79 | + |
| 80 | + uv_tty_t handle_; |
| 81 | +}; |
| 82 | + |
| 83 | +} // namespace node |
| 84 | + |
| 85 | +NODE_MODULE(node_tty_wrap, node::TTYWrap::Initialize); |
0 commit comments