Bug 1079120 - Make ToNumber(string) support binary and octal literals. r=till

This commit is contained in:
ziyunfei 2014-10-07 02:25:00 -04:00
parent be0ddfa0f1
commit 52125052b8
2 changed files with 52 additions and 17 deletions

View File

@ -1452,24 +1452,33 @@ CharsToNumber(ThreadSafeContext *cx, const CharT *chars, size_t length, double *
const CharT *end = chars + length;
const CharT *bp = SkipSpace(chars, end);
/* ECMA doesn't allow signed hex numbers (bug 273467). */
if (end - bp >= 2 && bp[0] == '0' && (bp[1] == 'x' || bp[1] == 'X')) {
/*
* It's probably a hex number. Accept if there's at least one hex
* digit after the 0x, and if no non-whitespace characters follow all
* the hex digits.
*/
const CharT *endptr;
double d;
if (!GetPrefixInteger(cx, bp + 2, end, 16, &endptr, &d) ||
endptr == bp + 2 ||
SkipSpace(endptr, end) != end)
{
*result = GenericNaN();
} else {
*result = d;
/* ECMA doesn't allow signed non-decimal numbers (bug 273467). */
if (end - bp >= 2 && bp[0] == '0') {
int radix = 0;
if (bp[1] == 'b' || bp[1] == 'B')
radix = 2;
else if (bp[1] == 'o' || bp[1] == 'O')
radix = 8;
else if (bp[1] == 'x' || bp[1] == 'X')
radix = 16;
if (radix != 0) {
/*
* It's probably a non-decimal number. Accept if there's at least one digit after
* the 0b|0o|0x, and if no non-whitespace characters follow all the digits.
*/
const CharT *endptr;
double d;
if (!GetPrefixInteger(cx, bp + 2, end, radix, &endptr, &d) ||
endptr == bp + 2 ||
SkipSpace(endptr, end) != end)
{
*result = GenericNaN();
} else {
*result = d;
}
return true;
}
return true;
}
/*

View File

@ -0,0 +1,26 @@
/*
* Any copyright is dedicated to the Public Domain.
* https://creativecommons.org/publicdomain/zero/1.0/
*/
assertEq(Number("0b11"), 3);
assertEq(Number("0B11"), 3);
assertEq(Number(" 0b11 "), 3);
assertEq(Number("0b12"), NaN);
assertEq(Number("-0b11"), NaN);
assertEq(+"0b11", 3);
assertEq(Number("0o66"), 54);
assertEq(Number("0O66"), 54);
assertEq(Number(" 0o66 "), 54);
assertEq(Number("0o88"), NaN);
assertEq(Number("-0o66"), NaN);
assertEq(+"0o66", 54);
if(typeof getSelfHostedValue === "function"){
assertEq(getSelfHostedValue("ToNumber")("0b11"), 3);
assertEq(getSelfHostedValue("ToNumber")("0o66"), 54);
}
if (typeof reportCompare === "function")
reportCompare(true, true);