Browse Source

Merge branch 'master' into es6

# Conflicts:
#	builtin_array.go
Dmitry Panov 5 years ago
parent
commit
9864537e3b
4 changed files with 46 additions and 9 deletions
  1. 1 1
      README.md
  2. 12 2
      builtin_array.go
  3. 32 0
      runtime_test.go
  4. 1 6
      string_unicode.go

+ 1 - 1
README.md

@@ -60,7 +60,7 @@ and it includes an event loop.
 There is now an es6 branch. This is work in progress and all new ES6 features will go there. Every commit
 in this branch represents a relatively stable state (i.e. it compiles and passes all enabled tc39 tests),
 however because the version of tc39 tests I use is quite old, it may be not as well tested as the ES5.1
-functionality. Because ES6 is a superset of ES5.1 it should now break your existing code.
+functionality. Because ES6 is a superset of ES5.1 it should not break your existing code.
 
 I will be adding features in their dependency order and as quickly as my time allows. Please do not ask
 for ETA. Eventually it will be merged into master. If you wish to implement a new feature please contact

+ 12 - 2
builtin_array.go

@@ -1315,10 +1315,20 @@ func (ctx *arraySortCtx) sortCompare(x, y Value) int {
 	}
 
 	if ctx.compare != nil {
-		return int(ctx.compare(FunctionCall{
+		f := ctx.compare(FunctionCall{
 			This:      _undefined,
 			Arguments: []Value{x, y},
-		}).ToInteger())
+		}).ToFloat()
+		if f > 0 {
+			return 1
+		}
+		if f < 0 {
+			return -1
+		}
+		if math.Signbit(f) {
+			return -1
+		}
+		return 0
 	}
 	return strings.Compare(x.String(), y.String())
 }

+ 32 - 0
runtime_test.go

@@ -827,6 +827,38 @@ func TestSortComparatorReturnValues(t *testing.T) {
 	testScript1(SCRIPT, _undefined, t)
 }
 
+func TestSortComparatorReturnValueFloats(t *testing.T) {
+	const SCRIPT = `
+	var a = [
+		5.97,
+		9.91,
+		4.13,
+		9.28,
+		3.29,
+	];
+	a.sort( function(a, b) { return a - b; } );
+	for (var i = 1; i < a.length; i++) {
+		if (a[i] < a[i-1]) {
+			throw new Error("Array is not sorted: " + a);
+		}
+	}
+	`
+	testScript1(SCRIPT, _undefined, t)
+}
+
+func TestSortComparatorReturnValueNegZero(t *testing.T) {
+	const SCRIPT = `
+	var a = [2, 1];
+	a.sort( function(a, b) { return a > b ? 0 : -0; } );
+	for (var i = 1; i < a.length; i++) {
+		if (a[i] < a[i-1]) {
+			throw new Error("Array is not sorted: " + a);
+		}
+	}
+	`
+	testScript1(SCRIPT, _undefined, t)
+}
+
 func TestNilApplyArg(t *testing.T) {
 	const SCRIPT = `
 	(function x(a, b) {

+ 1 - 6
string_unicode.go

@@ -10,7 +10,6 @@ import (
 	"io"
 	"math"
 	"reflect"
-	"regexp"
 	"strings"
 	"unicode/utf16"
 	"unicode/utf8"
@@ -32,10 +31,6 @@ var (
 	InvalidRuneError = errors.New("Invalid rune")
 )
 
-var (
-	unicodeTrimRegexp = regexp.MustCompile("^[" + parser.WhitespaceChars + "]*(.*?)[" + parser.WhitespaceChars + "]*$")
-)
-
 func (rr runeReaderReplace) ReadRune() (r rune, size int, err error) {
 	r, size, err = rr.wrapped.ReadRune()
 	if err == InvalidRuneError {
@@ -102,7 +97,7 @@ func (s unicodeString) toTrimmedUTF8() string {
 	if len(s) == 0 {
 		return ""
 	}
-	return unicodeTrimRegexp.FindStringSubmatch(s.String())[1]
+	return strings.Trim(s.String(), parser.WhitespaceChars)
 }
 
 func (s unicodeString) ToNumber() Value {