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: 25 66 37.9 %
Date: 2019-02-01 22:03:38 Branches: 4 33 12.1 %

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
164
void TTYWrap::Initialize(Local<Object> target,
45
                         Local<Value> unused,
46
                         Local<Context> context,
47
                         void* priv) {
48
164
  Environment* env = Environment::GetCurrent(context);
49
50
164
  Local<String> ttyString = FIXED_ONE_BYTE_STRING(env->isolate(), "TTY");
51
52
164
  Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
53
164
  t->SetClassName(ttyString);
54
328
  t->InstanceTemplate()->SetInternalFieldCount(1);
55
328
  t->Inherit(LibuvStreamWrap::GetConstructorTemplate(env));
56
57
164
  env->SetProtoMethodNoSideEffect(t, "getWindowSize", TTYWrap::GetWindowSize);
58
164
  env->SetProtoMethod(t, "setRawMode", SetRawMode);
59
60
164
  env->SetMethodNoSideEffect(target, "isTTY", IsTTY);
61
164
  env->SetMethodNoSideEffect(target, "guessHandleType", GuessHandleType);
62
63
  target->Set(env->context(),
64
              ttyString,
65
820
              t->GetFunction(env->context()).ToLocalChecked()).FromJust();
66
164
  env->set_tty_constructor_template(t);
67
164
}
68
69
70
404
void TTYWrap::GuessHandleType(const FunctionCallbackInfo<Value>& args) {
71
404
  Environment* env = Environment::GetCurrent(args);
72
  int fd;
73
2020
  if (!args[0]->Int32Value(env->context()).To(&fd)) return;
74
404
  CHECK_GE(fd, 0);
75
76
404
  uv_handle_type t = uv_guess_handle(fd);
77
404
  const char* type = nullptr;
78
79

404
  switch (t) {
80
  case UV_TCP: type = "TCP"; break;
81
  case UV_TTY: type = "TTY"; break;
82
  case UV_UDP: type = "UDP"; break;
83
12
  case UV_FILE: type = "FILE"; break;
84
392
  case UV_NAMED_PIPE: type = "PIPE"; break;
85
  case UV_UNKNOWN_HANDLE: type = "UNKNOWN"; break;
86
  default:
87
    ABORT();
88
  }
89
90
1212
  args.GetReturnValue().Set(OneByteString(env->isolate(), type));
91
}
92
93
94
void TTYWrap::IsTTY(const FunctionCallbackInfo<Value>& args) {
95
  Environment* env = Environment::GetCurrent(args);
96
  int fd;
97
  if (!args[0]->Int32Value(env->context()).To(&fd)) return;
98
  CHECK_GE(fd, 0);
99
  bool rc = uv_guess_handle(fd) == UV_TTY;
100
  args.GetReturnValue().Set(rc);
101
}
102
103
104
void TTYWrap::GetWindowSize(const FunctionCallbackInfo<Value>& args) {
105
  Environment* env = Environment::GetCurrent(args);
106
107
  TTYWrap* wrap;
108
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
109
                          args.Holder(),
110
                          args.GetReturnValue().Set(UV_EBADF));
111
  CHECK(args[0]->IsArray());
112
113
  int width, height;
114
  int err = uv_tty_get_winsize(&wrap->handle_, &width, &height);
115
116
  if (err == 0) {
117
    Local<v8::Array> a = args[0].As<Array>();
118
    a->Set(env->context(), 0, Integer::New(env->isolate(), width)).FromJust();
119
    a->Set(env->context(), 1, Integer::New(env->isolate(), height)).FromJust();
120
  }
121
122
  args.GetReturnValue().Set(err);
123
}
124
125
126
void TTYWrap::SetRawMode(const FunctionCallbackInfo<Value>& args) {
127
  TTYWrap* wrap;
128
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
129
                          args.Holder(),
130
                          args.GetReturnValue().Set(UV_EBADF));
131
  int err = uv_tty_set_mode(&wrap->handle_, args[0]->IsTrue());
132
  args.GetReturnValue().Set(err);
133
}
134
135
136
void TTYWrap::New(const FunctionCallbackInfo<Value>& args) {
137
  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
  CHECK(args.IsConstructCall());
143
144
  int fd;
145
  if (!args[0]->Int32Value(env->context()).To(&fd)) return;
146
  CHECK_GE(fd, 0);
147
148
  int err = 0;
149
  new TTYWrap(env, args.This(), fd, args[1]->IsTrue(), &err);
150
  if (err != 0) {
151
    env->CollectUVExceptionInfo(args[2], err, "uv_tty_init");
152
    args.GetReturnValue().SetUndefined();
153
  }
154
}
155
156
157
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
                      AsyncWrap::PROVIDER_TTYWRAP) {
166
  *init_err = uv_tty_init(env->event_loop(), &handle_, fd, readable);
167
  set_fd(fd);
168
  if (*init_err != 0)
169
    MarkAsUninitialized();
170
}
171
172
}  // namespace node
173
174
164
NODE_MODULE_CONTEXT_AWARE_INTERNAL(tty_wrap, node::TTYWrap::Initialize)