/src/serenity/Userland/Libraries/LibJS/Runtime/ValueTraits.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org> |
3 | | * Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org> |
4 | | * Copyright (c) 2020-2022, Idan Horowitz <idan.horowitz@serenityos.org> |
5 | | * |
6 | | * SPDX-License-Identifier: BSD-2-Clause |
7 | | */ |
8 | | |
9 | | #pragma once |
10 | | |
11 | | #include <LibJS/Runtime/BigInt.h> |
12 | | #include <LibJS/Runtime/PrimitiveString.h> |
13 | | #include <LibJS/Runtime/Value.h> |
14 | | |
15 | | namespace JS { |
16 | | struct ValueTraits : public Traits<Value> { |
17 | | static unsigned hash(Value value) |
18 | 0 | { |
19 | 0 | VERIFY(!value.is_empty()); |
20 | 0 | if (value.is_string()) { |
21 | | // FIXME: Propagate this error. |
22 | 0 | return value.as_string().byte_string().hash(); |
23 | 0 | } |
24 | | |
25 | 0 | if (value.is_bigint()) |
26 | 0 | return value.as_bigint().big_integer().hash(); |
27 | | |
28 | | // In the IEEE 754 standard a NaN value is encoded as any value from 0x7ff0000000000001 to 0x7fffffffffffffff, |
29 | | // with the least significant bits (referred to as the 'payload') carrying some kind of diagnostic information |
30 | | // indicating the source of the NaN. Since ECMA262 does not differentiate between different kinds of NaN values, |
31 | | // Sets and Maps must not differentiate between them either. |
32 | | // This is achieved by replacing any NaN value by a canonical qNaN. |
33 | 0 | if (value.is_nan()) |
34 | 0 | value = js_nan(); |
35 | |
|
36 | 0 | return u64_hash(value.encoded()); // FIXME: Is this the best way to hash pointers, doubles & ints? |
37 | 0 | } |
38 | | |
39 | | static bool equals(Value const a, Value const b) |
40 | 0 | { |
41 | 0 | return same_value(a, b); |
42 | 0 | } |
43 | | }; |
44 | | |
45 | | } |