diff --git a/boa/src/builtins/bigint/tests.rs b/boa/src/builtins/bigint/tests.rs index e190496ecbc..0a7f21113cb 100644 --- a/boa/src/builtins/bigint/tests.rs +++ b/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); assert!(result.contains(error_type)); } + +#[test] +fn division_by_zero() { + let mut engine = Context::new(); + assert_throws(&mut engine, "1n/0n", "RangeError"); +} diff --git a/boa/src/context.rs b/boa/src/context.rs index d6b95188dfe..f71e2bf3939 100644 --- a/boa/src/context.rs +++ b/boa/src/context.rs @@ -443,7 +443,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 /// ```ignore diff --git a/boa/src/value/operations.rs b/boa/src/value/operations.rs index b8c5153af92..c4d788b17df 100644 --- a/boa/src/value/operations.rs +++ b/boa/src/value/operations.rs @@ -106,6 +106,9 @@ impl Value { (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()) } @@ -113,6 +116,9 @@ impl Value { (_, _) => match (self.to_numeric(ctx)?, other.to_numeric(ctx)?) { (Numeric::Number(a), Numeric::Number(b)) => Self::rational(a / 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()) } (_, _) => {