1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-01 07:18:13 +00:00

LibJS: Ensure we only call toString on computed properties once

This commit is contained in:
Anonymous 2022-02-17 15:09:39 -08:00 committed by Linus Groh
parent c45922c637
commit a97b5393d0
2 changed files with 154 additions and 10 deletions

View file

@ -81,13 +81,11 @@ static void update_function_name(Value value, FlyString const& name)
static_cast<ECMAScriptFunctionObject&>(function).set_name(name);
}
static ThrowCompletionOr<String> get_function_name(GlobalObject& global_object, Value value)
static ThrowCompletionOr<String> get_function_property_name(PropertyKey key)
{
if (value.is_symbol())
return String::formatted("[{}]", value.as_symbol().description());
if (value.is_string())
return value.as_string().string();
return value.to_string(global_object);
if (key.is_symbol())
return String::formatted("[{}]", key.as_symbol()->description());
return key.to_string();
}
// 14.2.2 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-block-runtime-semantics-evaluation
@ -2999,7 +2997,8 @@ Completion ObjectExpression::execute(Interpreter& interpreter, GlobalObject& glo
if (value.is_function() && property.is_method())
static_cast<ECMAScriptFunctionObject&>(value.as_function()).set_home_object(object);
auto name = TRY(get_function_name(global_object, key));
auto property_key = TRY(PropertyKey::from_value(global_object, key));
auto name = TRY(get_function_property_name(property_key));
if (property.type() == ObjectProperty::Type::Getter) {
name = String::formatted("get {}", name);
} else if (property.type() == ObjectProperty::Type::Setter) {
@ -3011,14 +3010,14 @@ Completion ObjectExpression::execute(Interpreter& interpreter, GlobalObject& glo
switch (property.type()) {
case ObjectProperty::Type::Getter:
VERIFY(value.is_function());
object->define_direct_accessor(TRY(PropertyKey::from_value(global_object, key)), &value.as_function(), nullptr, Attribute::Configurable | Attribute::Enumerable);
object->define_direct_accessor(property_key, &value.as_function(), nullptr, Attribute::Configurable | Attribute::Enumerable);
break;
case ObjectProperty::Type::Setter:
VERIFY(value.is_function());
object->define_direct_accessor(TRY(PropertyKey::from_value(global_object, key)), nullptr, &value.as_function(), Attribute::Configurable | Attribute::Enumerable);
object->define_direct_accessor(property_key, nullptr, &value.as_function(), Attribute::Configurable | Attribute::Enumerable);
break;
case ObjectProperty::Type::KeyValue:
object->define_direct_property(TRY(PropertyKey::from_value(global_object, key)), value, JS::default_attributes);
object->define_direct_property(property_key, value, JS::default_attributes);
break;
case ObjectProperty::Type::Spread:
default: