mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 03:37:45 +00:00
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.
This commit is contained in:
parent
3c83298a26
commit
f9d58ec0b4
2 changed files with 22 additions and 19 deletions
|
@ -7,31 +7,12 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <AK/HashTable.h>
|
#include <AK/HashTable.h>
|
||||||
#include <LibJS/Runtime/BigInt.h>
|
|
||||||
#include <LibJS/Runtime/GlobalObject.h>
|
#include <LibJS/Runtime/GlobalObject.h>
|
||||||
#include <LibJS/Runtime/Object.h>
|
#include <LibJS/Runtime/Object.h>
|
||||||
#include <LibJS/Runtime/Value.h>
|
#include <LibJS/Runtime/Value.h>
|
||||||
|
|
||||||
namespace JS {
|
namespace JS {
|
||||||
|
|
||||||
struct ValueTraits : public Traits<Value> {
|
|
||||||
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 {
|
class Set : public Object {
|
||||||
JS_OBJECT(Set, Object);
|
JS_OBJECT(Set, Object);
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
#include <AK/String.h>
|
#include <AK/String.h>
|
||||||
#include <AK/Types.h>
|
#include <AK/Types.h>
|
||||||
#include <LibJS/Forward.h>
|
#include <LibJS/Forward.h>
|
||||||
|
#include <LibJS/Runtime/BigInt.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
// 2 ** 53 - 1
|
// 2 ** 53 - 1
|
||||||
|
@ -376,6 +377,27 @@ Function* species_constructor(GlobalObject&, const Object&, Function& default_co
|
||||||
Value require_object_coercible(GlobalObject&, Value);
|
Value require_object_coercible(GlobalObject&, Value);
|
||||||
MarkedValueList create_list_from_array_like(GlobalObject&, Value, AK::Function<Result<void, ErrorType>(Value)> = {});
|
MarkedValueList create_list_from_array_like(GlobalObject&, Value, AK::Function<Result<void, ErrorType>(Value)> = {});
|
||||||
|
|
||||||
|
struct ValueTraits : public Traits<Value> {
|
||||||
|
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 {
|
namespace AK {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue