The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.
Please select the desired protocol below to get the URL.
This URL has Read-Only access.
main_repo / src / timer_wrap.cc @ 6f043940
History | View | Annotate | Download (5.06 KB)
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 "node.h" |
23 |
#include "handle_wrap.h" |
24 |
|
25 |
namespace node {
|
26 |
|
27 |
using v8::Object;
|
28 |
using v8::Handle;
|
29 |
using v8::Local;
|
30 |
using v8::Persistent;
|
31 |
using v8::Value;
|
32 |
using v8::HandleScope;
|
33 |
using v8::FunctionTemplate;
|
34 |
using v8::String;
|
35 |
using v8::Function;
|
36 |
using v8::TryCatch;
|
37 |
using v8::Context;
|
38 |
using v8::Arguments;
|
39 |
using v8::Integer;
|
40 |
|
41 |
static Persistent<String> ontimeout_sym;
|
42 |
|
43 |
class TimerWrap : public HandleWrap { |
44 |
public:
|
45 |
static void Initialize(Handle<Object> target) { |
46 |
HandleScope scope; |
47 |
|
48 |
HandleWrap::Initialize(target); |
49 |
|
50 |
Local<FunctionTemplate> constructor = FunctionTemplate::New(New); |
51 |
constructor->InstanceTemplate()->SetInternalFieldCount(1);
|
52 |
constructor->SetClassName(String::NewSymbol("Timer"));
|
53 |
|
54 |
NODE_SET_METHOD(constructor, "now", Now);
|
55 |
|
56 |
NODE_SET_PROTOTYPE_METHOD(constructor, "close", HandleWrap::Close);
|
57 |
NODE_SET_PROTOTYPE_METHOD(constructor, "ref", HandleWrap::Ref);
|
58 |
NODE_SET_PROTOTYPE_METHOD(constructor, "unref", HandleWrap::Unref);
|
59 |
|
60 |
NODE_SET_PROTOTYPE_METHOD(constructor, "start", Start);
|
61 |
NODE_SET_PROTOTYPE_METHOD(constructor, "stop", Stop);
|
62 |
NODE_SET_PROTOTYPE_METHOD(constructor, "setRepeat", SetRepeat);
|
63 |
NODE_SET_PROTOTYPE_METHOD(constructor, "getRepeat", GetRepeat);
|
64 |
NODE_SET_PROTOTYPE_METHOD(constructor, "again", Again);
|
65 |
|
66 |
ontimeout_sym = NODE_PSYMBOL("ontimeout");
|
67 |
|
68 |
target->Set(String::NewSymbol("Timer"), constructor->GetFunction());
|
69 |
} |
70 |
|
71 |
private:
|
72 |
static Handle<Value> New(const Arguments& args) { |
73 |
// This constructor should not be exposed to public javascript.
|
74 |
// Therefore we assert that we are not trying to call this as a
|
75 |
// normal function.
|
76 |
assert(args.IsConstructCall()); |
77 |
|
78 |
HandleScope scope; |
79 |
TimerWrap *wrap = new TimerWrap(args.This());
|
80 |
assert(wrap); |
81 |
|
82 |
return scope.Close(args.This());
|
83 |
} |
84 |
|
85 |
TimerWrap(Handle<Object> object) |
86 |
: HandleWrap(object, (uv_handle_t*) &handle_) { |
87 |
int r = uv_timer_init(uv_default_loop(), &handle_);
|
88 |
assert(r == 0);
|
89 |
handle_.data = this;
|
90 |
} |
91 |
|
92 |
~TimerWrap() { |
93 |
} |
94 |
|
95 |
static Handle<Value> Start(const Arguments& args) { |
96 |
HandleScope scope; |
97 |
|
98 |
UNWRAP(TimerWrap) |
99 |
|
100 |
int64_t timeout = args[0]->IntegerValue();
|
101 |
int64_t repeat = args[1]->IntegerValue();
|
102 |
|
103 |
int r = uv_timer_start(&wrap->handle_, OnTimeout, timeout, repeat);
|
104 |
|
105 |
if (r) SetErrno(uv_last_error(uv_default_loop()));
|
106 |
|
107 |
return scope.Close(Integer::New(r));
|
108 |
} |
109 |
|
110 |
static Handle<Value> Stop(const Arguments& args) { |
111 |
HandleScope scope; |
112 |
|
113 |
UNWRAP(TimerWrap) |
114 |
|
115 |
int r = uv_timer_stop(&wrap->handle_);
|
116 |
|
117 |
if (r) SetErrno(uv_last_error(uv_default_loop()));
|
118 |
|
119 |
return scope.Close(Integer::New(r));
|
120 |
} |
121 |
|
122 |
static Handle<Value> Again(const Arguments& args) { |
123 |
HandleScope scope; |
124 |
|
125 |
UNWRAP(TimerWrap) |
126 |
|
127 |
int r = uv_timer_again(&wrap->handle_);
|
128 |
|
129 |
if (r) SetErrno(uv_last_error(uv_default_loop()));
|
130 |
|
131 |
return scope.Close(Integer::New(r));
|
132 |
} |
133 |
|
134 |
static Handle<Value> SetRepeat(const Arguments& args) { |
135 |
HandleScope scope; |
136 |
|
137 |
UNWRAP(TimerWrap) |
138 |
|
139 |
int64_t repeat = args[0]->IntegerValue();
|
140 |
|
141 |
uv_timer_set_repeat(&wrap->handle_, repeat); |
142 |
|
143 |
return scope.Close(Integer::New(0)); |
144 |
} |
145 |
|
146 |
static Handle<Value> GetRepeat(const Arguments& args) { |
147 |
HandleScope scope; |
148 |
|
149 |
UNWRAP(TimerWrap) |
150 |
|
151 |
int64_t repeat = uv_timer_get_repeat(&wrap->handle_); |
152 |
|
153 |
if (repeat < 0) SetErrno(uv_last_error(uv_default_loop())); |
154 |
|
155 |
return scope.Close(Integer::New(repeat));
|
156 |
} |
157 |
|
158 |
static void OnTimeout(uv_timer_t* handle, int status) { |
159 |
HandleScope scope; |
160 |
|
161 |
TimerWrap* wrap = static_cast<TimerWrap*>(handle->data);
|
162 |
assert(wrap); |
163 |
|
164 |
Local<Value> argv[1] = { Integer::New(status) };
|
165 |
MakeCallback(wrap->object_, ontimeout_sym, ARRAY_SIZE(argv), argv); |
166 |
} |
167 |
|
168 |
static Handle<Value> Now(const Arguments& args) { |
169 |
HandleScope scope; |
170 |
|
171 |
uv_update_time(uv_default_loop()); |
172 |
double now = static_cast<double>(uv_now(uv_default_loop())); |
173 |
return scope.Close(v8::Number::New(now));
|
174 |
} |
175 |
|
176 |
uv_timer_t handle_; |
177 |
}; |
178 |
|
179 |
|
180 |
} // namespace node
|
181 |
|
182 |
NODE_MODULE(node_timer_wrap, node::TimerWrap::Initialize) |