Node.js  v8.x
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine
tty_wrap.cc
Go to the documentation of this file.
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
21 
22 #include "tty_wrap.h"
23 
24 #include "env.h"
25 #include "env-inl.h"
26 #include "handle_wrap.h"
27 #include "node_buffer.h"
28 #include "node_wrap.h"
29 #include "req-wrap.h"
30 #include "req-wrap-inl.h"
31 #include "stream_wrap.h"
32 #include "util.h"
33 #include "util-inl.h"
34 
35 namespace node {
36 
37 using v8::Array;
38 using v8::Context;
39 using v8::FunctionCallbackInfo;
40 using v8::FunctionTemplate;
41 using v8::Integer;
42 using v8::Local;
43 using v8::Object;
44 using v8::String;
45 using v8::Value;
46 
47 
48 void TTYWrap::Initialize(Local<Object> target,
49  Local<Value> unused,
50  Local<Context> context) {
51  Environment* env = Environment::GetCurrent(context);
52 
53  Local<String> ttyString = FIXED_ONE_BYTE_STRING(env->isolate(), "TTY");
54 
55  Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
56  t->SetClassName(ttyString);
57  t->InstanceTemplate()->SetInternalFieldCount(1);
58 
59  AsyncWrap::AddWrapMethods(env, t);
60 
61  env->SetProtoMethod(t, "close", HandleWrap::Close);
62  env->SetProtoMethod(t, "unref", HandleWrap::Unref);
63  env->SetProtoMethod(t, "ref", HandleWrap::Ref);
64  env->SetProtoMethod(t, "hasRef", HandleWrap::HasRef);
65 
66  StreamWrap::AddMethods(env, t, StreamBase::kFlagNoShutdown);
67 
68  env->SetProtoMethod(t, "getWindowSize", TTYWrap::GetWindowSize);
69  env->SetProtoMethod(t, "setRawMode", SetRawMode);
70 
71  env->SetMethod(target, "isTTY", IsTTY);
72  env->SetMethod(target, "guessHandleType", GuessHandleType);
73 
74  target->Set(ttyString, t->GetFunction());
75  env->set_tty_constructor_template(t);
76 }
77 
78 
79 uv_tty_t* TTYWrap::UVHandle() {
80  return &handle_;
81 }
82 
83 
84 void TTYWrap::GuessHandleType(const FunctionCallbackInfo<Value>& args) {
85  Environment* env = Environment::GetCurrent(args);
86  int fd = args[0]->Int32Value();
87  CHECK_GE(fd, 0);
88 
89  uv_handle_type t = uv_guess_handle(fd);
90  const char* type = nullptr;
91 
92  switch (t) {
93  case UV_TCP: type = "TCP"; break;
94  case UV_TTY: type = "TTY"; break;
95  case UV_UDP: type = "UDP"; break;
96  case UV_FILE: type = "FILE"; break;
97  case UV_NAMED_PIPE: type = "PIPE"; break;
98  case UV_UNKNOWN_HANDLE: type = "UNKNOWN"; break;
99  default:
100  ABORT();
101  }
102 
103  args.GetReturnValue().Set(OneByteString(env->isolate(), type));
104 }
105 
106 
107 void TTYWrap::IsTTY(const FunctionCallbackInfo<Value>& args) {
108  int fd = args[0]->Int32Value();
109  CHECK_GE(fd, 0);
110  bool rc = uv_guess_handle(fd) == UV_TTY;
111  args.GetReturnValue().Set(rc);
112 }
113 
114 
115 void TTYWrap::GetWindowSize(const FunctionCallbackInfo<Value>& args) {
116  Environment* env = Environment::GetCurrent(args);
117 
118  TTYWrap* wrap;
119  ASSIGN_OR_RETURN_UNWRAP(&wrap,
120  args.Holder(),
121  args.GetReturnValue().Set(UV_EBADF));
122  CHECK(args[0]->IsArray());
123 
124  int width, height;
125  int err = uv_tty_get_winsize(&wrap->handle_, &width, &height);
126 
127  if (err == 0) {
128  Local<v8::Array> a = args[0].As<Array>();
129  a->Set(0, Integer::New(env->isolate(), width));
130  a->Set(1, Integer::New(env->isolate(), height));
131  }
132 
133  args.GetReturnValue().Set(err);
134 }
135 
136 
137 void TTYWrap::SetRawMode(const FunctionCallbackInfo<Value>& args) {
138  TTYWrap* wrap;
139  ASSIGN_OR_RETURN_UNWRAP(&wrap,
140  args.Holder(),
141  args.GetReturnValue().Set(UV_EBADF));
142  int err = uv_tty_set_mode(&wrap->handle_, args[0]->IsTrue());
143  args.GetReturnValue().Set(err);
144 }
145 
146 
147 void TTYWrap::New(const FunctionCallbackInfo<Value>& args) {
148  Environment* env = Environment::GetCurrent(args);
149 
150  // This constructor should not be exposed to public javascript.
151  // Therefore we assert that we are not trying to call this as a
152  // normal function.
153  CHECK(args.IsConstructCall());
154 
155  int fd = args[0]->Int32Value();
156  CHECK_GE(fd, 0);
157 
158  int err = 0;
159  TTYWrap* wrap = new TTYWrap(env, args.This(), fd, args[1]->IsTrue(), &err);
160  if (err != 0)
161  return env->ThrowUVException(err, "uv_tty_init");
162 
163  wrap->UpdateWriteQueueSize();
164 }
165 
166 
167 TTYWrap::TTYWrap(Environment* env,
168  Local<Object> object,
169  int fd,
170  bool readable,
171  int* init_err)
172  : StreamWrap(env,
173  object,
174  reinterpret_cast<uv_stream_t*>(&handle_),
175  AsyncWrap::PROVIDER_TTYWRAP) {
176  *init_err = uv_tty_init(env->event_loop(), &handle_, fd, readable);
177 }
178 
179 } // namespace node
180 
NODE_MODULE_CONTEXT_AWARE_BUILTIN(inspector, node::inspector::Agent::InitInspector)
QueryWrap * wrap
Definition: cares_wrap.cc:478
dtrace a
Definition: v8ustack.d:531
void Initialize(Local< Object > target, Local< Value > unused, Local< Context > context, void *priv)
Definition: node_http2.cc:1172
dtrace t
Definition: v8ustack.d:582
MaybeLocal< Object > New(Isolate *isolate, Local< String > string, enum encoding enc)
Definition: node_buffer.cc:241