Skip to content

Commit

Permalink
Fix remainder by zero (#882)
Browse files Browse the repository at this point in the history
* Fix remainder by zero

* Address review comments
  • Loading branch information
georgeroman authored Oct 18, 2020
1 parent 71bc080 commit 580c7f1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
6 changes: 6 additions & 0 deletions boa/src/builtins/bigint/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,9 @@ fn division_by_zero() {
let mut engine = Context::new();
assert_throws(&mut engine, "1n/0n", "RangeError");
}

#[test]
fn remainder_by_zero() {
let mut engine = Context::new();
assert_throws(&mut engine, "1n % 0n", "RangeError");
}
11 changes: 10 additions & 1 deletion boa/src/value/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,21 @@ impl Value {
pub fn rem(&self, other: &Self, ctx: &mut Context) -> Result<Value> {
Ok(match (self, other) {
// Fast path:
(Self::Integer(x), Self::Integer(y)) => Self::integer(x % *y),
(Self::Integer(x), Self::Integer(y)) => {
if *y == 0 {
Self::nan()
} else {
Self::integer(x % *y)
}
}
(Self::Rational(x), Self::Rational(y)) => Self::rational(x % y),
(Self::Integer(x), Self::Rational(y)) => Self::rational(f64::from(*x) % y),
(Self::Rational(x), Self::Integer(y)) => Self::rational(x % f64::from(*y)),

(Self::BigInt(ref a), Self::BigInt(ref b)) => {
if *b.as_inner() == BigInt::from(0) {
return ctx.throw_range_error("BigInt division by zero");
}
Self::bigint(a.as_inner().clone() % b.as_inner().clone())
}

Expand Down
18 changes: 18 additions & 0 deletions boa/src/value/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,24 @@ fn sub_string_and_number_object() {
assert!(value.is_nan());
}

#[test]
fn div_by_zero() {
let mut engine = Context::new();

let value = forward_val(&mut engine, "1 / 0").unwrap();
let value = value.to_number(&mut engine).unwrap();
assert!(value.is_infinite());
}

#[test]
fn rem_by_zero() {
let mut engine = Context::new();

let value = forward_val(&mut engine, "1 % 0").unwrap();
let value = value.to_number(&mut engine).unwrap();
assert!(value.is_nan());
}

#[test]
fn bitand_integer_and_integer() {
let mut engine = Context::new();
Expand Down

0 comments on commit 580c7f1

Please sign in to comment.