mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 10:17:35 +00:00
LibJS: Unify Symbol::description() and raw_description()
Let callers take care of handling the empty optional case (undefined in the spec).
This commit is contained in:
parent
fcdabd179a
commit
5e72fde954
5 changed files with 11 additions and 10 deletions
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
|
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
|
||||||
* Copyright (c) 2020-2022, Linus Groh <linusg@serenityos.org>
|
* Copyright (c) 2020-2023, Linus Groh <linusg@serenityos.org>
|
||||||
* Copyright (c) 2021-2022, David Tuin <davidot@serenityos.org>
|
* Copyright (c) 2021-2022, David Tuin <davidot@serenityos.org>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
@ -97,7 +97,7 @@ static void update_function_name(Value value, DeprecatedFlyString const& name)
|
||||||
static ThrowCompletionOr<DeprecatedString> get_function_property_name(PropertyKey key)
|
static ThrowCompletionOr<DeprecatedString> get_function_property_name(PropertyKey key)
|
||||||
{
|
{
|
||||||
if (key.is_symbol())
|
if (key.is_symbol())
|
||||||
return DeprecatedString::formatted("[{}]", key.as_symbol()->description());
|
return DeprecatedString::formatted("[{}]", key.as_symbol()->description().value_or(""));
|
||||||
return key.to_string();
|
return key.to_string();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1686,9 +1686,9 @@ ThrowCompletionOr<ClassElement::ClassValue> ClassMethod::class_element_evaluatio
|
||||||
[&](PropertyKey const& property_key) -> DeprecatedString {
|
[&](PropertyKey const& property_key) -> DeprecatedString {
|
||||||
if (property_key.is_symbol()) {
|
if (property_key.is_symbol()) {
|
||||||
auto description = property_key.as_symbol()->description();
|
auto description = property_key.as_symbol()->description();
|
||||||
if (description.is_empty())
|
if (!description.has_value() || description->is_empty())
|
||||||
return "";
|
return "";
|
||||||
return DeprecatedString::formatted("[{}]", description);
|
return DeprecatedString::formatted("[{}]", *description);
|
||||||
} else {
|
} else {
|
||||||
return property_key.to_string();
|
return property_key.to_string();
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,7 +37,7 @@ void FunctionObject::set_function_name(Variant<PropertyKey, PrivateName> const&
|
||||||
// 2. If Type(name) is Symbol, then
|
// 2. If Type(name) is Symbol, then
|
||||||
if (auto const* property_key = name_arg.get_pointer<PropertyKey>(); property_key && property_key->is_symbol()) {
|
if (auto const* property_key = name_arg.get_pointer<PropertyKey>(); property_key && property_key->is_symbol()) {
|
||||||
// a. Let description be name's [[Description]] value.
|
// a. Let description be name's [[Description]] value.
|
||||||
auto const& description = property_key->as_symbol()->raw_description();
|
auto const& description = property_key->as_symbol()->description();
|
||||||
|
|
||||||
// b. If description is undefined, set name to the empty String.
|
// b. If description is undefined, set name to the empty String.
|
||||||
if (!description.has_value())
|
if (!description.has_value())
|
||||||
|
|
|
@ -21,8 +21,7 @@ public:
|
||||||
|
|
||||||
virtual ~Symbol() = default;
|
virtual ~Symbol() = default;
|
||||||
|
|
||||||
DeprecatedString description() const { return m_description.value_or(""); }
|
Optional<DeprecatedString> const& description() const { return m_description; }
|
||||||
Optional<DeprecatedString> const& raw_description() const { return m_description; }
|
|
||||||
bool is_global() const { return m_is_global; }
|
bool is_global() const { return m_is_global; }
|
||||||
|
|
||||||
DeprecatedString descriptive_string() const;
|
DeprecatedString descriptive_string() const;
|
||||||
|
|
|
@ -87,8 +87,10 @@ JS_DEFINE_NATIVE_FUNCTION(SymbolConstructor::key_for)
|
||||||
return vm.throw_completion<TypeError>(ErrorType::NotASymbol, argument.to_string_without_side_effects());
|
return vm.throw_completion<TypeError>(ErrorType::NotASymbol, argument.to_string_without_side_effects());
|
||||||
|
|
||||||
auto& symbol = argument.as_symbol();
|
auto& symbol = argument.as_symbol();
|
||||||
if (symbol.is_global())
|
if (symbol.is_global()) {
|
||||||
return PrimitiveString::create(vm, symbol.description());
|
// NOTE: Global symbols should always have a description string
|
||||||
|
return PrimitiveString::create(vm, *symbol.description());
|
||||||
|
}
|
||||||
|
|
||||||
return js_undefined();
|
return js_undefined();
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,7 +53,7 @@ static ThrowCompletionOr<Symbol*> this_symbol_value(VM& vm, Value value)
|
||||||
JS_DEFINE_NATIVE_FUNCTION(SymbolPrototype::description_getter)
|
JS_DEFINE_NATIVE_FUNCTION(SymbolPrototype::description_getter)
|
||||||
{
|
{
|
||||||
auto* symbol = TRY(this_symbol_value(vm, vm.this_value()));
|
auto* symbol = TRY(this_symbol_value(vm, vm.this_value()));
|
||||||
auto& description = symbol->raw_description();
|
auto& description = symbol->description();
|
||||||
if (!description.has_value())
|
if (!description.has_value())
|
||||||
return js_undefined();
|
return js_undefined();
|
||||||
return PrimitiveString::create(vm, *description);
|
return PrimitiveString::create(vm, *description);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue