-
Notifications
You must be signed in to change notification settings - Fork 34
/
timer.hpp
82 lines (71 loc) · 2.38 KB
/
timer.hpp
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
#include <v8.h>
#include <uv.h>
struct timer
{
uv_timer_t uvTimer;
v8::Isolate *isolate;
v8::Global<v8::Function> callback;
};
uv_loop_t *loop;
class Timer
{
public:
static void Initialize(uv_loop_t *evloop)
{
loop = evloop;
}
static void Timeout(const v8::FunctionCallbackInfo<v8::Value> &args)
{
auto isolate = args.GetIsolate();
auto context = isolate->GetCurrentContext();
int64_t sleep = args[0]->IntegerValue(context).ToChecked();
int64_t interval = args[1]->IntegerValue(context).ToChecked();
// printf("sleep %lld, interval %lld\n", sleep, interval);
// v8::String::Utf8Value callbackStr(isolate, args[2]);
// printf("%s", *callbackStr);
v8::Local<v8::Value> callback = args[2];
if(!callback->IsFunction())
{
printf("callback not declared!");
return;
}
timer *timerWrap = new timer();
timerWrap->callback.Reset(isolate, callback.As<v8::Function>());
timerWrap->uvTimer.data = (void *)timerWrap;
timerWrap->isolate = isolate;
uv_timer_init(loop, &timerWrap->uvTimer);
uv_timer_start(&timerWrap->uvTimer, onTimerCallback, sleep, interval);
}
static void onTimerCallback(uv_timer_t *handle)
{
timer *timerWrap = (timer *)handle->data;
v8::Isolate *isolate = timerWrap->isolate;
v8::Local<v8::Context> context = isolate->GetCurrentContext();
if(isolate->IsDead()) {
printf("isolate is dead");
return;
}
v8::Local<v8::Function> callback = v8::Local<v8::Function>::New(
isolate,
timerWrap->callback
);
v8::Local<v8::Value> result;
v8::Handle<v8::Value> resultr [] = {
v8::Undefined(isolate),
v8_str("hello world")
};
if(callback->Call(
context,
v8::Undefined(isolate),
2,
resultr).ToLocal(&result)
)
{
// OK, the callback succeeded
}
else
{
// some exception happened
}
}
};