Skip to content

Commit c1ae6ea

Browse files
committed
Add TTYWrap
1 parent 6b98a63 commit c1ae6ea

5 files changed

Lines changed: 119 additions & 0 deletions

File tree

node.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
'src/stream_wrap.cc',
9494
'src/tcp_wrap.cc',
9595
'src/timer_wrap.cc',
96+
'src/tty_wrap.cc',
9697
'src/process_wrap.cc',
9798
'src/v8_typed_array.cc',
9899
'src/udp_wrap.cc',

src/node_extensions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ NODE_EXT_LIST_ITEM(node_udp_wrap)
4949
NODE_EXT_LIST_ITEM(node_pipe_wrap)
5050
NODE_EXT_LIST_ITEM(node_cares_wrap)
5151
NODE_EXT_LIST_ITEM(node_stdio_wrap)
52+
NODE_EXT_LIST_ITEM(node_tty_wrap)
5253
NODE_EXT_LIST_ITEM(node_process_wrap)
5354

5455
NODE_EXT_LIST_END

src/tty_wrap.cc

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include <node.h>
2+
#include <node_buffer.h>
3+
#include <req_wrap.h>
4+
#include <handle_wrap.h>
5+
#include <stream_wrap.h>
6+
7+
namespace node {
8+
9+
using v8::Object;
10+
using v8::Handle;
11+
using v8::Local;
12+
using v8::Persistent;
13+
using v8::Value;
14+
using v8::HandleScope;
15+
using v8::FunctionTemplate;
16+
using v8::String;
17+
using v8::Function;
18+
using v8::TryCatch;
19+
using v8::Context;
20+
using v8::Arguments;
21+
using v8::Integer;
22+
using v8::Undefined;
23+
24+
25+
class TTYWrap : StreamWrap {
26+
public:
27+
static void Initialize(Handle<Object> target) {
28+
StreamWrap::Initialize(target);
29+
30+
HandleScope scope;
31+
32+
Local<FunctionTemplate> t = FunctionTemplate::New(New);
33+
t->SetClassName(String::NewSymbol("TTY"));
34+
35+
t->InstanceTemplate()->SetInternalFieldCount(1);
36+
37+
NODE_SET_PROTOTYPE_METHOD(t, "close", HandleWrap::Close);
38+
39+
NODE_SET_PROTOTYPE_METHOD(t, "readStart", StreamWrap::ReadStart);
40+
NODE_SET_PROTOTYPE_METHOD(t, "readStop", StreamWrap::ReadStop);
41+
NODE_SET_PROTOTYPE_METHOD(t, "write", StreamWrap::Write);
42+
NODE_SET_PROTOTYPE_METHOD(t, "write", StreamWrap::Write);
43+
44+
NODE_SET_METHOD(target, "isTTY", IsTTY);
45+
46+
target->Set(String::NewSymbol("TTY"), t->GetFunction());
47+
}
48+
49+
private:
50+
static Handle<Value> IsTTY(const Arguments& args) {
51+
HandleScope scope;
52+
int fd = args[0]->Int32Value();
53+
assert(fd >= 0);
54+
return uv_is_tty(fd) ? v8::True() : v8::False();
55+
}
56+
57+
static Handle<Value> New(const Arguments& args) {
58+
HandleScope scope;
59+
60+
// This constructor should not be exposed to public javascript.
61+
// Therefore we assert that we are not trying to call this as a
62+
// normal function.
63+
assert(args.IsConstructCall());
64+
65+
int fd = args[0]->Int32Value();
66+
assert(fd >= 0);
67+
68+
TTYWrap* wrap = new TTYWrap(args.This(), fd);
69+
assert(wrap);
70+
wrap->UpdateWriteQueueSize();
71+
72+
return scope.Close(args.This());
73+
}
74+
75+
TTYWrap(Handle<Object> object, int fd)
76+
: StreamWrap(object, (uv_stream_t*)&handle_) {
77+
uv_tty_init(uv_default_loop(), &handle_, fd);
78+
}
79+
80+
uv_tty_t handle_;
81+
};
82+
83+
} // namespace node
84+
85+
NODE_MODULE(node_tty_wrap, node::TTYWrap::Initialize);

test/simple/test-tty-wrap.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var common = require('../common');
2+
var assert = require('assert');
3+
4+
var TTY = process.binding('tty_wrap').TTY;
5+
var isTTY = process.binding('tty_wrap').isTTY;
6+
7+
if (isTTY(1) == false) {
8+
console.error("fd 1 is not a tty. skipping test.");
9+
process.exit(0);
10+
}
11+
12+
var handle = new TTY(1);
13+
var callbacks = 0;
14+
15+
var req1 = handle.write(Buffer("hello world\n"));
16+
req1.oncomplete = function() {
17+
callbacks++;
18+
};
19+
20+
var req2 = handle.write(Buffer("hello world\n"));
21+
req2.oncomplete = function() {
22+
callbacks++;
23+
};
24+
25+
handle.close();
26+
27+
process.on('exit', function() {
28+
assert.equal(2, callbacks);
29+
});
30+
31+

wscript

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,7 @@ def build(bld):
889889
src/pipe_wrap.cc
890890
src/cares_wrap.cc
891891
src/stdio_wrap.cc
892+
src/tty_wrap.cc
892893
src/process_wrap.cc
893894
src/v8_typed_array.cc
894895
"""

0 commit comments

Comments
 (0)