From 3ffb6d9b5a9b9d723d4604884da9dcbb0fbe9466 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sun, 12 Feb 2023 21:08:32 -0500 Subject: [PATCH] LibJS: Define Value::to_string_without_side_effects for String --- Userland/Libraries/LibJS/Runtime/Value.cpp | 38 +++++++++++++++------- Userland/Libraries/LibJS/Runtime/Value.h | 1 + 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/Value.cpp b/Userland/Libraries/LibJS/Runtime/Value.cpp index 5ed6a184dd..4f07e2c76e 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.cpp +++ b/Userland/Libraries/LibJS/Runtime/Value.cpp @@ -355,35 +355,51 @@ StringView Value::typeof() const } } -DeprecatedString Value::to_deprecated_string_without_side_effects() const +ErrorOr Value::to_string_without_side_effects() const { if (is_double()) - return number_to_deprecated_string(m_value.as_double); + return number_to_string(m_value.as_double); switch (m_value.tag) { case UNDEFINED_TAG: - return "undefined"; + return String::from_utf8("undefined"sv); case NULL_TAG: - return "null"; + return String::from_utf8("null"sv); case BOOLEAN_TAG: - return as_bool() ? "true" : "false"; + return String::from_utf8(as_bool() ? "true"sv : "false"sv); case INT32_TAG: - return DeprecatedString::number(as_i32()); + return String::number(as_i32()); case STRING_TAG: - return MUST(as_string().deprecated_string()); + if (auto string = as_string().utf8_string(); string.is_throw_completion()) { + auto completion = string.release_error(); + + // We can't explicitly check for OOM because InternalError does not store the ErrorType + VERIFY(completion.value().has_value()); + VERIFY(completion.value()->is_object()); + VERIFY(is(completion.value()->as_object())); + + return AK::Error::from_errno(ENOMEM); + } else { + return string.release_value(); + } case SYMBOL_TAG: - return as_symbol().descriptive_string().release_value_but_fixme_should_propagate_errors().to_deprecated_string(); + return as_symbol().descriptive_string(); case BIGINT_TAG: - return as_bigint().to_deprecated_string(); + return as_bigint().to_string(); case OBJECT_TAG: - return DeprecatedString::formatted("[object {}]", as_object().class_name()); + return String::formatted("[object {}]", as_object().class_name()); case ACCESSOR_TAG: - return ""; + return String::from_utf8(""sv); default: VERIFY_NOT_REACHED(); } } +DeprecatedString Value::to_deprecated_string_without_side_effects() const +{ + return MUST(to_string_without_side_effects()).to_deprecated_string(); +} + ThrowCompletionOr Value::to_primitive_string(VM& vm) { if (is_string()) diff --git a/Userland/Libraries/LibJS/Runtime/Value.h b/Userland/Libraries/LibJS/Runtime/Value.h index d1349dbe7b..0fe5bac93a 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.h +++ b/Userland/Libraries/LibJS/Runtime/Value.h @@ -394,6 +394,7 @@ public: ThrowCompletionOr get(VM&, PropertyKey const&) const; ThrowCompletionOr get_method(VM&, PropertyKey const&) const; + ErrorOr to_string_without_side_effects() const; DeprecatedString to_deprecated_string_without_side_effects() const; Value value_or(Value fallback) const