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

Line 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::Function;
19
using v8::FunctionCallbackInfo;
20
using v8::FunctionTemplate;
21
using v8::Integer;
22
using v8::Local;
23
using v8::Object;
24
using v8::String;
25
using v8::Value;
26
27
28
1511
void TTYWrap::Initialize(Local<Object> target,
29
                         Local<Value> unused,
30
                         Local<Context> context) {
31
1511
  Environment* env = Environment::GetCurrent(context);
32
33
3022
  Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
34
1511
  t->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "TTY"));
35
3022
  t->InstanceTemplate()->SetInternalFieldCount(1);
36
37
1511
  env->SetProtoMethod(t, "close", HandleWrap::Close);
38
1511
  env->SetProtoMethod(t, "unref", HandleWrap::Unref);
39
1511
  env->SetProtoMethod(t, "hasRef", HandleWrap::HasRef);
40
41
1511
  StreamWrap::AddMethods(env, t, StreamBase::kFlagNoShutdown);
42
43
1511
  env->SetProtoMethod(t, "getWindowSize", TTYWrap::GetWindowSize);
44
1511
  env->SetProtoMethod(t, "setRawMode", SetRawMode);
45
46
1511
  env->SetMethod(target, "isTTY", IsTTY);
47
1511
  env->SetMethod(target, "guessHandleType", GuessHandleType);
48
49
6044
  target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "TTY"), t->GetFunction());
50
1511
  env->set_tty_constructor_template(t);
51
1511
}
52
53
54
uv_tty_t* TTYWrap::UVHandle() {
55
  return &handle_;
56
}
57
58
59
3425
void TTYWrap::GuessHandleType(const FunctionCallbackInfo<Value>& args) {
60
3425
  Environment* env = Environment::GetCurrent(args);
61
3425
  int fd = args[0]->Int32Value();
62
3425
  CHECK_GE(fd, 0);
63
64
3425
  uv_handle_type t = uv_guess_handle(fd);
65
3425
  const char* type = nullptr;
66
67
3425
  switch (t) {
68
  case UV_TCP: type = "TCP"; break;
69
8
  case UV_TTY: type = "TTY"; break;
70
  case UV_UDP: type = "UDP"; break;
71
2603
  case UV_FILE: type = "FILE"; break;
72
813
  case UV_NAMED_PIPE: type = "PIPE"; break;
73
1
  case UV_UNKNOWN_HANDLE: type = "UNKNOWN"; break;
74
  default:
75
    ABORT();
76
  }
77
78
10275
  args.GetReturnValue().Set(OneByteString(env->isolate(), type));
79
3425
}
80
81
82
5
void TTYWrap::IsTTY(const FunctionCallbackInfo<Value>& args) {
83
5
  int fd = args[0]->Int32Value();
84
5
  CHECK_GE(fd, 0);
85
5
  bool rc = uv_guess_handle(fd) == UV_TTY;
86
15
  args.GetReturnValue().Set(rc);
87
5
}
88
89
90
8
void TTYWrap::GetWindowSize(const FunctionCallbackInfo<Value>& args) {
91
8
  Environment* env = Environment::GetCurrent(args);
92
93
  TTYWrap* wrap;
94
8
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
95
                          args.Holder(),
96
                          args.GetReturnValue().Set(UV_EBADF));
97
8
  CHECK(args[0]->IsArray());
98
99
  int width, height;
100
8
  int err = uv_tty_get_winsize(&wrap->handle_, &width, &height);
101
102
8
  if (err == 0) {
103
16
    Local<v8::Array> a = args[0].As<Array>();
104
16
    a->Set(0, Integer::New(env->isolate(), width));
105
16
    a->Set(1, Integer::New(env->isolate(), height));
106
  }
107
108
16
  args.GetReturnValue().Set(err);
109
}
110
111
112
void TTYWrap::SetRawMode(const FunctionCallbackInfo<Value>& args) {
113
  TTYWrap* wrap;
114
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
115
                          args.Holder(),
116
                          args.GetReturnValue().Set(UV_EBADF));
117
  int err = uv_tty_set_mode(&wrap->handle_, args[0]->IsTrue());
118
  args.GetReturnValue().Set(err);
119
}
120
121
122
11
void TTYWrap::New(const FunctionCallbackInfo<Value>& args) {
123
11
  Environment* env = Environment::GetCurrent(args);
124
125
  // This constructor should not be exposed to public javascript.
126
  // Therefore we assert that we are not trying to call this as a
127
  // normal function.
128
11
  CHECK(args.IsConstructCall());
129
130
11
  int fd = args[0]->Int32Value();
131
11
  CHECK_GE(fd, 0);
132
133
22
  TTYWrap* wrap = new TTYWrap(env, args.This(), fd, args[1]->IsTrue());
134
11
  wrap->UpdateWriteQueueSize();
135
11
}
136
137
138
11
TTYWrap::TTYWrap(Environment* env, Local<Object> object, int fd, bool readable)
139
    : StreamWrap(env,
140
                 object,
141
                 reinterpret_cast<uv_stream_t*>(&handle_),
142
11
                 AsyncWrap::PROVIDER_TTYWRAP) {
143
22
  uv_tty_init(env->event_loop(), &handle_, fd, readable);
144
11
}
145
146
}  // namespace node
147
148
1560
NODE_MODULE_CONTEXT_AWARE_BUILTIN(tty_wrap, node::TTYWrap::Initialize)