Browse Source

Fix `BigInt` and `Number` comparison (#1887)

Fixes `BigInt` and `Number` comparison, and vice versa. Before we were removing the decimal point of the floating-point number which was causing cases like `0.000001 > 0n` (or `0n < 0.000001`) to fail.
pull/1943/head
Halid Odat 2 years ago
parent
commit
17a6c8661e
  1. 26
      boa_engine/src/value/operations.rs

26
boa_engine/src/value/operations.rs

@ -536,13 +536,12 @@ impl JsValue {
if y.is_infinite() {
return Ok(y.is_sign_positive().into());
}
let n = if y.is_sign_negative() {
y.floor()
} else {
y.ceil()
};
(*x < JsBigInt::try_from(n).expect("invalid conversion to BigInt"))
.into()
if let Ok(y) = JsBigInt::try_from(y) {
return Ok((*x < y).into());
}
(x.to_f64() < y).into()
}
(Numeric::Number(x), Numeric::BigInt(ref y)) => {
if x.is_nan() {
@ -551,13 +550,12 @@ impl JsValue {
if x.is_infinite() {
return Ok(x.is_sign_negative().into());
}
let n = if x.is_sign_negative() {
x.floor()
} else {
x.ceil()
};
(JsBigInt::try_from(n).expect("invalid conversion to BigInt") < *y)
.into()
if let Ok(x) = JsBigInt::try_from(x) {
return Ok((x < *y).into());
}
(x < y.to_f64()).into()
}
},
}

Loading…
Cancel
Save