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: 69 73 94.5 %
Date: 2017-12-18 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-inl.h"
25
#include "handle_wrap.h"
26
#include "node_buffer.h"
27
#include "node_wrap.h"
28
#include "req_wrap-inl.h"
29
#include "stream_wrap.h"
30
#include "util-inl.h"
31
32
namespace node {
33
34
using v8::Array;
35
using v8::Context;
36
using v8::FunctionCallbackInfo;
37
using v8::FunctionTemplate;
38
using v8::Integer;
39
using v8::Local;
40
using v8::Object;
41
using v8::String;
42
using v8::Value;
43
44
45
3348
void TTYWrap::Initialize(Local<Object> target,
46
                         Local<Value> unused,
47
                         Local<Context> context) {
48
3348
  Environment* env = Environment::GetCurrent(context);
49
50
3348
  Local<String> ttyString = FIXED_ONE_BYTE_STRING(env->isolate(), "TTY");
51
52
3348
  Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
53
3348
  t->SetClassName(ttyString);
54
6696
  t->InstanceTemplate()->SetInternalFieldCount(1);
55
56
3348
  AsyncWrap::AddWrapMethods(env, t);
57
58
3348
  env->SetProtoMethod(t, "close", HandleWrap::Close);
59
3348
  env->SetProtoMethod(t, "unref", HandleWrap::Unref);
60
3348
  env->SetProtoMethod(t, "ref", HandleWrap::Ref);
61
3348
  env->SetProtoMethod(t, "hasRef", HandleWrap::HasRef);
62
63
3348
  LibuvStreamWrap::AddMethods(env, t, StreamBase::kFlagNoShutdown);
64
65
3348
  env->SetProtoMethod(t, "getWindowSize", TTYWrap::GetWindowSize);
66
3348
  env->SetProtoMethod(t, "setRawMode", SetRawMode);
67
68
3348
  env->SetMethod(target, "isTTY", IsTTY);
69
3348
  env->SetMethod(target, "guessHandleType", GuessHandleType);
70
71
6696
  target->Set(ttyString, t->GetFunction());
72
3348
  env->set_tty_constructor_template(t);
73
3348
}
74
75
76
uv_tty_t* TTYWrap::UVHandle() {
77
  return &handle_;
78
}
79
80
81
8009
void TTYWrap::GuessHandleType(const FunctionCallbackInfo<Value>& args) {
82
8009
  Environment* env = Environment::GetCurrent(args);
83
16018
  int fd = args[0]->Int32Value();
84
8009
  CHECK_GE(fd, 0);
85
86
8009
  uv_handle_type t = uv_guess_handle(fd);
87
8009
  const char* type = nullptr;
88
89

8009
  switch (t) {
90
2
  case UV_TCP: type = "TCP"; break;
91
25
  case UV_TTY: type = "TTY"; break;
92
  case UV_UDP: type = "UDP"; break;
93
5522
  case UV_FILE: type = "FILE"; break;
94
2459
  case UV_NAMED_PIPE: type = "PIPE"; break;
95
1
  case UV_UNKNOWN_HANDLE: type = "UNKNOWN"; break;
96
  default:
97
    ABORT();
98
  }
99
100
24027
  args.GetReturnValue().Set(OneByteString(env->isolate(), type));
101
8009
}
102
103
104
181
void TTYWrap::IsTTY(const FunctionCallbackInfo<Value>& args) {
105
362
  int fd = args[0]->Int32Value();
106
181
  CHECK_GE(fd, 0);
107
181
  bool rc = uv_guess_handle(fd) == UV_TTY;
108
543
  args.GetReturnValue().Set(rc);
109
181
}
110
111
112
30
void TTYWrap::GetWindowSize(const FunctionCallbackInfo<Value>& args) {
113
30
  Environment* env = Environment::GetCurrent(args);
114
115
  TTYWrap* wrap;
116
60
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
117
                          args.Holder(),
118
                          args.GetReturnValue().Set(UV_EBADF));
119
60
  CHECK(args[0]->IsArray());
120
121
  int width, height;
122
30
  int err = uv_tty_get_winsize(&wrap->handle_, &width, &height);
123
124
30
  if (err == 0) {
125
60
    Local<v8::Array> a = args[0].As<Array>();
126
60
    a->Set(0, Integer::New(env->isolate(), width));
127
60
    a->Set(1, Integer::New(env->isolate(), height));
128
  }
129
130
60
  args.GetReturnValue().Set(err);
131
}
132
133
134
2
void TTYWrap::SetRawMode(const FunctionCallbackInfo<Value>& args) {
135
  TTYWrap* wrap;
136
4
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
137
                          args.Holder(),
138
                          args.GetReturnValue().Set(UV_EBADF));
139
4
  int err = uv_tty_set_mode(&wrap->handle_, args[0]->IsTrue());
140
4
  args.GetReturnValue().Set(err);
141
}
142
143
144
37
void TTYWrap::New(const FunctionCallbackInfo<Value>& args) {
145
37
  Environment* env = Environment::GetCurrent(args);
146
147
  // This constructor should not be exposed to public javascript.
148
  // Therefore we assert that we are not trying to call this as a
149
  // normal function.
150
37
  CHECK(args.IsConstructCall());
151
152
74
  int fd = args[0]->Int32Value();
153
37
  CHECK_GE(fd, 0);
154
155
37
  int err = 0;
156
111
  new TTYWrap(env, args.This(), fd, args[1]->IsTrue(), &err);
157
37
  if (err != 0) {
158
6
    env->CollectUVExceptionInfo(args[2], err, "uv_tty_init");
159
6
    args.GetReturnValue().SetUndefined();
160
  }
161
37
}
162
163
164
37
TTYWrap::TTYWrap(Environment* env,
165
                 Local<Object> object,
166
                 int fd,
167
                 bool readable,
168
                 int* init_err)
169
    : LibuvStreamWrap(env,
170
                      object,
171
                      reinterpret_cast<uv_stream_t*>(&handle_),
172
37
                      AsyncWrap::PROVIDER_TTYWRAP) {
173
37
  *init_err = uv_tty_init(env->event_loop(), &handle_, fd, readable);
174
37
}
175
176
}  // namespace node
177
178
3391
NODE_BUILTIN_MODULE_CONTEXT_AWARE(tty_wrap, node::TTYWrap::Initialize)