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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
#include "node.h"
#include "timer.h"
#include <assert.h>
using namespace v8;
using namespace node;
class Timer : ObjectWrap {
public:
Timer(Handle<Object> handle, Handle<Function> callback, ev_tstamp after, ev_tstamp repeat);
~Timer();
static Handle<Value> New (const Arguments& args);
static Handle<Value> Start (const Arguments& args);
static Handle<Value> Stop (const Arguments& args);
private:
static void OnTimeout (EV_P_ ev_timer *watcher, int revents);
ev_timer watcher_;
};
void
Timer::OnTimeout (EV_P_ ev_timer *watcher, int revents)
{
Timer *timer = static_cast<Timer*>(watcher->data);
HandleScope scope;
Local<Value> callback_value = timer->handle_->Get(String::NewSymbol("callback"));
if (!callback_value->IsFunction())
return;
Local<Function> callback = Local<Function>::Cast(callback_value);
TryCatch try_catch;
callback->Call (timer->handle_, 0, NULL);
if (try_catch.HasCaught())
fatal_exception(try_catch);
/* XXX i'm a bit worried if this is the correct test?
* it's rather crutial for memory leaks the conditional here test to see
* if the watcher will make another callback.
*/
if (false == ev_is_active(&timer->watcher_))
timer->Detach();
}
Timer::Timer (Handle<Object> handle, Handle<Function> callback, ev_tstamp after, ev_tstamp repeat)
: ObjectWrap(handle)
{
HandleScope scope;
handle_->Set(String::NewSymbol("callback"), callback);
ev_timer_init(&watcher_, Timer::OnTimeout, after, repeat);
watcher_.data = this;
ev_timer_start(EV_DEFAULT_UC_ &watcher_);
Attach();
}
Timer::~Timer ()
{
ev_timer_stop (EV_DEFAULT_UC_ &watcher_);
}
Handle<Value>
Timer::New (const Arguments& args)
{
if (args.Length() < 2)
return Undefined();
HandleScope scope;
Local<Function> callback = Local<Function>::Cast(args[0]);
ev_tstamp after = (double)(args[1]->IntegerValue()) / 1000.0;
ev_tstamp repeat = (double)(args[2]->IntegerValue()) / 1000.0;
new Timer(args.Holder(), callback, after, repeat);
return args.This();
}
Handle<Value>
Timer::Start (const Arguments& args)
{
Timer *timer = NODE_UNWRAP(Timer, args.Holder());
ev_timer_start(EV_DEFAULT_UC_ &timer->watcher_);
timer->Attach();
return Undefined();
}
Handle<Value>
Timer::Stop (const Arguments& args)
{
Timer *timer = NODE_UNWRAP(Timer, args.Holder());
ev_timer_stop(EV_DEFAULT_UC_ &timer->watcher_);
timer->Detach();
return Undefined();
}
void
node::Init_timer (Handle<Object> target)
{
HandleScope scope;
Local<FunctionTemplate> timer_template = FunctionTemplate::New(Timer::New);
timer_template->InstanceTemplate()->SetInternalFieldCount(1);
target->Set(String::NewSymbol("Timer"), timer_template->GetFunction());
NODE_SET_METHOD(timer_template->InstanceTemplate(), "start", Timer::Start);
NODE_SET_METHOD(timer_template->InstanceTemplate(), "stop", Timer::Stop);
}
|