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 |
|
4474 |
void TTYWrap::Initialize(Local<Object> target, |
45 |
|
|
Local<Value> unused, |
46 |
|
|
Local<Context> context, |
47 |
|
|
void* priv) { |
48 |
|
4474 |
Environment* env = Environment::GetCurrent(context); |
49 |
|
|
|
50 |
|
4474 |
Local<String> ttyString = FIXED_ONE_BYTE_STRING(env->isolate(), "TTY"); |
51 |
|
|
|
52 |
|
4474 |
Local<FunctionTemplate> t = env->NewFunctionTemplate(New); |
53 |
|
4474 |
t->SetClassName(ttyString); |
54 |
|
4474 |
t->InstanceTemplate() |
55 |
|
8948 |
->SetInternalFieldCount(StreamBase::kStreamBaseFieldCount); |
56 |
|
8948 |
t->Inherit(LibuvStreamWrap::GetConstructorTemplate(env)); |
57 |
|
|
|
58 |
|
4474 |
env->SetProtoMethodNoSideEffect(t, "getWindowSize", TTYWrap::GetWindowSize); |
59 |
|
4474 |
env->SetProtoMethod(t, "setRawMode", SetRawMode); |
60 |
|
|
|
61 |
|
4474 |
env->SetMethodNoSideEffect(target, "isTTY", IsTTY); |
62 |
|
|
|
63 |
|
|
Local<Value> func; |
64 |
✓✗✓✗ ✓✗✓✗
|
26844 |
if (t->GetFunction(env->context()).ToLocal(&func) && |
65 |
✓✗✓✗
|
17896 |
target->Set(env->context(), ttyString, func).IsJust()) { |
66 |
|
4474 |
env->set_tty_constructor_template(t); |
67 |
|
|
} |
68 |
|
4474 |
} |
69 |
|
|
|
70 |
|
|
|
71 |
|
237 |
void TTYWrap::IsTTY(const FunctionCallbackInfo<Value>& args) { |
72 |
|
237 |
Environment* env = Environment::GetCurrent(args); |
73 |
|
|
int fd; |
74 |
✗✓ |
1185 |
if (!args[0]->Int32Value(env->context()).To(&fd)) return; |
75 |
✗✓ |
237 |
CHECK_GE(fd, 0); |
76 |
|
237 |
bool rc = uv_guess_handle(fd) == UV_TTY; |
77 |
|
711 |
args.GetReturnValue().Set(rc); |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
|
81 |
|
27 |
void TTYWrap::GetWindowSize(const FunctionCallbackInfo<Value>& args) { |
82 |
|
27 |
Environment* env = Environment::GetCurrent(args); |
83 |
|
|
|
84 |
|
|
TTYWrap* wrap; |
85 |
✗✓ |
54 |
ASSIGN_OR_RETURN_UNWRAP(&wrap, |
86 |
|
|
args.Holder(), |
87 |
|
|
args.GetReturnValue().Set(UV_EBADF)); |
88 |
✗✓ |
54 |
CHECK(args[0]->IsArray()); |
89 |
|
|
|
90 |
|
|
int width, height; |
91 |
|
27 |
int err = uv_tty_get_winsize(&wrap->handle_, &width, &height); |
92 |
|
|
|
93 |
✓✗ |
27 |
if (err == 0) { |
94 |
|
54 |
Local<Array> a = args[0].As<Array>(); |
95 |
|
108 |
a->Set(env->context(), 0, Integer::New(env->isolate(), width)).Check(); |
96 |
|
108 |
a->Set(env->context(), 1, Integer::New(env->isolate(), height)).Check(); |
97 |
|
|
} |
98 |
|
|
|
99 |
|
54 |
args.GetReturnValue().Set(err); |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
|
103 |
|
8 |
void TTYWrap::SetRawMode(const FunctionCallbackInfo<Value>& args) { |
104 |
|
|
TTYWrap* wrap; |
105 |
✗✓ |
16 |
ASSIGN_OR_RETURN_UNWRAP(&wrap, |
106 |
|
|
args.Holder(), |
107 |
|
|
args.GetReturnValue().Set(UV_EBADF)); |
108 |
|
16 |
int err = uv_tty_set_mode(&wrap->handle_, args[0]->IsTrue()); |
109 |
|
16 |
args.GetReturnValue().Set(err); |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
|
113 |
|
43 |
void TTYWrap::New(const FunctionCallbackInfo<Value>& args) { |
114 |
|
43 |
Environment* env = Environment::GetCurrent(args); |
115 |
|
|
|
116 |
|
|
// This constructor should not be exposed to public javascript. |
117 |
|
|
// Therefore we assert that we are not trying to call this as a |
118 |
|
|
// normal function. |
119 |
✗✓ |
43 |
CHECK(args.IsConstructCall()); |
120 |
|
|
|
121 |
|
|
int fd; |
122 |
✗✓ |
215 |
if (!args[0]->Int32Value(env->context()).To(&fd)) return; |
123 |
✗✓ |
43 |
CHECK_GE(fd, 0); |
124 |
|
|
|
125 |
|
43 |
int err = 0; |
126 |
|
129 |
new TTYWrap(env, args.This(), fd, args[1]->IsTrue(), &err); |
127 |
✓✓ |
43 |
if (err != 0) { |
128 |
|
4 |
env->CollectUVExceptionInfo(args[2], err, "uv_tty_init"); |
129 |
|
4 |
args.GetReturnValue().SetUndefined(); |
130 |
|
|
} |
131 |
|
|
} |
132 |
|
|
|
133 |
|
|
|
134 |
|
43 |
TTYWrap::TTYWrap(Environment* env, |
135 |
|
|
Local<Object> object, |
136 |
|
|
int fd, |
137 |
|
|
bool readable, |
138 |
|
|
int* init_err) |
139 |
|
|
: LibuvStreamWrap(env, |
140 |
|
|
object, |
141 |
|
|
reinterpret_cast<uv_stream_t*>(&handle_), |
142 |
|
43 |
AsyncWrap::PROVIDER_TTYWRAP) { |
143 |
|
43 |
*init_err = uv_tty_init(env->event_loop(), &handle_, fd, readable); |
144 |
|
43 |
set_fd(fd); |
145 |
✓✓ |
43 |
if (*init_err != 0) |
146 |
|
2 |
MarkAsUninitialized(); |
147 |
|
43 |
} |
148 |
|
|
|
149 |
|
|
} // namespace node |
150 |
|
|
|
151 |
|
5033 |
NODE_MODULE_CONTEXT_AWARE_INTERNAL(tty_wrap, node::TTYWrap::Initialize) |