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
|
|
|
|
|
|
|
|
#include <AK/FlyString.h>
|
2020-07-08 04:38:46 +00:00
|
|
|
#include <LibJS/Runtime/StringOrSymbol.h>
|
2020-04-06 15:08:23 +00:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
|
|
class PropertyName {
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2020-09-27 16:36:49 +00:00
|
|
|
static PropertyName from_value(GlobalObject& global_object, Value value)
|
2020-07-08 04:38:46 +00:00
|
|
|
{
|
2020-10-07 18:53:30 +00:00
|
|
|
if (value.is_empty())
|
|
|
|
return {};
|
2020-07-08 04:38:46 +00:00
|
|
|
if (value.is_symbol())
|
2021-06-25 17:37:14 +00:00
|
|
|
return 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())
|
2021-06-25 17:41:10 +00:00
|
|
|
return value.as_u32();
|
2020-11-04 09:48:48 +00:00
|
|
|
auto string = value.to_string(global_object);
|
|
|
|
if (string.is_null())
|
|
|
|
return {};
|
|
|
|
return string;
|
2020-07-08 04:38:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PropertyName() { }
|
2020-04-06 18:24:45 +00:00
|
|
|
|
2021-06-25 17:41:10 +00:00
|
|
|
template<Integral T>
|
|
|
|
PropertyName(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()) {
|
|
|
|
m_string = String::number(index);
|
|
|
|
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-06-13 09:49:24 +00:00
|
|
|
PropertyName(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)
|
|
|
|
, m_string(FlyString(chars))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-06-13 09:49:24 +00:00
|
|
|
PropertyName(String 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)
|
|
|
|
, m_string(FlyString(string))
|
|
|
|
{
|
2021-09-11 17:03:38 +00:00
|
|
|
VERIFY(!m_string.is_null());
|
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
|
|
|
}
|
|
|
|
|
2021-09-11 17:03:38 +00:00
|
|
|
PropertyName(FlyString 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
|
|
|
{
|
2021-09-11 17:03:38 +00:00
|
|
|
VERIFY(!m_string.is_null());
|
2020-04-06 15:08:23 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 17:37:14 +00:00
|
|
|
PropertyName(Symbol& symbol)
|
2020-07-08 04:38:46 +00:00
|
|
|
: m_type(Type::Symbol)
|
2021-06-25 17:37:14 +00:00
|
|
|
, m_symbol(&symbol)
|
2020-07-08 04:38:46 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-06-13 09:49:24 +00:00
|
|
|
PropertyName(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;
|
|
|
|
|
|
|
|
return const_cast<PropertyName*>(this)->try_coerce_into_number();
|
|
|
|
}
|
|
|
|
bool is_string() const
|
|
|
|
{
|
|
|
|
if (m_type != Type::String)
|
|
|
|
return false;
|
|
|
|
if (!m_string_may_be_number)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return !const_cast<PropertyName*>(this)->try_coerce_into_number();
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2021-06-25 17:41:10 +00:00
|
|
|
auto property_index = m_string.to_uint(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;
|
|
|
|
}
|
|
|
|
|
2021-06-13 09:49:24 +00:00
|
|
|
FlyString 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
|
|
|
|
2020-04-27 10:10:16 +00:00
|
|
|
String to_string() const
|
|
|
|
{
|
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();
|
|
|
|
return String::number(as_number());
|
|
|
|
}
|
|
|
|
|
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 };
|
2020-04-06 15:08:23 +00:00
|
|
|
FlyString m_string;
|
2020-07-08 04:38:46 +00:00
|
|
|
Symbol* m_symbol { nullptr };
|
2020-04-06 15:08:23 +00:00
|
|
|
};
|
|
|
|
|
2021-06-13 01:04:28 +00:00
|
|
|
struct PropertyNameTraits : public Traits<PropertyName> {
|
|
|
|
static unsigned hash(PropertyName const& name)
|
|
|
|
{
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool equals(PropertyName const& a, PropertyName const& b)
|
|
|
|
{
|
|
|
|
if (a.type() != b.type())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (a.type()) {
|
|
|
|
case PropertyName::Type::Number:
|
|
|
|
return a.as_number() == b.as_number();
|
|
|
|
case PropertyName::Type::String:
|
|
|
|
return a.as_string() == b.as_string();
|
|
|
|
case PropertyName::Type::Symbol:
|
|
|
|
return a.as_symbol() == b.as_symbol();
|
|
|
|
default:
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-04-06 15:08:23 +00:00
|
|
|
}
|
2021-06-13 16:59:07 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct Formatter<JS::PropertyName> : Formatter<StringView> {
|
2021-07-04 16:59:07 +00:00
|
|
|
void format(FormatBuilder& builder, JS::PropertyName const& property_name)
|
|
|
|
{
|
|
|
|
if (!property_name.is_valid())
|
|
|
|
Formatter<StringView>::format(builder, "<invalid PropertyName>");
|
|
|
|
else if (property_name.is_number())
|
|
|
|
Formatter<StringView>::format(builder, String::number(property_name.as_number()));
|
|
|
|
else
|
|
|
|
Formatter<StringView>::format(builder, property_name.to_string_or_symbol().to_display_string());
|
2021-06-13 16:59:07 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|