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.

Statistics
| Branch: | Revision:

main_repo / src / node_timer.cc @ 1a126ed1

History | View | Annotate | Download (4.88 KB)

1
#include "node.h"
2
#include "node_timer.h"
3
#include <assert.h>
4

    
5
using namespace v8;
6

    
7
class Timer {
8
 public:
9
  Timer(Handle<Function> callback, int argc, Handle<Value> argv[], ev_tstamp after, ev_tstamp repeat);
10
  ~Timer();
11
  Local<External> CreateTimeoutID ();
12
  void CallCallback ();
13
 private:
14
  ev_timer watcher;
15
  Persistent<External> timeoutID;
16
  Persistent<Function> callback;
17
  int argc;
18
  Persistent<Value> argv[];
19
};
20

    
21
static void
22
onTimeout (struct ev_loop *loop, ev_timer *watcher, int revents)
23
{
24
  Timer *timer = static_cast<Timer*>(watcher->data);
25

    
26
  timer->CallCallback();
27

    
28
  // use ev_is_active instead?
29
  if(watcher->repeat == 0.) 
30
    delete timer;
31
}
32

    
33
Timer::Timer (Handle<Function> _callback, int _argc, Handle<Value> _argv[], ev_tstamp after, ev_tstamp repeat)
34
{
35
  HandleScope scope;
36
  callback = Persistent<Function>::New(_callback);
37
  argc = _argc;
38

    
39
  ev_timer_init (&watcher, onTimeout, after, repeat);
40
  watcher.data = this;
41
  ev_timer_start (node_loop(), &watcher);
42
}
43

    
44
Timer::~Timer ()
45
{
46
  ev_timer_stop (node_loop(), &watcher);
47

    
48
  callback.Dispose();
49

    
50
  timeoutID.Dispose();
51
  timeoutID.Clear();
52
}
53

    
54
void
55
Timer::CallCallback ()
56
{
57
  HandleScope scope;
58

    
59
  TryCatch try_catch;
60

    
61
  callback->Call (Context::GetCurrent()->Global(), argc, argv);
62

    
63
  if(try_catch.HasCaught())
64
    node_fatal_exception(try_catch);
65
}
66

    
67
Local<External>
68
Timer::CreateTimeoutID ()
69
{
70
  HandleScope scope;
71

    
72
  Local<External> timeoutID_local = External::New(this);
73

    
74
  timeoutID = Persistent<External>::New(timeoutID_local);
75

    
76
  return scope.Close(timeoutID_local);
77
}
78

    
79
static Timer *
80
UnwrapTimeoutID (Handle<External> timeoutID)
81
{
82
  HandleScope scope;
83

    
84
  Timer *timer = static_cast<Timer*>(timeoutID->Value());
85

    
86
  return timer;
87
}
88

    
89
// timeoutID = setTimeout(func, delay, [param1, param2, ...]);
90
// timeoutID = setTimeout(code, delay);
91
// 
92
// * timeoutID is the ID of the timeout, which can be used with
93
//   clearTimeout.
94
//
95
// * func is the function you want to execute after delay milliseconds.
96
//
97
// * code in the alternate syntax, is a string of code you want to execute
98
//   after delay milliseconds. (not recommended)
99
//
100
// * delay is the number of milliseconds (thousandths of a second) that the
101
//   function call should be delayed by. 
102
static Handle<Value>
103
setTimeout(const Arguments& args) 
104
{
105
  if (args.Length() < 2)
106
    return Undefined();
107

    
108
  HandleScope scope;
109

    
110
  Local<Function> callback = Local<Function>::Cast(args[0]);
111
  uint32_t delay = args[1]->Uint32Value();
112

    
113
  ev_tstamp after = (double)delay / 1000.0;
114

    
115
  if (args.Length() > 2)
116
    assert(0 && "extra params to setTimeout not yet implemented.");
117
  int argc = 0;
118
  Handle<Value> argv[] = {};
119
  /*
120
  int argc = args.Length() - 2;
121
  Handle<Value> argv[] = new Handle<Value>[argc];
122
  // the rest of the arguments, if any arg parameters for the callback
123
  for(int i = 2; i < args.Length(); i++)
124
    argv[i - 2] = args[i];
125
  */
126

    
127
  Timer *timer = new Timer(callback, argc, argv, after, 0.0);
128

    
129
  Local<External> timeoutID = timer->CreateTimeoutID();
130

    
131
  return scope.Close(timeoutID);
132
}
133

    
134
// clearTimeout(timeoutID)
135
static Handle<Value> clearTimeout
136
  ( const Arguments& args
137
  ) 
138
{
139
  if (args.Length() < 1)
140
    return Undefined();
141

    
142
  Handle<External> timeoutID = Handle<External>::Cast(args[0]);
143
  Timer *timer = UnwrapTimeoutID(timeoutID);
144

    
145
  delete timer;
146

    
147
  return Undefined();
148
}
149

    
150
// intervalID = setInterval(func, delay[, param1, param2, ...]);
151
// intervalID = setInterval(code, delay);
152
// 
153
// where
154
// 
155
// * intervalID is a unique interval ID you can pass to clearInterval().
156
//
157
// * func is the function you want to be called repeatedly.
158
//
159
// * code in the alternate syntax, is a string of code you want to be executed
160
//   repeatedly.
161
//
162
// * delay is the number of milliseconds (thousandths of a second) that the
163
//   setInterval() function should wait before each call to func.
164
static Handle<Value> setInterval
165
  ( const Arguments& args
166
  ) 
167
{
168
  if (args.Length() < 2)
169
    return Undefined();
170

    
171
  HandleScope scope;
172

    
173
  Local<Function> callback = Local<Function>::Cast(args[0]);
174
  uint32_t delay = args[1]->Uint32Value();
175

    
176
  ev_tstamp after = (double)delay / 1000.0;
177

    
178
  if (args.Length() > 2)
179
    assert(0 && "extra params to setInterval not yet implemented.");
180
  int argc = 0;
181
  Handle<Value> argv[] = {};
182

    
183
  Timer *timer = new Timer(callback, argc, argv, after, after);
184

    
185
  Local<External> timeoutID = timer->CreateTimeoutID();
186

    
187
  return scope.Close(timeoutID);
188
}
189

    
190
void
191
Init_timer (Handle<Object> target)
192
{
193
  HandleScope scope;
194

    
195
  target->Set ( String::New("setTimeout")
196
              , FunctionTemplate::New(setTimeout)->GetFunction()
197
              );
198

    
199
  target->Set ( String::New("clearTimeout")
200
              , FunctionTemplate::New(clearTimeout)->GetFunction()
201
              );
202

    
203
  target->Set ( String::New("setInterval")
204
              , FunctionTemplate::New(setInterval)->GetFunction()
205
              );
206

    
207
  target->Set ( String::New("clearInterval")
208
              , FunctionTemplate::New(clearTimeout)->GetFunction()
209
              );
210
}