From 22fdba4ddf989dd5546f036def56ff3446847a1b Mon Sep 17 00:00:00 2001 From: JohnDoneth Date: Sun, 4 Oct 2020 13:31:54 -0400 Subject: [PATCH 1/7] Throw RangeError when BigInt division by zero occurs --- boa/src/builtins/bigint/tests.rs | 7 +++++++ boa/src/syntax/ast/node/operator/bin_op/mod.rs | 10 +++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/boa/src/builtins/bigint/tests.rs b/boa/src/builtins/bigint/tests.rs index e190496ecbc..0a22f911e7e 100644 --- a/boa/src/builtins/bigint/tests.rs +++ b/boa/src/builtins/bigint/tests.rs @@ -351,3 +351,10 @@ 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"); +} \ No newline at end of file diff --git a/boa/src/syntax/ast/node/operator/bin_op/mod.rs b/boa/src/syntax/ast/node/operator/bin_op/mod.rs index db8985eec2a..aee50fffa36 100644 --- a/boa/src/syntax/ast/node/operator/bin_op/mod.rs +++ b/boa/src/syntax/ast/node/operator/bin_op/mod.rs @@ -1,4 +1,5 @@ use crate::{ + builtins::BigInt, exec::Executable, syntax::ast::{ node::Node, @@ -81,12 +82,19 @@ impl Executable for BinOp { op::BinOp::Num(op) => { let x = self.lhs().run(interpreter)?; let y = self.rhs().run(interpreter)?; + match op { NumOp::Add => x.add(&y, interpreter), NumOp::Sub => x.sub(&y, interpreter), NumOp::Mul => x.mul(&y, interpreter), NumOp::Exp => x.pow(&y, interpreter), - NumOp::Div => x.div(&y, interpreter), + NumOp::Div => { + if y == Value::bigint(BigInt::from(0)) { + return interpreter.throw_range_error("BigInt division by zero"); + } + + x.div(&y, interpreter) + } NumOp::Mod => x.rem(&y, interpreter), } } From cfe8b23db120511ed9af490d3598edaac8140251 Mon Sep 17 00:00:00 2001 From: JohnDoneth Date: Sun, 4 Oct 2020 13:36:06 -0400 Subject: [PATCH 2/7] Add newline --- boa/src/builtins/bigint/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boa/src/builtins/bigint/tests.rs b/boa/src/builtins/bigint/tests.rs index 0a22f911e7e..94a5d666095 100644 --- a/boa/src/builtins/bigint/tests.rs +++ b/boa/src/builtins/bigint/tests.rs @@ -357,4 +357,4 @@ fn division_by_zero() { let mut engine = Context::new(); assert_throws(&mut engine, "1n/0n", "RangeError"); -} \ No newline at end of file +} From 06ef441edd97a6818cdbc42efeaaff361219ed93 Mon Sep 17 00:00:00 2001 From: JohnDoneth Date: Sun, 4 Oct 2020 13:39:20 -0400 Subject: [PATCH 3/7] Remove extra whitespace --- boa/src/syntax/ast/node/operator/bin_op/mod.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/boa/src/syntax/ast/node/operator/bin_op/mod.rs b/boa/src/syntax/ast/node/operator/bin_op/mod.rs index aee50fffa36..d4336dd2d8b 100644 --- a/boa/src/syntax/ast/node/operator/bin_op/mod.rs +++ b/boa/src/syntax/ast/node/operator/bin_op/mod.rs @@ -82,7 +82,6 @@ impl Executable for BinOp { op::BinOp::Num(op) => { let x = self.lhs().run(interpreter)?; let y = self.rhs().run(interpreter)?; - match op { NumOp::Add => x.add(&y, interpreter), NumOp::Sub => x.sub(&y, interpreter), @@ -92,7 +91,6 @@ impl Executable for BinOp { if y == Value::bigint(BigInt::from(0)) { return interpreter.throw_range_error("BigInt division by zero"); } - x.div(&y, interpreter) } NumOp::Mod => x.rem(&y, interpreter), From 1517a297ae6eafe503d2e190cc1178a3770bb697 Mon Sep 17 00:00:00 2001 From: JohnDoneth Date: Sun, 4 Oct 2020 13:39:56 -0400 Subject: [PATCH 4/7] Remove extra whitespace --- boa/src/builtins/bigint/tests.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/boa/src/builtins/bigint/tests.rs b/boa/src/builtins/bigint/tests.rs index 94a5d666095..0a7f21113cb 100644 --- a/boa/src/builtins/bigint/tests.rs +++ b/boa/src/builtins/bigint/tests.rs @@ -355,6 +355,5 @@ fn assert_throws(engine: &mut Context, src: &str, error_type: &str) { #[test] fn division_by_zero() { let mut engine = Context::new(); - assert_throws(&mut engine, "1n/0n", "RangeError"); } From 3ebce39d7c001de949b3edfe0c1ce71a0e3251cb Mon Sep 17 00:00:00 2001 From: JohnDoneth Date: Sun, 4 Oct 2020 14:28:21 -0400 Subject: [PATCH 5/7] Move divide by zero checks to Value::div --- boa/src/context.rs | 4 ++-- boa/src/syntax/ast/node/operator/bin_op/mod.rs | 7 +------ boa/src/value/operations.rs | 6 ++++++ 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/boa/src/context.rs b/boa/src/context.rs index d6b95188dfe..e197f88f890 100644 --- a/boa/src/context.rs +++ b/boa/src/context.rs @@ -439,11 +439,11 @@ impl Context { let key = field.to_property_key(self)?; Ok(get_field.obj().run(self)?.set_field(key, value)) } - _ => panic!("TypeError: invalid assignment to {}", node), + _ => self.throw_type_error(format!("invalid assignment to {}", node)), } } - /// 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/syntax/ast/node/operator/bin_op/mod.rs b/boa/src/syntax/ast/node/operator/bin_op/mod.rs index d4336dd2d8b..b8addf18607 100644 --- a/boa/src/syntax/ast/node/operator/bin_op/mod.rs +++ b/boa/src/syntax/ast/node/operator/bin_op/mod.rs @@ -87,12 +87,7 @@ impl Executable for BinOp { NumOp::Sub => x.sub(&y, interpreter), NumOp::Mul => x.mul(&y, interpreter), NumOp::Exp => x.pow(&y, interpreter), - NumOp::Div => { - if y == Value::bigint(BigInt::from(0)) { - return interpreter.throw_range_error("BigInt division by zero"); - } - x.div(&y, interpreter) - } + NumOp::Div => x.div(&y, interpreter), NumOp::Mod => x.rem(&y, interpreter), } } 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()) } (_, _) => { From e933167ef92e7a048bb81a05936e076c12ee874f Mon Sep 17 00:00:00 2001 From: JohnDoneth Date: Sun, 4 Oct 2020 14:30:21 -0400 Subject: [PATCH 6/7] Undo un-related change --- boa/src/context.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boa/src/context.rs b/boa/src/context.rs index e197f88f890..f71e2bf3939 100644 --- a/boa/src/context.rs +++ b/boa/src/context.rs @@ -439,7 +439,7 @@ impl Context { let key = field.to_property_key(self)?; Ok(get_field.obj().run(self)?.set_field(key, value)) } - _ => self.throw_type_error(format!("invalid assignment to {}", node)), + _ => panic!("TypeError: invalid assignment to {}", node), } } From f464ce4804ff4bfebbf51b46b5602bb71c34ede4 Mon Sep 17 00:00:00 2001 From: JohnDoneth Date: Sun, 4 Oct 2020 14:31:15 -0400 Subject: [PATCH 7/7] Remove unused import --- boa/src/syntax/ast/node/operator/bin_op/mod.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/boa/src/syntax/ast/node/operator/bin_op/mod.rs b/boa/src/syntax/ast/node/operator/bin_op/mod.rs index b8addf18607..db8985eec2a 100644 --- a/boa/src/syntax/ast/node/operator/bin_op/mod.rs +++ b/boa/src/syntax/ast/node/operator/bin_op/mod.rs @@ -1,5 +1,4 @@ use crate::{ - builtins::BigInt, exec::Executable, syntax::ast::{ node::Node,