2020-04-06 17:08:23 +02:00
|
|
|
/*
|
2024-10-04 13:19:50 +02:00
|
|
|
* Copyright (c) 2020, Andreas Kling <andreas@ladybird.org>
|
2025-05-15 17:14:00 +03:00
|
|
|
* Copyright (c) 2025, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
|
2020-04-06 17:08:23 +02:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-06 17:08:23 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2025-08-02 19:27:29 -04:00
|
|
|
#include <AK/Utf16FlyString.h>
|
2021-10-12 17:49:01 +01:00
|
|
|
#include <LibJS/Runtime/Completion.h>
|
2025-05-15 17:14:00 +03:00
|
|
|
#include <LibJS/Runtime/PrimitiveString.h>
|
|
|
|
#include <LibJS/Runtime/Symbol.h>
|
2020-04-06 17:08:23 +02:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2025-07-19 10:41:08 -07:00
|
|
|
class PropertyKey {
|
2024-12-01 00:37:09 +01:00
|
|
|
public:
|
2021-06-13 18:59:07 +02:00
|
|
|
enum class StringMayBeNumber {
|
|
|
|
Yes,
|
|
|
|
No,
|
|
|
|
};
|
|
|
|
|
2022-08-21 14:00:56 +01:00
|
|
|
static ThrowCompletionOr<PropertyKey> from_value(VM& vm, Value value)
|
2020-07-07 21:38:46 -07:00
|
|
|
{
|
2025-04-04 23:16:34 +02:00
|
|
|
VERIFY(!value.is_special_empty_value());
|
2020-07-07 21:38:46 -07:00
|
|
|
if (value.is_symbol())
|
2022-01-04 22:33:30 +01:00
|
|
|
return PropertyKey { value.as_symbol() };
|
2021-07-06 16:45:54 +01:00
|
|
|
if (value.is_integral_number() && value.as_double() >= 0 && value.as_double() < NumericLimits<u32>::max())
|
2022-02-25 01:26:52 +01:00
|
|
|
return static_cast<u32>(value.as_double());
|
2025-08-02 19:27:29 -04:00
|
|
|
return TRY(value.to_utf16_string(vm));
|
2020-07-07 21:38:46 -07:00
|
|
|
}
|
|
|
|
|
2025-05-15 17:14:00 +03:00
|
|
|
static constexpr uintptr_t NORMAL_STRING_FLAG = 0;
|
|
|
|
static constexpr uintptr_t SHORT_STRING_FLAG = 1;
|
|
|
|
static constexpr uintptr_t SYMBOL_FLAG = 2;
|
|
|
|
static constexpr uintptr_t NUMBER_FLAG = 3;
|
|
|
|
|
|
|
|
bool is_string() const { return (m_bits & 3) == NORMAL_STRING_FLAG || (m_bits & 3) == SHORT_STRING_FLAG; }
|
|
|
|
bool is_number() const { return (m_bits & 3) == NUMBER_FLAG; }
|
|
|
|
bool is_symbol() const { return (m_bits & 3) == SYMBOL_FLAG; }
|
|
|
|
|
2024-11-01 21:36:48 +01:00
|
|
|
PropertyKey() = delete;
|
2020-04-06 20:24:45 +02:00
|
|
|
|
2025-05-15 17:14:00 +03:00
|
|
|
PropertyKey(PropertyKey const& other)
|
|
|
|
{
|
|
|
|
if (other.is_string())
|
2025-08-02 19:27:29 -04:00
|
|
|
new (&m_string) Utf16FlyString(other.m_string);
|
2025-05-15 17:14:00 +03:00
|
|
|
else
|
|
|
|
m_bits = other.m_bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
PropertyKey(PropertyKey&& other) noexcept
|
|
|
|
{
|
|
|
|
if (other.is_string())
|
2025-08-02 19:27:29 -04:00
|
|
|
new (&m_string) Utf16FlyString(move(other.m_string));
|
2025-05-15 17:14:00 +03:00
|
|
|
else
|
|
|
|
m_bits = exchange(other.m_bits, 0);
|
|
|
|
}
|
|
|
|
|
2021-06-25 18:41:10 +01:00
|
|
|
template<Integral T>
|
2021-10-24 16:01:24 +02:00
|
|
|
PropertyKey(T index)
|
2020-04-06 17:08:23 +02:00
|
|
|
{
|
2021-06-25 18:41:10 +01:00
|
|
|
// FIXME: Replace this with requires(IsUnsigned<T>)?
|
|
|
|
// Needs changes in various places using `int` (but not actually being in the negative range)
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(index >= 0);
|
2021-07-07 14:17:51 +02:00
|
|
|
if constexpr (NumericLimits<T>::max() >= NumericLimits<u32>::max()) {
|
|
|
|
if (index >= NumericLimits<u32>::max()) {
|
2025-08-02 19:27:29 -04:00
|
|
|
new (&m_string) Utf16FlyString { Utf16String::number(index) };
|
2021-07-07 14:17:51 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2025-05-15 17:14:00 +03:00
|
|
|
m_number = static_cast<u64>(index) << 2 | NUMBER_FLAG;
|
2020-04-06 17:08:23 +02:00
|
|
|
}
|
|
|
|
|
2025-08-02 19:27:29 -04:00
|
|
|
PropertyKey(Utf16FlyString string, StringMayBeNumber string_may_be_number = StringMayBeNumber::Yes)
|
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
|
|
|
{
|
2025-05-15 17:14:00 +03:00
|
|
|
if (string_may_be_number == StringMayBeNumber::Yes) {
|
2025-08-02 19:27:29 -04:00
|
|
|
if (!string.is_empty() && !(string.code_unit_at(0) == '0' && string.length_in_code_units() > 1)) {
|
|
|
|
auto property_index = string.to_number<u32>(TrimWhitespace::No);
|
2025-05-15 17:14:00 +03:00
|
|
|
if (property_index.has_value() && property_index.value() < NumericLimits<u32>::max()) {
|
|
|
|
m_number = static_cast<u64>(property_index.release_value()) << 2 | NUMBER_FLAG;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2025-08-02 19:27:29 -04:00
|
|
|
|
|
|
|
new (&m_string) Utf16FlyString(move(string));
|
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
|
|
|
}
|
|
|
|
|
2025-08-02 19:27:29 -04:00
|
|
|
PropertyKey(Utf16String const& string)
|
|
|
|
: PropertyKey(Utf16FlyString { string })
|
2020-04-06 17:08:23 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
PropertyKey(GC::Ref<Symbol> symbol)
|
2020-07-07 21:38:46 -07:00
|
|
|
{
|
2025-05-15 17:14:00 +03:00
|
|
|
m_bits = reinterpret_cast<uintptr_t>(symbol.ptr()) | SYMBOL_FLAG;
|
2020-07-07 21:38:46 -07:00
|
|
|
}
|
|
|
|
|
2025-05-15 17:14:00 +03:00
|
|
|
PropertyKey& operator=(PropertyKey const& other)
|
|
|
|
{
|
|
|
|
if (this != &other) {
|
|
|
|
if (is_string())
|
2025-08-02 19:27:29 -04:00
|
|
|
m_string.~Utf16FlyString();
|
2025-05-15 17:14:00 +03:00
|
|
|
new (this) PropertyKey(other);
|
2020-07-07 21:38:46 -07:00
|
|
|
}
|
2025-05-15 17:14:00 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
PropertyKey& operator=(PropertyKey&& other) noexcept
|
2021-06-16 20:51:07 +03:00
|
|
|
{
|
2025-05-15 17:14:00 +03:00
|
|
|
if (this != &other) {
|
|
|
|
if (is_string())
|
2025-08-02 19:27:29 -04:00
|
|
|
m_string.~Utf16FlyString();
|
2025-05-15 17:14:00 +03:00
|
|
|
new (this) PropertyKey(move(other));
|
|
|
|
}
|
|
|
|
return *this;
|
2021-06-16 20:51:07 +03:00
|
|
|
}
|
|
|
|
|
2025-05-15 17:14:00 +03:00
|
|
|
~PropertyKey()
|
|
|
|
{
|
|
|
|
if (is_string())
|
2025-08-02 19:27:29 -04:00
|
|
|
m_string.~Utf16FlyString();
|
2025-05-15 17:14:00 +03:00
|
|
|
}
|
2021-06-16 20:51:07 +03:00
|
|
|
|
2025-05-15 17:14:00 +03:00
|
|
|
u32 as_number() const
|
|
|
|
{
|
|
|
|
VERIFY(is_number());
|
|
|
|
return m_number >> 2;
|
|
|
|
}
|
|
|
|
|
2025-08-02 19:27:29 -04:00
|
|
|
Utf16FlyString const& as_string() const
|
2025-05-15 17:14:00 +03:00
|
|
|
{
|
|
|
|
VERIFY(is_string());
|
|
|
|
return m_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
Symbol const* as_symbol() const
|
|
|
|
{
|
|
|
|
VERIFY(is_symbol());
|
|
|
|
return reinterpret_cast<Symbol const*>(m_bits & ~3ULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value to_value(VM& vm) const
|
|
|
|
{
|
|
|
|
if (is_string())
|
|
|
|
return Value { PrimitiveString::create(vm, as_string()) };
|
|
|
|
if (is_symbol())
|
|
|
|
return Value { as_symbol() };
|
2025-10-04 11:00:20 +02:00
|
|
|
return Value { PrimitiveString::create_from_unsigned_integer(vm, as_number()) };
|
2025-05-15 17:14:00 +03:00
|
|
|
}
|
2020-04-06 17:08:23 +02:00
|
|
|
|
2025-08-02 19:27:29 -04:00
|
|
|
Utf16String to_string() const
|
2020-04-27 12:10:16 +02:00
|
|
|
{
|
|
|
|
if (is_string())
|
2025-08-02 19:27:29 -04:00
|
|
|
return as_string().to_utf16_string();
|
2025-05-15 17:14:00 +03:00
|
|
|
if (is_symbol())
|
2025-08-02 19:27:29 -04:00
|
|
|
return as_symbol()->descriptive_string();
|
|
|
|
return Utf16String::number(as_number());
|
2020-04-27 12:10:16 +02:00
|
|
|
}
|
|
|
|
|
2025-05-15 17:14:00 +03:00
|
|
|
void visit_edges(Cell::Visitor& visitor) const
|
2020-07-07 21:38:46 -07:00
|
|
|
{
|
2025-05-15 17:14:00 +03:00
|
|
|
if (is_symbol())
|
|
|
|
visitor.visit(const_cast<Symbol*>(as_symbol()));
|
2020-07-07 21:38:46 -07:00
|
|
|
}
|
|
|
|
|
2025-05-15 17:14:00 +03:00
|
|
|
bool operator==(PropertyKey const& other) const
|
2024-12-01 00:37:09 +01:00
|
|
|
{
|
2025-05-15 17:14:00 +03:00
|
|
|
if (is_string())
|
|
|
|
return other.is_string() && m_string == other.m_string;
|
|
|
|
if (is_symbol())
|
|
|
|
return other.is_symbol() && as_symbol() == other.as_symbol();
|
|
|
|
if (other.is_number())
|
|
|
|
return as_number() == other.as_number();
|
|
|
|
return false;
|
2024-12-01 00:37:09 +01:00
|
|
|
}
|
|
|
|
|
2025-05-15 17:14:00 +03:00
|
|
|
private:
|
|
|
|
friend Traits<PropertyKey>;
|
|
|
|
|
|
|
|
union {
|
2025-08-02 19:27:29 -04:00
|
|
|
Utf16FlyString m_string;
|
2025-05-15 17:14:00 +03:00
|
|
|
u64 m_number;
|
|
|
|
Symbol const* m_symbol;
|
|
|
|
uintptr_t m_bits;
|
|
|
|
};
|
2020-04-06 17:08:23 +02:00
|
|
|
};
|
|
|
|
|
2025-05-15 17:14:00 +03:00
|
|
|
static_assert(sizeof(PropertyKey) == sizeof(uintptr_t));
|
|
|
|
|
2021-10-24 16:19:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<>
|
2023-11-08 20:29:12 +01:00
|
|
|
struct Traits<JS::PropertyKey> : public DefaultTraits<JS::PropertyKey> {
|
2021-10-24 16:19:28 +02:00
|
|
|
static unsigned hash(JS::PropertyKey const& name)
|
2021-06-12 18:04:28 -07:00
|
|
|
{
|
2025-05-15 17:14:00 +03:00
|
|
|
if (name.is_string())
|
|
|
|
return name.as_string().hash();
|
|
|
|
if (name.is_symbol())
|
|
|
|
return ptr_hash(name.as_symbol());
|
|
|
|
if (name.is_number())
|
|
|
|
return int_hash(name.as_number());
|
|
|
|
VERIFY_NOT_REACHED();
|
2021-06-12 18:04:28 -07:00
|
|
|
}
|
|
|
|
|
2021-10-24 16:19:28 +02:00
|
|
|
static bool equals(JS::PropertyKey const& a, JS::PropertyKey const& b)
|
2021-06-12 18:04:28 -07:00
|
|
|
{
|
2025-05-15 17:14:00 +03:00
|
|
|
if (a.is_string())
|
|
|
|
return b.is_string() && a.as_string() == b.as_string();
|
|
|
|
if (a.is_symbol())
|
|
|
|
return b.is_symbol() && a.as_symbol() == b.as_symbol();
|
|
|
|
if (a.is_number())
|
|
|
|
return b.is_number() && a.as_number() == b.as_number();
|
|
|
|
VERIFY_NOT_REACHED();
|
2021-06-12 18:04:28 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-13 18:59:07 +02:00
|
|
|
template<>
|
2025-08-02 19:27:29 -04:00
|
|
|
struct Formatter<JS::PropertyKey> : Formatter<Utf16String> {
|
2022-02-06 15:59:04 +00:00
|
|
|
ErrorOr<void> format(FormatBuilder& builder, JS::PropertyKey const& property_key)
|
2021-07-04 17:59:07 +01:00
|
|
|
{
|
2022-02-06 15:59:04 +00:00
|
|
|
if (property_key.is_number())
|
2022-07-11 20:23:24 +00:00
|
|
|
return builder.put_u64(property_key.as_number());
|
2025-08-02 19:27:29 -04:00
|
|
|
return Formatter<Utf16String>::format(builder, property_key.to_string());
|
2021-06-13 18:59:07 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|