-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWinTimer.cpp
121 lines (112 loc) · 4 KB
/
WinTimer.cpp
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
115
116
117
118
119
120
121
#include "Win.h"
#include "JS.h"
namespace {
static std::map<unsigned int, JSValue> timeoutCB;
static std::map<unsigned int, JSValue> intervalCB;
unsigned int runningTimeoutCBId{ 0 };
unsigned int runningIntervalCBId{ 0 };
bool intervalCBUseless{ false };
}
void Win::timeoutProc(HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
runningTimeoutCBId = idEvent;
auto ctx = JS::GetCtx();
JSValue ret = JS_Call(ctx, timeoutCB[runningTimeoutCBId], JS::MakeVal(0, JS_TAG_UNDEFINED), 0, nullptr);
JS_FreeValue(ctx, ret);
JS_FreeValue(ctx,timeoutCB[runningTimeoutCBId]);
timeoutCB.erase(runningTimeoutCBId);
runningTimeoutCBId = 0;
}
void Win::intervalProc(HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
runningIntervalCBId = idEvent;
auto ctx = JS::GetCtx();
JSValue ret = JS_Call(ctx, intervalCB[runningIntervalCBId], JS::MakeVal(0, JS_TAG_UNDEFINED), 0, nullptr);
JS_FreeValue(ctx, ret);
if (intervalCBUseless) {
KillTimer(hWnd, runningIntervalCBId);
JS_FreeValue(ctx, intervalCB[runningIntervalCBId]);
intervalCB.erase(runningIntervalCBId);
}
runningIntervalCBId = 0;
intervalCBUseless = false;
}
void Win::regTimer(JSContext* ctx, JSValue& proto) {
JS_SetPropertyStr(ctx, proto, "setTimeout", JS_NewCFunction(ctx, &Win::setTimeout, "setTimeout", 2));
JS_SetPropertyStr(ctx, proto, "setInterval", JS_NewCFunction(ctx, &Win::setInterval, "setInterval", 2));
JS_SetPropertyStr(ctx, proto, "clearTimeout", JS_NewCFunction(ctx, &Win::clearTimeout, "clearTimeout", 1));
JS_SetPropertyStr(ctx, proto, "clearInterval", JS_NewCFunction(ctx, &Win::clearInterval, "clearInterval", 1));
}
JSValue Win::setTimeout(JSContext* ctx, JSValueConst thisVal, int argc, JSValueConst* argv)
{
auto win = getPtr(thisVal);
unsigned int ms;
if (JS_ToUint32(ctx, &ms, argv[1])) {
return JS_ThrowTypeError(ctx, "arg1 error");
}
static unsigned int timerId = 1;
timeoutCB.insert({ timerId,JS_DupValue(ctx, argv[0]) });
SetTimer(win->hwnd, timerId, ms, (TIMERPROC)timeoutProc);
auto ret = JS_NewUint32(ctx, timerId);
timerId += 1;
return ret;
}
JSValue Win::setInterval(JSContext* ctx, JSValueConst thisVal, int argc, JSValueConst* argv)
{
auto win = getPtr(thisVal);
unsigned int ms;
if (JS_ToUint32(ctx, &ms, argv[1])) {
return JS_ThrowTypeError(ctx, "arg1 error");
}
static unsigned int timerId = 1;
intervalCB.insert({ timerId,JS_DupValue(ctx, argv[0]) });
SetTimer(win->hwnd, timerId, ms, (TIMERPROC)intervalProc);
auto ret = JS_NewUint32(ctx, timerId);
timerId += 1;
return ret;
}
JSValue Win::clearTimeout(JSContext* ctx, JSValueConst thisVal, int argc, JSValueConst* argv)
{
auto win = getPtr(thisVal);
unsigned int id;
if (JS_ToUint32(ctx, &id, argv[0])) {
return JS_ThrowTypeError(ctx, "arg1 error");
}
if (id == runningTimeoutCBId) {
return JS::MakeVal(0, JS_TAG_UNDEFINED);
}
KillTimer(win->hwnd, id);
JS_FreeValue(ctx, timeoutCB[id]);
timeoutCB.erase(id);
return JS::MakeVal(0, JS_TAG_UNDEFINED);
}
JSValue Win::clearInterval(JSContext* ctx, JSValueConst thisVal, int argc, JSValueConst* argv)
{
auto win = getPtr(thisVal);
unsigned int id;
if (JS_ToUint32(ctx, &id, argv[0])) {
return JS_ThrowTypeError(ctx, "arg1 error");
}
if (id == runningIntervalCBId) {
intervalCBUseless = true;
}
else
{
KillTimer(win->hwnd, id);
JS_FreeValue(ctx, intervalCB[id]);
intervalCB.erase(id);
}
return JS::MakeVal(0, JS_TAG_UNDEFINED);
}
void Win::DisposeTimer()
{
auto ctx = JS::GetCtx();
for (const auto& [key, value] : timeoutCB) {
KillTimer(hwnd, key);
JS_FreeValue(ctx, value);
}
for (const auto& [key, value] : intervalCB) {
KillTimer(hwnd, key);
JS_FreeValue(ctx, value);
}
}