ECMA fix to (117144).

Array.prototype.sort(comparefn) was casting the result of the compare
to an int, which lost when the compare function returned (ecma-valid)
strange double values.  These now get clamped to -1, 0, 1.
This commit is contained in:
mccabe 1998-05-13 23:29:52 +00:00
parent df793a8c99
commit 9e50a5d27b
2 changed files with 28 additions and 4 deletions

View File

@ -622,10 +622,22 @@ sort_compare(const void *a, const void *b, void *arg)
ok = js_CallFunctionValue(cx,
OBJ_GET_PARENT(cx, JSVAL_TO_OBJECT(fval)),
fval, 2, argv, &rval);
if (ok)
if (ok) {
ok = js_ValueToNumber(cx, rval, &cmp);
if (!ok)
/* Clamp cmp to -1, 0, 1. */
if (JSDOUBLE_IS_NaN(cmp)) {
/* XXX report some kind of error here? ECMA talks about
* 'consistent compare functions' that don't return NaN, but is
* silent about what the result should be. So we currently
* ignore it.
*/
cmp = 0;
} else if (cmp != 0) {
cmp = cmp > 0 ? 1 : -1;
}
} else {
ca->status = ok;
}
}
return (int)cmp;
}

View File

@ -622,10 +622,22 @@ sort_compare(const void *a, const void *b, void *arg)
ok = js_CallFunctionValue(cx,
OBJ_GET_PARENT(cx, JSVAL_TO_OBJECT(fval)),
fval, 2, argv, &rval);
if (ok)
if (ok) {
ok = js_ValueToNumber(cx, rval, &cmp);
if (!ok)
/* Clamp cmp to -1, 0, 1. */
if (JSDOUBLE_IS_NaN(cmp)) {
/* XXX report some kind of error here? ECMA talks about
* 'consistent compare functions' that don't return NaN, but is
* silent about what the result should be. So we currently
* ignore it.
*/
cmp = 0;
} else if (cmp != 0) {
cmp = cmp > 0 ? 1 : -1;
}
} else {
ca->status = ok;
}
}
return (int)cmp;
}