From 716a2fbea02498dc020b287a7a650653c05e109c Mon Sep 17 00:00:00 2001 From: SUZUKI Sosuke Date: Fri, 19 Sep 2025 16:31:25 +0900 Subject: [PATCH] Refactor `functionName` in `ErrorStackTrace.cpp` (#22760) ### What does this PR do? Make `getName` inner lambda always returns function name instead of assign to `functionName` in parent scope ### How did you verify your code works? This is just refactoring --- src/bun.js/bindings/ErrorStackTrace.cpp | 95 +++++++++++-------------- 1 file changed, 43 insertions(+), 52 deletions(-) diff --git a/src/bun.js/bindings/ErrorStackTrace.cpp b/src/bun.js/bindings/ErrorStackTrace.cpp index 75ac0f70e9..b3935e107a 100644 --- a/src/bun.js/bindings/ErrorStackTrace.cpp +++ b/src/bun.js/bindings/ErrorStackTrace.cpp @@ -667,7 +667,6 @@ String functionName(JSC::VM& vm, JSC::JSGlobalObject* lexicalGlobalObject, JSC:: String functionName(JSC::VM& vm, JSC::JSGlobalObject* lexicalGlobalObject, const JSC::StackFrame& frame, bool isInFinalizer, unsigned int* flags) { - WTF::String functionName; bool isConstructor = false; if (isInFinalizer) { @@ -684,73 +683,65 @@ String functionName(JSC::VM& vm, JSC::JSGlobalObject* lexicalGlobalObject, const } }; - const auto getName = [&]() -> String { - // First try the "name" property. - { - unsigned attributes; - PropertyOffset offset = structure->getConcurrently(vm.propertyNames->name.impl(), attributes); - if (offset != invalidOffset && !(attributes & (PropertyAttribute::Accessor | PropertyAttribute::CustomAccessorOrValue))) { - JSValue name = object->getDirect(offset); - if (name && name.isString()) { - auto str = asString(name)->tryGetValueWithoutGC(); - if (!str->isEmpty()) { - setTypeFlagsIfNecessary(); - - return str.data; - } + // First try the "name" property. + { + unsigned attributes; + PropertyOffset offset = structure->getConcurrently(vm.propertyNames->name.impl(), attributes); + if (offset != invalidOffset && !(attributes & (PropertyAttribute::Accessor | PropertyAttribute::CustomAccessorOrValue))) { + JSValue name = object->getDirect(offset); + if (name && name.isString()) { + auto str = asString(name)->tryGetValueWithoutGC(); + if (!str->isEmpty()) { + setTypeFlagsIfNecessary(); + return str; } } } + } - // Then try the "displayName" property. - { - unsigned attributes; - PropertyOffset offset = structure->getConcurrently(vm.propertyNames->displayName.impl(), attributes); - if (offset != invalidOffset && !(attributes & (PropertyAttribute::Accessor | PropertyAttribute::CustomAccessorOrValue))) { - JSValue name = object->getDirect(offset); - if (name && name.isString()) { - auto str = asString(name)->tryGetValueWithoutGC(); - if (!str->isEmpty()) { - functionName = str.data; - if (!functionName.isEmpty()) { - setTypeFlagsIfNecessary(); - return functionName; - } - } + // Then try the "displayName" property. + { + unsigned attributes; + PropertyOffset offset = structure->getConcurrently(vm.propertyNames->displayName.impl(), attributes); + if (offset != invalidOffset && !(attributes & (PropertyAttribute::Accessor | PropertyAttribute::CustomAccessorOrValue))) { + JSValue name = object->getDirect(offset); + if (name && name.isString()) { + auto str = asString(name)->tryGetValueWithoutGC(); + if (!str->isEmpty()) { + setTypeFlagsIfNecessary(); + return str; } } } + } - // Lastly, try type-specific properties. - if (jstype == JSC::JSFunctionType) { - auto* function = jsCast(object); - if (function) { - functionName = function->nameWithoutGC(vm); - if (functionName.isEmpty() && !function->isHostFunction()) { - functionName = function->jsExecutable()->ecmaName().string(); - } + // Lastly, try type-specific properties. + if (jstype == JSC::JSFunctionType) { + auto* function = jsCast(object); + if (function) { + auto str = function->nameWithoutGC(vm); + if (str.isEmpty() && !function->isHostFunction()) { setTypeFlagsIfNecessary(); - return functionName; - } - } else if (jstype == JSC::InternalFunctionType) { - auto* function = jsCast(object); - if (function) { - functionName = function->name(); - setTypeFlagsIfNecessary(); - return functionName; + return function->jsExecutable()->ecmaName().string(); } + setTypeFlagsIfNecessary(); + return str; } - - return functionName; - }; - - functionName = getName(); + } else if (jstype == JSC::InternalFunctionType) { + auto* function = jsCast(object); + if (function) { + auto str = function->name(); + setTypeFlagsIfNecessary(); + return str; + } + } } } - return functionName; + return emptyString(); } + WTF::String functionName; if (frame.hasLineAndColumnInfo()) { auto* codeblock = frame.codeBlock(); if (codeblock->isConstructor()) {