diff --git a/internal/js_ast/js_ast_helpers.go b/internal/js_ast/js_ast_helpers.go index da82caa4c02..c51afd335c5 100644 --- a/internal/js_ast/js_ast_helpers.go +++ b/internal/js_ast/js_ast_helpers.go @@ -929,6 +929,11 @@ func ToInt32(f float64) int32 { return i } + // Special-case non-finite numbers (casting them is unspecified behavior in Go) + if math.IsNaN(f) || math.IsInf(f, 0) { + return 0 + } + // The hard way i = int32(uint32(math.Mod(math.Abs(f), 4294967296))) if math.Signbit(f) { diff --git a/internal/js_parser/js_parser_test.go b/internal/js_parser/js_parser_test.go index e2ae3d9674a..6487fc55fc5 100644 --- a/internal/js_parser/js_parser_test.go +++ b/internal/js_parser/js_parser_test.go @@ -3679,6 +3679,8 @@ func TestMangleCharCodeAt(t *testing.T) { expectPrintedMangle(t, "a = '🧀'.charCodeAt(2)", "a = NaN;\n") expectPrintedMangle(t, "a = 'xy'.charCodeAt(NaN)", "a = \"xy\".charCodeAt(NaN);\n") + expectPrintedMangle(t, "a = 'xy'.charCodeAt(-Infinity)", "a = \"xy\".charCodeAt(-Infinity);\n") + expectPrintedMangle(t, "a = 'xy'.charCodeAt(Infinity)", "a = \"xy\".charCodeAt(Infinity);\n") expectPrintedMangle(t, "a = 'xy'.charCodeAt(0.5)", "a = \"xy\".charCodeAt(0.5);\n") expectPrintedMangle(t, "a = 'xy'.charCodeAt(1e99)", "a = \"xy\".charCodeAt(1e99);\n") expectPrintedMangle(t, "a = 'xy'.charCodeAt('1')", "a = \"xy\".charCodeAt(\"1\");\n") @@ -3697,6 +3699,7 @@ func TestMangleFromCharCode(t *testing.T) { expectPrintedMangle(t, "a = String.fromCharCode(0x10078, 0x10079)", "a = \"xy\";\n") expectPrintedMangle(t, "a = String.fromCharCode(0x1_0000_FFFF)", "a = \"\uFFFF\";\n") expectPrintedMangle(t, "a = String.fromCharCode(NaN)", "a = \"\\0\";\n") + expectPrintedMangle(t, "a = String.fromCharCode(-Infinity)", "a = \"\\0\";\n") expectPrintedMangle(t, "a = String.fromCharCode(Infinity)", "a = \"\\0\";\n") expectPrintedMangle(t, "a = String.fromCharCode(null)", "a = \"\\0\";\n") expectPrintedMangle(t, "a = String.fromCharCode(undefined)", "a = \"\\0\";\n")