2020-07-07 21:38:46 -07:00
|
|
|
/*
|
2021-04-22 16:53:07 -07:00
|
|
|
* Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
|
2020-07-07 21:38:46 -07:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-07 21:38:46 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2025-03-18 18:08:02 -05:00
|
|
|
#include <AK/FlyString.h>
|
2020-07-07 21:38:46 -07:00
|
|
|
#include <LibJS/Runtime/PrimitiveString.h>
|
|
|
|
|
#include <LibJS/Runtime/Symbol.h>
|
|
|
|
|
#include <LibJS/Runtime/Value.h>
|
|
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
|
|
|
|
class StringOrSymbol {
|
|
|
|
|
public:
|
2024-01-22 16:10:45 -05:00
|
|
|
StringOrSymbol()
|
|
|
|
|
: m_bits(0)
|
|
|
|
|
{
|
|
|
|
|
}
|
2020-07-07 21:38:46 -07:00
|
|
|
|
2025-03-18 18:08:02 -05:00
|
|
|
StringOrSymbol(FlyString const& string)
|
2024-01-22 16:10:45 -05:00
|
|
|
: m_string(string)
|
2020-10-04 17:36:12 +02:00
|
|
|
{
|
2020-07-07 21:38:46 -07:00
|
|
|
}
|
|
|
|
|
|
2020-08-16 20:31:05 +02:00
|
|
|
~StringOrSymbol()
|
|
|
|
|
{
|
|
|
|
|
if (is_string())
|
2025-03-18 18:08:02 -05:00
|
|
|
m_string.~FlyString();
|
2020-08-16 20:31:05 +02:00
|
|
|
}
|
|
|
|
|
|
2021-06-13 11:49:24 +02:00
|
|
|
StringOrSymbol(Symbol const* symbol)
|
2024-01-22 16:10:45 -05:00
|
|
|
: m_symbol_with_tag(symbol)
|
2020-07-07 21:38:46 -07:00
|
|
|
{
|
|
|
|
|
set_symbol_flag();
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 11:49:24 +02:00
|
|
|
StringOrSymbol(StringOrSymbol const& other)
|
2020-07-07 21:38:46 -07:00
|
|
|
{
|
2024-01-22 16:10:45 -05:00
|
|
|
if (other.is_string())
|
2025-03-18 18:08:02 -05:00
|
|
|
new (&m_string) FlyString(other.m_string);
|
2024-01-22 16:10:45 -05:00
|
|
|
else
|
|
|
|
|
m_bits = other.m_bits;
|
2020-07-07 21:38:46 -07:00
|
|
|
}
|
|
|
|
|
|
2020-10-15 23:32:54 +02:00
|
|
|
StringOrSymbol(StringOrSymbol&& other)
|
|
|
|
|
{
|
2024-01-22 16:10:45 -05:00
|
|
|
if (other.is_string())
|
2025-03-18 18:08:02 -05:00
|
|
|
new (&m_string) FlyString(move(other.m_string));
|
2024-01-22 16:10:45 -05:00
|
|
|
else
|
|
|
|
|
m_bits = exchange(other.m_bits, 0);
|
2020-10-15 23:32:54 +02:00
|
|
|
}
|
|
|
|
|
|
2024-01-22 16:10:45 -05:00
|
|
|
ALWAYS_INLINE bool is_valid() const { return m_bits != 0; }
|
|
|
|
|
ALWAYS_INLINE bool is_symbol() const { return is_valid() && (m_bits & 2); }
|
|
|
|
|
ALWAYS_INLINE bool is_string() const { return is_valid() && !(m_bits & 2); }
|
2020-07-07 21:38:46 -07:00
|
|
|
|
2025-03-26 14:08:47 +00:00
|
|
|
ALWAYS_INLINE FlyString const& as_string() const
|
2020-07-07 21:38:46 -07:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(is_string());
|
2024-01-22 16:10:45 -05:00
|
|
|
return m_string;
|
2020-07-07 21:38:46 -07:00
|
|
|
}
|
|
|
|
|
|
2021-06-13 11:49:24 +02:00
|
|
|
ALWAYS_INLINE Symbol const* as_symbol() const
|
2020-07-07 21:38:46 -07:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(is_symbol());
|
2024-01-22 16:10:45 -05:00
|
|
|
return reinterpret_cast<Symbol const*>(m_bits & ~2ULL);
|
2020-07-07 21:38:46 -07:00
|
|
|
}
|
|
|
|
|
|
2025-03-18 18:08:02 -05:00
|
|
|
String to_display_string() const
|
2020-07-07 21:38:46 -07:00
|
|
|
{
|
|
|
|
|
if (is_string())
|
2025-03-18 18:08:02 -05:00
|
|
|
return as_string().to_string();
|
2020-07-07 21:38:46 -07:00
|
|
|
if (is_symbol())
|
2025-03-18 18:08:02 -05:00
|
|
|
return MUST(as_symbol()->descriptive_string());
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-07-07 21:38:46 -07:00
|
|
|
}
|
|
|
|
|
|
2020-09-27 19:55:21 +02:00
|
|
|
Value to_value(VM& vm) const
|
2020-07-07 21:38:46 -07:00
|
|
|
{
|
|
|
|
|
if (is_string())
|
2022-12-06 22:17:27 +00:00
|
|
|
return PrimitiveString::create(vm, as_string());
|
2020-07-07 21:38:46 -07:00
|
|
|
if (is_symbol())
|
|
|
|
|
return const_cast<Symbol*>(as_symbol());
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-28 14:33:36 +01:00
|
|
|
void visit_edges(Cell::Visitor& visitor)
|
2020-07-07 21:38:46 -07:00
|
|
|
{
|
|
|
|
|
if (is_symbol())
|
|
|
|
|
visitor.visit(const_cast<Symbol*>(as_symbol()));
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 11:49:24 +02:00
|
|
|
ALWAYS_INLINE bool operator==(StringOrSymbol const& other) const
|
2020-07-07 21:38:46 -07:00
|
|
|
{
|
2020-10-05 11:25:11 -04:00
|
|
|
if (is_string())
|
2024-01-22 16:10:45 -05:00
|
|
|
return other.is_string() && m_string == other.m_string;
|
2020-07-07 21:38:46 -07:00
|
|
|
if (is_symbol())
|
|
|
|
|
return other.is_symbol() && as_symbol() == other.as_symbol();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 11:49:24 +02:00
|
|
|
StringOrSymbol& operator=(StringOrSymbol const& other)
|
2020-07-07 21:38:46 -07:00
|
|
|
{
|
2024-01-22 16:10:45 -05:00
|
|
|
if (this != &other) {
|
|
|
|
|
this->~StringOrSymbol();
|
|
|
|
|
new (this) StringOrSymbol(other);
|
|
|
|
|
}
|
2020-07-07 21:38:46 -07:00
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-15 23:32:54 +02:00
|
|
|
StringOrSymbol& operator=(StringOrSymbol&& other)
|
|
|
|
|
{
|
2024-01-22 16:10:45 -05:00
|
|
|
if (this != &other) {
|
|
|
|
|
this->~StringOrSymbol();
|
|
|
|
|
new (this) StringOrSymbol(move(other));
|
|
|
|
|
}
|
2020-10-15 23:32:54 +02:00
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-04 17:27:38 +02:00
|
|
|
unsigned hash() const
|
|
|
|
|
{
|
|
|
|
|
if (is_string())
|
2024-01-22 16:10:45 -05:00
|
|
|
return m_string.hash();
|
2020-10-04 17:27:38 +02:00
|
|
|
return ptr_hash(as_symbol());
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-07 21:38:46 -07:00
|
|
|
private:
|
|
|
|
|
ALWAYS_INLINE void set_symbol_flag()
|
|
|
|
|
{
|
2024-01-22 16:10:45 -05:00
|
|
|
m_bits |= 2;
|
2020-10-05 11:25:11 -04:00
|
|
|
}
|
|
|
|
|
|
2024-01-22 16:10:45 -05:00
|
|
|
union {
|
2025-03-18 18:08:02 -05:00
|
|
|
FlyString m_string;
|
2024-01-22 16:10:45 -05:00
|
|
|
Symbol const* m_symbol_with_tag;
|
|
|
|
|
uintptr_t m_bits;
|
|
|
|
|
};
|
2020-07-07 21:38:46 -07:00
|
|
|
};
|
|
|
|
|
|
2024-01-22 16:10:45 -05:00
|
|
|
static_assert(sizeof(StringOrSymbol) == sizeof(uintptr_t));
|
|
|
|
|
|
2020-07-07 21:38:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<>
|
2023-11-08 20:29:12 +01:00
|
|
|
struct AK::Traits<JS::StringOrSymbol> : public DefaultTraits<JS::StringOrSymbol> {
|
2021-06-13 11:49:24 +02:00
|
|
|
static unsigned hash(JS::StringOrSymbol const& key)
|
2020-07-07 21:38:46 -07:00
|
|
|
{
|
2020-10-04 17:27:38 +02:00
|
|
|
return key.hash();
|
2020-07-07 21:38:46 -07:00
|
|
|
}
|
|
|
|
|
};
|