Bug 1040196 - Implement ES6 ToLength r=jorendorff

This commit is contained in:
Guptha Rajagopal 2014-07-30 10:34:00 +02:00
parent a035812740
commit 6645b03a5b
2 changed files with 52 additions and 0 deletions

View File

@ -144,6 +144,17 @@ function CheckObjectCoercible(v) {
ThrowError(JSMSG_CANT_CONVERT_TO, ToString(v), "object");
}
/* Spec: ECMAScript Draft, 6 edition May 22, 2014, 7.1.15 */
function ToLength(v) {
v = ToInteger(v);
if (v <= 0)
return 0;
// Math.pow(2, 53) - 1 = 0x1fffffffffffff
return v < 0x1fffffffffffff ? v : 0x1fffffffffffff;
}
/********** Testing code **********/
#ifdef ENABLE_PARALLEL_JS

View File

@ -0,0 +1,41 @@
// |reftest| skip-if(!xulRuntime.shell)
var BUGNUMBER = 1040196;
var summary = 'ToLength';
print(BUGNUMBER + ": " + summary);
var ToLength = getSelfHostedValue('ToLength');
// Negative operands
assertEq(ToLength(-0), 0);
assertEq(ToLength(-1), 0);
assertEq(ToLength(-2), 0);
assertEq(ToLength(-1 * Math.pow(2, 56)), 0);
assertEq(ToLength(-1 * Math.pow(2, 56) - 2), 0);
assertEq(ToLength(-1 * Math.pow(2, 56) - 2.4444), 0);
assertEq(ToLength(-Infinity), 0);
// Small non-negative operands
assertEq(ToLength(0), 0);
assertEq(ToLength(1), 1);
assertEq(ToLength(2), 2);
assertEq(ToLength(3.3), 3);
assertEq(ToLength(10/3), 3);
// Large non-negative operands
var maxLength = Math.pow(2, 53) - 1;
assertEq(ToLength(maxLength - 1), maxLength - 1);
assertEq(ToLength(maxLength - 0.0000001), maxLength);
assertEq(ToLength(maxLength), maxLength);
assertEq(ToLength(maxLength + 0.00000000000001), maxLength);
assertEq(ToLength(maxLength + 1), maxLength);
assertEq(ToLength(maxLength + 2), maxLength);
assertEq(ToLength(Math.pow(2,54)), maxLength);
assertEq(ToLength(Math.pow(2,64)), maxLength);
assertEq(ToLength(Infinity), maxLength);
// NaN operand
assertEq(ToLength(NaN), 0);
reportCompare(0, 0, "ok");