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

Merge remote-tracking branch 'upstream/master'

This commit is contained in:
Ernest Micklei 2015-08-02 10:30:24 +02:00
commit a05ab134d2
6 changed files with 82 additions and 3 deletions

View File

@ -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()

View File

@ -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
}

View File

@ -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()

View File

@ -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
}
}

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)
})
}