From 92629e916c7634b6d868839176bc49c54859256c Mon Sep 17 00:00:00 2001 From: Geoff Gustafson Date: Wed, 1 Mar 2017 17:40:47 -0800 Subject: [timers] Ensure coordinated timers execute in the order created This changes the output of samples/Timers.js to match what happens in Chrome and appears to be required by specification. We were previously inserting the timers in reverse and this was visible to the users if they created multiple timers with the same timeout. Also found we weren't checking malloc for success when allocating the timer callback argument storage, fixed that. Signed-off-by: Geoff Gustafson --- src/zjs_timers.c | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/zjs_timers.c b/src/zjs_timers.c index 34a63ec..31d0257 100644 --- a/src/zjs_timers.c +++ b/src/zjs_timers.c @@ -53,10 +53,7 @@ static zjs_timer_t *add_timer(uint32_t interval, uint32_t argc, const jerry_value_t argv[]) { - int i; - zjs_timer_t *tm; - - tm = zjs_malloc(sizeof(zjs_timer_t)); + zjs_timer_t *tm = zjs_malloc(sizeof(zjs_timer_t)); if (!tm) { ERR_PRINT("out of memory allocating timer struct\n"); return NULL; @@ -66,25 +63,35 @@ static zjs_timer_t *add_timer(uint32_t interval, tm->interval = interval; tm->repeat = repeat; tm->completed = false; - tm->next = zjs_timers; - if (tm->repeat) { - tm->callback_id = zjs_add_callback(callback, this, tm, NULL); - } else { - tm->callback_id = zjs_add_callback_once(callback, this, tm, NULL); - } + tm->next = NULL; tm->argc = argc; if (tm->argc) { tm->argv = zjs_malloc(sizeof(jerry_value_t) * argc); - for (i = 0; i < argc; ++i) { + if (!tm->argv) { + zjs_free(tm); + ERR_PRINT("out of memory allocating timer args\n"); + return NULL; + } + for (int i = 0; i < argc; ++i) { tm->argv[i] = jerry_acquire_value(argv[i + 2]); } } else { tm->argv = NULL; } - zjs_timers = tm; + if (tm->repeat) { + tm->callback_id = zjs_add_callback(callback, this, tm, NULL); + } else { + tm->callback_id = zjs_add_callback_once(callback, this, tm, NULL); + } + + zjs_timer_t **pnext = &zjs_timers; + while (*pnext != NULL) { + pnext = &(*pnext)->next; + } + *pnext = tm; - DBG_PRINT("adding timer. id=%d, interval=%lu, repeat=%u, argv=%p, argc=%lu\n", + DBG_PRINT("add timer, id=%d, interval=%lu, repeat=%u, argv=%p, argc=%lu\n", tm->callback_id, interval, repeat, argv, argc); zjs_port_timer_start(&tm->timer, interval); return tm; -- cgit v1.2.3