11//-------------------------------------------------------------------------------------------------------
22// Copyright (C) Microsoft. All rights reserved.
3+ // Copyright (c) ChakraCore Project Contributors. All rights reserved.
34// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
45//-------------------------------------------------------------------------------------------------------
56
@@ -134,6 +135,35 @@ const tests = [
134135 }
135136 } ,
136137 {
138+ name : "Array.prototype.sort with edited prototypes and compare function side effects" ,
139+ body ( ) {
140+ const arrayOne = new Array ( 2 ) ;
141+ arrayOne [ 0 ] = 12 ;
142+ arrayOne [ 1 ] = 10 ;
143+ const resultOne = [ 10 , 12 ] ;
144+ compareSparseArrays ( resultOne , arrayOne . sort ( ( x , y ) => { arrayOne [ 0 ] = "test" ; return x - y ; } ) , "Compare function set element does not effect array" ) ;
145+ compareSparseArrays ( resultOne , arrayOne , "Compare function side effect does not effect array" ) ;
146+
147+ const arrayTwo = new Array ( 3 ) ;
148+ arrayTwo [ 0 ] = 12 ;
149+ arrayTwo [ 2 ] = 10 ;
150+ const resultTwo = [ 10 , 12 , , ] ;
151+ compareSparseArrays ( resultTwo , arrayTwo . sort ( ( x , y ) => { delete arrayTwo [ 0 ] ; return x - y ; } ) , "Compare function delete element does not effect array" ) ;
152+ compareSparseArrays ( resultTwo , arrayTwo , "Compare function delete element does not effect array" ) ;
153+ }
154+ } ,
155+ {
156+ name : "Array.prototype.sort default comparison should not call valueOf" ,
157+ body ( ) {
158+ valueOf = false ;
159+ const arr = [ {
160+ valueOf ( ) { valueOf = true ; return 0 ; }
161+ } , 1 , 1 , 1 , ] ;
162+ arr . sort ( ) ;
163+ assert . isFalse ( valueOf ) ;
164+ }
165+ } ,
166+ { // NOTE this test edits the array prototype and hence must be placed after other tests
137167 name : "Array.prototype.sort prototype lookups" ,
138168 body ( ) {
139169 for ( let i = 0 ; i < 20 ; i = i + 4 ) {
@@ -196,6 +226,7 @@ const tests = [
196226 } ,
197227 {
198228 //Test that bug 5719 is fixed https://github.com/Microsoft/ChakraCore/issues/5719
229+ //NOTE this test depends upon the prototype editing done in the previous test
199230 name : "Array-like object does not pull values from Array.prototype" ,
200231 body ( ) {
201232 const arrayLike = {
@@ -205,35 +236,6 @@ const tests = [
205236 assert . areEqual ( "o0" , arrayLike [ 0 ] , "Array.prototype.sort pulls values form Object.prototype for array-like object" ) ;
206237 assert . areNotEqual ( "p3" , arrayLike [ 1 ] , "Array.prototype.sort does not pull values form Array.prototype for array-like object" ) ;
207238 }
208- } ,
209- {
210- name : "Array.prototype.sort with edited prototypes and compare function side effects" ,
211- body ( ) {
212- const arrayOne = new Array ( 2 ) ;
213- arrayOne [ 0 ] = 12 ;
214- arrayOne [ 1 ] = 10 ;
215- const resultOne = [ 10 , "test" ] ;
216- compareSparseArrays ( resultOne , arrayOne . sort ( ( x , y ) => { arrayOne [ 0 ] = "test" ; return x - y ; } ) , "Compare function set element effects array" ) ;
217- compareSparseArrays ( resultOne , arrayOne , "Compare function side effect effects array" ) ;
218-
219- const arrayTwo = new Array ( 3 ) ;
220- arrayTwo [ 0 ] = 12 ;
221- arrayTwo [ 2 ] = 10 ;
222- const resultTwo = [ 0 , 0 , 10 ] ;
223- compareSparseArrays ( resultTwo , arrayTwo . sort ( ( x , y ) => { delete arrayTwo [ 0 ] ; return x - y ; } ) , "Compare function delete element effects array" ) ;
224- compareSparseArrays ( resultTwo , arrayTwo , "Compare function delete element effects array" ) ;
225- }
226- } ,
227- {
228- name : "Array.prototype.sort default comparison should not call valueOf" ,
229- body ( ) {
230- valueOf = false ;
231- const arr = [ {
232- valueOf ( ) { valueOf = true ; return 0 ; }
233- } , 1 , 1 , 1 , ] ;
234- arr . sort ( ) ;
235- assert . isFalse ( valueOf ) ;
236- }
237239 }
238240] ;
239241
0 commit comments