Skip to content

Commit b257545

Browse files
authored
Better output from JS_ToCString() on exception (#274)
`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: #273
1 parent 56d6002 commit b257545

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

quickjs.c

+25-5
Original file line numberDiff line numberDiff line change
@@ -3938,16 +3938,36 @@ const char *JS_ToCStringLen2(JSContext *ctx, size_t *plen, JSValue val1, BOOL ce
39383938
JSValue val;
39393939
JSString *str, *str_new;
39403940
int pos, len, c, c1;
3941+
JSObject *p;
39413942
uint8_t *q;
39423943

3943-
if (JS_VALUE_GET_TAG(val1) != JS_TAG_STRING) {
3944-
val = JS_ToString(ctx, val1);
3945-
if (JS_IsException(val))
3946-
goto fail;
3947-
} else {
3944+
if (JS_VALUE_GET_TAG(val1) == JS_TAG_STRING) {
39483945
val = js_dup(val1);
3946+
goto go;
3947+
}
3948+
3949+
val = JS_ToString(ctx, val1);
3950+
if (!JS_IsException(val))
3951+
goto go;
3952+
3953+
// Stringification can fail when there is an exception pending,
3954+
// e.g. a stack overflow InternalError. Special-case exception
3955+
// objects to make debugging easier, look up the .message property
3956+
// and stringify that.
3957+
if (JS_VALUE_GET_TAG(val1) != JS_TAG_OBJECT)
3958+
goto fail;
3959+
3960+
p = JS_VALUE_GET_OBJ(val1);
3961+
if (p->class_id != JS_CLASS_ERROR)
3962+
goto fail;
3963+
3964+
val = JS_GetProperty(ctx, val1, JS_ATOM_message);
3965+
if (JS_VALUE_GET_TAG(val) != JS_TAG_STRING) {
3966+
JS_FreeValue(ctx, val);
3967+
goto fail;
39493968
}
39503969

3970+
go:
39513971
str = JS_VALUE_GET_STRING(val);
39523972
len = str->len;
39533973
if (!str->is_wide_char) {

0 commit comments

Comments
 (0)