2020-04-06 17:08:23 +02:00
|
|
|
/*
|
2024-10-04 13:19:50 +02:00
|
|
|
* Copyright (c) 2020, Andreas Kling <andreas@ladybird.org>
|
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
|
|
|
|
|
2024-03-31 11:56:13 +02:00
|
|
|
#include <AK/FlyString.h>
|
2024-11-15 04:01:23 +13:00
|
|
|
#include <LibGC/Root.h>
|
2021-10-12 17:49:01 +01:00
|
|
|
#include <LibJS/Runtime/Completion.h>
|
2020-07-07 21:38:46 -07:00
|
|
|
#include <LibJS/Runtime/StringOrSymbol.h>
|
2020-04-06 17:08:23 +02:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2021-10-24 16:01:24 +02:00
|
|
|
class PropertyKey {
|
2024-12-01 00:37:09 +01:00
|
|
|
AK_MAKE_DEFAULT_COPYABLE(PropertyKey);
|
|
|
|
AK_MAKE_DEFAULT_MOVABLE(PropertyKey);
|
2020-04-06 17:08:23 +02:00
|
|
|
|
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-03-18 18:08:02 -05:00
|
|
|
return TRY(value.to_string(vm));
|
2020-07-07 21:38:46 -07:00
|
|
|
}
|
|
|
|
|
2024-11-01 21:36:48 +01:00
|
|
|
PropertyKey() = delete;
|
2020-04-06 20:24:45 +02:00
|
|
|
|
2021-06-25 18:41:10 +01:00
|
|
|
template<Integral T>
|
2021-10-24 16:01:24 +02:00
|
|
|
PropertyKey(T index)
|
2024-12-01 00:37:09 +01:00
|
|
|
: m_data(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-03-18 18:08:02 -05:00
|
|
|
m_data = FlyString { String::number(index) };
|
2021-07-07 14:17:51 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-04-06 17:08:23 +02:00
|
|
|
}
|
|
|
|
|
2025-03-18 18:08:02 -05:00
|
|
|
PropertyKey(FlyString string, StringMayBeNumber string_may_be_number = StringMayBeNumber::Yes)
|
2024-12-01 00:37:09 +01:00
|
|
|
: m_data { try_coerce_into_number(move(string), string_may_be_number) }
|
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-03-16 20:45:02 -05:00
|
|
|
PropertyKey(String const& string)
|
2025-03-18 18:08:02 -05:00
|
|
|
: PropertyKey(FlyString(string))
|
2020-04-06 17:08:23 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
PropertyKey(GC::Ref<Symbol> symbol)
|
2024-12-01 00:37:09 +01:00
|
|
|
: m_data { symbol }
|
2020-07-07 21:38:46 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-10-24 16:01:24 +02:00
|
|
|
PropertyKey(StringOrSymbol const& string_or_symbol)
|
2024-12-01 00:37:09 +01:00
|
|
|
: m_data {
|
|
|
|
string_or_symbol.is_string()
|
2025-03-18 18:08:02 -05:00
|
|
|
? Variant<FlyString, GC::Root<Symbol>, u32> { string_or_symbol.as_string() }
|
|
|
|
: Variant<FlyString, GC::Root<Symbol>, u32> { const_cast<Symbol*>(string_or_symbol.as_symbol()) }
|
2020-07-07 21:38:46 -07:00
|
|
|
}
|
2021-06-16 20:51:07 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-12-01 00:37:09 +01:00
|
|
|
bool is_number() const { return m_data.has<u32>(); }
|
2025-03-18 18:08:02 -05:00
|
|
|
bool is_string() const { return m_data.has<FlyString>(); }
|
2024-12-01 00:37:09 +01:00
|
|
|
bool is_symbol() const { return m_data.has<GC::Root<Symbol>>(); }
|
2021-06-16 20:51:07 +03:00
|
|
|
|
2024-12-01 00:37:09 +01:00
|
|
|
u32 as_number() const { return m_data.get<u32>(); }
|
2025-03-18 18:08:02 -05:00
|
|
|
FlyString const& as_string() const { return m_data.get<FlyString>(); }
|
2024-12-01 00:37:09 +01:00
|
|
|
Symbol const* as_symbol() const { return m_data.get<GC::Root<Symbol>>(); }
|
2020-04-06 17:08:23 +02:00
|
|
|
|
2025-03-18 18:08:02 -05:00
|
|
|
String to_string() const
|
2020-04-27 12:10:16 +02:00
|
|
|
{
|
2025-05-16 04:14:12 +01:00
|
|
|
VERIFY(!is_symbol());
|
2020-04-27 12:10:16 +02:00
|
|
|
if (is_string())
|
2025-03-18 18:08:02 -05:00
|
|
|
return as_string().to_string();
|
|
|
|
return String::number(as_number());
|
2020-04-27 12:10:16 +02:00
|
|
|
}
|
|
|
|
|
2020-07-07 21:38:46 -07:00
|
|
|
StringOrSymbol to_string_or_symbol() const
|
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(!is_number());
|
2020-07-07 21:38:46 -07:00
|
|
|
if (is_string())
|
|
|
|
return StringOrSymbol(as_string());
|
|
|
|
return StringOrSymbol(as_symbol());
|
|
|
|
}
|
|
|
|
|
2025-03-24 16:25:33 +00:00
|
|
|
bool operator==(PropertyKey const&) const = default;
|
|
|
|
|
2020-04-06 17:08:23 +02:00
|
|
|
private:
|
2024-12-01 00:37:09 +01:00
|
|
|
friend Traits<JS::PropertyKey>;
|
|
|
|
|
2025-03-18 18:08:02 -05:00
|
|
|
static Variant<FlyString, u32> try_coerce_into_number(FlyString string, StringMayBeNumber string_may_be_number)
|
2024-12-01 00:37:09 +01:00
|
|
|
{
|
|
|
|
if (string_may_be_number != StringMayBeNumber::Yes)
|
|
|
|
return string;
|
2025-03-25 08:18:45 +00:00
|
|
|
auto view = string.bytes_as_string_view();
|
|
|
|
if (view.is_empty())
|
2024-12-01 00:37:09 +01:00
|
|
|
return string;
|
2025-03-25 08:18:45 +00:00
|
|
|
if (view[0] == '0' && view.length() > 1)
|
2024-12-01 00:37:09 +01:00
|
|
|
return string;
|
2025-03-25 08:18:45 +00:00
|
|
|
auto property_index = view.to_number<u32>(TrimWhitespace::No);
|
2024-12-01 00:37:09 +01:00
|
|
|
if (!property_index.has_value() || property_index.value() >= NumericLimits<u32>::max())
|
|
|
|
return string;
|
|
|
|
return property_index.release_value();
|
|
|
|
}
|
|
|
|
|
2025-03-18 18:08:02 -05:00
|
|
|
Variant<FlyString, u32, GC::Root<Symbol>> m_data;
|
2020-04-06 17:08:23 +02:00
|
|
|
};
|
|
|
|
|
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
|
|
|
{
|
2024-12-01 00:37:09 +01:00
|
|
|
return name.m_data.visit(
|
2025-03-18 18:08:02 -05:00
|
|
|
[](FlyString const& string) { return string.hash(); },
|
2024-12-01 00:37:09 +01:00
|
|
|
[](GC::Root<JS::Symbol> const& symbol) { return ptr_hash(symbol.ptr()); },
|
|
|
|
[](u32 const& number) { return int_hash(number); });
|
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
|
|
|
{
|
2024-12-01 00:37:09 +01:00
|
|
|
return a.m_data == b.m_data;
|
2021-06-12 18:04:28 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-13 18:59:07 +02:00
|
|
|
template<>
|
2021-10-24 16:01:24 +02:00
|
|
|
struct Formatter<JS::PropertyKey> : Formatter<StringView> {
|
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());
|
|
|
|
return builder.put_string(property_key.to_string_or_symbol().to_display_string());
|
2021-06-13 18:59:07 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|