1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 05:27:45 +00:00

LibJS: Enable storing Value and Handle<Value> in HashMaps

We have the right conversions to make this work, so let's make it
possible to have a `HashMap<JS::Handle<T>, V>` and look for a specific
T inside it without having to create a temporary handle.

This involves adding some operator== implementations, and some
specializations of AK::Traits.
This commit is contained in:
Andrew Kaster 2023-08-21 15:13:18 -06:00 committed by Andreas Kling
parent 685ef4ec82
commit 96600e77c2
2 changed files with 42 additions and 0 deletions

View file

@ -619,6 +619,18 @@ public:
return *this;
}
template<typename O>
ALWAYS_INLINE bool operator==(Optional<O> const& other) const
{
return has_value() == other.has_value() && (!has_value() || value() == other.value());
}
template<typename O>
ALWAYS_INLINE bool operator==(O const& other) const
{
return has_value() && value() == other;
}
void clear()
{
m_value = {};
@ -688,4 +700,9 @@ struct Formatter<JS::Value> : Formatter<StringView> {
}
};
template<>
struct Traits<JS::Value> : GenericTraits<JS::Value> {
static unsigned hash(JS::Value value) { return Traits<u64>::hash(value.encoded()); }
};
}