GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/tty_wrap.cc Lines: 66 67 98.5 %
Date: 2019-02-13 22:28:58 Branches: 21 33 63.6 %

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 "stream_base-inl.h"
28
#include "stream_wrap.h"
29
#include "util-inl.h"
30
31
namespace node {
32
33
using v8::Array;
34
using v8::Context;
35
using v8::FunctionCallbackInfo;
36
using v8::FunctionTemplate;
37
using v8::Integer;
38
using v8::Local;
39
using v8::Object;
40
using v8::String;
41
using v8::Value;
42
43
44
4131
void TTYWrap::Initialize(Local<Object> target,
45
                         Local<Value> unused,
46
                         Local<Context> context,
47
                         void* priv) {
48
4131
  Environment* env = Environment::GetCurrent(context);
49
50
4131
  Local<String> ttyString = FIXED_ONE_BYTE_STRING(env->isolate(), "TTY");
51
52
4131
  Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
53
4131
  t->SetClassName(ttyString);
54
8262
  t->InstanceTemplate()->SetInternalFieldCount(1);
55
8262
  t->Inherit(LibuvStreamWrap::GetConstructorTemplate(env));
56
57
4131
  env->SetProtoMethodNoSideEffect(t, "getWindowSize", TTYWrap::GetWindowSize);
58
4131
  env->SetProtoMethod(t, "setRawMode", SetRawMode);
59
60
4131
  env->SetMethodNoSideEffect(target, "isTTY", IsTTY);
61
4131
  env->SetMethodNoSideEffect(target, "guessHandleType", GuessHandleType);
62
63
  target->Set(env->context(),
64
              ttyString,
65
20655
              t->GetFunction(env->context()).ToLocalChecked()).FromJust();
66
4131
  env->set_tty_constructor_template(t);
67
4131
}
68
69
70
3857
void TTYWrap::GuessHandleType(const FunctionCallbackInfo<Value>& args) {
71
3857
  Environment* env = Environment::GetCurrent(args);
72
  int fd;
73
19285
  if (!args[0]->Int32Value(env->context()).To(&fd)) return;
74
3857
  CHECK_GE(fd, 0);
75
76
3857
  uv_handle_type t = uv_guess_handle(fd);
77
3857
  const char* type = nullptr;
78
79

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