Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Fix exponent operator #2681

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions boa_engine/src/value/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,22 +195,41 @@ impl JsValue {
}

/// Perform the binary `**` operator on the value and return the result.
// NOTE: There are some cases in the spec where we have to compare floats
#[allow(clippy::float_cmp)]
pub fn pow(&self, other: &Self, context: &mut Context<'_>) -> JsResult<Self> {
Ok(match (self, other) {
// Fast path:
(Self::Integer(x), Self::Integer(y)) => u32::try_from(*y)
.ok()
.and_then(|y| x.checked_pow(y))
.map_or_else(|| Self::new(f64::from(*x).powi(*y)), Self::new),
(Self::Rational(x), Self::Rational(y)) => Self::new(x.powf(*y)),
(Self::Integer(x), Self::Rational(y)) => Self::new(f64::from(*x).powf(*y)),
(Self::Rational(x), Self::Rational(y)) => {
if x.abs() == 1.0 && y.is_infinite() {
Self::nan()
} else {
Self::new(x.powf(*y))
}
}
(Self::Integer(x), Self::Rational(y)) => {
if x.abs() == 1 && y.is_infinite() {
jedel1043 marked this conversation as resolved.
Show resolved Hide resolved
Self::nan()
} else {
Self::new(f64::from(*x).powf(*y))
}
}
(Self::Rational(x), Self::Integer(y)) => Self::new(x.powi(*y)),

(Self::BigInt(ref a), Self::BigInt(ref b)) => Self::new(JsBigInt::pow(a, b)?),

// Slow path:
(_, _) => match (self.to_numeric(context)?, other.to_numeric(context)?) {
(Numeric::Number(a), Numeric::Number(b)) => Self::new(a.powf(b)),
(Numeric::Number(a), Numeric::Number(b)) => {
if a.abs() == 1.0 && b.is_infinite() {
Self::nan()
} else {
Self::new(a.powf(b))
}
}
(Numeric::BigInt(ref a), Numeric::BigInt(ref b)) => Self::new(JsBigInt::pow(a, b)?),
(_, _) => {
return Err(JsNativeError::typ()
Expand Down