mirror of
https://github.com/robertkrimen/otto
synced 2025-10-19 19:55:30 +08:00
Add floatToString and streamline toString
floatToString does not properly implement ECMA-262 9.8.1 yet
This commit is contained in:
parent
e06ac35b32
commit
95d7d729ad
|
|
@ -25,4 +25,8 @@ func TestNumber_toString(t *testing.T) {
|
|||
test(`raise:
|
||||
new Number(451).toString(1);
|
||||
`, "RangeError: RangeError: toString() radix must be between 2 and 36")
|
||||
|
||||
test(`raise:
|
||||
new Number(451).toString(Infinity);
|
||||
`, "RangeError: RangeError: toString() radix must be between 2 and 36")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,8 +3,22 @@ package otto
|
|||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func floatToString(value float64, bitsize int) string {
|
||||
// TODO Reimplement according to ECMA-262 9.8.1
|
||||
if math.IsNaN(value) {
|
||||
return "NaN"
|
||||
} else if math.IsInf(value, 0) {
|
||||
if math.Signbit(value) {
|
||||
return "-Infinity"
|
||||
}
|
||||
return "Infinity"
|
||||
}
|
||||
return strconv.FormatFloat(value, 'g', -1, bitsize)
|
||||
}
|
||||
|
||||
func toString(value Value) string {
|
||||
if value._valueType == valueString {
|
||||
return value.value.(string)
|
||||
|
|
@ -15,29 +29,37 @@ func toString(value Value) string {
|
|||
if value.IsNull() {
|
||||
return "null"
|
||||
}
|
||||
switch realValue := value.value.(type) {
|
||||
switch value := value.value.(type) {
|
||||
case bool:
|
||||
return fmt.Sprintf("%v", realValue)
|
||||
case int, int8, int16, int32, int64:
|
||||
return fmt.Sprintf("%v", realValue)
|
||||
case uint, uint8, uint16, uint32, uint64:
|
||||
return fmt.Sprintf("%v", realValue)
|
||||
return strconv.FormatBool(value)
|
||||
case int:
|
||||
return strconv.FormatInt(int64(value), 10)
|
||||
case int8:
|
||||
return strconv.FormatInt(int64(value), 10)
|
||||
case int16:
|
||||
return strconv.FormatInt(int64(value), 10)
|
||||
case int32:
|
||||
return strconv.FormatInt(int64(value), 10)
|
||||
case int64:
|
||||
return strconv.FormatInt(value, 10)
|
||||
case uint:
|
||||
return strconv.FormatUint(uint64(value), 10)
|
||||
case uint8:
|
||||
return strconv.FormatUint(uint64(value), 10)
|
||||
case uint16:
|
||||
return strconv.FormatUint(uint64(value), 10)
|
||||
case uint32:
|
||||
return strconv.FormatUint(uint64(value), 10)
|
||||
case uint64:
|
||||
return strconv.FormatUint(value, 10)
|
||||
case float32:
|
||||
return fmt.Sprintf("%v", realValue)
|
||||
return floatToString(float64(value), 32)
|
||||
case float64:
|
||||
if math.IsNaN(realValue) {
|
||||
return "NaN"
|
||||
} else if math.IsInf(realValue, 0) {
|
||||
if math.Signbit(realValue) {
|
||||
return "-Infinity"
|
||||
}
|
||||
return "Infinity"
|
||||
}
|
||||
return fmt.Sprintf("%v", realValue)
|
||||
return floatToString(value, 64)
|
||||
case string:
|
||||
return realValue
|
||||
return value
|
||||
case *_object:
|
||||
return toString(realValue.DefaultValue(defaultValueHintString))
|
||||
return toString(value.DefaultValue(defaultValueHintString))
|
||||
}
|
||||
panic(fmt.Errorf("toString(%v %T)", value.value, value.value))
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user