From 3fe894273c1e8b407bf64f4f306e0f5c7b597392 Mon Sep 17 00:00:00 2001 From: n14little Date: Wed, 24 Jun 2020 07:05:45 -0500 Subject: [PATCH] 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 --- boa/src/builtins/math/mod.rs | 120 +++++++++++++++++------------------ 1 file changed, 60 insertions(+), 60 deletions(-) diff --git a/boa/src/builtins/math/mod.rs b/boa/src/builtins/math/mod.rs index 4e6b3f4dbb..152797f7c2 100644 --- a/boa/src/builtins/math/mod.rs +++ b/boa/src/builtins/math/mod.rs @@ -134,12 +134,11 @@ impl Math { /// [spec]: https://tc39.es/ecma262/#sec-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 { - Ok(Value::from(if args.is_empty() { - f64::NAN - } else { - f64::from(args.get(0).expect("Could not get argument")) - .atan2(args.get(1).expect("Could not get argument").to_number()) - })) + Ok(match (args.get(0), args.get(1)) { + (Some(y), Some(x)) => f64::from(y).atan2(f64::from(x)), + _ => f64::NAN, + } + .into()) } /// Get the cubic root of a number. @@ -226,17 +225,17 @@ impl Math { /// [spec]: https://tc39.es/ecma262/#sec-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 { - Ok(Value::from(if args.is_empty() { - f64::NAN - } else { - let value = f64::from(args.get(0).expect("Could not get argument")); - - if value <= 0.0 { - f64::NAN - } else { - value.log(f64::consts::E) - } - })) + Ok(args + .get(0) + .map_or(f64::NAN, |value| { + let x = f64::from(value); + if x <= 0.0 { + f64::NAN + } else { + x.log(f64::consts::E) + } + }) + .into()) } /// Get the base 10 logarithm of the number. @@ -248,17 +247,18 @@ impl Math { /// [spec]: https://tc39.es/ecma262/#sec-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 { - Ok(Value::from(if args.is_empty() { - f64::NAN - } else { - let value = f64::from(args.get(0).expect("Could not get argument")); - - if value <= 0.0 { - f64::NAN - } else { - value.log10() - } - })) + Ok(args + .get(0) + .map_or(f64::NAN, |value| { + let x = f64::from(value); + + if x <= 0.0 { + f64::NAN + } else { + x.log10() + } + }) + .into()) } /// Get the base 2 logarithm of the number. @@ -270,17 +270,18 @@ impl Math { /// [spec]: https://tc39.es/ecma262/#sec-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 { - Ok(Value::from(if args.is_empty() { - f64::NAN - } else { - let value = f64::from(args.get(0).expect("Could not get argument")); - - if value <= 0.0 { - f64::NAN - } else { - value.log2() - } - })) + Ok(args + .get(0) + .map_or(f64::NAN, |value| { + let x = f64::from(value); + + if x <= 0.0 { + f64::NAN + } else { + x.log2() + } + }) + .into()) } /// Get the maximum of several numbers. @@ -309,12 +310,12 @@ impl Math { /// [spec]: https://tc39.es/ecma262/#sec-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 { - let mut max = f64::INFINITY; + let mut min = f64::INFINITY; for arg in args { 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. @@ -326,13 +327,11 @@ impl Math { /// [spec]: https://tc39.es/ecma262/#sec-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 { - Ok(Value::from(if args.len() >= 2 { - let num = f64::from(args.get(0).expect("Could not get argument")); - let power = f64::from(args.get(1).expect("Could not get argument")); - num.powf(power) - } else { - f64::NAN - })) + Ok(match (args.get(0), args.get(1)) { + (Some(base), Some(exponent)) => f64::from(base).powf(f64::from(exponent)), + _ => f64::NAN, + } + .into()) } /// Generate a random floating-point number between `0` and `1`. @@ -371,17 +370,18 @@ impl Math { /// [spec]: https://tc39.es/ecma262/#sec-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 { - Ok(Value::from(if args.is_empty() { - f64::NAN - } else { - let value = f64::from(args.get(0).expect("Could not get argument")); - - if value == 0.0 || value == -0.0 { - value - } else { - value.signum() - } - })) + Ok(args + .get(0) + .map_or(f64::NAN, |value| { + let x = f64::from(value); + + if x == 0.0 || x == -0.0 { + x + } else { + x.signum() + } + }) + .into()) } /// Get the sine of a number.