Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 166 lines (121 sloc) 4.64 kb
f35a396c » Igor Zinkovsky
2011-10-28 Revert "remove node_io_watcher"
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
ff4a9d38 » bnoordhuis
2012-03-10 core: use proper #include directives
22 #include "node_io_watcher.h"
f35a396c » Igor Zinkovsky
2011-10-28 Revert "remove node_io_watcher"
23
ff4a9d38 » bnoordhuis
2012-03-10 core: use proper #include directives
24 #include "node.h"
25 #include "v8.h"
f35a396c » Igor Zinkovsky
2011-10-28 Revert "remove node_io_watcher"
26
27 #include <assert.h>
28
29 namespace node {
30
31 using namespace v8;
32
33 Persistent<FunctionTemplate> IOWatcher::constructor_template;
34 Persistent<String> callback_symbol;
35
36
37 void IOWatcher::Initialize(Handle<Object> target) {
38 HandleScope scope;
39
40 Local<FunctionTemplate> t = FunctionTemplate::New(IOWatcher::New);
41 constructor_template = Persistent<FunctionTemplate>::New(t);
42 constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
43 constructor_template->SetClassName(String::NewSymbol("IOWatcher"));
44
45 NODE_SET_PROTOTYPE_METHOD(constructor_template, "start", IOWatcher::Start);
46 NODE_SET_PROTOTYPE_METHOD(constructor_template, "stop", IOWatcher::Stop);
47 NODE_SET_PROTOTYPE_METHOD(constructor_template, "set", IOWatcher::Set);
48
49 target->Set(String::NewSymbol("IOWatcher"), constructor_template->GetFunction());
50
51 callback_symbol = NODE_PSYMBOL("callback");
52 }
53
54
55 void IOWatcher::Callback(EV_P_ ev_io *w, int revents) {
56 IOWatcher *io = static_cast<IOWatcher*>(w->data);
57 assert(w == &io->watcher_);
58 HandleScope scope;
59
60 Local<Value> callback_v = io->handle_->Get(callback_symbol);
61 if (!callback_v->IsFunction()) {
62 io->Stop();
63 return;
64 }
65
66 Local<Function> callback = Local<Function>::Cast(callback_v);
67
68 Local<Value> argv[2];
69 argv[0] = Local<Value>::New(revents & EV_READ ? True() : False());
70 argv[1] = Local<Value>::New(revents & EV_WRITE ? True() : False());
71
a26bee8f » isaacs
2012-04-12 MakeCallback: Consistent symbol usage
72 MakeCallback(io->handle_, callback, ARRAY_SIZE(argv), argv);
f35a396c » Igor Zinkovsky
2011-10-28 Revert "remove node_io_watcher"
73 }
74
75
76 //
77 // var io = new process.IOWatcher();
78 // process.callback = function (readable, writable) { ... };
79 // io.set(fd, true, false);
80 // io.start();
81 //
82 Handle<Value> IOWatcher::New(const Arguments& args) {
83 if (!args.IsConstructCall()) {
84 return FromConstructorTemplate(constructor_template, args);
85 }
86
48c3d200 » isaacs
2012-06-21 Apply --no-deprecation to C++ warnings as well
87 if (!no_deprecation) {
88 fprintf(stderr, "WARNING: don't use IOWatcher, it'll be removed in v0.9\n");
89 }
90
f35a396c » Igor Zinkovsky
2011-10-28 Revert "remove node_io_watcher"
91 HandleScope scope;
92 IOWatcher *s = new IOWatcher();
93 s->Wrap(args.This());
94 return args.This();
95 }
96
97
98 Handle<Value> IOWatcher::Start(const Arguments& args) {
99 HandleScope scope;
100 IOWatcher *io = ObjectWrap::Unwrap<IOWatcher>(args.Holder());
101 io->Start();
102 return Undefined();
103 }
104
105
106 Handle<Value> IOWatcher::Stop(const Arguments& args) {
107 HandleScope scope;
108 IOWatcher *io = ObjectWrap::Unwrap<IOWatcher>(args.Holder());
109 io->Stop();
110 return Undefined();
111 }
112
113
114 void IOWatcher::Start() {
115 if (!ev_is_active(&watcher_)) {
116 ev_io_start(EV_DEFAULT_UC_ &watcher_);
117 Ref();
118 }
119 }
120
121
122 void IOWatcher::Stop() {
123 if (ev_is_active(&watcher_)) {
124 ev_io_stop(EV_DEFAULT_UC_ &watcher_);
125 Unref();
126 }
127 }
128
129
130 Handle<Value> IOWatcher::Set(const Arguments& args) {
131 HandleScope scope;
132
133 IOWatcher *io = ObjectWrap::Unwrap<IOWatcher>(args.Holder());
134
135 if (!args[0]->IsInt32()) {
136 return ThrowException(Exception::TypeError(
137 String::New("First arg should be a file descriptor.")));
138 }
139
140 int fd = args[0]->Int32Value();
141
142 if (!args[1]->IsBoolean()) {
143 return ThrowException(Exception::TypeError(
144 String::New("Second arg should boolean (readable).")));
145 }
146
147 int events = 0;
148
149 if (args[1]->IsTrue()) events |= EV_READ;
150
151 if (!args[2]->IsBoolean()) {
152 return ThrowException(Exception::TypeError(
153 String::New("Third arg should boolean (writable).")));
154 }
155
156 if (args[2]->IsTrue()) events |= EV_WRITE;
157
158 assert(!io->watcher_.active);
159 ev_io_set(&io->watcher_, fd, events);
160
161 return Undefined();
162 }
163
164
165
166 } // namespace node
Something went wrong with that request. Please try again.