Ignore:
Timestamp:
Jan 2, 2012, 8:32:00 PM (14 years ago)
Author:
[email protected]
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.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.