From e51f519f9969e939e4348f492c032c4a1834df96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Mon, 18 Dec 2023 09:44:39 +0100 Subject: [PATCH] core: workaround libuv change in behavior In https://github.com/libuv/libuv/issues/3686 libuv changed when timers run respective to check handles. Change the way we run the loop by running it again if necessary, untill there is no more work to do of the loop has been stopped. Fixes: https://github.com/saghul/txiki.js/issues/443 --- src/private.h | 2 -- src/timers.c | 4 ---- src/vm.c | 12 +++++++----- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/private.h b/src/private.h index 5b744ca5..b89bb224 100644 --- a/src/private.h +++ b/src/private.h @@ -84,8 +84,6 @@ JSValue tjs_throw_errno(JSContext *ctx, int err); JSValue tjs_new_pipe(JSContext *ctx); uv_stream_t *tjs_pipe_get_stream(JSContext *ctx, JSValueConst obj); -void tjs_execute_jobs(JSContext *ctx); - JSModuleDef *tjs__load_builtin(JSContext *ctx, const char *name); int tjs__load_file(JSContext *ctx, DynBuf *dbuf, const char *filename); JSModuleDef *tjs_module_loader(JSContext *ctx, const char *module_name, void *opaque); diff --git a/src/timers.c b/src/timers.c index d7467857..0bc84a73 100644 --- a/src/timers.c +++ b/src/timers.c @@ -70,10 +70,6 @@ static void uv__timer_cb(uv_timer_t *handle) { TJSTimer *th = handle->data; CHECK_NOT_NULL(th); - /* Timer always executes before check phase in libuv, - so clear the microtask queue here before running setTimeout macrotasks */ - tjs_execute_jobs(th->ctx); - call_timer(th); if (!th->interval) clear_timer(th); diff --git a/src/vm.c b/src/vm.c index 461f3e8e..7336aa8b 100644 --- a/src/vm.c +++ b/src/vm.c @@ -303,7 +303,7 @@ static void uv__prepare_cb(uv_prepare_t *handle) { uv__maybe_idle(qrt); } -void tjs_execute_jobs(JSContext *ctx) { +static void tjs__execute_jobs(JSContext *ctx) { JSContext *ctx1; int err; @@ -322,7 +322,7 @@ static void uv__check_cb(uv_check_t *handle) { TJSRuntime *qrt = handle->data; CHECK_NOT_NULL(qrt); - tjs_execute_jobs(qrt->ctx); + tjs__execute_jobs(qrt->ctx); uv__maybe_idle(qrt); } @@ -347,9 +347,11 @@ int TJS_Run(TJSRuntime *qrt) { if (ret != 0) return ret; - uv__maybe_idle(qrt); - - uv_run(&qrt->loop, UV_RUN_DEFAULT); + int r; + do { + uv__maybe_idle(qrt); + r = uv_run(&qrt->loop, UV_RUN_DEFAULT); + } while (r == 0 && JS_IsJobPending(qrt->rt)); JSValue exc = JS_GetException(qrt->ctx); if (!JS_IsNull(exc)) {