Assert that comparator is not null in Array#sort (#155)

This commit is contained in:
Max Graey 2018-07-14 15:56:05 +03:00 committed by Daniel Wirtz
parent 9e508de69a
commit 29081b6323
3 changed files with 88 additions and 1 deletions

View File

@ -302,6 +302,9 @@ export class Array<T> {
} }
sort(comparator: (a: T, b: T) => i32 = defaultComparator<T>()): this { sort(comparator: (a: T, b: T) => i32 = defaultComparator<T>()): this {
// TODO remove this when flow will allow trackcing null
assert(comparator); // The comparison function must be a function
var length = this.length_; var length = this.length_;
if (length <= 1) return this; if (length <= 1) return this;
var buffer = this.buffer_; var buffer = this.buffer_;
@ -316,7 +319,7 @@ export class Array<T> {
} }
if (isReference<T>()) { if (isReference<T>()) {
// TODO replace this to stable sort when it implemented // TODO replace this to faster stable sort (TimSort) when it implemented
return changetype<this>(insertionSort<T>(this, comparator)); return changetype<this>(insertionSort<T>(this, comparator));
} else { } else {
return changetype<this>(length < 256 return changetype<this>(length < 256

View File

@ -4937,6 +4937,20 @@
(local $2 i32) (local $2 i32)
(local $3 i32) (local $3 i32)
(local $4 i32) (local $4 i32)
(if
(i32.eqz
(get_local $1)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 306)
(i32.const 4)
)
(unreachable)
)
)
(if (if
(i32.le_s (i32.le_s
(tee_local $2 (tee_local $2
@ -5254,6 +5268,20 @@
(local $2 i32) (local $2 i32)
(local $3 i32) (local $3 i32)
(local $4 i32) (local $4 i32)
(if
(i32.eqz
(get_local $1)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 306)
(i32.const 4)
)
(unreachable)
)
)
(if (if
(i32.le_s (i32.le_s
(tee_local $2 (tee_local $2

View File

@ -6175,6 +6175,20 @@
(local $4 i32) (local $4 i32)
(local $5 i32) (local $5 i32)
(local $6 i32) (local $6 i32)
(if
(i32.eqz
(get_local $1)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 306)
(i32.const 4)
)
(unreachable)
)
)
(set_local $2 (set_local $2
(i32.load offset=4 (i32.load offset=4
(get_local $0) (get_local $0)
@ -6899,6 +6913,20 @@
(local $4 i32) (local $4 i32)
(local $5 i32) (local $5 i32)
(local $6 i32) (local $6 i32)
(if
(i32.eqz
(get_local $1)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 306)
(i32.const 4)
)
(unreachable)
)
)
(set_local $2 (set_local $2
(i32.load offset=4 (i32.load offset=4
(get_local $0) (get_local $0)
@ -7495,6 +7523,20 @@
(local $4 i32) (local $4 i32)
(local $5 i32) (local $5 i32)
(local $6 i32) (local $6 i32)
(if
(i32.eqz
(get_local $1)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 306)
(i32.const 4)
)
(unreachable)
)
)
(set_local $2 (set_local $2
(i32.load offset=4 (i32.load offset=4
(get_local $0) (get_local $0)
@ -8150,6 +8192,20 @@
(local $4 i32) (local $4 i32)
(local $5 i32) (local $5 i32)
(local $6 i32) (local $6 i32)
(if
(i32.eqz
(get_local $1)
)
(block
(call $~lib/env/abort
(i32.const 0)
(i32.const 8)
(i32.const 306)
(i32.const 4)
)
(unreachable)
)
)
(set_local $2 (set_local $2
(i32.load offset=4 (i32.load offset=4
(get_local $0) (get_local $0)