From 89700a210195a4cdffb7e02f82896f5ebc2e313f Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sat, 11 Feb 2023 15:51:44 +0000 Subject: [PATCH] LibJS: Rename Symbol::to_deprecated_string() to descriptive_string() This implements the spec's SymbolDescriptiveString AO and should be named accordingly. --- Userland/Libraries/LibJS/Runtime/Reference.cpp | 2 +- .../Libraries/LibJS/Runtime/StringConstructor.cpp | 2 +- Userland/Libraries/LibJS/Runtime/StringOrSymbol.h | 2 +- Userland/Libraries/LibJS/Runtime/Symbol.cpp | 14 +++++++++++++- Userland/Libraries/LibJS/Runtime/Symbol.h | 5 +++-- .../Libraries/LibJS/Runtime/SymbolPrototype.cpp | 4 ++-- Userland/Libraries/LibJS/Runtime/Value.cpp | 2 +- 7 files changed, 22 insertions(+), 9 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/Reference.cpp b/Userland/Libraries/LibJS/Runtime/Reference.cpp index 4d21d76705..549d811c8e 100644 --- a/Userland/Libraries/LibJS/Runtime/Reference.cpp +++ b/Userland/Libraries/LibJS/Runtime/Reference.cpp @@ -216,7 +216,7 @@ DeprecatedString Reference::to_deprecated_string() const if (!m_name.is_valid()) builder.append(""sv); else if (m_name.is_symbol()) - builder.appendff("{}", m_name.as_symbol()->to_deprecated_string()); + builder.appendff("{}", m_name.as_symbol()->descriptive_string()); else builder.appendff("{}", m_name.to_string()); builder.appendff(", Strict={}", m_strict); diff --git a/Userland/Libraries/LibJS/Runtime/StringConstructor.cpp b/Userland/Libraries/LibJS/Runtime/StringConstructor.cpp index 87bc647a9a..0715342f96 100644 --- a/Userland/Libraries/LibJS/Runtime/StringConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/StringConstructor.cpp @@ -47,7 +47,7 @@ ThrowCompletionOr StringConstructor::call() if (!vm.argument_count()) return PrimitiveString::create(vm, String {}); if (vm.argument(0).is_symbol()) - return PrimitiveString::create(vm, vm.argument(0).as_symbol().to_deprecated_string()); + return PrimitiveString::create(vm, vm.argument(0).as_symbol().descriptive_string()); return TRY(vm.argument(0).to_primitive_string(vm)); } diff --git a/Userland/Libraries/LibJS/Runtime/StringOrSymbol.h b/Userland/Libraries/LibJS/Runtime/StringOrSymbol.h index c58076d081..2941e611f0 100644 --- a/Userland/Libraries/LibJS/Runtime/StringOrSymbol.h +++ b/Userland/Libraries/LibJS/Runtime/StringOrSymbol.h @@ -79,7 +79,7 @@ public: if (is_string()) return as_string(); if (is_symbol()) - return as_symbol()->to_deprecated_string(); + return as_symbol()->descriptive_string(); VERIFY_NOT_REACHED(); } diff --git a/Userland/Libraries/LibJS/Runtime/Symbol.cpp b/Userland/Libraries/LibJS/Runtime/Symbol.cpp index eabb625b24..9d24970b69 100644 --- a/Userland/Libraries/LibJS/Runtime/Symbol.cpp +++ b/Userland/Libraries/LibJS/Runtime/Symbol.cpp @@ -1,6 +1,6 @@ /* * Copyright (c) 2020, Matthew Olsson - * Copyright (c) 2022, Linus Groh + * Copyright (c) 2022-2023, Linus Groh * * SPDX-License-Identifier: BSD-2-Clause */ @@ -22,4 +22,16 @@ NonnullGCPtr Symbol::create(VM& vm, Optional descripti return vm.heap().allocate_without_realm(move(description), is_global); } +// 20.4.3.3.1 SymbolDescriptiveString ( sym ), https://tc39.es/ecma262/#sec-symboldescriptivestring +DeprecatedString Symbol::descriptive_string() const +{ + // 1. Let desc be sym's [[Description]] value. + // 2. If desc is undefined, set desc to the empty String. + // 3. Assert: desc is a String. + auto description = m_description.value_or(""); + + // 4. Return the string-concatenation of "Symbol(", desc, and ")". + return DeprecatedString::formatted("Symbol({})", description); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Symbol.h b/Userland/Libraries/LibJS/Runtime/Symbol.h index f323489f27..2b030c1ff4 100644 --- a/Userland/Libraries/LibJS/Runtime/Symbol.h +++ b/Userland/Libraries/LibJS/Runtime/Symbol.h @@ -1,6 +1,6 @@ /* * Copyright (c) 2020, Matthew Olsson - * Copyright (c) 2022, Linus Groh + * Copyright (c) 2022-2023, Linus Groh * * SPDX-License-Identifier: BSD-2-Clause */ @@ -24,7 +24,8 @@ public: DeprecatedString description() const { return m_description.value_or(""); } Optional const& raw_description() const { return m_description; } bool is_global() const { return m_is_global; } - DeprecatedString to_deprecated_string() const { return DeprecatedString::formatted("Symbol({})", description()); } + + DeprecatedString descriptive_string() const; private: Symbol(Optional, bool); diff --git a/Userland/Libraries/LibJS/Runtime/SymbolPrototype.cpp b/Userland/Libraries/LibJS/Runtime/SymbolPrototype.cpp index a32d654e3c..87f8d5cc8f 100644 --- a/Userland/Libraries/LibJS/Runtime/SymbolPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/SymbolPrototype.cpp @@ -1,6 +1,6 @@ /* * Copyright (c) 2020, Matthew Olsson - * Copyright (c) 2021-2022, Linus Groh + * Copyright (c) 2021-2023, Linus Groh * * SPDX-License-Identifier: BSD-2-Clause */ @@ -63,7 +63,7 @@ JS_DEFINE_NATIVE_FUNCTION(SymbolPrototype::description_getter) JS_DEFINE_NATIVE_FUNCTION(SymbolPrototype::to_string) { auto* symbol = TRY(this_symbol_value(vm, vm.this_value())); - return PrimitiveString::create(vm, symbol->to_deprecated_string()); + return PrimitiveString::create(vm, symbol->descriptive_string()); } // 20.4.3.4 Symbol.prototype.valueOf ( ), https://tc39.es/ecma262/#sec-symbol.prototype.valueof diff --git a/Userland/Libraries/LibJS/Runtime/Value.cpp b/Userland/Libraries/LibJS/Runtime/Value.cpp index 2ded93f26a..e464cace50 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.cpp +++ b/Userland/Libraries/LibJS/Runtime/Value.cpp @@ -372,7 +372,7 @@ DeprecatedString Value::to_string_without_side_effects() const case STRING_TAG: return MUST(as_string().deprecated_string()); case SYMBOL_TAG: - return as_symbol().to_deprecated_string(); + return as_symbol().descriptive_string(); case BIGINT_TAG: return as_bigint().to_deprecated_string(); case OBJECT_TAG: