| 
									
										
										
										
											2022-02-12 00:48:23 -08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2024-10-04 13:19:50 +02:00
										 |  |  |  * Copyright (c) 2020-2021, Andreas Kling <andreas@ladybird.org> | 
					
						
							| 
									
										
										
										
											2022-02-12 00:48:23 -08:00
										 |  |  |  * Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org> | 
					
						
							| 
									
										
										
										
											2022-04-02 15:10:53 +03:00
										 |  |  |  * Copyright (c) 2020-2022, Idan Horowitz <idan.horowitz@serenityos.org> | 
					
						
							| 
									
										
										
										
											2022-02-12 00:48:23 -08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-28 11:26:03 -05:00
										 |  |  | #include <LibJS/Runtime/BigInt.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-12 00:48:23 -08:00
										 |  |  | #include <LibJS/Runtime/PrimitiveString.h>
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/Value.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace JS { | 
					
						
							|  |  |  | struct ValueTraits : public Traits<Value> { | 
					
						
							|  |  |  |     static unsigned hash(Value value) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         VERIFY(!value.is_empty()); | 
					
						
							| 
									
										
										
										
											2025-03-28 13:55:39 +00:00
										 |  |  |         if (value.is_string()) | 
					
						
							|  |  |  |             return value.as_string().utf8_string().hash(); | 
					
						
							| 
									
										
										
										
											2022-02-12 00:48:23 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (value.is_bigint()) | 
					
						
							|  |  |  |             return value.as_bigint().big_integer().hash(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-02 15:10:53 +03:00
										 |  |  |         // In the IEEE 754 standard a NaN value is encoded as any value from 0x7ff0000000000001 to 0x7fffffffffffffff,
 | 
					
						
							|  |  |  |         // with the least significant bits (referred to as the 'payload') carrying some kind of diagnostic information
 | 
					
						
							|  |  |  |         // indicating the source of the NaN. Since ECMA262 does not differentiate between different kinds of NaN values,
 | 
					
						
							|  |  |  |         // Sets and Maps must not differentiate between them either.
 | 
					
						
							|  |  |  |         // This is achieved by replacing any NaN value by a canonical qNaN.
 | 
					
						
							| 
									
										
										
										
											2024-07-09 15:24:28 -04:00
										 |  |  |         if (value.is_nan()) | 
					
						
							| 
									
										
										
										
											2022-04-02 15:10:53 +03:00
										 |  |  |             value = js_nan(); | 
					
						
							| 
									
										
										
										
											2022-02-12 00:48:23 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return u64_hash(value.encoded()); // FIXME: Is this the best way to hash pointers, doubles & ints?
 | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-07-09 15:24:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-18 15:32:56 -04:00
										 |  |  |     static bool equals(Value const a, Value const b) | 
					
						
							| 
									
										
										
										
											2022-02-12 00:48:23 -08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-07-09 15:24:28 -04:00
										 |  |  |         return same_value(a, b); | 
					
						
							| 
									
										
										
										
											2022-02-12 00:48:23 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |