summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNico Weber <thakis@chromium.org>2020-10-05 11:25:11 -0400
committerAndreas Kling <kling@serenityos.org>2020-10-05 17:35:27 +0200
commitd8d00d3ac7ed73fc25f53a0a5a093b25f243242c (patch)
treefc9d8ac7e9ff682deb3192375072709ac3a9deb6
parentcc765e14caf24e5a4c28c22aa76fd607e529a0b8 (diff)
downloadserenity-d8d00d3ac7ed73fc25f53a0a5a093b25f243242c.zip
LibJS: Add StringOrSymbol::as_string_impl() helper
-rw-r--r--Libraries/LibJS/Runtime/StringOrSymbol.h29
1 files changed, 15 insertions, 14 deletions
diff --git a/Libraries/LibJS/Runtime/StringOrSymbol.h b/Libraries/LibJS/Runtime/StringOrSymbol.h
index e647b50b0f..b2f42641a8 100644
--- a/Libraries/LibJS/Runtime/StringOrSymbol.h
+++ b/Libraries/LibJS/Runtime/StringOrSymbol.h
@@ -55,20 +55,20 @@ public:
: m_ptr(string.impl())
{
ASSERT(!string.is_null());
- static_cast<const StringImpl*>(m_ptr)->ref();
+ as_string_impl().ref();
}
StringOrSymbol(const FlyString& string)
: m_ptr(string.impl())
{
ASSERT(!string.is_null());
- static_cast<const StringImpl*>(m_ptr)->ref();
+ as_string_impl().ref();
}
~StringOrSymbol()
{
if (is_string())
- reinterpret_cast<const StringImpl*>(m_ptr)->unref();
+ as_string_impl().unref();
}
StringOrSymbol(const Symbol* symbol)
@@ -81,7 +81,7 @@ public:
{
m_ptr = other.m_ptr;
if (is_string())
- reinterpret_cast<const StringImpl*>(m_ptr)->ref();
+ as_string_impl().ref();
}
ALWAYS_INLINE bool is_valid() const { return m_ptr != nullptr; }
@@ -91,7 +91,7 @@ public:
ALWAYS_INLINE String as_string() const
{
ASSERT(is_string());
- return reinterpret_cast<const StringImpl*>(m_ptr);
+ return as_string_impl();
}
ALWAYS_INLINE const Symbol* as_symbol() const
@@ -126,13 +126,8 @@ public:
ALWAYS_INLINE bool operator==(const StringOrSymbol& other) const
{
- if (is_string()) {
- if (!other.is_string())
- return false;
- auto* this_impl = static_cast<const StringImpl*>(m_ptr);
- auto* other_impl = static_cast<const StringImpl*>(other.m_ptr);
- return *this_impl == *other_impl;
- }
+ if (is_string())
+ return other.is_string() && as_string_impl() == other.as_string_impl();
if (is_symbol())
return other.is_symbol() && as_symbol() == other.as_symbol();
return true;
@@ -144,14 +139,14 @@ public:
return *this;
m_ptr = other.m_ptr;
if (is_string())
- reinterpret_cast<const StringImpl*>(m_ptr)->ref();
+ as_string_impl().ref();
return *this;
}
unsigned hash() const
{
if (is_string())
- return static_cast<const StringImpl*>(m_ptr)->hash();
+ return as_string_impl().hash();
return ptr_hash(as_symbol());
}
@@ -166,6 +161,12 @@ private:
m_ptr = reinterpret_cast<const void*>(bits() | 1ul);
}
+ ALWAYS_INLINE const StringImpl& as_string_impl() const
+ {
+ ASSERT(is_string());
+ return *reinterpret_cast<const StringImpl*>(m_ptr);
+ }
+
const void* m_ptr { nullptr };
};