LibJS: Make StringOrSymbol not leak strings

Ideally this thing would not allocate strings at all, but I'll leave
that as a separate exercise.
This commit is contained in:
Andreas Kling 2020-08-16 20:31:05 +02:00
parent bc27aa9b6f
commit 6444f49d22
Notes: sideshowbarker 2024-07-19 03:32:21 +09:00

View file

@ -55,6 +55,12 @@ public:
{
}
~StringOrSymbol()
{
if (is_string())
reinterpret_cast<const StringImpl*>(m_ptr)->unref();
}
StringOrSymbol(const Symbol* symbol)
: m_ptr(symbol)
{
@ -64,6 +70,8 @@ public:
StringOrSymbol(const StringOrSymbol& other)
{
m_ptr = other.m_ptr;
if (is_string())
reinterpret_cast<const StringImpl*>(m_ptr)->ref();
}
ALWAYS_INLINE bool is_valid() const { return m_ptr != nullptr; }
@ -120,6 +128,8 @@ public:
if (this == &other)
return *this;
m_ptr = other.m_ptr;
if (is_string())
reinterpret_cast<const StringImpl*>(m_ptr)->ref();
return *this;
}