GCC Code Coverage Report
Directory: ../src/ Exec Total Coverage
File: /home/node-core-coverage/node-core-coverage/workdir/node/out/../src/tty_wrap.cc Lines: 62 66 93.9 %
Date: 2017-01-02 Branches: 12 23 52.2 %

Line Branch Exec Source
1
#include "tty_wrap.h"
2
3
#include "env.h"
4
#include "env-inl.h"
5
#include "handle_wrap.h"
6
#include "node_buffer.h"
7
#include "node_wrap.h"
8
#include "req-wrap.h"
9
#include "req-wrap-inl.h"
10
#include "stream_wrap.h"
11
#include "util.h"
12
#include "util-inl.h"
13
14
namespace node {
15
16
using v8::Array;
17
using v8::Context;
18
using v8::FunctionCallbackInfo;
19
using v8::FunctionTemplate;
20
using v8::Integer;
21
using v8::Local;
22
using v8::Object;
23
using v8::Value;
24
25
26
1706
void TTYWrap::Initialize(Local<Object> target,
27
                         Local<Value> unused,
28
                         Local<Context> context) {
29
1706
  Environment* env = Environment::GetCurrent(context);
30
31
3412
  Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
32
1706
  t->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "TTY"));
33
3412
  t->InstanceTemplate()->SetInternalFieldCount(1);
34
35
1706
  env->SetProtoMethod(t, "close", HandleWrap::Close);
36
1706
  env->SetProtoMethod(t, "unref", HandleWrap::Unref);
37
1706
  env->SetProtoMethod(t, "hasRef", HandleWrap::HasRef);
38
39
1706
  StreamWrap::AddMethods(env, t, StreamBase::kFlagNoShutdown);
40
41
1706
  env->SetProtoMethod(t, "getWindowSize", TTYWrap::GetWindowSize);
42
1706
  env->SetProtoMethod(t, "setRawMode", SetRawMode);
43
44
1706
  env->SetMethod(target, "isTTY", IsTTY);
45
1706
  env->SetMethod(target, "guessHandleType", GuessHandleType);
46
47
6824
  target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "TTY"), t->GetFunction());
48
1706
  env->set_tty_constructor_template(t);
49
1706
}
50
51
52
uv_tty_t* TTYWrap::UVHandle() {
53
  return &handle_;
54
}
55
56
57
3862
void TTYWrap::GuessHandleType(const FunctionCallbackInfo<Value>& args) {
58
3862
  Environment* env = Environment::GetCurrent(args);
59
3862
  int fd = args[0]->Int32Value();
60
3862
  CHECK_GE(fd, 0);
61
62
3862
  uv_handle_type t = uv_guess_handle(fd);
63
3862
  const char* type = nullptr;
64
65

3862
  switch (t) {
66
  case UV_TCP: type = "TCP"; break;
67
13
  case UV_TTY: type = "TTY"; break;
68
  case UV_UDP: type = "UDP"; break;
69
2943
  case UV_FILE: type = "FILE"; break;
70
905
  case UV_NAMED_PIPE: type = "PIPE"; break;
71
1
  case UV_UNKNOWN_HANDLE: type = "UNKNOWN"; break;
72
  default:
73
    ABORT();
74
  }
75
76
11586
  args.GetReturnValue().Set(OneByteString(env->isolate(), type));
77
3862
}
78
79
80
6
void TTYWrap::IsTTY(const FunctionCallbackInfo<Value>& args) {
81
6
  int fd = args[0]->Int32Value();
82
6
  CHECK_GE(fd, 0);
83
6
  bool rc = uv_guess_handle(fd) == UV_TTY;
84
18
  args.GetReturnValue().Set(rc);
85
6
}
86
87
88
14
void TTYWrap::GetWindowSize(const FunctionCallbackInfo<Value>& args) {
89
14
  Environment* env = Environment::GetCurrent(args);
90
91
  TTYWrap* wrap;
92
14
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
93
                          args.Holder(),
94
                          args.GetReturnValue().Set(UV_EBADF));
95
14
  CHECK(args[0]->IsArray());
96
97
  int width, height;
98
14
  int err = uv_tty_get_winsize(&wrap->handle_, &width, &height);
99
100
14
  if (err == 0) {
101
28
    Local<v8::Array> a = args[0].As<Array>();
102
28
    a->Set(0, Integer::New(env->isolate(), width));
103
28
    a->Set(1, Integer::New(env->isolate(), height));
104
  }
105
106
28
  args.GetReturnValue().Set(err);
107
}
108
109
110
2
void TTYWrap::SetRawMode(const FunctionCallbackInfo<Value>& args) {
111
  TTYWrap* wrap;
112
2
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
113
                          args.Holder(),
114
                          args.GetReturnValue().Set(UV_EBADF));
115
4
  int err = uv_tty_set_mode(&wrap->handle_, args[0]->IsTrue());
116
4
  args.GetReturnValue().Set(err);
117
}
118
119
120
16
void TTYWrap::New(const FunctionCallbackInfo<Value>& args) {
121
16
  Environment* env = Environment::GetCurrent(args);
122
123
  // This constructor should not be exposed to public javascript.
124
  // Therefore we assert that we are not trying to call this as a
125
  // normal function.
126
16
  CHECK(args.IsConstructCall());
127
128
16
  int fd = args[0]->Int32Value();
129
16
  CHECK_GE(fd, 0);
130
131
32
  TTYWrap* wrap = new TTYWrap(env, args.This(), fd, args[1]->IsTrue());
132
16
  wrap->UpdateWriteQueueSize();
133
16
}
134
135
136
16
TTYWrap::TTYWrap(Environment* env, Local<Object> object, int fd, bool readable)
137
    : StreamWrap(env,
138
                 object,
139
                 reinterpret_cast<uv_stream_t*>(&handle_),
140
16
                 AsyncWrap::PROVIDER_TTYWRAP) {
141
32
  uv_tty_init(env->event_loop(), &handle_, fd, readable);
142
16
}
143
144
}  // namespace node
145
146
1757
NODE_MODULE_CONTEXT_AWARE_BUILTIN(tty_wrap, node::TTYWrap::Initialize)