GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/node-core-coverage/node-core-coverage/workdir/node/out/../src/tty_wrap.cc Lines: 58 66 87.9 %
Date: 2016-09-10 Branches: 11 23 47.8 %

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
1590
void TTYWrap::Initialize(Local<Object> target,
27
                         Local<Value> unused,
28
                         Local<Context> context) {
29
1590
  Environment* env = Environment::GetCurrent(context);
30
31
3180
  Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
32
1590
  t->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "TTY"));
33
3180
  t->InstanceTemplate()->SetInternalFieldCount(1);
34
35
1590
  env->SetProtoMethod(t, "close", HandleWrap::Close);
36
1590
  env->SetProtoMethod(t, "unref", HandleWrap::Unref);
37
1590
  env->SetProtoMethod(t, "hasRef", HandleWrap::HasRef);
38
39
1590
  StreamWrap::AddMethods(env, t, StreamBase::kFlagNoShutdown);
40
41
1590
  env->SetProtoMethod(t, "getWindowSize", TTYWrap::GetWindowSize);
42
1590
  env->SetProtoMethod(t, "setRawMode", SetRawMode);
43
44
1590
  env->SetMethod(target, "isTTY", IsTTY);
45
1590
  env->SetMethod(target, "guessHandleType", GuessHandleType);
46
47
6360
  target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "TTY"), t->GetFunction());
48
1590
  env->set_tty_constructor_template(t);
49
1590
}
50
51
52
uv_tty_t* TTYWrap::UVHandle() {
53
  return &handle_;
54
}
55
56
57
3580
void TTYWrap::GuessHandleType(const FunctionCallbackInfo<Value>& args) {
58
3580
  Environment* env = Environment::GetCurrent(args);
59
3580
  int fd = args[0]->Int32Value();
60
3580
  CHECK_GE(fd, 0);
61
62
3580
  uv_handle_type t = uv_guess_handle(fd);
63
3580
  const char* type = nullptr;
64
65

3580
  switch (t) {
66
  case UV_TCP: type = "TCP"; break;
67
8
  case UV_TTY: type = "TTY"; break;
68
  case UV_UDP: type = "UDP"; break;
69
2712
  case UV_FILE: type = "FILE"; break;
70
859
  case UV_NAMED_PIPE: type = "PIPE"; break;
71
1
  case UV_UNKNOWN_HANDLE: type = "UNKNOWN"; break;
72
  default:
73
    ABORT();
74
  }
75
76
10740
  args.GetReturnValue().Set(OneByteString(env->isolate(), type));
77
3580
}
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
8
void TTYWrap::GetWindowSize(const FunctionCallbackInfo<Value>& args) {
89
8
  Environment* env = Environment::GetCurrent(args);
90
91
  TTYWrap* wrap;
92
8
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
93
                          args.Holder(),
94
                          args.GetReturnValue().Set(UV_EBADF));
95
8
  CHECK(args[0]->IsArray());
96
97
  int width, height;
98
8
  int err = uv_tty_get_winsize(&wrap->handle_, &width, &height);
99
100
8
  if (err == 0) {
101
16
    Local<v8::Array> a = args[0].As<Array>();
102
16
    a->Set(0, Integer::New(env->isolate(), width));
103
16
    a->Set(1, Integer::New(env->isolate(), height));
104
  }
105
106
16
  args.GetReturnValue().Set(err);
107
}
108
109
110
void TTYWrap::SetRawMode(const FunctionCallbackInfo<Value>& args) {
111
  TTYWrap* wrap;
112
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
113
                          args.Holder(),
114
                          args.GetReturnValue().Set(UV_EBADF));
115
  int err = uv_tty_set_mode(&wrap->handle_, args[0]->IsTrue());
116
  args.GetReturnValue().Set(err);
117
}
118
119
120
11
void TTYWrap::New(const FunctionCallbackInfo<Value>& args) {
121
11
  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
11
  CHECK(args.IsConstructCall());
127
128
11
  int fd = args[0]->Int32Value();
129
11
  CHECK_GE(fd, 0);
130
131
22
  TTYWrap* wrap = new TTYWrap(env, args.This(), fd, args[1]->IsTrue());
132
11
  wrap->UpdateWriteQueueSize();
133
11
}
134
135
136
11
TTYWrap::TTYWrap(Environment* env, Local<Object> object, int fd, bool readable)
137
    : StreamWrap(env,
138
                 object,
139
                 reinterpret_cast<uv_stream_t*>(&handle_),
140
11
                 AsyncWrap::PROVIDER_TTYWRAP) {
141
22
  uv_tty_init(env->event_loop(), &handle_, fd, readable);
142
11
}
143
144
}  // namespace node
145
146
1637
NODE_MODULE_CONTEXT_AWARE_BUILTIN(tty_wrap, node::TTYWrap::Initialize)