From 8ac0e3f0e55522232a3383ec3ed038f47dabcdd0 Mon Sep 17 00:00:00 2001 From: Dan Klishch Date: Wed, 24 Jan 2024 15:15:48 -0500 Subject: [PATCH] AK+LibJS: Remove null state from DeprecatedFlyString :^) --- AK/ByteString.cpp | 2 +- AK/ByteString.h | 6 +++--- AK/DeprecatedFlyString.cpp | 21 +++++++++---------- AK/DeprecatedFlyString.h | 20 ++++++++++-------- .../Runtime/ECMAScriptFunctionObject.cpp | 1 - .../Libraries/LibJS/Runtime/PropertyKey.h | 2 -- .../Libraries/LibJS/Runtime/StringOrSymbol.h | 1 - 7 files changed, 25 insertions(+), 28 deletions(-) diff --git a/AK/ByteString.cpp b/AK/ByteString.cpp index 1efd396105a..9ac308511fb 100644 --- a/AK/ByteString.cpp +++ b/AK/ByteString.cpp @@ -338,7 +338,7 @@ ByteString escape_html_entities(StringView html) } ByteString::ByteString(DeprecatedFlyString const& string) - : m_impl(*(string.impl() ?: &StringImpl::the_empty_stringimpl())) + : m_impl(string.impl()) { } diff --git a/AK/ByteString.h b/AK/ByteString.h index 052e0571f8f..f4f5023d5c0 100644 --- a/AK/ByteString.h +++ b/AK/ByteString.h @@ -239,7 +239,7 @@ public: return StringImpl::the_empty_stringimpl(); } - [[nodiscard]] StringImpl const* impl() const { return m_impl.ptr(); } + [[nodiscard]] NonnullRefPtr impl() const { return m_impl; } ByteString& operator=(ByteString&& other) { @@ -325,12 +325,12 @@ private: template<> struct Traits : public DefaultTraits { - static unsigned hash(ByteString const& s) { return s.impl() ? s.impl()->hash() : 0; } + static unsigned hash(ByteString const& s) { return s.impl()->hash(); } }; // FIXME: Rename this to indicate that it's about ASCII-only case insensitivity. struct CaseInsensitiveStringTraits : public Traits { - static unsigned hash(ByteString const& s) { return s.impl() ? s.impl()->case_insensitive_hash() : 0; } + static unsigned hash(ByteString const& s) { return s.impl()->case_insensitive_hash(); } static bool equals(ByteString const& a, ByteString const& b) { return a.equals_ignoring_ascii_case(b); } }; diff --git a/AK/DeprecatedFlyString.cpp b/AK/DeprecatedFlyString.cpp index e5d77f6f1ab..252fc3c8b21 100644 --- a/AK/DeprecatedFlyString.cpp +++ b/AK/DeprecatedFlyString.cpp @@ -14,12 +14,10 @@ namespace AK { -struct DeprecatedFlyStringImplTraits : public Traits { - static unsigned hash(StringImpl const* s) { return s ? s->hash() : 0; } +struct DeprecatedFlyStringImplTraits : public Traits { + static unsigned hash(StringImpl const* s) { return s->hash(); } static bool equals(StringImpl const* a, StringImpl const* b) { - VERIFY(a); - VERIFY(b); return *a == *b; } }; @@ -37,23 +35,24 @@ void DeprecatedFlyString::did_destroy_impl(Badge, StringImpl& impl) } DeprecatedFlyString::DeprecatedFlyString(ByteString const& string) + : m_impl(string.impl()) { - if (string.impl()->is_fly()) { - m_impl = string.impl(); + if (string.impl()->is_fly()) return; - } - auto it = fly_impls().find(const_cast(string.impl())); + + auto it = fly_impls().find(string.impl()); if (it == fly_impls().end()) { - fly_impls().set(const_cast(string.impl())); + fly_impls().set(string.impl()); string.impl()->set_fly({}, true); m_impl = string.impl(); } else { VERIFY((*it)->is_fly()); - m_impl = *it; + m_impl = **it; } } DeprecatedFlyString::DeprecatedFlyString(StringView string) + : m_impl(StringImpl::the_empty_stringimpl()) { if (string.is_null()) return; @@ -67,7 +66,7 @@ DeprecatedFlyString::DeprecatedFlyString(StringView string) m_impl = new_string.impl(); } else { VERIFY((*it)->is_fly()); - m_impl = *it; + m_impl = **it; } } diff --git a/AK/DeprecatedFlyString.h b/AK/DeprecatedFlyString.h index 51aa425c96a..d952f5e1bef 100644 --- a/AK/DeprecatedFlyString.h +++ b/AK/DeprecatedFlyString.h @@ -13,7 +13,10 @@ namespace AK { class DeprecatedFlyString { public: - DeprecatedFlyString() = default; + DeprecatedFlyString() + : m_impl(StringImpl::the_empty_stringimpl()) + { + } DeprecatedFlyString(DeprecatedFlyString const& other) : m_impl(other.impl()) { @@ -49,8 +52,7 @@ public: return *this; } - bool is_empty() const { return !m_impl || !m_impl->length(); } - bool is_null() const { return !m_impl; } + bool is_empty() const { return !m_impl->length(); } bool operator==(DeprecatedFlyString const& other) const { return m_impl == other.m_impl; } @@ -60,12 +62,12 @@ public: bool operator==(char const*) const; - StringImpl const* impl() const { return m_impl; } - char const* characters() const { return m_impl ? m_impl->characters() : nullptr; } - size_t length() const { return m_impl ? m_impl->length() : 0; } + NonnullRefPtr impl() const { return m_impl; } + char const* characters() const { return m_impl->characters(); } + size_t length() const { return m_impl->length(); } - ALWAYS_INLINE u32 hash() const { return m_impl ? m_impl->existing_hash() : 0; } - ALWAYS_INLINE StringView view() const { return m_impl ? m_impl->view() : StringView {}; } + ALWAYS_INLINE u32 hash() const { return m_impl->existing_hash(); } + ALWAYS_INLINE StringView view() const { return m_impl->view(); } DeprecatedFlyString to_lowercase() const; @@ -88,7 +90,7 @@ public: } private: - RefPtr m_impl; + NonnullRefPtr m_impl; }; template<> diff --git a/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.cpp b/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.cpp index bb8c6423721..79444dcdf5f 100644 --- a/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.cpp @@ -1261,7 +1261,6 @@ Completion ECMAScriptFunctionObject::ordinary_call_evaluate_body() void ECMAScriptFunctionObject::set_name(DeprecatedFlyString const& name) { - VERIFY(!name.is_null()); auto& vm = this->vm(); m_name = name; m_name_string = PrimitiveString::create(vm, m_name); diff --git a/Userland/Libraries/LibJS/Runtime/PropertyKey.h b/Userland/Libraries/LibJS/Runtime/PropertyKey.h index d10a694d99b..24319b65581 100644 --- a/Userland/Libraries/LibJS/Runtime/PropertyKey.h +++ b/Userland/Libraries/LibJS/Runtime/PropertyKey.h @@ -69,7 +69,6 @@ public: : m_type(Type::String) , m_string(DeprecatedFlyString(string)) { - VERIFY(!m_string.is_null()); } PropertyKey(DeprecatedFlyString string, StringMayBeNumber string_may_be_number = StringMayBeNumber::Yes) @@ -77,7 +76,6 @@ public: , m_type(Type::String) , m_string(move(string)) { - VERIFY(!m_string.is_null()); } PropertyKey(NonnullGCPtr symbol) diff --git a/Userland/Libraries/LibJS/Runtime/StringOrSymbol.h b/Userland/Libraries/LibJS/Runtime/StringOrSymbol.h index 728eb7ded02..473ed3e7f5e 100644 --- a/Userland/Libraries/LibJS/Runtime/StringOrSymbol.h +++ b/Userland/Libraries/LibJS/Runtime/StringOrSymbol.h @@ -33,7 +33,6 @@ public: StringOrSymbol(DeprecatedFlyString const& string) : m_string(string) { - VERIFY(!string.is_null()); } ~StringOrSymbol()