Project: v8 Issues People Development process History Sign in
New issue
Advanced search Search tips
Issue 2264 Array.prototype.sort does not call ToString() for identical elements
Starred by 1 user Reported by andrebar...@gmail.com, Jul 29 2012 Back to list
Status: Invalid
Owner:
Closed: Apr 2015
HW: ----
OS: ----
Priority: ----
Type: ----



Sign in to add a comment
The SortCompare abstract operation (cf. Array.prototype.sort [15.4.4.11]) calls ToString() for each element which is going to be compared. This ToString() call is currently omitted for identical elements, this is not spec compliant. 

test case:
var c = 0;
var o = {toString: function(){ c++; return ""; }}
[o, o].sort();
print(c);

Expected output:
2

Actual output:
0
 
Note the missing ";" after the object literal
Owner: yangguo@chromium.org
Status: Assigned
The standard is intentionally vague about how many times SortCompare is called.  Is this result not consistent with SortCompare not being called at all?

"Perform an implementation-dependent sequence of calls to the [[Get]] , [[Put]], and [[Delete]] internal methods of obj and to SortCompare"

In this case the implementation-dependent sequencce does not include any calls to SortCompare.
SortCompare is being called in the current code, but the current implementation of SortCompare [1] does not follow the specification (see line 752 [2]). 


[1] http://code.google.com/p/v8/source/browse/trunk/src/array.js#751
[2] http://code.google.com/p/v8/source/browse/trunk/src/array.js#752
Status: Invalid
When the identity short cut is used, the user of the sort function cannot detect that SortCompare is called at all, therefore V8s behaviour is indistinguishable from a sort routine that is merely clever at minimising how often it calls the comparison function.  This is allowed.
Sign in to add a comment