2020-07-08 01:38:46 -03:00
|
|
|
/*
|
2021-04-22 20:53:07 -03:00
|
|
|
* Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
|
2020-07-08 01:38:46 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-08 01:38:46 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2020-10-04 12:36:12 -03:00
|
|
|
#include <AK/FlyString.h>
|
2020-07-08 01:38:46 -03:00
|
|
|
#include <LibJS/Runtime/PrimitiveString.h>
|
|
|
|
|
#include <LibJS/Runtime/Symbol.h>
|
|
|
|
|
#include <LibJS/Runtime/Value.h>
|
|
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
|
|
|
|
class StringOrSymbol {
|
|
|
|
|
public:
|
|
|
|
|
StringOrSymbol() = default;
|
|
|
|
|
|
2021-06-13 06:49:24 -03:00
|
|
|
StringOrSymbol(char const* chars)
|
2021-06-13 07:00:27 -03:00
|
|
|
: StringOrSymbol(FlyString(chars))
|
2020-07-08 01:38:46 -03:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 06:49:24 -03:00
|
|
|
StringOrSymbol(String const& string)
|
2021-06-13 07:00:27 -03:00
|
|
|
: StringOrSymbol(FlyString(string))
|
2020-07-08 01:38:46 -03:00
|
|
|
{
|
2020-10-04 12:36:12 -03:00
|
|
|
}
|
|
|
|
|
|
2021-06-13 06:49:24 -03:00
|
|
|
StringOrSymbol(FlyString const& string)
|
2020-10-04 12:36:12 -03:00
|
|
|
: m_ptr(string.impl())
|
|
|
|
|
{
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(!string.is_null());
|
2020-10-05 12:25:11 -03:00
|
|
|
as_string_impl().ref();
|
2020-07-08 01:38:46 -03:00
|
|
|
}
|
|
|
|
|
|
2020-08-16 15:31:05 -03:00
|
|
|
~StringOrSymbol()
|
|
|
|
|
{
|
|
|
|
|
if (is_string())
|
2020-10-05 12:25:11 -03:00
|
|
|
as_string_impl().unref();
|
2020-08-16 15:31:05 -03:00
|
|
|
}
|
|
|
|
|
|
2021-06-13 06:49:24 -03:00
|
|
|
StringOrSymbol(Symbol const* symbol)
|
2020-07-08 01:38:46 -03:00
|
|
|
: m_ptr(symbol)
|
|
|
|
|
{
|
|
|
|
|
set_symbol_flag();
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 06:49:24 -03:00
|
|
|
StringOrSymbol(StringOrSymbol const& other)
|
2020-07-08 01:38:46 -03:00
|
|
|
{
|
|
|
|
|
m_ptr = other.m_ptr;
|
2020-08-16 15:31:05 -03:00
|
|
|
if (is_string())
|
2020-10-05 12:25:11 -03:00
|
|
|
as_string_impl().ref();
|
2020-07-08 01:38:46 -03:00
|
|
|
}
|
|
|
|
|
|
2020-10-15 18:32:54 -03:00
|
|
|
StringOrSymbol(StringOrSymbol&& other)
|
|
|
|
|
{
|
|
|
|
|
m_ptr = exchange(other.m_ptr, nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-08 01:38:46 -03:00
|
|
|
ALWAYS_INLINE bool is_valid() const { return m_ptr != nullptr; }
|
|
|
|
|
ALWAYS_INLINE bool is_symbol() const { return is_valid() && (bits() & 1ul); }
|
|
|
|
|
ALWAYS_INLINE bool is_string() const { return is_valid() && !(bits() & 1ul); }
|
|
|
|
|
|
2021-06-13 07:00:27 -03:00
|
|
|
ALWAYS_INLINE FlyString as_string() const
|
2020-07-08 01:38:46 -03:00
|
|
|
{
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(is_string());
|
2021-06-13 07:00:27 -03:00
|
|
|
return FlyString::from_fly_impl(as_string_impl());
|
2020-07-08 01:38:46 -03:00
|
|
|
}
|
|
|
|
|
|
2021-06-13 06:49:24 -03:00
|
|
|
ALWAYS_INLINE Symbol const* as_symbol() const
|
2020-07-08 01:38:46 -03:00
|
|
|
{
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(is_symbol());
|
2021-06-13 06:49:24 -03:00
|
|
|
return reinterpret_cast<Symbol const*>(bits() & ~1ul);
|
2020-07-08 01:38:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String to_display_string() const
|
|
|
|
|
{
|
|
|
|
|
if (is_string())
|
|
|
|
|
return as_string();
|
|
|
|
|
if (is_symbol())
|
|
|
|
|
return as_symbol()->to_string();
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-07-08 01:38:46 -03:00
|
|
|
}
|
|
|
|
|
|
2020-09-27 14:55:21 -03:00
|
|
|
Value to_value(VM& vm) const
|
2020-07-08 01:38:46 -03:00
|
|
|
{
|
|
|
|
|
if (is_string())
|
2020-09-27 14:55:21 -03:00
|
|
|
return js_string(vm, as_string());
|
2020-07-08 01:38:46 -03:00
|
|
|
if (is_symbol())
|
|
|
|
|
return const_cast<Symbol*>(as_symbol());
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-28 10:33:36 -03:00
|
|
|
void visit_edges(Cell::Visitor& visitor)
|
2020-07-08 01:38:46 -03:00
|
|
|
{
|
|
|
|
|
if (is_symbol())
|
|
|
|
|
visitor.visit(const_cast<Symbol*>(as_symbol()));
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 06:49:24 -03:00
|
|
|
ALWAYS_INLINE bool operator==(StringOrSymbol const& other) const
|
2020-07-08 01:38:46 -03:00
|
|
|
{
|
2020-10-05 12:25:11 -03:00
|
|
|
if (is_string())
|
2021-06-13 07:00:27 -03:00
|
|
|
return other.is_string() && &as_string_impl() == &other.as_string_impl();
|
2020-07-08 01:38:46 -03:00
|
|
|
if (is_symbol())
|
|
|
|
|
return other.is_symbol() && as_symbol() == other.as_symbol();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-13 06:49:24 -03:00
|
|
|
StringOrSymbol& operator=(StringOrSymbol const& other)
|
2020-07-08 01:38:46 -03:00
|
|
|
{
|
|
|
|
|
if (this == &other)
|
|
|
|
|
return *this;
|
|
|
|
|
m_ptr = other.m_ptr;
|
2020-08-16 15:31:05 -03:00
|
|
|
if (is_string())
|
2020-10-05 12:25:11 -03:00
|
|
|
as_string_impl().ref();
|
2020-07-08 01:38:46 -03:00
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-15 18:32:54 -03:00
|
|
|
StringOrSymbol& operator=(StringOrSymbol&& other)
|
|
|
|
|
{
|
|
|
|
|
if (this != &other)
|
|
|
|
|
m_ptr = exchange(other.m_ptr, nullptr);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-04 12:27:38 -03:00
|
|
|
unsigned hash() const
|
|
|
|
|
{
|
|
|
|
|
if (is_string())
|
2020-10-05 12:25:11 -03:00
|
|
|
return as_string_impl().hash();
|
2020-10-04 12:27:38 -03:00
|
|
|
return ptr_hash(as_symbol());
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-08 01:38:46 -03:00
|
|
|
private:
|
|
|
|
|
ALWAYS_INLINE u64 bits() const
|
|
|
|
|
{
|
|
|
|
|
return reinterpret_cast<uintptr_t>(m_ptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ALWAYS_INLINE void set_symbol_flag()
|
|
|
|
|
{
|
2021-06-13 06:49:24 -03:00
|
|
|
m_ptr = reinterpret_cast<void const*>(bits() | 1ul);
|
2020-07-08 01:38:46 -03:00
|
|
|
}
|
|
|
|
|
|
2021-06-13 06:49:24 -03:00
|
|
|
ALWAYS_INLINE StringImpl const& as_string_impl() const
|
2020-10-05 12:25:11 -03:00
|
|
|
{
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(is_string());
|
2021-06-13 06:49:24 -03:00
|
|
|
return *reinterpret_cast<StringImpl const*>(m_ptr);
|
2020-10-05 12:25:11 -03:00
|
|
|
}
|
|
|
|
|
|
2021-06-13 06:49:24 -03:00
|
|
|
void const* m_ptr { nullptr };
|
2020-07-08 01:38:46 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
struct AK::Traits<JS::StringOrSymbol> : public GenericTraits<JS::StringOrSymbol> {
|
2021-06-13 06:49:24 -03:00
|
|
|
static unsigned hash(JS::StringOrSymbol const& key)
|
2020-07-08 01:38:46 -03:00
|
|
|
{
|
2020-10-04 12:27:38 -03:00
|
|
|
return key.hash();
|
2020-07-08 01:38:46 -03:00
|
|
|
}
|
|
|
|
|
};
|