From 29081b6323da5e2cf68595cc30604cf92400fc26 Mon Sep 17 00:00:00 2001 From: Max Graey Date: Sat, 14 Jul 2018 15:56:05 +0300 Subject: [PATCH] Assert that comparator is not null in Array#sort (#155) --- std/assembly/array.ts | 5 ++- tests/compiler/std/array.optimized.wat | 28 +++++++++++++ tests/compiler/std/array.untouched.wat | 56 ++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 1 deletion(-) diff --git a/std/assembly/array.ts b/std/assembly/array.ts index abdbe5b6..bb70d76e 100644 --- a/std/assembly/array.ts +++ b/std/assembly/array.ts @@ -302,6 +302,9 @@ export class Array { } sort(comparator: (a: T, b: T) => i32 = defaultComparator()): this { + // TODO remove this when flow will allow trackcing null + assert(comparator); // The comparison function must be a function + var length = this.length_; if (length <= 1) return this; var buffer = this.buffer_; @@ -316,7 +319,7 @@ export class Array { } if (isReference()) { - // TODO replace this to stable sort when it implemented + // TODO replace this to faster stable sort (TimSort) when it implemented return changetype(insertionSort(this, comparator)); } else { return changetype(length < 256 diff --git a/tests/compiler/std/array.optimized.wat b/tests/compiler/std/array.optimized.wat index d596e726..e6a98665 100644 --- a/tests/compiler/std/array.optimized.wat +++ b/tests/compiler/std/array.optimized.wat @@ -4937,6 +4937,20 @@ (local $2 i32) (local $3 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 (i32.le_s (tee_local $2 @@ -5254,6 +5268,20 @@ (local $2 i32) (local $3 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 (i32.le_s (tee_local $2 diff --git a/tests/compiler/std/array.untouched.wat b/tests/compiler/std/array.untouched.wat index afb9491e..584853b4 100644 --- a/tests/compiler/std/array.untouched.wat +++ b/tests/compiler/std/array.untouched.wat @@ -6175,6 +6175,20 @@ (local $4 i32) (local $5 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 (i32.load offset=4 (get_local $0) @@ -6899,6 +6913,20 @@ (local $4 i32) (local $5 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 (i32.load offset=4 (get_local $0) @@ -7495,6 +7523,20 @@ (local $4 i32) (local $5 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 (i32.load offset=4 (get_local $0) @@ -8150,6 +8192,20 @@ (local $4 i32) (local $5 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 (i32.load offset=4 (get_local $0)