< prev index next >

src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeArray.java

Print this page

        

@@ -1226,10 +1226,11 @@
         final ScriptFunction cmp = compareFunction(comparefn);
 
         final List<Object> list = Arrays.asList(array);
         final Object cmpThis = cmp == null || cmp.isStrict() ? ScriptRuntime.UNDEFINED : Global.instance();
 
+        try {
         Collections.sort(list, new Comparator<Object>() {
             private final MethodHandle call_cmp = getCALL_CMP();
             @Override
             public int compare(final Object x, final Object y) {
                 if (x == ScriptRuntime.UNDEFINED && y == ScriptRuntime.UNDEFINED) {

@@ -1251,10 +1252,19 @@
                 }
 
                 return JSType.toString(x).compareTo(JSType.toString(y));
             }
         });
+        } catch (final IllegalArgumentException iae) {
+            // Collections.sort throws IllegalArgumentException when
+            // Comparison method violates its general contract
+
+            // See ECMA spec 15.4.4.11 Array.prototype.sort (comparefn).
+            // If "comparefn" is not undefined and is not a consistent
+            // comparison function for the elements of this array, the
+            // behaviour of sort is implementation-defined.
+        }
 
         return list.toArray(new Object[array.length]);
     }
 
     /**
< prev index next >