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: https://github.com/quickjs-ng/quickjs/issues/273
This commit is contained in:
Ben Noordhuis 2024-02-19 16:31:17 +01:00 committed by GitHub
parent 56d60020f4
commit b257545b6f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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) {