Browse Source

Throw RangeError when BigInt division by zero occurs (#790)

pull/795/head
John Doneth 4 years ago committed by GitHub
parent
commit
ff358d1d1d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      boa/src/builtins/bigint/tests.rs
  2. 2
      boa/src/context.rs
  3. 6
      boa/src/value/operations.rs

6
boa/src/builtins/bigint/tests.rs

@ -351,3 +351,9 @@ fn assert_throws(engine: &mut Context, src: &str, error_type: &str) {
let result = forward(engine, src); let result = forward(engine, src);
assert!(result.contains(error_type)); assert!(result.contains(error_type));
} }
#[test]
fn division_by_zero() {
let mut engine = Context::new();
assert_throws(&mut engine, "1n/0n", "RangeError");
}

2
boa/src/context.rs

@ -573,7 +573,7 @@ impl Context {
} }
} }
/// Register a global class of type `T`, where `T` implemets `Class`. /// Register a global class of type `T`, where `T` implements `Class`.
/// ///
/// # Example /// # Example
/// ```ignore /// ```ignore

6
boa/src/value/operations.rs

@ -106,6 +106,9 @@ impl Value {
(Self::Rational(x), Self::Integer(y)) => Self::rational(x / f64::from(*y)), (Self::Rational(x), Self::Integer(y)) => Self::rational(x / f64::from(*y)),
(Self::BigInt(ref a), Self::BigInt(ref b)) => { (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()) Self::bigint(a.as_inner().clone() / b.as_inner().clone())
} }
@ -113,6 +116,9 @@ impl Value {
(_, _) => match (self.to_numeric(ctx)?, other.to_numeric(ctx)?) { (_, _) => match (self.to_numeric(ctx)?, other.to_numeric(ctx)?) {
(Numeric::Number(a), Numeric::Number(b)) => Self::rational(a / b), (Numeric::Number(a), Numeric::Number(b)) => Self::rational(a / b),
(Numeric::BigInt(ref a), Numeric::BigInt(ref b)) => { (Numeric::BigInt(ref a), Numeric::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()) Self::bigint(a.as_inner().clone() / b.as_inner().clone())
} }
(_, _) => { (_, _) => {

Loading…
Cancel
Save