boa_engine/value/equality.rs
1use super::{JsBigInt, JsObject, JsResult, JsValue, PreferredType};
2use crate::{Context, JsVariant, builtins::Number};
3use std::collections::HashSet;
4
5impl JsValue {
6 /// Inner loop of the deep equality comparison, strict.
7 pub(crate) fn deep_strict_equals_inner(
8 &self,
9 other: &Self,
10 encounters: &mut HashSet<usize>,
11 context: &mut Context,
12 ) -> JsResult<bool> {
13 match (self.as_object(), other.as_object()) {
14 (None, None) => Ok(self.strict_equals(other)),
15 (Some(x), Some(y)) => JsObject::deep_strict_equals_inner(&x, &y, encounters, context),
16 _ => Ok(false),
17 }
18 }
19
20 /// Deep strict equality.
21 ///
22 /// If the value is an object/array, also compare the key-values.
23 /// It uses `strict_equals()` for non-object values.
24 pub fn deep_strict_equals(&self, other: &Self, context: &mut Context) -> JsResult<bool> {
25 self.deep_strict_equals_inner(other, &mut HashSet::new(), context)
26 }
27
28 /// Strict equality comparison.
29 ///
30 /// This method is executed when doing strict equality comparisons with the `===` operator.
31 /// For more information, check <https://tc39.es/ecma262/#sec-strict-equality-comparison>.
32 #[must_use]
33 pub fn strict_equals(&self, other: &Self) -> bool {
34 // 1. If Type(x) is different from Type(y), return false.
35 if self.get_type() != other.get_type() {
36 return false;
37 }
38
39 match (self.variant(), other.variant()) {
40 // 2. If Type(x) is Number or BigInt, then
41 // a. Return ! Type(x)::equal(x, y).
42 (JsVariant::BigInt(x), JsVariant::BigInt(y)) => JsBigInt::equal(&x, &y),
43 (JsVariant::Float64(x), JsVariant::Float64(y)) => Number::equal(x, y),
44 (JsVariant::Float64(x), JsVariant::Integer32(y)) => Number::equal(x, f64::from(y)),
45 (JsVariant::Integer32(x), JsVariant::Float64(y)) => Number::equal(f64::from(x), y),
46 (JsVariant::Integer32(x), JsVariant::Integer32(y)) => x == y,
47
48 //Null has to be handled specially because "typeof null" returns object and if we managed
49 //this without a special case we would compare self and other as if they were actually
50 //objects which unfortunately fails
51 //Specification Link: https://tc39.es/ecma262/#sec-typeof-operator
52 (JsVariant::Null, JsVariant::Null) => true,
53
54 // 3. Return ! SameValueNonNumeric(x, y).
55 (_, _) => Self::same_value_non_numeric(self, other),
56 }
57 }
58
59 /// Abstract equality comparison.
60 ///
61 /// This method is executed when doing abstract equality comparisons with the `==` operator.
62 /// For more information, check <https://tc39.es/ecma262/#sec-abstract-equality-comparison>
63 #[allow(clippy::float_cmp)]
64 pub fn equals(&self, other: &Self, context: &mut Context) -> JsResult<bool> {
65 // 1. If Type(x) is the same as Type(y), then
66 // a. Return the result of performing Strict Equality Comparison x === y.
67 if self.get_type() == other.get_type() {
68 return Ok(self.strict_equals(other));
69 }
70
71 Ok(match (self.variant(), other.variant()) {
72 // 2. If x is null and y is undefined, return true.
73 // 3. If x is undefined and y is null, return true.
74 (JsVariant::Null, JsVariant::Undefined) | (JsVariant::Undefined, JsVariant::Null) => {
75 true
76 }
77
78 // 3. If Type(x) is Number and Type(y) is String, return the result of the comparison x == ! ToNumber(y).
79 // 4. If Type(x) is String and Type(y) is Number, return the result of the comparison ! ToNumber(x) == y.
80 //
81 // https://github.com/rust-lang/rust/issues/54883
82 (
83 JsVariant::Integer32(_) | JsVariant::Float64(_),
84 JsVariant::String(_) | JsVariant::Boolean(_),
85 )
86 | (JsVariant::String(_), JsVariant::Integer32(_) | JsVariant::Float64(_)) => {
87 let x = self.to_number(context)?;
88 let y = other.to_number(context)?;
89 Number::equal(x, y)
90 }
91
92 // 6. If Type(x) is BigInt and Type(y) is String, then
93 // a. Let n be ! StringToBigInt(y).
94 // b. If n is NaN, return false.
95 // c. Return the result of the comparison x == n.
96 (JsVariant::BigInt(a), JsVariant::String(b)) => JsBigInt::from_js_string(&b) == Some(a),
97
98 // 7. If Type(x) is String and Type(y) is BigInt, return the result of the comparison y == x.
99 (JsVariant::String(a), JsVariant::BigInt(b)) => JsBigInt::from_js_string(&a) == Some(b),
100
101 // 8. If Type(x) is Boolean, return the result of the comparison ! ToNumber(x) == y.
102 (JsVariant::Boolean(x), _) => {
103 return other.equals(&JsValue::new(i32::from(x)), context);
104 }
105
106 // 9. If Type(y) is Boolean, return the result of the comparison x == ! ToNumber(y).
107 (_, JsVariant::Boolean(y)) => return self.equals(&JsValue::new(i32::from(y)), context),
108
109 // 10. If Type(x) is either String, Number, BigInt, or Symbol and Type(y) is Object, return the result
110 // of the comparison x == ? ToPrimitive(y).
111 (
112 JsVariant::Object(_),
113 JsVariant::String(_)
114 | JsVariant::Float64(_)
115 | JsVariant::Integer32(_)
116 | JsVariant::BigInt(_)
117 | JsVariant::Symbol(_),
118 ) => {
119 let primitive = self.to_primitive(context, PreferredType::Default)?;
120 return Ok(primitive
121 .equals(other, context)
122 .expect("should not fail according to spec"));
123 }
124
125 // 11. If Type(x) is Object and Type(y) is either String, Number, BigInt, or Symbol, return the result
126 // of the comparison ? ToPrimitive(x) == y.
127 (
128 JsVariant::String(_)
129 | JsVariant::Float64(_)
130 | JsVariant::Integer32(_)
131 | JsVariant::BigInt(_)
132 | JsVariant::Symbol(_),
133 JsVariant::Object(_),
134 ) => {
135 let primitive = other.to_primitive(context, PreferredType::Default)?;
136 return Ok(primitive
137 .equals(self, context)
138 .expect("should not fail according to spec"));
139 }
140
141 // 12. If Type(x) is BigInt and Type(y) is Number, or if Type(x) is Number and Type(y) is BigInt, then
142 // a. If x or y are any of NaN, +∞, or -∞, return false.
143 // b. If the mathematical value of x is equal to the mathematical value of y, return true; otherwise return false.
144 (JsVariant::BigInt(a), JsVariant::Float64(b)) => a == b,
145 (JsVariant::Float64(a), JsVariant::BigInt(b)) => a == b,
146 (JsVariant::BigInt(a), JsVariant::Integer32(b)) => a == b,
147 (JsVariant::Integer32(a), JsVariant::BigInt(b)) => a == b,
148
149 // 13. Return false.
150 _ => false,
151 })
152 }
153
154 /// The internal comparison abstract operation SameValue(x, y),
155 /// where x and y are ECMAScript language values, produces true or false.
156 ///
157 /// More information:
158 /// - [ECMAScript][spec]
159 ///
160 /// [spec]: https://tc39.es/ecma262/#sec-samevalue
161 #[must_use]
162 pub fn same_value(x: &Self, y: &Self) -> bool {
163 // 1. If Type(x) is different from Type(y), return false.
164 if x.get_type() != y.get_type() {
165 return false;
166 }
167
168 match (x.variant(), y.variant()) {
169 // 2. If Type(x) is Number or BigInt, then
170 // a. Return ! Type(x)::SameValue(x, y).
171 (JsVariant::BigInt(x), JsVariant::BigInt(y)) => JsBigInt::same_value(&x, &y),
172 (JsVariant::Float64(x), JsVariant::Float64(y)) => Number::same_value(x, y),
173 (JsVariant::Float64(x), JsVariant::Integer32(y)) => Number::same_value(x, f64::from(y)),
174 (JsVariant::Integer32(x), JsVariant::Float64(y)) => Number::same_value(f64::from(x), y),
175 (JsVariant::Integer32(x), JsVariant::Integer32(y)) => x == y,
176
177 // 3. Return ! SameValueNonNumeric(x, y).
178 (_, _) => Self::same_value_non_numeric(x, y),
179 }
180 }
181
182 /// The internal comparison abstract operation `SameValueZero(x, y)`,
183 /// where `x` and `y` are ECMAScript language values, produces `true` or `false`.
184 ///
185 /// `SameValueZero` differs from `SameValue` only in its treatment of `+0` and `-0`.
186 ///
187 /// More information:
188 /// - [ECMAScript][spec]
189 ///
190 /// [spec]: https://tc39.es/ecma262/#sec-samevaluezero
191 #[must_use]
192 pub fn same_value_zero(x: &Self, y: &Self) -> bool {
193 if x.get_type() != y.get_type() {
194 return false;
195 }
196
197 match (x.variant(), y.variant()) {
198 // 2. If Type(x) is Number or BigInt, then
199 // a. Return ! Type(x)::SameValueZero(x, y).
200 (JsVariant::BigInt(x), JsVariant::BigInt(y)) => JsBigInt::same_value_zero(&x, &y),
201
202 (JsVariant::Float64(x), JsVariant::Float64(y)) => Number::same_value_zero(x, y),
203 (JsVariant::Float64(x), JsVariant::Integer32(y)) => {
204 Number::same_value_zero(x, f64::from(y))
205 }
206 (JsVariant::Integer32(x), JsVariant::Float64(y)) => {
207 Number::same_value_zero(f64::from(x), y)
208 }
209 (JsVariant::Integer32(x), JsVariant::Integer32(y)) => x == y,
210
211 // 3. Return ! SameValueNonNumeric(x, y).
212 (_, _) => Self::same_value_non_numeric(x, y),
213 }
214 }
215
216 fn same_value_non_numeric(x: &Self, y: &Self) -> bool {
217 debug_assert!(x.get_type() == y.get_type());
218 match (x.variant(), y.variant()) {
219 (JsVariant::Null, JsVariant::Null) | (JsVariant::Undefined, JsVariant::Undefined) => {
220 true
221 }
222 (JsVariant::String(x), JsVariant::String(y)) => x == y,
223 (JsVariant::Boolean(x), JsVariant::Boolean(y)) => x == y,
224 (JsVariant::Object(x), JsVariant::Object(y)) => JsObject::equals(&x, &y),
225 (JsVariant::Symbol(x), JsVariant::Symbol(y)) => x == y,
226 _ => false,
227 }
228 }
229}