From b62da273273691e40ae11e27a891712e026f4d5b Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Sun, 4 Oct 2026 20:09:52 +0200 Subject: [PATCH] Pass JSContext to JSInterruptHandler Pass JSContext instead of JSRuntime to the callback, to allow better targeted interruption of JS code. Fixes: https://github.com/quickjs-ng/quickjs/issues/1789 --- api-test.c | 2 +- quickjs-libc.c | 4 ++-- quickjs.c | 4 ++-- quickjs.h | 2 +- run-test262.c | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/api-test.c b/api-test.c index 2d5f304e2..371df4cbd 100644 --- a/api-test.c +++ b/api-test.c @@ -173,7 +173,7 @@ static void cfunctions(void) #define MAX_TIME 10 -static int timeout_interrupt_handler(JSRuntime *rt, void *opaque) +static int timeout_interrupt_handler(JSContext *ctx, void *opaque) { int *time = (int *)opaque; return (*time)++ > MAX_TIME; diff --git a/quickjs-libc.c b/quickjs-libc.c index 161401d23..afdff5004 100644 --- a/quickjs-libc.c +++ b/quickjs-libc.c @@ -1085,14 +1085,14 @@ static JSValue js_std_gc(JSContext *ctx, JSValueConst this_val, return JS_UNDEFINED; } -static int interrupt_handler(JSRuntime *rt, void *opaque) +static int interrupt_handler(JSContext *ctx, void *opaque) { JSThreadState *ts = opaque; if (1 & (os_pending_signals >> SIGINT)) return 1; if (ts->prev_interrupt_handler) - return ts->prev_interrupt_handler(rt, ts->prev_interrupt_opaque); + return ts->prev_interrupt_handler(ctx, ts->prev_interrupt_opaque); return 0; } diff --git a/quickjs.c b/quickjs.c index ed65ce420..d86bce9cf 100644 --- a/quickjs.c +++ b/quickjs.c @@ -8656,7 +8656,7 @@ static no_inline __exception int __js_poll_interrupts(JSContext *ctx) JSRuntime *rt = ctx->rt; ctx->interrupt_counter = JS_INTERRUPT_COUNTER_INIT; if (rt->interrupt_handler) { - if (rt->interrupt_handler(rt, rt->interrupt_opaque)) { + if (rt->interrupt_handler(ctx, rt->interrupt_opaque)) { JS_ThrowInterrupted(ctx); return -1; } @@ -49661,7 +49661,7 @@ int lre_check_timeout(void *opaque) JSContext *ctx = opaque; JSRuntime *rt = ctx->rt; return (rt->interrupt_handler && - rt->interrupt_handler(rt, rt->interrupt_opaque)); + rt->interrupt_handler(ctx, rt->interrupt_opaque)); } void *lre_realloc(void *opaque, void *ptr, size_t size) diff --git a/quickjs.h b/quickjs.h index 7db40213c..c9a0a6c14 100644 --- a/quickjs.h +++ b/quickjs.h @@ -1187,7 +1187,7 @@ typedef void JSHostPromiseRejectionTracker(JSContext *ctx, JSValueConst promise, JS_EXTERN void JS_SetHostPromiseRejectionTracker(JSRuntime *rt, JSHostPromiseRejectionTracker *cb, void *opaque); /* return != 0 if the JS code needs to be interrupted */ -typedef int JSInterruptHandler(JSRuntime *rt, void *opaque); +typedef int JSInterruptHandler(JSContext *ctx, void *opaque); JS_EXTERN void JS_SetInterruptHandler(JSRuntime *rt, JSInterruptHandler *cb, void *opaque); /* if can_block is true, Atomics.wait() can be used */ JS_EXTERN void JS_SetCanBlock(JSRuntime *rt, bool can_block); diff --git a/run-test262.c b/run-test262.c index 482085cd9..4f0627ad5 100644 --- a/run-test262.c +++ b/run-test262.c @@ -1728,7 +1728,7 @@ JSContext *JS_NewCustomContext(JSRuntime *rt) return ctx; } -static int interrupt_handler(JSRuntime *rt, void *opaque) +static int interrupt_handler(JSContext *ctx, void *opaque) { int *interrupt_countdown = opaque; return !--*interrupt_countdown;