diff --git a/bug_test.go b/bug_test.go index 5e4d90b..a9db3db 100644 --- a/bug_test.go +++ b/bug_test.go @@ -5,6 +5,18 @@ import ( "time" ) +func Test_issue116(t *testing.T) { + tt(t, func() { + test, _ := test() + + test(` + [1,-1].sort(function(a, b) { + return a - b; + }); + `, "-1,1") + }) +} + func Test_262(t *testing.T) { tt(t, func() { test, _ := test() diff --git a/builtin_array.go b/builtin_array.go index 44bf885..160a251 100644 --- a/builtin_array.go +++ b/builtin_array.go @@ -420,7 +420,7 @@ func arraySortQuickPartition(thisObject *_object, left, right, pivot uint, compa arraySortSwap(thisObject, pivot, right) // Right is now the pivot value cursor := left for index := left; index < right; index++ { - if sortCompare(thisObject, index, right, compare) == -1 { // Compare to the pivot value + if sortCompare(thisObject, index, right, compare) < 0 { // Compare to the pivot value arraySortSwap(thisObject, index, cursor) cursor += 1 } diff --git a/reflect_test.go b/reflect_test.go index 3c5daa5..090409f 100644 --- a/reflect_test.go +++ b/reflect_test.go @@ -391,6 +391,63 @@ func Test_reflectMap(t *testing.T) { }) } +func Test_reflectMapIterateKeys(t *testing.T) { + tt(t, func() { + test, vm := test() + + // map[string]interface{} + { + abc := map[string]interface{}{ + "Xyzzy": "Nothing happens.", + "def": 1, + } + vm.Set("abc", abc) + test(` + var keys = []; + for (var key in abc) { + keys.push(key); + } + keys.sort(); + keys; + `, "Xyzzy,def") + } + + // map[uint]interface{} + { + abc := map[uint]interface{}{ + 456: "Nothing happens.", + 123: 1, + } + vm.Set("abc", abc) + test(` + var keys = []; + for (var key in abc) { + keys.push(key); + } + keys.sort(); + keys; + `, "123,456") + } + + // map[byte]interface{} + { + abc := map[byte]interface{}{ + 10: "Nothing happens.", + 20: 1, + } + vm.Set("abc", abc) + test(` + for (var key in abc) { + abc[key] = "123"; + } + `) + is(abc[10], "123") + is(abc[20], "123") + } + + }) +} + func Test_reflectSlice(t *testing.T) { tt(t, func() { test, vm := test() diff --git a/type_go_map.go b/type_go_map.go index 542a2c2..3e204a0 100644 --- a/type_go_map.go +++ b/type_go_map.go @@ -60,7 +60,7 @@ func goMapEnumerate(self *_object, all bool, each func(string) bool) { object := self.value.(*_goMapObject) keys := object.value.MapKeys() for _, key := range keys { - if !each(key.String()) { + if !each(toValue(key).String()) { return } } diff --git a/value.go b/value.go index 05d61dd..121f377 100644 --- a/value.go +++ b/value.go @@ -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 diff --git a/value_test.go b/value_test.go index 4a9bd54..ef637c8 100644 --- a/value_test.go +++ b/value_test.go @@ -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) + }) +}