Repository navigation
Expand file tree
/
Copy pathTimers.mm
More file actions
155 lines (124 loc) · 4.32 KB
/
Copy pathTimers.mm
File metadata and controls
155 lines (124 loc) · 4.32 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
#include "js_native_api.h"
#ifdef __APPLE__
#include "Timers.h"
#import <Foundation/Foundation.h>
#include <objc/runtime.h>
namespace charon {
void Timers::init(napi_env env) {
napi_value global, Performance, performance;
napi_get_global(env, &global);
const napi_property_descriptor properties[] = {
{
.utf8name = "setTimeout",
.name = nullptr,
.method = setTimeout,
.getter = nullptr,
.setter = nullptr,
.value = nullptr,
.attributes = napi_default,
.data = nullptr,
},
{
.utf8name = "setInterval",
.name = nullptr,
.method = setInterval,
.getter = nullptr,
.setter = nullptr,
.value = nullptr,
.attributes = napi_default,
.data = nullptr,
},
{
.utf8name = "clearTimeout",
.name = nullptr,
.method = clearTimer,
.getter = nullptr,
.setter = nullptr,
.value = nullptr,
.attributes = napi_default,
.data = nullptr,
},
{
.utf8name = "clearInterval",
.name = nullptr,
.method = clearTimer,
.getter = nullptr,
.setter = nullptr,
.value = nullptr,
.attributes = napi_default,
.data = nullptr,
},
};
napi_define_properties(env, global, 4, properties);
}
napi_value Timers::setTimeout(napi_env env, napi_callback_info cbinfo) {
size_t argc = 2;
napi_value argv[2];
napi_get_cb_info(env, cbinfo, &argc, argv, nullptr, nullptr);
double ms;
napi_get_value_double(env, argv[1], &ms);
NSTimeInterval interval = ms / 1000;
napi_ref callback;
napi_create_reference(env, argv[0], 1, &callback);
NSTimer *timer = [NSTimer
timerWithTimeInterval:interval
repeats:NO
block:^(NSTimer *timer) {
napi_value global, callbackValue;
napi_get_global(env, &global);
napi_get_reference_value(env, callback, &callbackValue);
napi_call_function(env, global, callbackValue, 0,
nullptr, nullptr);
napi_delete_reference(env, callback);
objc_setAssociatedObject(timer, "callback", nil,
OBJC_ASSOCIATION_ASSIGN);
}];
objc_setAssociatedObject(timer, "callback", (id)callback,
OBJC_ASSOCIATION_ASSIGN);
napi_value result;
napi_create_int64(env, (int64_t)timer, &result);
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
return result;
}
napi_value Timers::setInterval(napi_env env, napi_callback_info cbinfo) {
size_t argc = 2;
napi_value argv[2];
napi_get_cb_info(env, cbinfo, &argc, argv, nullptr, nullptr);
double ms;
napi_get_value_double(env, argv[1], &ms);
NSTimeInterval interval = ms / 1000;
napi_ref callback;
napi_create_reference(env, argv[0], 1, &callback);
NSTimer *timer = [NSTimer
timerWithTimeInterval:interval
repeats:YES
block:^(NSTimer *timer) {
napi_value global, callbackValue;
napi_get_global(env, &global);
napi_get_reference_value(env, callback, &callbackValue);
napi_call_function(env, global, callbackValue, 0,
nullptr, nullptr);
}];
objc_setAssociatedObject(timer, "callback", (id)callback,
OBJC_ASSOCIATION_ASSIGN);
napi_value result;
napi_create_int64(env, (int64_t)timer, &result);
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
return result;
}
napi_value Timers::clearTimer(napi_env env, napi_callback_info cbinfo) {
size_t argc = 1;
napi_value argv[1];
napi_get_cb_info(env, cbinfo, &argc, argv, nullptr, nullptr);
int64_t timer;
napi_get_value_int64(env, argv[0], &timer);
NSTimer *t = (NSTimer *)timer;
[t invalidate];
napi_ref callback = (napi_ref)objc_getAssociatedObject(t, "callback");
if (callback != nil) {
napi_delete_reference(env, callback);
}
return nullptr;
}
} // namespace charon
#endif // __APPLE__