< prev index next >

src/jdk/nashorn/internal/objects/NativeArray.java

Print this page
rev 1337 : 8080182: Array.prototype.sort throws IAE on inconsistent comparison
Reviewed-by: lagergren, hannesw

*** 1231,1240 **** --- 1231,1241 ---- 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) {
*** 1256,1265 **** --- 1257,1275 ---- } 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 >