Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 8 additions & 6 deletions core/engine/src/value/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,10 @@ impl JsValue {
if y == 0 {
Self::nan()
} else {
match x % y {
rem if rem == 0 && x < 0 => Self::new(-0.0),
rem => Self::new(rem),
match x.checked_rem(y) {
Some(rem) if rem == 0 && x < 0 => Self::new(-0.0),
Some(rem) => Self::new(rem),
None => Self::new((f64::from(x) % f64::from(y)).copysign(f64::from(x))),
}
}
}
Expand Down Expand Up @@ -754,9 +755,10 @@ impl JsValue {
if y == 0 {
return Some(Self::nan());
}
return Some(match x % y {
rem if rem == 0 && x < 0 => Self::new(-0.0),
rem => Self::new(rem),
return Some(match x.checked_rem(y) {
Some(rem) if rem == 0 && x < 0 => Self::new(-0.0),
Some(rem) => Self::new(rem),
None => Self::new((f64::from(x) % f64::from(y)).copysign(f64::from(x))),
Comment on lines +758 to +761

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to do the None branch here, you can just delegate that to the end of the function which already does the same.

});
}
let x = self.as_number_cheap()?;
Expand Down
23 changes: 23 additions & 0 deletions core/engine/src/value/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,29 @@ fn rem_by_zero() {
run_test_actions([TestAction::assert_eq("1 % 0", f64::NAN)]);
}

#[test]
fn rem_i32_min_by_neg_one() {
run_test_actions([TestAction::assert_with_op(
"(-2147483648 | 0) % (-1 | 0)",
|val, _| {
val.as_number()
.is_some_and(|n| n == 0.0 && n.is_sign_negative())
},
)]);
}

#[test]
fn rem_fast_i32_min_by_neg_one() {
let x = JsValue::from(i32::MIN);
let y = JsValue::from(-1_i32);
let result = x.rem_fast(&y).unwrap();
assert!(
result
.as_number()
.is_some_and(|n| n == 0.0 && n.is_sign_negative())
);
}

#[test]
fn bitand_integer_and_integer() {
run_test_actions([TestAction::assert_eq("0xFFFF & 0xFF", 255)]);
Expand Down