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

Add missing Number methods. #562

Merged
merged 5 commits into from
Jul 13, 2020
Merged
Show file tree
Hide file tree
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
Prev Previous commit
Next Next commit
Methods shouldn't allow Number objects.
  • Loading branch information
joshwd36 committed Jul 12, 2020
commit 6fae6f88059ebd0b04a7ea0e8a704bab95c8d5f7
71 changes: 19 additions & 52 deletions boa/src/builtins/number/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,14 +632,6 @@ impl Number {
match val {
Value::Integer(_) => true,
Value::Rational(number) => number.is_finite(),
Value::Object(ref obj) => {
let obj = obj.borrow();
if let Some(number) = obj.as_number() {
number.is_finite()
} else {
false
}
}
_ => false,
}
} else {
Expand All @@ -662,11 +654,7 @@ impl Number {
args: &[Value],
_ctx: &mut Interpreter,
) -> ResultValue {
Ok(Value::Boolean(if let Some(val) = args.get(0) {
Number::is_integer(val).is_some()
} else {
false
}))
Ok(args.get(0).map_or(false, Self::is_integer).into())
joshwd36 marked this conversation as resolved.
Show resolved Hide resolved
}

/// `Number.isNaN( number )`
Expand All @@ -692,14 +680,6 @@ impl Number {
match val {
Value::Integer(_) => false,
Value::Rational(number) => number.is_nan(),
Value::Object(ref obj) => {
let obj = obj.borrow();
if let Some(number) = obj.as_number() {
number.is_nan()
} else {
false
}
}
_ => false,
}
} else {
Expand All @@ -726,15 +706,14 @@ impl Number {
args: &[Value],
_ctx: &mut Interpreter,
) -> ResultValue {
Ok(Value::Boolean(if let Some(val) = args.get(0) {
if let Some(integer) = Number::is_integer(val) {
integer.abs() <= Number::MAX_SAFE_INTEGER
} else {
false
Ok((match args.get(0) {
Some(Value::Integer(_)) => true,
Some(Value::Rational(number)) if Self::is_float_integer(*number) => {
number.abs() <= Number::MAX_SAFE_INTEGER
}
} else {
false
}))
_ => false,
})
.into())
}

/// Checks if the argument is a finite integer Number value.
Expand All @@ -743,33 +722,21 @@ impl Number {
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-isinteger
#[allow(clippy::float_cmp)]
pub(crate) fn is_integer(val: &Value) -> Option<f64> {
pub(crate) fn is_integer(val: &Value) -> bool {
match val {
Value::Integer(number) => Some((*number).into()),
Value::Rational(number) => {
if number.is_finite() && number.abs().floor() == number.abs() {
Some(*number)
} else {
None
}
}
Value::Object(obj) => {
let obj = obj.borrow();
if let Some(number) = obj.as_number() {
if number.abs().floor() == number.abs() {
Some(number)
} else {
None
}
} else {
None
}
}
_ => None,
Value::Integer(_) => true,
Value::Rational(number) => Number::is_float_integer(*number),
_ => false,
}
}

/// Checks if the float argument is an integer.
#[inline]
#[allow(clippy::float_cmp)]
pub(crate) fn is_float_integer(number: f64) -> bool {
number.is_finite() && number.abs().floor() == number.abs()
}

/// Initialise the `Number` object on the global object.
#[inline]
pub(crate) fn init(global: &Value) -> (&str, Value) {
Expand Down
24 changes: 24 additions & 0 deletions boa/src/builtins/number/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,14 @@ fn number_is_finite() {
assert_eq!("false", &forward(&mut engine, "Number.isFinite()"));
assert_eq!("false", &forward(&mut engine, "Number.isFinite({})"));
assert_eq!("true", &forward(&mut engine, "Number.isFinite(Number(5))"));
assert_eq!(
"false",
&forward(&mut engine, "Number.isFinite(new Number(5))")
);
assert_eq!(
"false",
&forward(&mut engine, "Number.isFinite(new Number(NaN))")
);
assert_eq!("false", &forward(&mut engine, "Number.isFinite(BigInt(5))"));
}

Expand Down Expand Up @@ -777,6 +785,10 @@ fn number_is_integer() {
&forward(&mut engine, "Number.isInteger(Number(5.0000000000000001))")
);
assert_eq!("false", &forward(&mut engine, "Number.isInteger()"));
assert_eq!(
"false",
&forward(&mut engine, "Number.isInteger(new Number(5))")
);
}

#[test]
Expand All @@ -803,6 +815,14 @@ fn number_is_nan() {
assert_eq!("false", &forward(&mut engine, "Number.isNaN(Number(5))"));
assert_eq!("true", &forward(&mut engine, "Number.isNaN(Number(NaN))"));
assert_eq!("false", &forward(&mut engine, "Number.isNaN(BigInt(5))"));
assert_eq!(
"false",
&forward(&mut engine, "Number.isNaN(new Number(5))")
);
assert_eq!(
"false",
&forward(&mut engine, "Number.isNaN(new Number(NaN))")
);
}

#[test]
Expand All @@ -827,4 +847,8 @@ fn number_is_safe_integer() {
assert_eq!("false", &forward(&mut engine, "Number.isSafeInteger('3')"));
assert_eq!("false", &forward(&mut engine, "Number.isSafeInteger(3.1)"));
assert_eq!("true", &forward(&mut engine, "Number.isSafeInteger(3.0)"));
assert_eq!(
"false",
&forward(&mut engine, "Number.isSafeInteger(new Number(5))")
);
}