From f9d58ec0b444e7e4ff2f4cd443f8f046e5abf9ae Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Sat, 12 Jun 2021 23:15:19 +0300 Subject: [PATCH] LibJS: Move ValueTraits to Value.h and add special case for -0.0 This will allow us to use these traits for other hash-based containers (like Map). This commit also adds a special case for negative zero values, because while the equality check used same_value_zero which is negative/positive zero insensitive, the hash was not. --- Userland/Libraries/LibJS/Runtime/Set.h | 19 ------------------- Userland/Libraries/LibJS/Runtime/Value.h | 22 ++++++++++++++++++++++ 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/Set.h b/Userland/Libraries/LibJS/Runtime/Set.h index 0e17fa7196..4f3fc0cf68 100644 --- a/Userland/Libraries/LibJS/Runtime/Set.h +++ b/Userland/Libraries/LibJS/Runtime/Set.h @@ -7,31 +7,12 @@ #pragma once #include -#include #include #include #include namespace JS { -struct ValueTraits : public Traits { - static unsigned hash(Value value) - { - VERIFY(!value.is_empty()); - if (value.is_string()) - return value.as_string().string().hash(); - - if (value.is_bigint()) - return value.as_bigint().big_integer().hash(); - - return u64_hash(value.encoded()); // FIXME: Is this the best way to hash pointers, doubles & ints? - } - static bool equals(const Value a, const Value b) - { - return same_value_zero(a, b); - } -}; - class Set : public Object { JS_OBJECT(Set, Object); diff --git a/Userland/Libraries/LibJS/Runtime/Value.h b/Userland/Libraries/LibJS/Runtime/Value.h index 654992c5f0..8a158d02d9 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.h +++ b/Userland/Libraries/LibJS/Runtime/Value.h @@ -16,6 +16,7 @@ #include #include #include +#include #include // 2 ** 53 - 1 @@ -376,6 +377,27 @@ Function* species_constructor(GlobalObject&, const Object&, Function& default_co Value require_object_coercible(GlobalObject&, Value); MarkedValueList create_list_from_array_like(GlobalObject&, Value, AK::Function(Value)> = {}); +struct ValueTraits : public Traits { + static unsigned hash(Value value) + { + VERIFY(!value.is_empty()); + if (value.is_string()) + return value.as_string().string().hash(); + + if (value.is_bigint()) + return value.as_bigint().big_integer().hash(); + + if (value.is_negative_zero()) + value = Value(0); + + return u64_hash(value.encoded()); // FIXME: Is this the best way to hash pointers, doubles & ints? + } + static bool equals(const Value a, const Value b) + { + return same_value_zero(a, b); + } +}; + } namespace AK {