summaryrefslogtreecommitdiff
path: root/src/tty_wrap.cc
blob: 692b2bbafd1f9dde488a63f29150d3b1d8b406d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <node.h>
#include <node_buffer.h>
#include <req_wrap.h>
#include <handle_wrap.h>
#include <stream_wrap.h>

namespace node {

using v8::Object;
using v8::Handle;
using v8::Local;
using v8::Persistent;
using v8::Value;
using v8::HandleScope;
using v8::FunctionTemplate;
using v8::String;
using v8::Function;
using v8::TryCatch;
using v8::Context;
using v8::Arguments;
using v8::Integer;
using v8::Undefined;


class TTYWrap : StreamWrap {
 public:
  static void Initialize(Handle<Object> target) {
    StreamWrap::Initialize(target);

    HandleScope scope;

    Local<FunctionTemplate> t = FunctionTemplate::New(New);
    t->SetClassName(String::NewSymbol("TTY"));

    t->InstanceTemplate()->SetInternalFieldCount(1);

    NODE_SET_PROTOTYPE_METHOD(t, "close", HandleWrap::Close);

    NODE_SET_PROTOTYPE_METHOD(t, "readStart", StreamWrap::ReadStart);
    NODE_SET_PROTOTYPE_METHOD(t, "readStop", StreamWrap::ReadStop);
    NODE_SET_PROTOTYPE_METHOD(t, "write", StreamWrap::Write);
    NODE_SET_PROTOTYPE_METHOD(t, "write", StreamWrap::Write);

    NODE_SET_METHOD(target, "isTTY", IsTTY);

    target->Set(String::NewSymbol("TTY"), t->GetFunction());
  }

 private:
  static Handle<Value> IsTTY(const Arguments& args) {
    HandleScope scope;
    int fd = args[0]->Int32Value();
    assert(fd >= 0);
    return uv_is_tty(fd) ? v8::True() : v8::False();
  }

  static Handle<Value> New(const Arguments& args) {
    HandleScope scope;

    // This constructor should not be exposed to public javascript.
    // Therefore we assert that we are not trying to call this as a
    // normal function.
    assert(args.IsConstructCall());

    int fd = args[0]->Int32Value();
    assert(fd >= 0);

    TTYWrap* wrap = new TTYWrap(args.This(), fd);
    assert(wrap);
    wrap->UpdateWriteQueueSize();

    return scope.Close(args.This());
  }

  TTYWrap(Handle<Object> object, int fd)
      : StreamWrap(object, (uv_stream_t*)&handle_) {
    uv_tty_init(uv_default_loop(), &handle_, fd);
  }

  uv_tty_t handle_;
};

}  // namespace node

NODE_MODULE(node_tty_wrap, node::TTYWrap::Initialize);