Skip to content

Use native JS API for ParseInt where possible #1221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
12 changes: 11 additions & 1 deletion compiler/natives/src/strconv/atoi.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ const (
minInt32 float64 = -1 << 31
)

//gopherjs:keep-original
func ParseInt(s string, base, bitSize int) (i int64, err error) {
if base == 10 && (bitSize == 0 || bitSize == 32) {
i, err := Atoi(s)
return int64(i), err
}
return _gopherjs_original_ParseInt(s, base, bitSize)
}

// Atoi returns the result of ParseInt(s, 10, 0) converted to type int.
func Atoi(s string) (int, error) {
const fnAtoi = "Atoi"
Expand Down Expand Up @@ -39,7 +48,8 @@ func Atoi(s string) (int, error) {
floatval := jsValue.Float()
if floatval > maxInt32 {
return int(maxInt32), rangeError(fnAtoi, s)
} else if floatval < minInt32 {
}
if floatval < minInt32 {
return int(minInt32), rangeError(fnAtoi, s)
}
// Success!
Expand Down