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: 54 54 100.0 %
Date: 2020-05-27 22:15:15 Branches: 16 28 57.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::Function;
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
3768
void TTYWrap::Initialize(Local<Object> target,
45
                         Local<Value> unused,
46
                         Local<Context> context,
47
                         void* priv) {
48
3768
  Environment* env = Environment::GetCurrent(context);
49
50
3768
  Local<String> ttyString = FIXED_ONE_BYTE_STRING(env->isolate(), "TTY");
51
52
3768
  Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
53
3768
  t->SetClassName(ttyString);
54
7536
  t->InstanceTemplate()->SetInternalFieldCount(StreamBase::kInternalFieldCount);
55
7536
  t->Inherit(LibuvStreamWrap::GetConstructorTemplate(env));
56
57
3768
  env->SetProtoMethodNoSideEffect(t, "getWindowSize", TTYWrap::GetWindowSize);
58
3768
  env->SetProtoMethod(t, "setRawMode", SetRawMode);
59
60
3768
  env->SetMethodNoSideEffect(target, "isTTY", IsTTY);
61
62
  Local<Value> func;
63

18840
  if (t->GetFunction(env->context()).ToLocal(&func) &&
64
15072
      target->Set(env->context(), ttyString, func).IsJust()) {
65
3768
    env->set_tty_constructor_template(t);
66
  }
67
3768
}
68
69
70
164
void TTYWrap::IsTTY(const FunctionCallbackInfo<Value>& args) {
71
164
  Environment* env = Environment::GetCurrent(args);
72
  int fd;
73
656
  if (!args[0]->Int32Value(env->context()).To(&fd)) return;
74
164
  CHECK_GE(fd, 0);
75
164
  bool rc = uv_guess_handle(fd) == UV_TTY;
76
492
  args.GetReturnValue().Set(rc);
77
}
78
79
80
31
void TTYWrap::GetWindowSize(const FunctionCallbackInfo<Value>& args) {
81
31
  Environment* env = Environment::GetCurrent(args);
82
83
  TTYWrap* wrap;
84
31
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
85
                          args.Holder(),
86
                          args.GetReturnValue().Set(UV_EBADF));
87
62
  CHECK(args[0]->IsArray());
88
89
  int width, height;
90
31
  int err = uv_tty_get_winsize(&wrap->handle_, &width, &height);
91
92
31
  if (err == 0) {
93
62
    Local<Array> a = args[0].As<Array>();
94
124
    a->Set(env->context(), 0, Integer::New(env->isolate(), width)).Check();
95
124
    a->Set(env->context(), 1, Integer::New(env->isolate(), height)).Check();
96
  }
97
98
62
  args.GetReturnValue().Set(err);
99
}
100
101
102
8
void TTYWrap::SetRawMode(const FunctionCallbackInfo<Value>& args) {
103
  TTYWrap* wrap;
104
8
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
105
                          args.Holder(),
106
                          args.GetReturnValue().Set(UV_EBADF));
107
16
  int err = uv_tty_set_mode(&wrap->handle_, args[0]->IsTrue());
108
16
  args.GetReturnValue().Set(err);
109
}
110
111
112
47
void TTYWrap::New(const FunctionCallbackInfo<Value>& args) {
113
47
  Environment* env = Environment::GetCurrent(args);
114
115
  // This constructor should not be exposed to public javascript.
116
  // Therefore we assert that we are not trying to call this as a
117
  // normal function.
118
47
  CHECK(args.IsConstructCall());
119
120
  int fd;
121
188
  if (!args[0]->Int32Value(env->context()).To(&fd)) return;
122
47
  CHECK_GE(fd, 0);
123
124
47
  int err = 0;
125
141
  new TTYWrap(env, args.This(), fd, args[1]->IsTrue(), &err);
126
47
  if (err != 0) {
127
4
    env->CollectUVExceptionInfo(args[2], err, "uv_tty_init");
128
4
    args.GetReturnValue().SetUndefined();
129
  }
130
}
131
132
133
47
TTYWrap::TTYWrap(Environment* env,
134
                 Local<Object> object,
135
                 int fd,
136
                 bool readable,
137
47
                 int* init_err)
138
    : LibuvStreamWrap(env,
139
                      object,
140
47
                      reinterpret_cast<uv_stream_t*>(&handle_),
141
47
                      AsyncWrap::PROVIDER_TTYWRAP) {
142
47
  *init_err = uv_tty_init(env->event_loop(), &handle_, fd, readable);
143
47
  set_fd(fd);
144
47
  if (*init_err != 0)
145
2
    MarkAsUninitialized();
146
47
}
147
148
}  // namespace node
149
150
4325
NODE_MODULE_CONTEXT_AWARE_INTERNAL(tty_wrap, node::TTYWrap::Initialize)