1
0
mirror of https://github.com/robertkrimen/otto synced 2025-10-12 20:27:30 +08:00

Fix toReflectValue(0) => float32

This fixes #123
This commit is contained in:
Robert Krimen 2015-05-07 22:53:02 -07:00
parent d1b4d8ef0e
commit 7597815bd0
2 changed files with 11 additions and 1 deletions

View File

@ -857,7 +857,7 @@ func (value Value) toReflectValue(kind reflect.Kind) (reflect.Value, error) {
if 0 > tmp1 {
tmp1 = -tmp1
}
if tmp1 < math.SmallestNonzeroFloat32 || tmp1 > math.MaxFloat32 {
if tmp1 > 0 && (tmp1 < math.SmallestNonzeroFloat32 || tmp1 > math.MaxFloat32) {
return reflect.Value{}, fmt.Errorf("RangeError: %f (%v) to float32", tmp, value)
} else {
return reflect.ValueOf(float32(tmp)), nil

View File

@ -3,6 +3,7 @@ package otto
import (
"encoding/json"
"math"
"reflect"
"testing"
)
@ -279,3 +280,12 @@ func TestExport(t *testing.T) {
}
})
}
func Test_toReflectValue(t *testing.T) {
tt(t, func() {
value := toValue(0.0)
tmp, err := value.toReflectValue(reflect.Float32)
is(tmp.Float(), 0.0)
is(err, nil)
})
}