Skip to content

Commit f6eae0c

Browse files
committed
fix #3569: incorrect ToInt32 behavior on riscv64
1 parent 6ee8225 commit f6eae0c

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

internal/js_ast/js_ast_helpers.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,11 @@ func ToInt32(f float64) int32 {
929929
return i
930930
}
931931

932+
// Special-case non-finite numbers (casting them is unspecified behavior in Go)
933+
if math.IsNaN(f) || math.IsInf(f, 0) {
934+
return 0
935+
}
936+
932937
// The hard way
933938
i = int32(uint32(math.Mod(math.Abs(f), 4294967296)))
934939
if math.Signbit(f) {

internal/js_parser/js_parser_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3679,6 +3679,8 @@ func TestMangleCharCodeAt(t *testing.T) {
36793679
expectPrintedMangle(t, "a = '🧀'.charCodeAt(2)", "a = NaN;\n")
36803680

36813681
expectPrintedMangle(t, "a = 'xy'.charCodeAt(NaN)", "a = \"xy\".charCodeAt(NaN);\n")
3682+
expectPrintedMangle(t, "a = 'xy'.charCodeAt(-Infinity)", "a = \"xy\".charCodeAt(-Infinity);\n")
3683+
expectPrintedMangle(t, "a = 'xy'.charCodeAt(Infinity)", "a = \"xy\".charCodeAt(Infinity);\n")
36823684
expectPrintedMangle(t, "a = 'xy'.charCodeAt(0.5)", "a = \"xy\".charCodeAt(0.5);\n")
36833685
expectPrintedMangle(t, "a = 'xy'.charCodeAt(1e99)", "a = \"xy\".charCodeAt(1e99);\n")
36843686
expectPrintedMangle(t, "a = 'xy'.charCodeAt('1')", "a = \"xy\".charCodeAt(\"1\");\n")
@@ -3697,6 +3699,7 @@ func TestMangleFromCharCode(t *testing.T) {
36973699
expectPrintedMangle(t, "a = String.fromCharCode(0x10078, 0x10079)", "a = \"xy\";\n")
36983700
expectPrintedMangle(t, "a = String.fromCharCode(0x1_0000_FFFF)", "a = \"\uFFFF\";\n")
36993701
expectPrintedMangle(t, "a = String.fromCharCode(NaN)", "a = \"\\0\";\n")
3702+
expectPrintedMangle(t, "a = String.fromCharCode(-Infinity)", "a = \"\\0\";\n")
37003703
expectPrintedMangle(t, "a = String.fromCharCode(Infinity)", "a = \"\\0\";\n")
37013704
expectPrintedMangle(t, "a = String.fromCharCode(null)", "a = \"\\0\";\n")
37023705
expectPrintedMangle(t, "a = String.fromCharCode(undefined)", "a = \"\\0\";\n")

0 commit comments

Comments
 (0)