Issue 2264 in v8: Array.prototype.sort does not call ToString() for identical elements

12 views
Skip to first unread message

codesite...@google.com

unread,
Jul 29, 2012, 10:07:59 AM7/29/12
to v8-...@googlegroups.com
Status: New
Owner: ----

New issue 2264 by andrebar...@gmail.com: Array.prototype.sort does not call
ToString() for identical elements
http://code.google.com/p/v8/issues/detail?id=2264

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

codesite...@google.com

unread,
Jul 29, 2012, 10:31:11 AM7/29/12
to v8-...@googlegroups.com

Comment #1 on issue 2264 by andrebar...@gmail.com: Array.prototype.sort
does not call ToString() for identical elements
http://code.google.com/p/v8/issues/detail?id=2264

Note the missing ";" after the object literal

codesite...@google.com

unread,
Jul 30, 2012, 3:50:55 AM7/30/12
to v8-...@googlegroups.com
Updates:
Status: Assigned
Owner: yan...@chromium.org

Comment #2 on issue 2264 by yan...@chromium.org: Array.prototype.sort does
not call ToString() for identical elements
http://code.google.com/p/v8/issues/detail?id=2264

(No comment was entered for this change.)

codesite...@google.com

unread,
Jul 30, 2012, 4:56:00 AM7/30/12
to v8-...@googlegroups.com

Comment #3 on issue 2264 by erik.corry: Array.prototype.sort does not call
ToString() for identical elements
http://code.google.com/p/v8/issues/detail?id=2264

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.

codesite...@google.com

unread,
Jul 30, 2012, 6:12:44 AM7/30/12
to v8-...@googlegroups.com

Comment #4 on issue 2264 by andrebar...@gmail.com: Array.prototype.sort
does not call ToString() for identical elements
http://code.google.com/p/v8/issues/detail?id=2264

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

codesite...@google.com

unread,
Nov 25, 2012, 3:45:36 PM11/25/12
to v8-...@googlegroups.com
Updates:
Status: Invalid

Comment #5 on issue 2264 by erik.corry: Array.prototype.sort does not call
ToString() for identical elements
http://code.google.com/p/v8/issues/detail?id=2264

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.

Reply all
Reply to author
Forward
0 new messages