One less alloc for setTimeout.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4188 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2023-02-19 01:28:14 +00:00
parent a9551b057b
commit 1548a8a852

View File

@ -194,13 +194,15 @@ bool tf_util_report_error(JSContext* context, JSValue value)
} }
typedef struct _timeout_t { typedef struct _timeout_t {
uv_timer_t _timer;
tf_task_t* _task; tf_task_t* _task;
JSValue _callback; JSValue _callback;
} timeout_t; } timeout_t;
static void _handle_closed(uv_handle_t* handle) static void _handle_closed(uv_handle_t* handle)
{ {
tf_free(handle); timeout_t* timeout = handle->data;
tf_free(timeout);
} }
static void _util_timeoutCallback(uv_timer_t* handle) static void _util_timeoutCallback(uv_timer_t* handle)
@ -218,7 +220,6 @@ static void _util_timeoutCallback(uv_timer_t* handle)
JS_FreeValue(context, result); JS_FreeValue(context, result);
JS_FreeValue(context, timeout->_callback); JS_FreeValue(context, timeout->_callback);
tf_trace_end(tf_task_get_trace(timeout->_task)); tf_trace_end(tf_task_get_trace(timeout->_task));
tf_free(timeout);
uv_close((uv_handle_t*)handle, _handle_closed); uv_close((uv_handle_t*)handle, _handle_closed);
} }
@ -231,16 +232,17 @@ static JSValue _util_setTimeout(JSContext* context, JSValueConst this_val, int a
{ {
._task = task, ._task = task,
._callback = JS_DupValue(context, argv[0]), ._callback = JS_DupValue(context, argv[0]),
._timer = { .data = timeout },
}; };
uv_timer_t* timer = tf_malloc(sizeof(uv_timer_t)); uv_timer_init(tf_task_get_loop(task), &timeout->_timer);
memset(timer, 0, sizeof(uv_timer_t));
uv_timer_init(tf_task_get_loop(task), timer);
timer->data = timeout;
int64_t duration; int64_t duration;
JS_ToInt64(context, &duration, argv[1]); JS_ToInt64(context, &duration, argv[1]);
uv_timer_start(timer, _util_timeoutCallback, duration, 0); if (uv_timer_start(&timeout->_timer, _util_timeoutCallback, duration, 0) != 0)
{
tf_free(timeout);
}
return JS_NULL; return JS_NULL;
} }