Changeset 103922 in webkit


Ignore:
Timestamp:
Jan 2, 2012 8:32:00 PM (12 years ago)
Author:
barraclough@apple.com
Message:

ES5 prohibits parseInt from supporting octal
https://bugs.webkit.org/show_bug.cgi?id=75455

Reviewed by Sam Weinig.

See sections 15.1.2.2 and annex E.

Source/JavaScriptCore:

  • runtime/JSGlobalObjectFunctions.cpp:

(JSC::parseInt):

LayoutTests:

  • fast/js/kde/GlobalObject-expected.txt:
  • fast/js/kde/script-tests/GlobalObject.js:
    • Change test to check for correct result.
  • sputnik/Implementation_Diagnostics/S15.1.2.2_D1.1-expected.txt:
  • sputnik/Implementation_Diagnostics/S15.1.2.2_D1.2-expected.txt:
    • One test asserts parseInt handles octal, one asserts it does not! This patch switches which one passes & which fails.
Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r103921 r103922  
     12012-01-02  Gavin Barraclough  <barraclough@apple.com>
     2
     3        ES5 prohibits parseInt from supporting octal
     4        https://bugs.webkit.org/show_bug.cgi?id=75455
     5
     6        Reviewed by Sam Weinig.
     7
     8        See sections 15.1.2.2 and annex E.
     9
     10        * fast/js/kde/GlobalObject-expected.txt:
     11        * fast/js/kde/script-tests/GlobalObject.js:
     12            - Change test to check for correct result.
     13        * sputnik/Implementation_Diagnostics/S15.1.2.2_D1.1-expected.txt:
     14        * sputnik/Implementation_Diagnostics/S15.1.2.2_D1.2-expected.txt:
     15            - One test asserts parseInt handles octal, one asserts it does not!
     16              This patch switches which one passes & which fails.
     17
    1182012-01-02  Gavin Barraclough  <barraclough@apple.com>
    219
  • trunk/LayoutTests/fast/js/kde/GlobalObject-expected.txt

    r11962 r103922  
    3939PASS isNaN(parseInt('Infinity')) is true
    4040PASS parseInt("15") is 15
    41 PASS parseInt("017") is 15
     41PASS parseInt("015") is 15
    4242PASS parseInt("0xf") is 15
    4343PASS parseInt("15", 0) is 15
  • trunk/LayoutTests/fast/js/kde/script-tests/GlobalObject.js

    r98407 r103922  
    4747// all should return 15
    4848shouldBe('parseInt("15")', "15");
    49 shouldBe('parseInt("017")', "15"); /// The spec says it's ok, and even encouraged, to parse this as 17!
     49shouldBe('parseInt("015")', "15"); // ES5 prohibits parseInt from handling octal, see annex E.
    5050shouldBe('parseInt("0xf")', "15");
    5151shouldBe('parseInt("15", 0)', "15");
  • trunk/LayoutTests/sputnik/Implementation_Diagnostics/S15.1.2.2_D1.1-expected.txt

    r58534 r103922  
    11S15.1.2.2_D1.1
    22
    3 PASS
     3FAIL SputnikError: #1: parseInt("08") === parseInt("08", 8)
    44
    55TEST COMPLETE
  • trunk/LayoutTests/sputnik/Implementation_Diagnostics/S15.1.2.2_D1.2-expected.txt

    r58534 r103922  
    11S15.1.2.2_D1.2
    22
    3 FAIL SputnikError: #1: parseInt("08") === parseInt("08", 10)
     3PASS
    44
    55TEST COMPLETE
  • trunk/Source/JavaScriptCore/ChangeLog

    r103921 r103922  
     12012-01-02  Gavin Barraclough  <barraclough@apple.com>
     2
     3        ES5 prohibits parseInt from supporting octal
     4        https://bugs.webkit.org/show_bug.cgi?id=75455
     5
     6        Reviewed by Sam Weinig.
     7
     8        See sections 15.1.2.2 and annex E.
     9
     10        * runtime/JSGlobalObjectFunctions.cpp:
     11        (JSC::parseInt):
     12
    1132012-01-02  Gavin Barraclough  <barraclough@apple.com>
    214
  • trunk/Source/JavaScriptCore/runtime/JSGlobalObjectFunctions.cpp

    r102182 r103922  
    231231}
    232232
     233// ES5.1 15.1.2.2
    233234template <typename CharType>
    234235ALWAYS_INLINE
    235236static double parseInt(const UString& s, const CharType* data, int radix)
    236237{
     238    // 1. Let inputString be ToString(string).
     239    // 2. Let S be a newly created substring of inputString consisting of the first character that is not a
     240    //    StrWhiteSpaceChar and all characters following that character. (In other words, remove leading white
     241    //    space.) If inputString does not contain any such characters, let S be the empty string.
    237242    int length = s.length();
    238243    int p = 0;
    239 
    240244    while (p < length && isStrWhiteSpace(data[p]))
    241245        ++p;
    242246
     247    // 3. Let sign be 1.
     248    // 4. If S is not empty and the first character of S is a minus sign -, let sign be -1.
     249    // 5. If S is not empty and the first character of S is a plus sign + or a minus sign -, then remove the first character from S.
    243250    double sign = 1;
    244251    if (p < length) {
     
    251258    }
    252259
     260    // 6. Let R = ToInt32(radix).
     261    // 7. Let stripPrefix be true.
     262    // 8. If R != 0,then
     263    //   b. If R != 16, let stripPrefix be false.
     264    // 9. Else, R == 0
     265    //   a. LetR = 10.
     266    // 10. If stripPrefix is true, then
     267    //   a. If the length of S is at least 2 and the first two characters of S are either ―0x or ―0X,
     268    //      then remove the first two characters from S and let R = 16.
     269    // 11. If S contains any character that is not a radix-R digit, then let Z be the substring of S
     270    //     consisting of all characters before the first such character; otherwise, let Z be S.
    253271    if ((radix == 0 || radix == 16) && length - p >= 2 && data[p] == '0' && (data[p + 1] == 'x' || data[p + 1] == 'X')) {
    254272        radix = 16;
    255273        p += 2;
    256     } else if (radix == 0) {
    257         if (p < length && data[p] == '0')
    258             radix = 8;
    259         else
    260             radix = 10;
    261     }
    262 
     274    } else if (radix == 0)
     275        radix = 10;
     276
     277    // 8.a If R < 2 or R > 36, then return NaN.
    263278    if (radix < 2 || radix > 36)
    264279        return std::numeric_limits<double>::quiet_NaN();
    265280
     281    // 13. Let mathInt be the mathematical integer value that is represented by Z in radix-R notation, using the letters
     282    //     A-Z and a-z for digits with values 10 through 35. (However, if R is 10 and Z contains more than 20 significant
     283    //     digits, every significant digit after the 20th may be replaced by a 0 digit, at the option of the implementation;
     284    //     and if R is not 2, 4, 8, 10, 16, or 32, then mathInt may be an implementation-dependent approximation to the
     285    //     mathematical integer value that is represented by Z in radix-R notation.)
     286    // 14. Let number be the Number value for mathInt.
    266287    int firstDigitPosition = p;
    267288    bool sawDigit = false;
     
    276297        ++p;
    277298    }
    278 
    279299    if (number >= mantissaOverflowLowerBound) {
    280300        if (radix == 10)
     
    284304    }
    285305
     306    // 12. If Z is empty, return NaN.
    286307    if (!sawDigit)
    287308        return std::numeric_limits<double>::quiet_NaN();
    288309
     310    // 15. Return sign x number.
    289311    return sign * number;
    290312}
Note: See TracChangeset for help on using the changeset viewer.