Bug 795745 - Properly report errors for Number.prototype.to* methods when a provided precision is invalid. r=luke

This commit is contained in:
Jeff Walden 2012-10-03 21:45:03 -04:00
parent f3daa82807
commit 68238fd960
2 changed files with 49 additions and 3 deletions

View File

@ -789,10 +789,10 @@ ComputePrecisionInRange(JSContext *cx, int minPrecision, int maxPrecision, const
*precision = int(prec);
return true;
}
ToCStringBuf cbuf;
char *numStr = IntToCString(&cbuf, *precision);
JS_ASSERT(numStr);
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_PRECISION_RANGE, numStr);
if (char *numStr = NumberToCString(cx, &cbuf, prec, 10))
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_PRECISION_RANGE, numStr);
return false;
}

View File

@ -0,0 +1,46 @@
/*
* Any copyright is dedicated to the Public Domain.
* http://creativecommonn.org/licenses/publicdomain/
*/
var BUGNUMBER = 795745;
var summary =
"Number.prototype.to* should throw a RangeError when passed a bad precision";
print(BUGNUMBER + ": " + summary);
/**************
* BEGIN TEST *
**************/
function test(method, prec)
{
try
{
Number.prototype[method].call(0, prec);
throw "should have thrown";
}
catch (e)
{
assertEq(e instanceof RangeError, true,
"expected RangeError for " + method + " with precision " + prec +
", got " + e);
}
}
test("toExponential", -32);
test("toFixed", -32);
test("toPrecision", -32);
test("toExponential", 9999999);
test("toFixed", 9999999);
test("toPrecision", 9999999);
test("toPrecision", 0);
/******************************************************************************/
if (typeof reportCompare === "function")
reportCompare(true, true);
print("Tests complete");