1use super::JsValue;
2use crate::JsVariant;
3use crate::builtins::Number;
4use std::hash::{Hash, Hasher};
5
6impl PartialEq for JsValue {
7 fn eq(&self, other: &Self) -> bool {
8 Self::same_value_zero(self, other)
9 }
10}
11
12impl Eq for JsValue {}
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
15struct UndefinedHashable;
16
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
18struct NullHashable;
19
20#[derive(Debug, Clone, Copy)]
21struct RationalHashable(f64);
22
23impl PartialEq for RationalHashable {
24 #[inline]
25 fn eq(&self, other: &Self) -> bool {
26 Number::same_value(self.0, other.0)
27 }
28}
29
30impl Eq for RationalHashable {}
31
32impl Hash for RationalHashable {
33 fn hash<H: Hasher>(&self, state: &mut H) {
34 self.0.to_bits().hash(state);
35 }
36}
37
38impl Hash for JsValue {
39 fn hash<H: Hasher>(&self, state: &mut H) {
40 match self.variant() {
41 JsVariant::Undefined => UndefinedHashable.hash(state),
42 JsVariant::Null => NullHashable.hash(state),
43 JsVariant::String(string) => string.hash(state),
44 JsVariant::Boolean(boolean) => boolean.hash(state),
45 JsVariant::Integer32(integer) => RationalHashable(f64::from(integer)).hash(state),
46 JsVariant::BigInt(bigint) => bigint.hash(state),
47 JsVariant::Float64(rational) => RationalHashable(rational).hash(state),
48 JsVariant::Symbol(symbol) => Hash::hash(&symbol, state),
49 JsVariant::Object(object) => object.hash(state),
50 }
51 }
52}