Skip to content
Merged
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
18 changes: 11 additions & 7 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,10 +755,13 @@ 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),
});
if let Some(rem) = x.checked_rem(y) {
return Some(if rem == 0 && x < 0 {
Self::new(-0.0)
} else {
Self::new(rem)
});
}
}
let x = self.as_number_cheap()?;
let y = other.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
Loading