2020-04-06 15:08:23 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-06 15:08:23 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-01-09 00:23:00 +00:00
|
|
|
#include <AK/DeprecatedFlyString.h>
|
2022-10-01 14:17:57 +00:00
|
|
|
#include <LibJS/Heap/Handle.h>
|
2021-10-12 16:49:01 +00:00
|
|
|
#include <LibJS/Runtime/Completion.h>
|
2020-07-08 04:38:46 +00:00
|
|
|
#include <LibJS/Runtime/StringOrSymbol.h>
|
2020-04-06 15:08:23 +00:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2021-10-24 14:01:24 +00:00
|
|
|
class PropertyKey {
|
2020-04-06 15:08:23 +00:00
|
|
|
public:
|
2021-06-13 16:59:07 +00:00
|
|
|
enum class Type : u8 {
|
2020-04-06 18:24:45 +00:00
|
|
|
Invalid,
|
2020-04-06 15:08:23 +00:00
|
|
|
Number,
|
|
|
|
String,
|
2020-07-08 04:38:46 +00:00
|
|
|
Symbol,
|
2020-04-06 15:08:23 +00:00
|
|
|
};
|
|
|
|
|
2021-06-13 16:59:07 +00:00
|
|
|
enum class StringMayBeNumber {
|
|
|
|
Yes,
|
|
|
|
No,
|
|
|
|
};
|
|
|
|
|
2022-08-21 13:00:56 +00:00
|
|
|
static ThrowCompletionOr<PropertyKey> from_value(VM& vm, Value value)
|
2020-07-08 04:38:46 +00:00
|
|
|
{
|
2020-10-07 18:53:30 +00:00
|
|
|
if (value.is_empty())
|
2022-01-04 21:33:30 +00:00
|
|
|
return PropertyKey {};
|
2020-07-08 04:38:46 +00:00
|
|
|
if (value.is_symbol())
|
2022-01-04 21:33:30 +00:00
|
|
|
return PropertyKey { value.as_symbol() };
|
2021-07-06 15:45:54 +00:00
|
|
|
if (value.is_integral_number() && value.as_double() >= 0 && value.as_double() < NumericLimits<u32>::max())
|
2022-02-25 00:26:52 +00:00
|
|
|
return static_cast<u32>(value.as_double());
|
2023-12-16 14:19:34 +00:00
|
|
|
return TRY(value.to_byte_string(vm));
|
2020-07-08 04:38:46 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 16:25:06 +00:00
|
|
|
PropertyKey() = default;
|
2020-04-06 18:24:45 +00:00
|
|
|
|
2021-06-25 17:41:10 +00:00
|
|
|
template<Integral T>
|
2021-10-24 14:01:24 +00:00
|
|
|
PropertyKey(T index)
|
2020-04-06 15:08:23 +00:00
|
|
|
{
|
2021-06-25 17:41:10 +00: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 19:42:32 +00:00
|
|
|
VERIFY(index >= 0);
|
2021-07-07 12:17:51 +00:00
|
|
|
if constexpr (NumericLimits<T>::max() >= NumericLimits<u32>::max()) {
|
|
|
|
if (index >= NumericLimits<u32>::max()) {
|
2023-12-16 14:19:34 +00:00
|
|
|
m_string = ByteString::number(index);
|
2021-07-07 12:17:51 +00:00
|
|
|
m_type = Type::String;
|
|
|
|
m_string_may_be_number = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_type = Type::Number;
|
|
|
|
m_number = index;
|
2020-04-06 15:08:23 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 14:01:24 +00:00
|
|
|
PropertyKey(char const* chars)
|
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-27 04:33:37 +00:00
|
|
|
: m_type(Type::String)
|
2023-01-09 00:23:00 +00:00
|
|
|
, m_string(DeprecatedFlyString(chars))
|
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-27 04:33:37 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
PropertyKey(ByteString const& 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-27 04:33:37 +00:00
|
|
|
: m_type(Type::String)
|
2023-01-09 00:23:00 +00:00
|
|
|
, m_string(DeprecatedFlyString(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-27 04:33:37 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-01-09 00:23:00 +00:00
|
|
|
PropertyKey(DeprecatedFlyString string, StringMayBeNumber string_may_be_number = StringMayBeNumber::Yes)
|
2021-10-10 07:00:15 +00:00
|
|
|
: m_string_may_be_number(string_may_be_number == StringMayBeNumber::Yes)
|
|
|
|
, m_type(Type::String)
|
2021-09-11 17:03:38 +00:00
|
|
|
, m_string(move(string))
|
2020-04-06 15:08:23 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-04-12 23:14:45 +00:00
|
|
|
PropertyKey(NonnullGCPtr<Symbol> symbol)
|
2020-07-08 04:38:46 +00:00
|
|
|
: m_type(Type::Symbol)
|
2023-04-12 23:14:45 +00:00
|
|
|
, m_symbol(symbol)
|
2020-07-08 04:38:46 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-10-24 14:01:24 +00:00
|
|
|
PropertyKey(StringOrSymbol const& string_or_symbol)
|
2020-07-08 04:38:46 +00:00
|
|
|
{
|
|
|
|
if (string_or_symbol.is_string()) {
|
|
|
|
m_string = string_or_symbol.as_string();
|
|
|
|
m_type = Type::String;
|
|
|
|
} else if (string_or_symbol.is_symbol()) {
|
|
|
|
m_symbol = const_cast<Symbol*>(string_or_symbol.as_symbol());
|
|
|
|
m_type = Type::Symbol;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-13 01:04:28 +00:00
|
|
|
ALWAYS_INLINE Type type() const { return m_type; }
|
|
|
|
|
2020-04-06 18:24:45 +00:00
|
|
|
bool is_valid() const { return m_type != Type::Invalid; }
|
2021-06-16 17:51:07 +00:00
|
|
|
bool is_number() const
|
|
|
|
{
|
|
|
|
if (m_type == Type::Number)
|
|
|
|
return true;
|
|
|
|
if (m_type != Type::String || !m_string_may_be_number)
|
|
|
|
return false;
|
|
|
|
|
2021-10-24 14:01:24 +00:00
|
|
|
return const_cast<PropertyKey*>(this)->try_coerce_into_number();
|
2021-06-16 17:51:07 +00:00
|
|
|
}
|
|
|
|
bool is_string() const
|
|
|
|
{
|
|
|
|
if (m_type != Type::String)
|
|
|
|
return false;
|
|
|
|
if (!m_string_may_be_number)
|
|
|
|
return true;
|
|
|
|
|
2021-10-24 14:01:24 +00:00
|
|
|
return !const_cast<PropertyKey*>(this)->try_coerce_into_number();
|
2021-06-16 17:51:07 +00:00
|
|
|
}
|
2020-07-08 04:38:46 +00:00
|
|
|
bool is_symbol() const { return m_type == Type::Symbol; }
|
2021-06-16 17:51:07 +00:00
|
|
|
|
|
|
|
bool try_coerce_into_number()
|
|
|
|
{
|
|
|
|
VERIFY(m_string_may_be_number);
|
2021-06-22 18:13:42 +00:00
|
|
|
if (m_string.is_empty()) {
|
|
|
|
m_string_may_be_number = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (char first = m_string.characters()[0]; first < '0' || first > '9') {
|
|
|
|
m_string_may_be_number = false;
|
|
|
|
return false;
|
|
|
|
} else if (m_string.length() > 1 && first == '0') {
|
|
|
|
m_string_may_be_number = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-12-23 02:59:14 +00:00
|
|
|
auto property_index = m_string.to_number<unsigned>(TrimWhitespace::No);
|
2021-07-06 15:45:54 +00:00
|
|
|
if (!property_index.has_value() || property_index.value() == NumericLimits<u32>::max()) {
|
2021-06-16 17:51:07 +00:00
|
|
|
m_string_may_be_number = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
m_type = Type::Number;
|
2021-06-25 17:41:10 +00:00
|
|
|
m_number = *property_index;
|
2021-06-16 17:51:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-04-06 15:08:23 +00:00
|
|
|
|
2021-04-25 20:42:48 +00:00
|
|
|
u32 as_number() const
|
2020-07-08 04:38:46 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(is_number());
|
2020-07-08 04:38:46 +00:00
|
|
|
return m_number;
|
|
|
|
}
|
|
|
|
|
2023-01-09 00:23:00 +00:00
|
|
|
DeprecatedFlyString const& as_string() const
|
2020-07-08 04:38:46 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(is_string());
|
2020-07-08 04:38:46 +00:00
|
|
|
return m_string;
|
|
|
|
}
|
|
|
|
|
2021-06-13 09:49:24 +00:00
|
|
|
Symbol const* as_symbol() const
|
2020-07-08 04:38:46 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(is_symbol());
|
2020-07-08 04:38:46 +00:00
|
|
|
return m_symbol;
|
|
|
|
}
|
2020-04-06 15:08:23 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString to_string() const
|
2020-04-27 10:10:16 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(is_valid());
|
|
|
|
VERIFY(!is_symbol());
|
2020-04-27 10:10:16 +00:00
|
|
|
if (is_string())
|
|
|
|
return as_string();
|
2023-12-16 14:19:34 +00:00
|
|
|
return ByteString::number(as_number());
|
2020-04-27 10:10:16 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 04:38:46 +00:00
|
|
|
StringOrSymbol to_string_or_symbol() const
|
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(is_valid());
|
|
|
|
VERIFY(!is_number());
|
2020-07-08 04:38:46 +00:00
|
|
|
if (is_string())
|
|
|
|
return StringOrSymbol(as_string());
|
|
|
|
return StringOrSymbol(as_symbol());
|
|
|
|
}
|
|
|
|
|
2020-04-06 15:08:23 +00:00
|
|
|
private:
|
2021-06-13 16:59:07 +00:00
|
|
|
bool m_string_may_be_number { true };
|
2021-10-10 07:00:15 +00:00
|
|
|
Type m_type { Type::Invalid };
|
|
|
|
u32 m_number { 0 };
|
2023-01-09 00:23:00 +00:00
|
|
|
DeprecatedFlyString m_string;
|
2022-10-01 14:17:57 +00:00
|
|
|
Handle<Symbol> m_symbol;
|
2020-04-06 15:08:23 +00:00
|
|
|
};
|
|
|
|
|
2021-10-24 14:19:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<>
|
2023-11-08 19:29:12 +00:00
|
|
|
struct Traits<JS::PropertyKey> : public DefaultTraits<JS::PropertyKey> {
|
2021-10-24 14:19:28 +00:00
|
|
|
static unsigned hash(JS::PropertyKey const& name)
|
2021-06-13 01:04:28 +00:00
|
|
|
{
|
|
|
|
VERIFY(name.is_valid());
|
|
|
|
if (name.is_string())
|
|
|
|
return name.as_string().hash();
|
|
|
|
if (name.is_number())
|
|
|
|
return int_hash(name.as_number());
|
|
|
|
return ptr_hash(name.as_symbol());
|
|
|
|
}
|
|
|
|
|
2021-10-24 14:19:28 +00:00
|
|
|
static bool equals(JS::PropertyKey const& a, JS::PropertyKey const& b)
|
2021-06-13 01:04:28 +00:00
|
|
|
{
|
|
|
|
if (a.type() != b.type())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (a.type()) {
|
2021-10-24 14:19:28 +00:00
|
|
|
case JS::PropertyKey::Type::Number:
|
2021-06-13 01:04:28 +00:00
|
|
|
return a.as_number() == b.as_number();
|
2021-10-24 14:19:28 +00:00
|
|
|
case JS::PropertyKey::Type::String:
|
2021-06-13 01:04:28 +00:00
|
|
|
return a.as_string() == b.as_string();
|
2021-10-24 14:19:28 +00:00
|
|
|
case JS::PropertyKey::Type::Symbol:
|
2021-06-13 01:04:28 +00:00
|
|
|
return a.as_symbol() == b.as_symbol();
|
|
|
|
default:
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-13 16:59:07 +00:00
|
|
|
template<>
|
2021-10-24 14:01:24 +00: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 16:59:07 +00:00
|
|
|
{
|
2022-02-06 15:59:04 +00:00
|
|
|
if (!property_key.is_valid())
|
2022-07-11 20:23:24 +00:00
|
|
|
return builder.put_string("<invalid PropertyKey>"sv);
|
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 16:59:07 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|