Skip to content

Commit

Permalink
fix #3569: incorrect ToInt32 behavior on riscv64
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jan 1, 2024
1 parent 6ee8225 commit f6eae0c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/js_ast/js_ast_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions internal/js_parser/js_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand Down

0 comments on commit f6eae0c

Please sign in to comment.