5 #include "handle_wrap.h"
6 #include "node_buffer.h"
9 #include "req-wrap-inl.h"
10 #include "stream_wrap.h"
19 using v8::FunctionCallbackInfo;
20 using v8::FunctionTemplate;
24 using v8::PropertyAttribute;
29 void TTYWrap::Initialize(Local<Object> target,
31 Local<Context> context) {
32 Environment* env = Environment::GetCurrent(context);
34 Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
35 t->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "TTY"));
36 t->InstanceTemplate()->SetInternalFieldCount(1);
38 env->SetProtoMethod(t, "close", HandleWrap::Close);
39 env->SetProtoMethod(t, "unref", HandleWrap::Unref);
41 StreamWrap::AddMethods(env, t, StreamBase::kFlagNoShutdown);
43 env->SetProtoMethod(t, "getWindowSize", TTYWrap::GetWindowSize);
44 env->SetProtoMethod(t, "setRawMode", SetRawMode);
46 env->SetMethod(target, "isTTY", IsTTY);
47 env->SetMethod(target, "guessHandleType", GuessHandleType);
49 target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "TTY"), t->GetFunction());
50 env->set_tty_constructor_template(t);
54 uv_tty_t* TTYWrap::UVHandle() {
59 void TTYWrap::GuessHandleType(const FunctionCallbackInfo<Value>& args) {
60 Environment* env = Environment::GetCurrent(args);
61 int fd = args[0]->Int32Value();
64 uv_handle_type t = uv_guess_handle(fd);
65 const char* type = nullptr;
68 case UV_TCP: type = "TCP"; break;
69 case UV_TTY: type = "TTY"; break;
70 case UV_UDP: type = "UDP"; break;
71 case UV_FILE: type = "FILE"; break;
72 case UV_NAMED_PIPE: type = "PIPE"; break;
73 case UV_UNKNOWN_HANDLE: type = "UNKNOWN"; break;
78 args.GetReturnValue().Set(OneByteString(env->isolate(), type));
82 void TTYWrap::IsTTY(const FunctionCallbackInfo<Value>& args) {
83 int fd = args[0]->Int32Value();
85 bool rc = uv_guess_handle(fd) == UV_TTY;
86 args.GetReturnValue().Set(rc);
90 void TTYWrap::GetWindowSize(const FunctionCallbackInfo<Value>& args) {
91 Environment* env = Environment::GetCurrent(args);
93 TTYWrap* wrap = Unwrap<TTYWrap>(args.Holder());
94 CHECK(args[0]->IsArray());
97 int err = uv_tty_get_winsize(&wrap->handle_, &width, &height);
100 Local<v8::Array> a = args[0].As<Array>();
101 a->Set(0, Integer::New(env->isolate(), width));
102 a->Set(1, Integer::New(env->isolate(), height));
105 args.GetReturnValue().Set(err);
109 void TTYWrap::SetRawMode(const FunctionCallbackInfo<Value>& args) {
110 TTYWrap* wrap = Unwrap<TTYWrap>(args.Holder());
111 int err = uv_tty_set_mode(&wrap->handle_, args[0]->IsTrue());
112 args.GetReturnValue().Set(err);
116 void TTYWrap::New(const FunctionCallbackInfo<Value>& args) {
117 Environment* env = Environment::GetCurrent(args);
119 // This constructor should not be exposed to public javascript.
120 // Therefore we assert that we are not trying to call this as a
122 CHECK(args.IsConstructCall());
124 int fd = args[0]->Int32Value();
127 TTYWrap* wrap = new TTYWrap(env, args.This(), fd, args[1]->IsTrue());
128 wrap->UpdateWriteQueueSize();
132 TTYWrap::TTYWrap(Environment* env, Local<Object> object, int fd, bool readable)
135 reinterpret_cast<uv_stream_t*>(&handle_),
136 AsyncWrap::PROVIDER_TTYWRAP) {
137 uv_tty_init(env->event_loop(), &handle_, fd, readable);
142 NODE_MODULE_CONTEXT_AWARE_BUILTIN(tty_wrap, node::TTYWrap::Initialize)