c_eventloop.c

filter: Statementstmt 57.7% 108 / 187 Branchbran 40.9% 27 / 66 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 53.3% 135 / 253 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/JavaScript/Duktape/XS.pm ↑ Summary util.c »
1 /*
2 * C eventloop example.
3 *
4 * Timer management is similar to eventloop.js but implemented in C.
5 * In particular, timer insertion is an O(n) operation; in a real world
6 * eventloop based on a heap insertion would be O(log N).
7 */
8  
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <stdint.h>
13 #include <sys/time.h>
14 #include <poll.h>
15  
16 #include "util.h"
17 #include "duk_console.h"
18 #include "duktape.h"
19 #include "c_eventloop.h"
20  
21 #if !defined(DUKTAPE_EVENTLOOP_DEBUG)
22 #define DUKTAPE_EVENTLOOP_DEBUG 0 /* set to 1 to debug with printf */
23 #endif
24  
25 #define TIMERS_SLOT_NAME "eventTimers"
26 #define MIN_DELAY 1.0
27 #define MIN_WAIT 1.0
28 #define MAX_WAIT 60000.0
29 #define MAX_EXPIRIES 10
30  
31 #define MAX_FDS 256
32 #define MAX_TIMERS 4096 /* this is quite excessive for embedded use, but good for testing */
33  
34 typedef struct {
35 int64_t id; /* numeric ID (returned from e.g. setTimeout); zero if unused */
36 double target; /* next target time */
37 double delay; /* delay/interval */
38 int oneshot; /* oneshot=1 (setTimeout), repeated=0 (setInterval) */
39 int removed; /* timer has been requested for removal */
40  
41 /* The callback associated with the timer is held in the "global stash",
42 * in <stash>.eventTimers[String(id)]. The references must be deleted
43 * when a timer struct is deleted.
44 */
45 } ev_timer;
46  
47 /* Active timers. Dense list, terminates to end of list or first unused timer.
48 * The list is sorted by 'target', with lowest 'target' (earliest expiry) last
49 * in the list. When a timer's callback is being called, the timer is moved
50 * to 'timer_expiring' as it needs special handling should the user callback
51 * delete that particular timer.
52 */
53 static ev_timer timer_list[MAX_TIMERS];
54 static ev_timer timer_expiring;
55 static int timer_count; /* last timer at timer_count - 1 */
56 static int64_t timer_next_id = 1;
57  
58 /* Socket poll state. */
59 static struct pollfd poll_list[MAX_FDS];
60 static int poll_count = 0;
61  
62 /* Misc */
63 static int exit_requested = 0;
64  
65 13 static ev_timer *find_nearest_timer(void) {
66 /* Last timer expires first (list is always kept sorted). */
67 13 ▶ if (timer_count <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (timer_count <= 0) { 4 9 13
68 4 return NULL;
69 }
70 9 return timer_list + timer_count - 1;
71 }
72  
73 /* Bubble last timer on timer list backwards until it has been moved to
74 * its proper sorted position (based on 'target' time).
75 */
76 8 static void bubble_last_timer(void) {
77 int i;
78 8 int n = timer_count;
79 ev_timer *t;
80 ev_timer tmp;
81  
82 8 ▶ for (i = n - 1; i > 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = n - 1; i > 0; i--) { 2 6 8
83 /* Timer to bubble is at index i, timer to compare to is
84 * at i-1 (both guaranteed to exist).
85 */
86 2 t = timer_list + i;
87 2 ▶ if (t->target <= (t-1)->target) {
Branch: 1 branch
BranchTrueFalseTotal
if (t->target <= (t-1)->target) { 2 0 2
88 /* 't' expires earlier than (or same time as) 't-1', so we're done. */
89 2 break;
90 } else {
91 /* 't' expires later than 't-1', so swap them and repeat. */
92 0 memcpy((void *) &tmp, (void *) (t - 1), sizeof(ev_timer));
93 0 memcpy((void *) (t - 1), (void *) t, sizeof(ev_timer));
94 0 memcpy((void *) t, (void *) &tmp, sizeof(ev_timer));
95 }
96 }
97 8 }
98  
99 13 static void expire_timers(duk_context *ctx) {
100 ev_timer *t;
101 13 int sanity = MAX_EXPIRIES;
102 double now;
103 int rc;
104  
105 /* Because a user callback can mutate the timer list (by adding or deleting
106 * a timer), we expire one timer and then rescan from the end again. There
107 * is a sanity limit on how many times we do this per expiry round.
108 */
109  
110 13 duk_push_global_stash(ctx);
111 13 duk_get_prop_string(ctx, -1, TIMERS_SLOT_NAME);
112  
113 /* [ ... stash eventTimers ] */
114  
115 13 now = now_us() / 1000.0;
116 21 ▶ while (sanity-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (sanity-- > 0) { 21 0 21
117 /*
118 * If exit has been requested, exit without running further
119 * callbacks.
120 */
121  
122 21 ▶ if (exit_requested) {
Branch: 1 branch
BranchTrueFalseTotal
if (exit_requested) { 0 21 21
123 #if DUKTAPE_EVENTLOOP_DEBUG > 0
124 fprintf(stderr, "exit requested, exiting timer expiry loop\n");
125 fflush(stderr);
126 #endif
127 0 break;
128 }
129  
130 /*
131 * Expired timer(s) still exist?
132 */
133  
134 21 ▶ if (timer_count <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (timer_count <= 0) { 4 17 21
135 4 break;
136 }
137 17 t = timer_list + timer_count - 1;
138 17 ▶ if (t->target > now) {
Branch: 1 branch
BranchTrueFalseTotal
if (t->target > now) { 9 8 17
139 9 break;
140 }
141  
142 /*
143 * Move the timer to 'expiring' for the duration of the callback.
144 * Mark a one-shot timer deleted, compute a new target for an interval.
145 */
146  
147 8 memcpy((void *) &timer_expiring, (void *) t, sizeof(ev_timer));
148 8 memset((void *) t, 0, sizeof(ev_timer));
149 8 timer_count--;
150 8 t = &timer_expiring;
151  
152 8 ▶ if (t->oneshot) {
Branch: 1 branch
BranchTrueFalseTotal
if (t->oneshot) { 8 0 8
153 8 t->removed = 1;
154 } else {
155 0 t->target = now + t->delay; /* XXX: or t->target + t->delay? */
156 }
157  
158 /*
159 * Call timer callback. The callback can operate on the timer list:
160 * add new timers, remove timers. The callback can even remove the
161 * expired timer whose callback we're calling. However, because the
162 * timer being expired has been moved to 'timer_expiring', we don't
163 * need to worry about the timer's offset changing on the timer list.
164 */
165  
166 #if DUKTAPE_EVENTLOOP_DEBUG > 0
167 fprintf(stderr, "calling user callback for timer id %d\n", (int) t->id);
168 fflush(stderr);
169 #endif
170  
171 8 duk_push_number(ctx, (double) t->id);
172 8 duk_get_prop(ctx, -2); /* -> [ ... stash eventTimers func ] */
173 8 rc = duk_pcall(ctx, 0 /*nargs*/); /* -> [ ... stash eventTimers retval ] */
174 8 check_duktape_call_for_errors(rc, ctx);
175 8 duk_pop(ctx); /* [ ... stash eventTimers ] */
176  
177 8 ▶ if (t->removed) {
Branch: 1 branch
BranchTrueFalseTotal
if (t->removed) { 8 0 8
178 /* One-shot timer (always removed) or removed by user callback. */
179 #if DUKTAPE_EVENTLOOP_DEBUG > 0
180 fprintf(stderr, "deleting callback state for timer %d\n", (int) t->id);
181 fflush(stderr);
182 #endif
183 8 duk_push_number(ctx, (double) t->id);
184 8 duk_del_prop(ctx, -2);
185 } else {
186 /* Interval timer, not removed by user callback. Queue back to
187 * timer list and bubble to its final sorted position.
188 */
189 #if DUKTAPE_EVENTLOOP_DEBUG > 0
190 fprintf(stderr, "queueing timer %d back into active list\n", (int) t->id);
191 fflush(stderr);
192 #endif
193 0 ▶ if (timer_count >= MAX_TIMERS) {
Branch: 1 branch
BranchTrueFalseTotal
if (timer_count >= MAX_TIMERS) { 0 0 0
194 0 (void) duk_error(ctx, DUK_ERR_RANGE_ERROR, "out of timer slots");
195 }
196 0 memcpy((void *) (timer_list + timer_count), (void *) t, sizeof(ev_timer));
197 0 timer_count++;
198 0 bubble_last_timer();
199 }
200 }
201  
202 13 memset((void *) &timer_expiring, 0, sizeof(ev_timer));
203  
204 13 duk_pop_2(ctx); /* -> [ ... ] */
205 13 }
206  
207 13 static void compact_poll_list(void) {
208 int i, j, n;
209  
210 /* i = input index
211 * j = output index (initially same as i)
212 */
213  
214 13 n = poll_count;
215 13 ▶ for (i = 0, j = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0, j = 0; i < n; i++) { 0 13 13
216 0 struct pollfd *pfd = poll_list + i;
217 0 ▶ if (pfd->fd == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (pfd->fd == 0) { 0 0 0
218 /* keep output index the same */
219 #if DUKTAPE_EVENTLOOP_DEBUG > 0
220 fprintf(stderr, "remove pollfd (index %d): fd=%d, events=%d, revents=%d\n",
221 i, pfd->fd, pfd->events, pfd->revents),
222 fflush(stderr);
223 #endif
224  
225 0 continue;
226 }
227 #if DUKTAPE_EVENTLOOP_DEBUG > 0
228 fprintf(stderr, "keep pollfd (index %d -> %d): fd=%d, events=%d, revents=%d\n",
229 i, j, pfd->fd, pfd->events, pfd->revents),
230 fflush(stderr);
231 #endif
232 0 ▶ if (i != j) {
Branch: 1 branch
BranchTrueFalseTotal
if (i != j) { 0 0 0
233 /* copy only if indices have diverged */
234 0 memcpy((void *) (poll_list + j), (void *) (poll_list + i), sizeof(struct pollfd));
235 }
236 0 j++;
237 }
238  
239 13 ▶ if (j < poll_count) {
Branch: 1 branch
BranchTrueFalseTotal
if (j < poll_count) { 0 13 13
240 /* zeroize unused entries for sanity */
241 0 memset((void *) (poll_list + j), 0, (poll_count - j) * sizeof(struct pollfd));
242 }
243  
244 13 poll_count = j;
245 13 }
246  
247 4 duk_ret_t eventloop_run(duk_context *ctx, void *udata) {
248 ev_timer *t;
249 double now;
250 double diff;
251 int timeout;
252 int rc;
253 int i, n;
254 int idx_eventloop;
255 int idx_fd_handler;
256  
257 (void) udata;
258  
259 /* The Ecmascript poll handler is passed through EventLoop.fdPollHandler
260 * which c_eventloop.js sets before we come here.
261 */
262 4 duk_push_global_object(ctx);
263 4 duk_get_prop_string(ctx, -1, "EventLoop");
264 4 duk_get_prop_string(ctx, -1, "fdPollHandler"); /* -> [ global EventLoop fdPollHandler ] */
265 4 idx_fd_handler = duk_get_top_index(ctx);
266 4 idx_eventloop = idx_fd_handler - 1;
267  
268 for (;;) {
269 /*
270 * Expire timers.
271 */
272  
273 13 expire_timers(ctx);
274  
275 /*
276 * If exit requested, bail out as fast as possible.
277 */
278  
279 13 ▶ if (exit_requested) {
Branch: 1 branch
BranchTrueFalseTotal
if (exit_requested) { 0 13 13
280 #if DUKTAPE_EVENTLOOP_DEBUG > 0
281 fprintf(stderr, "exit requested, exiting event loop\n");
282 fflush(stderr);
283 #endif
284 0 break;
285 }
286  
287 /*
288 * Compact poll list by removing pollfds with fd == 0.
289 */
290  
291 13 compact_poll_list();
292  
293 /*
294 * Determine poll() timeout (as close to poll() as possible as
295 * the wait is relative).
296 */
297  
298 13 now = now_us() / 1000.0;
299 13 t = find_nearest_timer();
300 13 ▶ if (t) {
Branch: 1 branch
BranchTrueFalseTotal
if (t) { 9 4 13
301 9 diff = t->target - now;
302 9 ▶ if (diff < MIN_WAIT) {
Branch: 1 branch
BranchTrueFalseTotal
if (diff < MIN_WAIT) { 7 2 9
303 7 diff = MIN_WAIT;
304 2 ▶ } else if (diff > MAX_WAIT) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (diff > MAX_WAIT) { 0 2 2
305 0 diff = MAX_WAIT;
306 }
307 9 timeout = (int) diff; /* clamping ensures that fits */
308 } else {
309 4 ▶ if (poll_count == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (poll_count == 0) { 4 0 4
310 #if DUKTAPE_EVENTLOOP_DEBUG > 0
311 fprintf(stderr, "no timers and no sockets to poll, exiting\n");
312 fflush(stderr);
313 #endif
314 4 break;
315 }
316 0 timeout = (int) MAX_WAIT;
317 }
318  
319 /*
320 * Poll for activity or timeout.
321 */
322  
323 #if DUKTAPE_EVENTLOOP_DEBUG > 0
324 fprintf(stderr, "going to poll, timeout %d ms, pollfd count %d\n", timeout, poll_count);
325 fflush(stderr);
326 #endif
327  
328 9 rc = poll(poll_list, poll_count, timeout);
329 #if DUKTAPE_EVENTLOOP_DEBUG > 0
330 fprintf(stderr, "poll rc: %d\n", rc);
331 fflush(stderr);
332 #endif
333 if (rc < 0) {
334 /* error */
335 } else if (rc == 0) {
336 /* timeout */
337 } else {
338 /* 'rc' fds active */
339 }
340  
341 /*
342 * Check socket activity, handle all sockets. Handling is offloaded to
343 * Ecmascript code (fd + revents).
344 *
345 * If FDs are removed from the poll list while we're processing callbacks,
346 * the entries are simply marked unused (fd set to 0) without actually
347 * removing them from the poll list. This ensures indices are not
348 * disturbed. The poll list is compacted before next poll().
349 */
350  
351 9 ▶ n = (rc == 0 ? 0 : poll_count); /* if timeout, no need to check pollfd */
Branch: 1 branch
BranchTrueFalseTotal
n = (rc == 0 ? 0 : poll_count); /* if timeout, no need to check pollfd */ 0 9 9
352 9 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 9 9
353 0 struct pollfd *pfd = poll_list + i;
354  
355 0 ▶ if (pfd->fd == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (pfd->fd == 0) { 0 0 0
356 /* deleted, perhaps by previous callback */
357 0 continue;
358 }
359  
360 0 ▶ if (pfd->revents) {
Branch: 1 branch
BranchTrueFalseTotal
if (pfd->revents) { 0 0 0
361 #if DUKTAPE_EVENTLOOP_DEBUG > 0
362 fprintf(stderr, "fd %d has revents: %d\n", (int) pfd->fd, (int) pfd->revents);
363 fflush(stderr);
364 #endif
365 0 duk_dup(ctx, idx_fd_handler);
366 0 duk_dup(ctx, idx_eventloop);
367 0 duk_push_int(ctx, pfd->fd);
368 0 duk_push_int(ctx, pfd->revents);
369 0 rc = duk_pcall_method(ctx, 2 /*nargs*/);
370 0 check_duktape_call_for_errors(rc, ctx);
371 0 duk_pop(ctx);
372  
373 0 pfd->revents = 0;
374 }
375  
376 }
377 }
378  
379 4 duk_pop_n(ctx, 3);
380  
381 4 return 0;
382 }
383  
384 8 static int create_timer(duk_context *ctx) {
385 double delay;
386 int oneshot;
387 int idx;
388 int64_t timer_id;
389 double now;
390 ev_timer *t;
391  
392 8 now = now_us() / 1000.0;
393  
394 /* indexes:
395 * 0 = function (callback)
396 * 1 = delay
397 * 2 = boolean: oneshot
398 */
399  
400 8 delay = duk_require_number(ctx, 1);
401 8 ▶ if (delay < MIN_DELAY) {
Branch: 1 branch
BranchTrueFalseTotal
if (delay < MIN_DELAY) { 6 2 8
402 6 delay = MIN_DELAY;
403 }
404 8 oneshot = duk_require_boolean(ctx, 2);
405  
406 8 ▶ if (timer_count >= MAX_TIMERS) {
Branch: 1 branch
BranchTrueFalseTotal
if (timer_count >= MAX_TIMERS) { 0 8 8
407 0 (void) duk_error(ctx, DUK_ERR_RANGE_ERROR, "out of timer slots");
408 }
409 8 idx = timer_count++;
410 8 timer_id = timer_next_id++;
411 8 t = timer_list + idx;
412  
413 8 memset((void *) t, 0, sizeof(ev_timer));
414 8 t->id = timer_id;
415 8 t->target = now + delay;
416 8 t->delay = delay;
417 8 t->oneshot = oneshot;
418 8 t->removed = 0;
419  
420 /* Timer is now at the last position; use swaps to "bubble" it to its
421 * correct sorted position.
422 */
423  
424 8 bubble_last_timer();
425  
426 /* Finally, register the callback to the global stash 'eventTimers' object. */
427  
428 8 duk_push_global_stash(ctx);
429 8 duk_get_prop_string(ctx, -1, TIMERS_SLOT_NAME); /* -> [ func delay oneshot stash eventTimers ] */
430 8 duk_push_number(ctx, (double) timer_id);
431 8 duk_dup(ctx, 0);
432 8 duk_put_prop(ctx, -3); /* eventTimers[timer_id] = callback */
433  
434 /* Return timer id. */
435  
436 8 duk_push_number(ctx, (double) timer_id);
437 #if DUKTAPE_EVENTLOOP_DEBUG > 0
438 fprintf(stderr, "created timer id: %d\n", (int) timer_id);
439 fflush(stderr);
440 #endif
441 8 return 1;
442 }
443  
444 0 static int delete_timer(duk_context *ctx) {
445 int i, n;
446 int64_t timer_id;
447 ev_timer *t;
448 0 int found = 0;
449  
450 /* indexes:
451 * 0 = timer id
452 */
453  
454 0 timer_id = (int64_t) duk_require_number(ctx, 0);
455  
456 /*
457 * Unlike insertion, deletion needs a full scan of the timer list
458 * and an expensive remove. If no match is found, nothing is deleted.
459 * Caller gets a boolean return code indicating match.
460 *
461 * When a timer is being expired and its user callback is running,
462 * the timer has been moved to 'timer_expiring' and its deletion
463 * needs special handling: just mark it to-be-deleted and let the
464 * expiry code remove it.
465 */
466  
467 0 t = &timer_expiring;
468 0 ▶ if (t->id == timer_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (t->id == timer_id) { 0 0 0
469 0 t->removed = 1;
470 0 duk_push_true(ctx);
471 #if DUKTAPE_EVENTLOOP_DEBUG > 0
472 fprintf(stderr, "deleted expiring timer id: %d\n", (int) timer_id);
473 fflush(stderr);
474 #endif
475 0 return 1;
476 }
477  
478 0 n = timer_count;
479 0 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 0 0
480 0 t = timer_list + i;
481 0 ▶ if (t->id == timer_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (t->id == timer_id) { 0 0 0
482 0 found = 1;
483  
484 /* Shift elements downwards to keep the timer list dense
485 * (no need if last element).
486 */
487 0 ▶ if (i < timer_count - 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (i < timer_count - 1) { 0 0 0
488 0 memmove((void *) t, (void *) (t + 1), (timer_count - i - 1) * sizeof(ev_timer));
489 }
490  
491 /* Zero last element for clarity. */
492 0 memset((void *) (timer_list + n - 1), 0, sizeof(ev_timer));
493  
494 /* Update timer_count. */
495 0 timer_count--;
496  
497 /* The C state is now up-to-date, but we still need to delete
498 * the timer callback state from the global 'stash'.
499 */
500  
501 0 duk_push_global_stash(ctx);
502 0 duk_get_prop_string(ctx, -1, TIMERS_SLOT_NAME); /* -> [ timer_id stash eventTimers ] */
503 0 duk_push_number(ctx, (double) timer_id);
504 0 duk_del_prop(ctx, -2); /* delete eventTimers[timer_id] */
505  
506 #if DUKTAPE_EVENTLOOP_DEBUG > 0
507 fprintf(stderr, "deleted timer id: %d\n", (int) timer_id);
508 fflush(stderr);
509 #endif
510 0 break;
511 }
512 }
513  
514 #if DUKTAPE_EVENTLOOP_DEBUG > 0
515 if (!found) {
516 fprintf(stderr, "trying to delete timer id %d, but not found; ignoring\n", (int) timer_id);
517 fflush(stderr);
518 }
519 #endif
520  
521 0 duk_push_boolean(ctx, found);
522 0 return 1;
523 }
524  
525 0 static int listen_fd(duk_context *ctx) {
526 0 int fd = duk_require_int(ctx, 0);
527 0 int events = duk_require_int(ctx, 1);
528 int i, n;
529 struct pollfd *pfd;
530  
531 #if DUKTAPE_EVENTLOOP_DEBUG > 0
532 fprintf(stderr, "listen_fd: fd=%d, events=%d\n", fd, events);
533 fflush(stderr);
534 #endif
535 /* events == 0 means stop listening to the FD */
536  
537 0 n = poll_count;
538 0 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 0 0
539 0 pfd = poll_list + i;
540 0 ▶ if (pfd->fd == fd) {
Branch: 1 branch
BranchTrueFalseTotal
if (pfd->fd == fd) { 0 0 0
541 #if DUKTAPE_EVENTLOOP_DEBUG > 0
542 fprintf(stderr, "listen_fd: fd found at index %d\n", i);
543 fflush(stderr);
544 #endif
545 0 ▶ if (events == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (events == 0) { 0 0 0
546 /* mark to-be-deleted, cleaned up by next poll */
547 0 pfd->fd = 0;
548 } else {
549 0 pfd->events = events;
550 }
551 0 return 0;
552 }
553 }
554  
555 /* not found, append to list */
556 #if DUKTAPE_EVENTLOOP_DEBUG > 0
557 fprintf(stderr, "listen_fd: fd not found on list, add new entry\n");
558 fflush(stderr);
559 #endif
560  
561 0 ▶ if (poll_count >= MAX_FDS) {
Branch: 1 branch
BranchTrueFalseTotal
if (poll_count >= MAX_FDS) { 0 0 0
562 0 (void) duk_error(ctx, DUK_ERR_ERROR, "out of fd slots");
563 }
564  
565 0 pfd = poll_list + poll_count;
566 0 pfd->fd = fd;
567 0 pfd->events = events;
568 0 pfd->revents = 0;
569 0 poll_count++;
570  
571 0 return 0;
572 }
573  
574 0 static int request_exit(duk_context *ctx) {
575 (void) ctx;
576 0 exit_requested = 1;
577 0 return 0;
578 }
579  
580 static duk_function_list_entry eventloop_funcs[] = {
581 { "createTimer", create_timer, 3 },
582 { "deleteTimer", delete_timer, 1 },
583 { "listenFd", listen_fd, 2 },
584 { "requestExit", request_exit, 0 },
585 { NULL, NULL, 0 }
586 };
587  
588 2014 void eventloop_register(duk_context *ctx) {
589 2014 memset((void *) timer_list, 0, MAX_TIMERS * sizeof(ev_timer));
590 2014 memset((void *) &timer_expiring, 0, sizeof(ev_timer));
591 2014 memset((void *) poll_list, 0, MAX_FDS * sizeof(struct pollfd));
592  
593 /* Set global 'EventLoop'. */
594 2014 duk_push_global_object(ctx);
595 2014 duk_push_object(ctx);
596 2014 duk_put_function_list(ctx, -1, eventloop_funcs);
597 2014 duk_put_prop_string(ctx, -2, "EventLoop");
598 2014 duk_pop(ctx);
599  
600 /* Initialize global stash 'eventTimers'. */
601 2014 duk_push_global_stash(ctx);
602 2014 duk_push_object(ctx);
603 2014 duk_put_prop_string(ctx, -2, TIMERS_SLOT_NAME);
604 2014 duk_pop(ctx);
605 2014 }
« blib/lib/JavaScript/Duktape/XS.pm ↑ Summary util.c »