-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathqtimer_wrapper.h
More file actions
226 lines (208 loc) · 7.69 KB
/
Copy pathqtimer_wrapper.h
File metadata and controls
226 lines (208 loc) · 7.69 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#pragma once
namespace shelllet {
namespace core {
template<typename T, typename U, bool C>
class QTimerWrapper : public QObjectWrapper<T, U, C>
{
public:
template <bool M>
QTimerWrapper(QTimerPrivate<T, M>& d, const v8::FunctionCallbackInfo<v8::Value>& args)
: QObjectWrapper(d, args) {
}
QTimerWrapper(v8::Isolate* isolate, const v8::Local<v8::FunctionTemplate>& tpl) : QObjectWrapper(isolate, tpl) {}
QTimerWrapper(v8::Isolate* isolate, const v8::Local<v8::ObjectTemplate>& proto) : QObjectWrapper(isolate, proto)
{
proto->Set(isolate, "callOnTimeout", v8::FunctionTemplate::New(isolate, CallOnTimeout));
proto->Set(isolate, "interval", v8::FunctionTemplate::New(isolate, Interval));
proto->Set(isolate, "intervalAsDuration", v8::FunctionTemplate::New(isolate, IntervalAsDuration));
proto->Set(isolate, "isActive", v8::FunctionTemplate::New(isolate, IsActive));
proto->Set(isolate, "isSingleShot", v8::FunctionTemplate::New(isolate, IsSingleShot));
proto->Set(isolate, "remainingTime", v8::FunctionTemplate::New(isolate, RemainingTime));
proto->Set(isolate, "remainingTimeAsDuration", v8::FunctionTemplate::New(isolate, RemainingTimeAsDuration));
proto->Set(isolate, "setInterval", v8::FunctionTemplate::New(isolate, SetInterval));
proto->Set(isolate, "setSingleShot", v8::FunctionTemplate::New(isolate, SetSingleShot));
proto->Set(isolate, "setTimerType", v8::FunctionTemplate::New(isolate, SetTimerType));
proto->Set(isolate, "start", v8::FunctionTemplate::New(isolate, Start));
proto->Set(isolate, "timerId", v8::FunctionTemplate::New(isolate, TimerId));
proto->Set(isolate, "timerType", v8::FunctionTemplate::New(isolate, TimerType));
proto->Set(isolate, "stop", v8::FunctionTemplate::New(isolate, Stop));
proto->SetAccessor(V8_NEW_STRING_VAR(isolate, "timeout"), nullptr, Timeout);
}
protected:
static void CallOnTimeout(const v8::FunctionCallbackInfo<v8::Value>& args)
{
V8_CREATE_LOCAL_CONTEXT_WITHOUT_LOCKER_BY_CALLBACK_INFO(args)
{
auto* self = UnWrap<QTimerWrapper<T, U, C>>(args.This());
if (!self)
throw std::domain_error(K_CONST_ERROR_CALLED);
}
V8_CREATE_LOCAL_CONTEXT_END
}
static void Interval(const v8::FunctionCallbackInfo<v8::Value>& args)
{
V8_CREATE_LOCAL_CONTEXT_WITHOUT_LOCKER_BY_CALLBACK_INFO(args)
{
auto* self = UnWrap<QTimerWrapper<T, U, C>>(args.This());
if (!self)
throw std::domain_error(K_CONST_ERROR_CALLED);
}
V8_CREATE_LOCAL_CONTEXT_END
}
static void IntervalAsDuration(const v8::FunctionCallbackInfo<v8::Value>& args)
{
V8_CREATE_LOCAL_CONTEXT_WITHOUT_LOCKER_BY_CALLBACK_INFO(args)
{
auto* self = UnWrap<QTimerWrapper<T, U, C>>(args.This());
if (!self)
throw std::domain_error(K_CONST_ERROR_CALLED);
}
V8_CREATE_LOCAL_CONTEXT_END
}
static void IsActive(const v8::FunctionCallbackInfo<v8::Value>& args)
{
V8_CREATE_LOCAL_CONTEXT_WITHOUT_LOCKER_BY_CALLBACK_INFO(args)
{
auto* self = UnWrap<QTimerWrapper<T, U, C>>(args.This());
if (!self)
throw std::domain_error(K_CONST_ERROR_CALLED);
}
V8_CREATE_LOCAL_CONTEXT_END
}
static void IsSingleShot(const v8::FunctionCallbackInfo<v8::Value>& args)
{
V8_CREATE_LOCAL_CONTEXT_WITHOUT_LOCKER_BY_CALLBACK_INFO(args)
{
auto* self = UnWrap<QTimerWrapper<T, U, C>>(args.This());
if (!self)
throw std::domain_error(K_CONST_ERROR_CALLED);
}
V8_CREATE_LOCAL_CONTEXT_END
}
static void RemainingTime(const v8::FunctionCallbackInfo<v8::Value>& args)
{
V8_CREATE_LOCAL_CONTEXT_WITHOUT_LOCKER_BY_CALLBACK_INFO(args)
{
auto* self = UnWrap<QTimerWrapper<T, U, C>>(args.This());
if (!self)
throw std::domain_error(K_CONST_ERROR_CALLED);
}
V8_CREATE_LOCAL_CONTEXT_END
}
static void RemainingTimeAsDuration(const v8::FunctionCallbackInfo<v8::Value>& args)
{
V8_CREATE_LOCAL_CONTEXT_WITHOUT_LOCKER_BY_CALLBACK_INFO(args)
{
auto* self = UnWrap<QTimerWrapper<T, U, C>>(args.This());
if (!self)
throw std::domain_error(K_CONST_ERROR_CALLED);
}
V8_CREATE_LOCAL_CONTEXT_END
}
static void SetInterval(const v8::FunctionCallbackInfo<v8::Value>& args)
{
V8_CREATE_LOCAL_CONTEXT_WITHOUT_LOCKER_BY_CALLBACK_INFO(args)
{
auto* self = UnWrap<QTimerWrapper<T, U, C>>(args.This());
if (!self)
throw std::domain_error(K_CONST_ERROR_CALLED);
(*self)->setInterval(args[0]->Uint32Value(context).FromJust());
}
V8_CREATE_LOCAL_CONTEXT_END
}
static void SetSingleShot(const v8::FunctionCallbackInfo<v8::Value>& args)
{
V8_CREATE_LOCAL_CONTEXT_WITHOUT_LOCKER_BY_CALLBACK_INFO(args)
{
auto* self = UnWrap<QTimerWrapper<T, U, C>>(args.This());
if (!self)
throw std::domain_error(K_CONST_ERROR_CALLED);
}
V8_CREATE_LOCAL_CONTEXT_END
}
static void SetTimerType(const v8::FunctionCallbackInfo<v8::Value>& args)
{
V8_CREATE_LOCAL_CONTEXT_WITHOUT_LOCKER_BY_CALLBACK_INFO(args)
{
auto* self = UnWrap<QTimerWrapper<T, U, C>>(args.This());
if (!self)
throw std::domain_error(K_CONST_ERROR_CALLED);
}
V8_CREATE_LOCAL_CONTEXT_END
}
static void Start(const v8::FunctionCallbackInfo<v8::Value>& args)
{
V8_CREATE_LOCAL_CONTEXT_WITHOUT_LOCKER_BY_CALLBACK_INFO(args)
{
auto* self = UnWrap<QTimerWrapper<T, U, C>>(args.This());
if (!self)
throw std::domain_error(K_CONST_ERROR_CALLED);
if (args.Length() > 0) {
(*self)->start(args[0]->Uint32Value(context).FromJust());
}
else {
(*self)->start();
}
}
V8_CREATE_LOCAL_CONTEXT_END
}
static void TimerId(const v8::FunctionCallbackInfo<v8::Value>& args)
{
V8_CREATE_LOCAL_CONTEXT_WITHOUT_LOCKER_BY_CALLBACK_INFO(args)
{
auto* self = UnWrap<QTimerWrapper<T, U, C>>(args.This());
if (!self)
throw std::domain_error(K_CONST_ERROR_CALLED);
}
V8_CREATE_LOCAL_CONTEXT_END
}
static void TimerType(const v8::FunctionCallbackInfo<v8::Value>& args)
{
V8_CREATE_LOCAL_CONTEXT_WITHOUT_LOCKER_BY_CALLBACK_INFO(args)
{
auto* self = UnWrap<QTimerWrapper<T, U, C>>(args.This());
if (!self)
throw std::domain_error(K_CONST_ERROR_CALLED);
}
V8_CREATE_LOCAL_CONTEXT_END
}
static void Stop(const v8::FunctionCallbackInfo<v8::Value>& args)
{
V8_CREATE_LOCAL_CONTEXT_WITHOUT_LOCKER_BY_CALLBACK_INFO(args)
{
auto* self = UnWrap<QTimerWrapper<T, U, C>>(args.This());
if (!self)
throw std::domain_error(K_CONST_ERROR_CALLED);
}
V8_CREATE_LOCAL_CONTEXT_END
}
static void Timeout(v8::Local<v8::String> property, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<void>& info)
{
V8_CREATE_LOCAL_CONTEXT_WITHOUT_LOCKER_BY_CALLBACK_INFO(info)
{
auto* self = UnWrap<QTimerWrapper<T, U, C>>(info.This());
if (!self)
throw std::domain_error(K_CONST_ERROR_CALLED);
if (value->IsFunction()) {
std::shared_ptr<v8::Global<v8::Function>>weakPersistent = std::make_shared<v8::Global<v8::Function>>(isolate, v8::Local<v8::Function>::Cast(value));
QObject::connect(*self, &QTimer::timeout, [isolate, weakPersistent]() {
V8_CREATE_LOCAL_LOCKER_CONTEXT_BY_ISOLATE(isolate)
{
v8::TryCatch tryCatch(isolate);
weakPersistent->Get(isolate)->CallAsFunction(context, context->Global(), 0, {});
if (tryCatch.HasCaught()) {
throw std::runtime_error(GetException(isolate, tryCatch));
}
}
V8_CREATE_LOCAL_CONTEXT_END
});
}
else if (value->IsNullOrUndefined()) {
QObject::disconnect(*self, &QTimer::timeout, nullptr, nullptr);
}
}
V8_CREATE_LOCAL_CONTEXT_END
}
};
}
}