Skip to content

Commit

Permalink
Rework JS_SetUncatchableError (#827)
Browse files Browse the repository at this point in the history
Remove the (cryptic!) `flag` argument and add JS_ClearUncatchableError.

Fixes: #826
  • Loading branch information
bnoordhuis authored Jan 14, 2025
1 parent d02a4ba commit 8bcc070
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
17 changes: 14 additions & 3 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1320,6 +1320,7 @@ static JSValue js_module_ns_autoinit(JSContext *ctx, JSObject *p, JSAtom atom,
void *opaque);
static JSValue JS_InstantiateFunctionListItem2(JSContext *ctx, JSObject *p,
JSAtom atom, void *opaque);
static void js_set_uncatchable_error(JSContext *ctx, JSValue val, BOOL flag);

static JSValue js_new_callsite(JSContext *ctx, JSCallSiteData *csd);
static void js_new_callsite_data(JSContext *ctx, JSCallSiteData *csd, JSStackFrame *sf);
Expand Down Expand Up @@ -7065,7 +7066,7 @@ static no_inline __exception int __js_poll_interrupts(JSContext *ctx)
if (rt->interrupt_handler(rt, rt->interrupt_opaque)) {
/* XXX: should set a specific flag to avoid catching */
JS_ThrowInternalError(ctx, "interrupted");
JS_SetUncatchableError(ctx, ctx->rt->current_exception, TRUE);
js_set_uncatchable_error(ctx, ctx->rt->current_exception, TRUE);
return -1;
}
}
Expand Down Expand Up @@ -10134,7 +10135,7 @@ BOOL JS_IsUncatchableError(JSContext *ctx, JSValue val)
return p->class_id == JS_CLASS_ERROR && p->is_uncatchable_error;
}

void JS_SetUncatchableError(JSContext *ctx, JSValue val, BOOL flag)
static void js_set_uncatchable_error(JSContext *ctx, JSValue val, BOOL flag)
{
JSObject *p;
if (JS_VALUE_GET_TAG(val) != JS_TAG_OBJECT)
Expand All @@ -10144,9 +10145,19 @@ void JS_SetUncatchableError(JSContext *ctx, JSValue val, BOOL flag)
p->is_uncatchable_error = flag;
}

void JS_SetUncatchableError(JSContext *ctx, JSValue val)
{
js_set_uncatchable_error(ctx, val, TRUE);
}

void JS_ClearUncatchableError(JSContext *ctx, JSValue val)
{
js_set_uncatchable_error(ctx, val, FALSE);
}

void JS_ResetUncatchableError(JSContext *ctx)
{
JS_SetUncatchableError(ctx, ctx->rt->current_exception, FALSE);
js_set_uncatchable_error(ctx, ctx->rt->current_exception, FALSE);
}

int JS_SetOpaque(JSValue obj, void *opaque)
Expand Down
7 changes: 6 additions & 1 deletion quickjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -616,8 +616,13 @@ JS_EXTERN JSValue JS_Throw(JSContext *ctx, JSValue obj);
JS_EXTERN JSValue JS_GetException(JSContext *ctx);
JS_EXTERN JS_BOOL JS_HasException(JSContext *ctx);
JS_EXTERN JS_BOOL JS_IsError(JSContext *ctx, JSValue val);
JS_EXTERN void JS_SetUncatchableError(JSContext *ctx, JSValue val, JS_BOOL flag);
JS_EXTERN JS_BOOL JS_IsUncatchableError(JSContext* ctx, JSValue val);
JS_EXTERN void JS_SetUncatchableError(JSContext *ctx, JSValue val);
JS_EXTERN void JS_ClearUncatchableError(JSContext *ctx, JSValue val);
// Shorthand for:
// JSValue exc = JS_GetException(ctx);
// JS_ClearUncatchableError(ctx, exc);
// JS_Throw(ctx, exc);
JS_EXTERN void JS_ResetUncatchableError(JSContext *ctx);
JS_EXTERN JSValue JS_NewError(JSContext *ctx);
JS_EXTERN JSValue __js_printf_like(2, 3) JS_ThrowPlainError(JSContext *ctx, const char *fmt, ...);
Expand Down

0 comments on commit 8bcc070

Please sign in to comment.