Skip to content

Commit

Permalink
Moved Interpreter::to_numeric_number() to Value::to_numeric_number()
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat committed Aug 12, 2020
1 parent ff82661 commit 9ae1dc7
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 21 deletions.
2 changes: 1 addition & 1 deletion boa/src/builtins/date/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion boa/src/builtins/number/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
13 changes: 13 additions & 0 deletions boa/src/builtins/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<f64, Value> {
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 {
Expand Down
6 changes: 3 additions & 3 deletions boa/src/builtins/value/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}),
Expand All @@ -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<bool, Value> {
Ok(!self.to_boolean())
}

/// Abstract relational comparison
Expand Down
14 changes: 0 additions & 14 deletions boa/src/exec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<f64, Value> {
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
Expand Down
3 changes: 1 addition & 2 deletions boa/src/exec/operator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 9ae1dc7

Please sign in to comment.