1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 22:47:47 +00:00

LibJS: Provide default hash traits for JS::PropertyKey

Let's not require people to use PropertyNameTraits everywhere when we
can just specialize AK::Traits<JS::PropertyKey> instead. :^)
This commit is contained in:
Andreas Kling 2021-10-24 16:19:28 +02:00
parent c02e992de2
commit 7ccb8c8609
5 changed files with 15 additions and 14 deletions

View file

@ -445,7 +445,7 @@ ThrowCompletionOr<MarkedValueList> Object::enumerable_own_property_names(Propert
}
// 7.3.25 CopyDataProperties ( target, source, excludedItems ), https://tc39.es/ecma262/#sec-copydataproperties
ThrowCompletionOr<Object*> Object::copy_data_properties(Value source, HashTable<PropertyKey, PropertyNameTraits> const& seen_names, GlobalObject& global_object)
ThrowCompletionOr<Object*> Object::copy_data_properties(Value source, HashTable<PropertyKey> const& seen_names, GlobalObject& global_object)
{
if (source.is_nullish())
return this;

View file

@ -101,7 +101,7 @@ public:
ThrowCompletionOr<bool> set_integrity_level(IntegrityLevel);
ThrowCompletionOr<bool> test_integrity_level(IntegrityLevel) const;
ThrowCompletionOr<MarkedValueList> enumerable_own_property_names(PropertyKind kind) const;
ThrowCompletionOr<Object*> copy_data_properties(Value source, HashTable<PropertyKey, PropertyNameTraits> const& seen_names, GlobalObject& global_object);
ThrowCompletionOr<Object*> copy_data_properties(Value source, HashTable<PropertyKey> const& seen_names, GlobalObject& global_object);
PrivateElement* private_element_find(PrivateName const& name);
ThrowCompletionOr<void> private_field_add(PrivateName const& name, Value value);

View file

@ -189,8 +189,13 @@ private:
Symbol* m_symbol { nullptr };
};
struct PropertyNameTraits : public Traits<PropertyKey> {
static unsigned hash(PropertyKey const& name)
}
namespace AK {
template<>
struct Traits<JS::PropertyKey> : public GenericTraits<JS::PropertyKey> {
static unsigned hash(JS::PropertyKey const& name)
{
VERIFY(name.is_valid());
if (name.is_string())
@ -200,17 +205,17 @@ struct PropertyNameTraits : public Traits<PropertyKey> {
return ptr_hash(name.as_symbol());
}
static bool equals(PropertyKey const& a, PropertyKey const& b)
static bool equals(JS::PropertyKey const& a, JS::PropertyKey const& b)
{
if (a.type() != b.type())
return false;
switch (a.type()) {
case PropertyKey::Type::Number:
case JS::PropertyKey::Type::Number:
return a.as_number() == b.as_number();
case PropertyKey::Type::String:
case JS::PropertyKey::Type::String:
return a.as_string() == b.as_string();
case PropertyKey::Type::Symbol:
case JS::PropertyKey::Type::Symbol:
return a.as_symbol() == b.as_symbol();
default:
VERIFY_NOT_REACHED();
@ -218,10 +223,6 @@ struct PropertyNameTraits : public Traits<PropertyKey> {
}
};
}
namespace AK {
template<>
struct Formatter<JS::PropertyKey> : Formatter<StringView> {
void format(FormatBuilder& builder, JS::PropertyKey const& property_name)

View file

@ -219,7 +219,7 @@ ThrowCompletionOr<void> VM::property_binding_initialization(BindingPattern const
{
auto* object = TRY(value.to_object(global_object));
HashTable<PropertyKey, PropertyNameTraits> seen_names;
HashTable<PropertyKey> seen_names;
for (auto& property : binding.entries) {
VERIFY(!property.is_elision());

View file

@ -279,7 +279,7 @@ private:
[[nodiscard]] ThrowCompletionOr<Value> call_internal(FunctionObject&, Value this_value, Optional<MarkedValueList> arguments);
ThrowCompletionOr<Object*> copy_data_properties(Object& rest_object, Object const& source, HashTable<PropertyKey, PropertyNameTraits> const& seen_names, GlobalObject& global_object);
ThrowCompletionOr<Object*> copy_data_properties(Object& rest_object, Object const& source, HashTable<PropertyKey> const& seen_names, GlobalObject& global_object);
ThrowCompletionOr<void> property_binding_initialization(BindingPattern const& binding, Value value, Environment* environment, GlobalObject& global_object);
ThrowCompletionOr<void> iterator_binding_initialization(BindingPattern const& binding, Object* iterator, bool& iterator_done, Environment* environment, GlobalObject& global_object);