1use super::*;
2
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 #[inline]
34 fn hash<H: Hasher>(&self, state: &mut H) {
35 self.0.to_bits().hash(state);
36 }
37}
38
39impl Hash for JsValue {
40 fn hash<H: Hasher>(&self, state: &mut H) {
41 match self {
42 Self::Undefined => UndefinedHashable.hash(state),
43 Self::Null => NullHashable.hash(state),
44 Self::String(ref string) => string.hash(state),
45 Self::Boolean(boolean) => boolean.hash(state),
46 Self::Integer(integer) => RationalHashable(f64::from(*integer)).hash(state),
47 Self::BigInt(ref bigint) => bigint.hash(state),
48 Self::Rational(rational) => RationalHashable(*rational).hash(state),
49 Self::Symbol(ref symbol) => Hash::hash(symbol, state),
50 Self::Object(ref object) => std::ptr::hash(object.as_ref(), state),
51 }
52 }
53}