JavaScriptCore:

2009-02-13  Geoffrey Garen  <ggaren@apple.com>

        Reviewed by Darin Adler.
        
        Fixed <rdar://problem/6584057> Optimize sort by JS numeric comparison
        function not to run the comparison function
        
        * bytecode/CodeBlock.cpp:
        (JSC::CodeBlock::CodeBlock):
        * bytecode/CodeBlock.h:
        (JSC::CodeBlock::setIsNumericCompareFunction):
        (JSC::CodeBlock::isNumericCompareFunction): Added the ability to track
        whether a CodeBlock performs a sort-like numeric comparison.

        * bytecompiler/BytecodeGenerator.cpp:
        (JSC::BytecodeGenerator::generate): Set the isNumericCompareFunction bit
        after compiling.

        * parser/Nodes.cpp:
        (JSC::FunctionBodyNode::emitBytecode): Fixed a bug that caused us to
        codegen an extra return at the end of all functions (eek!), since this
        made it harder / weirder to detect the numeric comparison pattern in
        bytecode.

        * runtime/ArrayPrototype.cpp:
        (JSC::arrayProtoFuncSort): Use the isNumericCompareFunction bit to do
        a faster sort if we can.

        * runtime/FunctionConstructor.cpp:
        (JSC::extractFunctionBody):
        (JSC::constructFunction):
        * runtime/FunctionConstructor.h: Renamed and exported extractFunctionBody for
        use in initializing lazyNumericCompareFunction.

        * runtime/JSArray.cpp:
        (JSC::compareNumbersForQSort):
        (JSC::compareByStringPairForQSort):
        (JSC::JSArray::sortNumeric):
        (JSC::JSArray::sort):
        * runtime/JSArray.h: Added a fast numeric sort. Renamed ArrayQSortPair
        to be more specific since we do different kinds of qsort now.

        * runtime/JSGlobalData.cpp:
        (JSC::JSGlobalData::JSGlobalData):
        (JSC::JSGlobalData::numericCompareFunction):
        (JSC::JSGlobalData::ClientData::~ClientData):
        * runtime/JSGlobalData.h: Added helper data for computing the
        isNumericCompareFunction bit.

LayoutTests:

2009-02-13  Geoffrey Garen  <ggaren@apple.com>

        Reviewed by Sam Weinig.
        
        Added a test for an edge case in <rdar://problem/6584057>.

        * fast/js/resources/sort-non-numbers.js: Added.
        * fast/js/sort-non-numbers.html: Added.
        * fast/js/sort-non-numbers-expected.txt: Added.



git-svn-id: http://svn.webkit.org/repository/webkit/trunk@40993 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/LayoutTests/fast/js/resources/sort-non-numbers.js b/LayoutTests/fast/js/resources/sort-non-numbers.js
new file mode 100644
index 0000000..21cb5b9
--- /dev/null
+++ b/LayoutTests/fast/js/resources/sort-non-numbers.js
@@ -0,0 +1,10 @@
+description("This tests numerically sorting an array of non-numbers.");
+
+var test = [ "2", "1", "3" ];
+test.sort(function (v1, v2) {
+    return v1 - v2;
+});
+
+shouldBe("String(test)", "'1,2,3'");
+
+var successfullyParsed = true;
diff --git a/LayoutTests/fast/js/sort-non-numbers-expected.txt b/LayoutTests/fast/js/sort-non-numbers-expected.txt
new file mode 100644
index 0000000..7e63c5b
--- /dev/null
+++ b/LayoutTests/fast/js/sort-non-numbers-expected.txt
@@ -0,0 +1,10 @@
+This tests numerically sorting an array of non-numbers.
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+PASS String(test) is '1,2,3'
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
diff --git a/LayoutTests/fast/js/sort-non-numbers.html b/LayoutTests/fast/js/sort-non-numbers.html
new file mode 100644
index 0000000..340e50a
--- /dev/null
+++ b/LayoutTests/fast/js/sort-non-numbers.html
@@ -0,0 +1,13 @@
+<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
+<html>
+<head>
+<link rel="stylesheet" href="resources/js-test-style.css">
+<script src="resources/js-test-pre.js"></script>
+</head>
+<body>
+<p id="description"></p>
+<div id="console"></div>
+<script src="resources/sort-non-numbers.js"></script>
+<script src="resources/js-test-post.js"></script>
+</body>
+</html>