GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../src/tty_wrap.cc Lines: 68 72 94.4 %
Date: 2017-10-21 Branches: 15 25 60.0 %

Line Branch Exec Source
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
3200
void TTYWrap::Initialize(Local<Object> target,
49
                         Local<Value> unused,
50
                         Local<Context> context) {
51
3200
  Environment* env = Environment::GetCurrent(context);
52
53
3200
  Local<String> ttyString = FIXED_ONE_BYTE_STRING(env->isolate(), "TTY");
54
55
3200
  Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
56
3200
  t->SetClassName(ttyString);
57
6400
  t->InstanceTemplate()->SetInternalFieldCount(1);
58
59
3200
  AsyncWrap::AddWrapMethods(env, t);
60
61
3200
  env->SetProtoMethod(t, "close", HandleWrap::Close);
62
3200
  env->SetProtoMethod(t, "unref", HandleWrap::Unref);
63
3200
  env->SetProtoMethod(t, "ref", HandleWrap::Ref);
64
3200
  env->SetProtoMethod(t, "hasRef", HandleWrap::HasRef);
65
66
3200
  LibuvStreamWrap::AddMethods(env, t, StreamBase::kFlagNoShutdown);
67
68
3200
  env->SetProtoMethod(t, "getWindowSize", TTYWrap::GetWindowSize);
69
3200
  env->SetProtoMethod(t, "setRawMode", SetRawMode);
70
71
3200
  env->SetMethod(target, "isTTY", IsTTY);
72
3200
  env->SetMethod(target, "guessHandleType", GuessHandleType);
73
74
6400
  target->Set(ttyString, t->GetFunction());
75
3200
  env->set_tty_constructor_template(t);
76
3200
}
77
78
79
uv_tty_t* TTYWrap::UVHandle() {
80
  return &handle_;
81
}
82
83
84
7692
void TTYWrap::GuessHandleType(const FunctionCallbackInfo<Value>& args) {
85
7692
  Environment* env = Environment::GetCurrent(args);
86
15384
  int fd = args[0]->Int32Value();
87
7692
  CHECK_GE(fd, 0);
88
89
7692
  uv_handle_type t = uv_guess_handle(fd);
90
7692
  const char* type = nullptr;
91
92

7692
  switch (t) {
93
2
  case UV_TCP: type = "TCP"; break;
94
17
  case UV_TTY: type = "TTY"; break;
95
  case UV_UDP: type = "UDP"; break;
96
5253
  case UV_FILE: type = "FILE"; break;
97
2419
  case UV_NAMED_PIPE: type = "PIPE"; break;
98
1
  case UV_UNKNOWN_HANDLE: type = "UNKNOWN"; break;
99
  default:
100
    ABORT();
101
  }
102
103
23076
  args.GetReturnValue().Set(OneByteString(env->isolate(), type));
104
7692
}
105
106
107
143
void TTYWrap::IsTTY(const FunctionCallbackInfo<Value>& args) {
108
286
  int fd = args[0]->Int32Value();
109
143
  CHECK_GE(fd, 0);
110
143
  bool rc = uv_guess_handle(fd) == UV_TTY;
111
429
  args.GetReturnValue().Set(rc);
112
143
}
113
114
115
18
void TTYWrap::GetWindowSize(const FunctionCallbackInfo<Value>& args) {
116
18
  Environment* env = Environment::GetCurrent(args);
117
118
  TTYWrap* wrap;
119
36
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
120
                          args.Holder(),
121
                          args.GetReturnValue().Set(UV_EBADF));
122
36
  CHECK(args[0]->IsArray());
123
124
  int width, height;
125
18
  int err = uv_tty_get_winsize(&wrap->handle_, &width, &height);
126
127
18
  if (err == 0) {
128
36
    Local<v8::Array> a = args[0].As<Array>();
129
36
    a->Set(0, Integer::New(env->isolate(), width));
130
36
    a->Set(1, Integer::New(env->isolate(), height));
131
  }
132
133
36
  args.GetReturnValue().Set(err);
134
}
135
136
137
2
void TTYWrap::SetRawMode(const FunctionCallbackInfo<Value>& args) {
138
  TTYWrap* wrap;
139
4
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
140
                          args.Holder(),
141
                          args.GetReturnValue().Set(UV_EBADF));
142
4
  int err = uv_tty_set_mode(&wrap->handle_, args[0]->IsTrue());
143
4
  args.GetReturnValue().Set(err);
144
}
145
146
147
23
void TTYWrap::New(const FunctionCallbackInfo<Value>& args) {
148
23
  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
23
  CHECK(args.IsConstructCall());
154
155
46
  int fd = args[0]->Int32Value();
156
23
  CHECK_GE(fd, 0);
157
158
23
  int err = 0;
159
69
  TTYWrap* wrap = new TTYWrap(env, args.This(), fd, args[1]->IsTrue(), &err);
160
23
  if (err != 0)
161
26
    return env->ThrowUVException(err, "uv_tty_init");
162
163
20
  wrap->UpdateWriteQueueSize();
164
}
165
166
167
23
TTYWrap::TTYWrap(Environment* env,
168
                 Local<Object> object,
169
                 int fd,
170
                 bool readable,
171
                 int* init_err)
172
    : LibuvStreamWrap(env,
173
                      object,
174
                      reinterpret_cast<uv_stream_t*>(&handle_),
175
23
                      AsyncWrap::PROVIDER_TTYWRAP) {
176
23
  *init_err = uv_tty_init(env->event_loop(), &handle_, fd, readable);
177
23
}
178
179
}  // namespace node
180
181
3245
NODE_MODULE_CONTEXT_AWARE_BUILTIN(tty_wrap, node::TTYWrap::Initialize)