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-08-22 22:13:06 Branches: 18 32 56.3 %

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

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

17859
NODE_MODULE_CONTEXT_AWARE_INTERNAL(tty_wrap, node::TTYWrap::Initialize)