summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2023-02-11 16:02:14 +0000
committerLinus Groh <mail@linusgroh.de>2023-02-11 21:47:57 +0000
commit5e72fde954f4b677a71a9b369905dcd0cd7c36aa (patch)
tree33993c077e7e8bf33cd2f043b1a3a8315267d724
parentfcdabd179acdbd3eb9e0489039f530b90570b783 (diff)
downloadserenity-5e72fde954f4b677a71a9b369905dcd0cd7c36aa.zip
LibJS: Unify Symbol::description() and raw_description()
Let callers take care of handling the empty optional case (undefined in the spec).
-rw-r--r--Userland/Libraries/LibJS/AST.cpp8
-rw-r--r--Userland/Libraries/LibJS/Runtime/FunctionObject.cpp2
-rw-r--r--Userland/Libraries/LibJS/Runtime/Symbol.h3
-rw-r--r--Userland/Libraries/LibJS/Runtime/SymbolConstructor.cpp6
-rw-r--r--Userland/Libraries/LibJS/Runtime/SymbolPrototype.cpp2
5 files changed, 11 insertions, 10 deletions
diff --git a/Userland/Libraries/LibJS/AST.cpp b/Userland/Libraries/LibJS/AST.cpp
index 73e3cc4d12..27dedae69a 100644
--- a/Userland/Libraries/LibJS/AST.cpp
+++ b/Userland/Libraries/LibJS/AST.cpp
@@ -1,6 +1,6 @@
/*
* 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>
*
* 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)
{
if (key.is_symbol())
- return DeprecatedString::formatted("[{}]", key.as_symbol()->description());
+ return DeprecatedString::formatted("[{}]", key.as_symbol()->description().value_or(""));
return key.to_string();
}
@@ -1686,9 +1686,9 @@ ThrowCompletionOr<ClassElement::ClassValue> ClassMethod::class_element_evaluatio
[&](PropertyKey const& property_key) -> DeprecatedString {
if (property_key.is_symbol()) {
auto description = property_key.as_symbol()->description();
- if (description.is_empty())
+ if (!description.has_value() || description->is_empty())
return "";
- return DeprecatedString::formatted("[{}]", description);
+ return DeprecatedString::formatted("[{}]", *description);
} else {
return property_key.to_string();
}
diff --git a/Userland/Libraries/LibJS/Runtime/FunctionObject.cpp b/Userland/Libraries/LibJS/Runtime/FunctionObject.cpp
index 03a4f6527f..41c038c99d 100644
--- a/Userland/Libraries/LibJS/Runtime/FunctionObject.cpp
+++ b/Userland/Libraries/LibJS/Runtime/FunctionObject.cpp
@@ -37,7 +37,7 @@ void FunctionObject::set_function_name(Variant<PropertyKey, PrivateName> const&
// 2. If Type(name) is Symbol, then
if (auto const* property_key = name_arg.get_pointer<PropertyKey>(); property_key && property_key->is_symbol()) {
// 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.
if (!description.has_value())
diff --git a/Userland/Libraries/LibJS/Runtime/Symbol.h b/Userland/Libraries/LibJS/Runtime/Symbol.h
index 2b030c1ff4..01448ec27d 100644
--- a/Userland/Libraries/LibJS/Runtime/Symbol.h
+++ b/Userland/Libraries/LibJS/Runtime/Symbol.h
@@ -21,8 +21,7 @@ public:
virtual ~Symbol() = default;
- DeprecatedString description() const { return m_description.value_or(""); }
- Optional<DeprecatedString> const& raw_description() const { return m_description; }
+ Optional<DeprecatedString> const& description() const { return m_description; }
bool is_global() const { return m_is_global; }
DeprecatedString descriptive_string() const;
diff --git a/Userland/Libraries/LibJS/Runtime/SymbolConstructor.cpp b/Userland/Libraries/LibJS/Runtime/SymbolConstructor.cpp
index 35f5aa97e1..307fa03c39 100644
--- a/Userland/Libraries/LibJS/Runtime/SymbolConstructor.cpp
+++ b/Userland/Libraries/LibJS/Runtime/SymbolConstructor.cpp
@@ -87,8 +87,10 @@ JS_DEFINE_NATIVE_FUNCTION(SymbolConstructor::key_for)
return vm.throw_completion<TypeError>(ErrorType::NotASymbol, argument.to_string_without_side_effects());
auto& symbol = argument.as_symbol();
- if (symbol.is_global())
- return PrimitiveString::create(vm, symbol.description());
+ if (symbol.is_global()) {
+ // NOTE: Global symbols should always have a description string
+ return PrimitiveString::create(vm, *symbol.description());
+ }
return js_undefined();
}
diff --git a/Userland/Libraries/LibJS/Runtime/SymbolPrototype.cpp b/Userland/Libraries/LibJS/Runtime/SymbolPrototype.cpp
index 87f8d5cc8f..b6c6c1d920 100644
--- a/Userland/Libraries/LibJS/Runtime/SymbolPrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/SymbolPrototype.cpp
@@ -53,7 +53,7 @@ static ThrowCompletionOr<Symbol*> this_symbol_value(VM& vm, Value value)
JS_DEFINE_NATIVE_FUNCTION(SymbolPrototype::description_getter)
{
auto* symbol = TRY(this_symbol_value(vm, vm.this_value()));
- auto& description = symbol->raw_description();
+ auto& description = symbol->description();
if (!description.has_value())
return js_undefined();
return PrimitiveString::create(vm, *description);