From 74572de625a3e08fa0d56d5d744c5d26d0aba98b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Fri, 27 Dec 2024 22:48:25 +0900 Subject: [PATCH] fix(ecmascript): incorrect `to_int_32` value for Infinity (#8144) https://tc39.es/ecma262/#sec-toint32 Infinity should return `0` when passed to `ToInt32` abstract operation. --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- crates/oxc_ecmascript/src/to_int_32.rs | 5 +++++ .../oxc_minifier/src/ast_passes/peephole_fold_constants.rs | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/crates/oxc_ecmascript/src/to_int_32.rs b/crates/oxc_ecmascript/src/to_int_32.rs index d0a69a48cf16d..468a8c5688c4c 100644 --- a/crates/oxc_ecmascript/src/to_int_32.rs +++ b/crates/oxc_ecmascript/src/to_int_32.rs @@ -56,6 +56,11 @@ impl ToInt32 for f64 { let number = *self; + // NOTE: this also matches with negative zero + if !number.is_finite() || number == 0.0 { + return 0; + } + if number.is_finite() && number <= f64::from(i32::MAX) && number >= f64::from(i32::MIN) { let i = number as i32; if f64::from(i) == number { diff --git a/crates/oxc_minifier/src/ast_passes/peephole_fold_constants.rs b/crates/oxc_minifier/src/ast_passes/peephole_fold_constants.rs index 3e915b39e0802..d0e55587a4aa3 100644 --- a/crates/oxc_minifier/src/ast_passes/peephole_fold_constants.rs +++ b/crates/oxc_minifier/src/ast_passes/peephole_fold_constants.rs @@ -1545,9 +1545,9 @@ mod test { #[test] fn test_fold_left_child_op() { - test_same("x & infinity & 2"); // FIXME: want x & 0 - test_same("x - infinity - 2"); // FIXME: want "x-infinity" - test_same("x - 1 + infinity"); + test("x & Infinity & 2", "x & 0"); + test_same("x - Infinity - 2"); // FIXME: want "x-Infinity" + test_same("x - 1 + Infinity"); test_same("x - 2 + 1"); test_same("x - 2 + 3"); test_same("1 + x - 2 + 1");