From 9ae1dc7ce34586fbff34b3e57c9ffb446a3e70dc Mon Sep 17 00:00:00 2001 From: HalidOdat Date: Wed, 12 Aug 2020 19:57:17 +0200 Subject: [PATCH] Moved `Interpreter::to_numeric_number()` to `Value::to_numeric_number()` --- boa/src/builtins/date/mod.rs | 2 +- boa/src/builtins/number/mod.rs | 2 +- boa/src/builtins/value/mod.rs | 13 +++++++++++++ boa/src/builtins/value/operations.rs | 6 +++--- boa/src/exec/mod.rs | 14 -------------- boa/src/exec/operator/mod.rs | 3 +-- 6 files changed, 19 insertions(+), 21 deletions(-) diff --git a/boa/src/builtins/date/mod.rs b/boa/src/builtins/date/mod.rs index 4f2ce9c8661..06d62a8f4fe 100644 --- a/boa/src/builtins/date/mod.rs +++ b/boa/src/builtins/date/mod.rs @@ -62,7 +62,7 @@ macro_rules! setter_method { args .get($e) .and_then(|value| { - ctx.to_numeric_number(value).map_or_else( + value.to_numeric_number(ctx).map_or_else( |_| None, |value| { if value == 0f64 || value.is_normal() { diff --git a/boa/src/builtins/number/mod.rs b/boa/src/builtins/number/mod.rs index a8dd52f5404..ef5c874e2e6 100644 --- a/boa/src/builtins/number/mod.rs +++ b/boa/src/builtins/number/mod.rs @@ -135,7 +135,7 @@ impl Number { /// `[[Call]]` - Creates a number primitive pub(crate) fn make_number(this: &Value, args: &[Value], ctx: &mut Interpreter) -> ResultValue { let data = match args.get(0) { - Some(ref value) => ctx.to_numeric_number(value)?, + Some(ref value) => value.to_numeric_number(ctx)?, None => 0.0, }; this.set_data(ObjectData::Number(data)); diff --git a/boa/src/builtins/value/mod.rs b/boa/src/builtins/value/mod.rs index 62d79f43f98..788b7e61c3b 100644 --- a/boa/src/builtins/value/mod.rs +++ b/boa/src/builtins/value/mod.rs @@ -970,6 +970,19 @@ impl Value { } } } + + /// This is a more specialized version of `to_numeric`. + /// + /// It returns value converted to a numeric value of type `Number`. + /// + /// See: https://tc39.es/ecma262/#sec-tonumeric + pub fn to_numeric_number(&self, ctx: &mut Interpreter) -> Result { + let primitive = self.to_primitive(ctx, PreferredType::Number)?; + if let Some(ref bigint) = primitive.as_bigint() { + return Ok(bigint.to_f64()); + } + primitive.to_number(ctx) + } } impl Default for Value { diff --git a/boa/src/builtins/value/operations.rs b/boa/src/builtins/value/operations.rs index d9eee3e6db0..d0a00bd0203 100644 --- a/boa/src/builtins/value/operations.rs +++ b/boa/src/builtins/value/operations.rs @@ -387,7 +387,7 @@ impl Value { pub fn neg(&self, interpreter: &mut Interpreter) -> ResultValue { Ok(match *self { Self::Symbol(_) | Self::Undefined => Self::rational(NAN), - Self::Object(_) => Self::rational(match interpreter.to_numeric_number(self) { + Self::Object(_) => Self::rational(match self.to_numeric_number(interpreter) { Ok(num) => -num, Err(_) => NAN, }), @@ -404,8 +404,8 @@ impl Value { } #[inline] - pub fn not(&self, _: &mut Interpreter) -> ResultValue { - Ok(Self::boolean(!self.to_boolean())) + pub fn not(&self, _: &mut Interpreter) -> Result { + Ok(!self.to_boolean()) } /// Abstract relational comparison diff --git a/boa/src/exec/mod.rs b/boa/src/exec/mod.rs index 05915294f72..5f923ea5c15 100644 --- a/boa/src/exec/mod.rs +++ b/boa/src/exec/mod.rs @@ -187,20 +187,6 @@ impl Interpreter { } } - /// This is a more specialized version of `to_numeric`. - /// - /// It returns value converted to a numeric value of type `Number`. - /// - /// See: https://tc39.es/ecma262/#sec-tonumeric - #[allow(clippy::wrong_self_convention)] - pub(crate) fn to_numeric_number(&mut self, value: &Value) -> Result { - let primitive = value.to_primitive(self, PreferredType::Number)?; - if let Some(ref bigint) = primitive.as_bigint() { - return Ok(bigint.to_f64()); - } - primitive.to_number(self) - } - /// Converts an array object into a rust vector of values. /// /// This is useful for the spread operator, for any other object an `Err` is returned diff --git a/boa/src/exec/operator/mod.rs b/boa/src/exec/operator/mod.rs index 9b6c0ff5532..05efcdfb053 100644 --- a/boa/src/exec/operator/mod.rs +++ b/boa/src/exec/operator/mod.rs @@ -194,10 +194,9 @@ impl Executable for UnaryOp { let result = x.to_number(interpreter)? - 1.0; interpreter.set_value(self.target(), result.into())? } - op::UnaryOp::Not => x.not(interpreter)?, + op::UnaryOp::Not => x.not(interpreter)?.into(), op::UnaryOp::Tilde => { let num_v_a = x.to_number(interpreter)?; - // NOTE: possible UB: https://github.com/rust-lang/rust/issues/10184 Value::from(if num_v_a.is_nan() { -1 } else {