8011552: Arrays with missing elements are not properly sorted
authorsundar
Thu, 04 Apr 2013 20:46:31 +0530 (2013-04-04)
changeset 16779 61fd89656ce3
parent 16778 55e38b535002
child 16780 f23743ec1a93
8011552: Arrays with missing elements are not properly sorted Reviewed-by: jlaskey, lagergren
nashorn/src/jdk/nashorn/internal/objects/NativeArray.java
nashorn/test/script/basic/JDK-8011552.js
--- a/nashorn/src/jdk/nashorn/internal/objects/NativeArray.java	Thu Apr 04 15:55:42 2013 +0200
+++ b/nashorn/src/jdk/nashorn/internal/objects/NativeArray.java	Thu Apr 04 20:46:31 2013 +0530
@@ -849,17 +849,26 @@
             final long         len     = JSType.toUint32(sobj.getLength());
 
             if (len > 1) {
-                final Object[] src = new Object[(int) len];
-                for (int i = 0; i < src.length; i++) {
-                    src[i] = sobj.get(i);
+                // Get only non-missing elements. Missing elements go at the end
+                // of the sorted array. So, just don't copy these to sort input.
+
+                final ArrayList<Object> src = new ArrayList<>();
+                for (int i = 0; i < (int)len; i++) {
+                    if (sobj.has(i)) {
+                        src.add(sobj.get(i));
+                    }
                 }
 
-                final Object[] sorted = sort(src, comparefn);
-                assert sorted.length == src.length;
+                final Object[] sorted = sort(src.toArray(), comparefn);
 
                 for (int i = 0; i < sorted.length; i++) {
                     sobj.set(i, sorted[i], strict);
                 }
+
+                // delete missing elements - which are at the end of sorted array
+                for (int j = sorted.length; j < (int)len; j++) {
+                    sobj.delete(j, strict);
+                }
             }
 
             return sobj;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8011552.js	Thu Apr 04 20:46:31 2013 +0530
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ * 
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ * 
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ * 
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ * 
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * JDK-8011552: Arrays with missing elements are not properly sorted
+ *
+ * @test
+ * @run
+ */
+
+if ([,void 0].sort().hasOwnProperty("1")) {
+    fail("missing element found in sorted array");
+}
+
+if ([1,,2,,-1].sort().toString() != "-1,1,2,,") {
+    faiil("missing elements are not at the end of sorted array");
+}