From: Fabrice Bellard Date: Tue, 24 Nov 2026 18:13:04 +0000 (+0100) Subject: Improve computed property lookup error messages X-Git-Url: https://git.kaiwu.me/http/%22../$%7BGITURL%7D/static/gitweb.js?a=commitdiff_plain;h=9984f4fba6b5e4529510fe839459a1a9746b7e46;p=quickjs.git Improve computed property lookup error messages --- diff --git a/quickjs.c b/quickjs.c index 60e6c21..67c3bd4 100644 --- a/quickjs.c +++ b/quickjs.c @@ -9343,8 +9343,23 @@ static JSValue JS_GetPropertyValue(JSContext *ctx, JSValueConst this_obj, slow_path: /* ToObject() must be done before ToPropertyKey() */ if (JS_IsNull(this_obj) || JS_IsUndefined(this_obj)) { - JS_FreeValue(ctx, prop); - return JS_ThrowTypeError(ctx, "cannot read property of %s", JS_IsNull(this_obj) ? "null" : "undefined"); + /* avoid side effects, so only print the property name for common cases */ + if (JS_IsNumber(prop) || JS_IsString(prop)) { + atom = JS_ValueToAtom(ctx, prop); + JS_FreeValue(ctx, prop); + if (unlikely(atom == JS_ATOM_NULL)) + return JS_EXCEPTION; + if (JS_IsNull(this_obj)) { + JS_ThrowTypeErrorAtom(ctx, "cannot read property '%s' of null", atom); + } else { + JS_ThrowTypeErrorAtom(ctx, "cannot read property '%s' of undefined", atom); + } + JS_FreeAtom(ctx, atom); + return JS_EXCEPTION; + } else { + JS_FreeValue(ctx, prop); + return JS_ThrowTypeError(ctx, "cannot read property of %s", JS_IsNull(this_obj) ? "null" : "undefined"); + } } atom = JS_ValueToAtom(ctx, prop); JS_FreeValue(ctx, prop);