Skip to content

Commit

Permalink
Better output from JS_ToCString() on exception (bellard#274)
Browse files Browse the repository at this point in the history
`ToString(object)` can fail when there is a pending exception. Add a
special case for exception objects to help debugging. Getting an empty
string when the real error was "InternalError: stack overflow" is rage
inducing.

Fixes: quickjs-ng/quickjs#273
  • Loading branch information
bnoordhuis authored Feb 19, 2024
1 parent 56d6002 commit b257545
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -3938,16 +3938,36 @@ const char *JS_ToCStringLen2(JSContext *ctx, size_t *plen, JSValue val1, BOOL ce
JSValue val;
JSString *str, *str_new;
int pos, len, c, c1;
JSObject *p;
uint8_t *q;

if (JS_VALUE_GET_TAG(val1) != JS_TAG_STRING) {
val = JS_ToString(ctx, val1);
if (JS_IsException(val))
goto fail;
} else {
if (JS_VALUE_GET_TAG(val1) == JS_TAG_STRING) {
val = js_dup(val1);
goto go;
}

val = JS_ToString(ctx, val1);
if (!JS_IsException(val))
goto go;

// Stringification can fail when there is an exception pending,
// e.g. a stack overflow InternalError. Special-case exception
// objects to make debugging easier, look up the .message property
// and stringify that.
if (JS_VALUE_GET_TAG(val1) != JS_TAG_OBJECT)
goto fail;

p = JS_VALUE_GET_OBJ(val1);
if (p->class_id != JS_CLASS_ERROR)
goto fail;

val = JS_GetProperty(ctx, val1, JS_ATOM_message);
if (JS_VALUE_GET_TAG(val) != JS_TAG_STRING) {
JS_FreeValue(ctx, val);
goto fail;
}

go:
str = JS_VALUE_GET_STRING(val);
len = str->len;
if (!str->is_wide_char) {
Expand Down

0 comments on commit b257545

Please sign in to comment.