Browse Source

clean up the rest of the math methods (#523)

* clean up the rest of the math methods

* wrap match with Ok(Value::from(...))

* cargo fmt
pull/527/head
n14little 4 years ago committed by GitHub
parent
commit
3fe894273c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 120
      boa/src/builtins/math/mod.rs

120
boa/src/builtins/math/mod.rs

@ -134,12 +134,11 @@ impl Math {
/// [spec]: https://tc39.es/ecma262/#sec-math.atan2 /// [spec]: https://tc39.es/ecma262/#sec-math.atan2
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/atan2 /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/atan2
pub(crate) fn atan2(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue { pub(crate) fn atan2(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
Ok(Value::from(if args.is_empty() { Ok(match (args.get(0), args.get(1)) {
f64::NAN (Some(y), Some(x)) => f64::from(y).atan2(f64::from(x)),
} else { _ => f64::NAN,
f64::from(args.get(0).expect("Could not get argument")) }
.atan2(args.get(1).expect("Could not get argument").to_number()) .into())
}))
} }
/// Get the cubic root of a number. /// Get the cubic root of a number.
@ -226,17 +225,17 @@ impl Math {
/// [spec]: https://tc39.es/ecma262/#sec-math.log /// [spec]: https://tc39.es/ecma262/#sec-math.log
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log
pub(crate) fn log(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue { pub(crate) fn log(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
Ok(Value::from(if args.is_empty() { Ok(args
f64::NAN .get(0)
} else { .map_or(f64::NAN, |value| {
let value = f64::from(args.get(0).expect("Could not get argument")); let x = f64::from(value);
if x <= 0.0 {
if value <= 0.0 { f64::NAN
f64::NAN } else {
} else { x.log(f64::consts::E)
value.log(f64::consts::E) }
} })
})) .into())
} }
/// Get the base 10 logarithm of the number. /// Get the base 10 logarithm of the number.
@ -248,17 +247,18 @@ impl Math {
/// [spec]: https://tc39.es/ecma262/#sec-math.log10 /// [spec]: https://tc39.es/ecma262/#sec-math.log10
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log10 /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log10
pub(crate) fn log10(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue { pub(crate) fn log10(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
Ok(Value::from(if args.is_empty() { Ok(args
f64::NAN .get(0)
} else { .map_or(f64::NAN, |value| {
let value = f64::from(args.get(0).expect("Could not get argument")); let x = f64::from(value);
if value <= 0.0 { if x <= 0.0 {
f64::NAN f64::NAN
} else { } else {
value.log10() x.log10()
} }
})) })
.into())
} }
/// Get the base 2 logarithm of the number. /// Get the base 2 logarithm of the number.
@ -270,17 +270,18 @@ impl Math {
/// [spec]: https://tc39.es/ecma262/#sec-math.log2 /// [spec]: https://tc39.es/ecma262/#sec-math.log2
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log2 /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log2
pub(crate) fn log2(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue { pub(crate) fn log2(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
Ok(Value::from(if args.is_empty() { Ok(args
f64::NAN .get(0)
} else { .map_or(f64::NAN, |value| {
let value = f64::from(args.get(0).expect("Could not get argument")); let x = f64::from(value);
if value <= 0.0 { if x <= 0.0 {
f64::NAN f64::NAN
} else { } else {
value.log2() x.log2()
} }
})) })
.into())
} }
/// Get the maximum of several numbers. /// Get the maximum of several numbers.
@ -309,12 +310,12 @@ impl Math {
/// [spec]: https://tc39.es/ecma262/#sec-math.min /// [spec]: https://tc39.es/ecma262/#sec-math.min
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/min /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/min
pub(crate) fn min(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue { pub(crate) fn min(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
let mut max = f64::INFINITY; let mut min = f64::INFINITY;
for arg in args { for arg in args {
let num = f64::from(arg); let num = f64::from(arg);
max = max.min(num); min = min.min(num);
} }
Ok(Value::from(max)) Ok(Value::from(min))
} }
/// Raise a number to a power. /// Raise a number to a power.
@ -326,13 +327,11 @@ impl Math {
/// [spec]: https://tc39.es/ecma262/#sec-math.pow /// [spec]: https://tc39.es/ecma262/#sec-math.pow
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/pow /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/pow
pub(crate) fn pow(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue { pub(crate) fn pow(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
Ok(Value::from(if args.len() >= 2 { Ok(match (args.get(0), args.get(1)) {
let num = f64::from(args.get(0).expect("Could not get argument")); (Some(base), Some(exponent)) => f64::from(base).powf(f64::from(exponent)),
let power = f64::from(args.get(1).expect("Could not get argument")); _ => f64::NAN,
num.powf(power) }
} else { .into())
f64::NAN
}))
} }
/// Generate a random floating-point number between `0` and `1`. /// Generate a random floating-point number between `0` and `1`.
@ -371,17 +370,18 @@ impl Math {
/// [spec]: https://tc39.es/ecma262/#sec-math.sign /// [spec]: https://tc39.es/ecma262/#sec-math.sign
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sign /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sign
pub(crate) fn sign(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue { pub(crate) fn sign(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
Ok(Value::from(if args.is_empty() { Ok(args
f64::NAN .get(0)
} else { .map_or(f64::NAN, |value| {
let value = f64::from(args.get(0).expect("Could not get argument")); let x = f64::from(value);
if value == 0.0 || value == -0.0 { if x == 0.0 || x == -0.0 {
value x
} else { } else {
value.signum() x.signum()
} }
})) })
.into())
} }
/// Get the sine of a number. /// Get the sine of a number.

Loading…
Cancel
Save