Changeset 27095 in webkit for trunk/JavaScriptCore/kjs/value.cpp


Ignore:
Timestamp:
Oct 26, 2007, 12:51:25 AM (18 years ago)
Author:
darin
Message:

JavaScriptCore:

Reviewed by Maciej.

Gives about 1% gain on SunSpider.

  • kjs/value.h: Added toIntegerPreserveNan, removed toUInt16. (KJS::JSValue::toInt32): Changed to call getTruncatedInt32 in a way that works with both immediate and number values. (KJS::JSValue::toUInt32): Ditto.
  • kjs/value.cpp: (KJS::JSValue::toInteger): Moved the logic from roundValue here, with a couple differences. One is that it now correctly returns 0 for NaN, and another is that there's no special case for 0 or infinity, since the general case already handles those correctly. (KJS::JSValue::toIntegerPreserveNaN): Added. Like toInteger, but without the check for NaN. (KJS::JSValue::toInt32SlowCase): Call toNumber instead of roundValue. The truncation done by the typecast already does the necessary truncation that roundValue was doing. (KJS::JSValue::toUInt32SlowCase): Ditto. (KJS::JSValue::toUInt16): Removed.
  • kjs/internal.h: Removed roundValue.
  • kjs/internal.cpp: Ditto.
  • kjs/array_object.cpp: (KJS::ArrayProtoFunc::callAsFunction): Remove unneeded code to handle NaN in Array.slice; toInteger now never returns NaN as specified.
  • kjs/date_object.cpp: (KJS::fillStructuresUsingTimeArgs): Replaced call to roundValue with a call to toNumber as specified. (KJS::DateProtoFunc::callAsFunction): In SetTime case, replaced call to roundValue with a call to toNumber and timeClip as specified. (KJS::DateObjectImp::construct): Removed unnecessary checks of numArgs in cases where the default behavior of toInt32 (returning 0) was already correct. Replaced call to roundValue with a call to toNumber as specified. (KJS::DateObjectFuncImp::callAsFunction): Ditto.
  • kjs/math_object.cpp: (MathFuncImp::callAsFunction): Removed unnecessary special cases for the pow function that the library already handles correctly.
  • kjs/number_object.cpp: (NumberProtoFunc::callAsFunction): Changed ToString to call toIntegerPreserveNaN, so we can continue to handle the NaN case differently. The real toInteger now returns 0 for NaN. Took out unneeded special case in ToFixed for undefined; was only needed because our toInteger was wrong. Same thing in ToExponential. Changed ToPrecision to call toIntegerPreserveNaN.
  • kjs/string_object.cpp: (KJS::StringProtoFunc::callAsFunction): Took out CharAt and CharCodeAt special cases for undefined that were only needed because toInteger was wrong. Same in IndexOf, and was able to remove some special cases. In LastIndexOf, used toIntegerPreserveNaN, but was able to remove some special cases there too. Changed Substr implementation to preserve correct behavior with the change to toInteger and match the specification. Also made sure we weren't converting an out of range double to an int. (KJS::StringObjectFuncImp::callAsFunction): Changed constructor to just use toUInt32, because truncating toUInt32 to 16 bits is the same thing and there's no reason to have toUInt16 as a second, less-optimized function that's only called at this one call site.
  • wtf/MathExtras.h: Added trunc function for Windows.

LayoutTests:

Reviewed by Maciej.

  • fast/js/resources/char-at.js: Updated test to expect that we get the first character if we pass NaN to charAt and charCodeAt; it's what the specification asks for and matches other browsers too.
  • fast/js/char-at-expected.txt: Updated.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/kjs/value.cpp

    r26912 r27095  
    6262    if (getTruncatedInt32(i))
    6363        return i;
    64     return roundValue(exec, const_cast<JSValue*>(this));
     64    double d = toNumber(exec);
     65    return isNaN(d) ? 0.0 : trunc(d);
     66}
     67
     68double JSValue::toIntegerPreserveNaN(ExecState *exec) const
     69{
     70    int32_t i;
     71    if (getTruncatedInt32(i))
     72        return i;
     73    return trunc(toNumber(exec));
    6574}
    6675
     
    6978    ok = true;
    7079
    71     double d = roundValue(exec, const_cast<JSValue*>(this));
     80    double d = toNumber(exec);
    7281    if (d >= -D32 / 2 && d < D32 / 2)
    7382        return static_cast<int32_t>(d);
     
    7786        return 0;
    7887    }
    79     double d32 = fmod(d, D32);
    8088
     89    double d32 = fmod(trunc(d), D32);
    8190    if (d32 >= D32 / 2)
    8291        d32 -= D32;
    8392    else if (d32 < -D32 / 2)
    8493        d32 += D32;
    85 
    8694    return static_cast<int32_t>(d32);
    8795}
     
    9199    ok = true;
    92100
    93     double d = roundValue(exec, const_cast<JSValue*>(this));
     101    double d = toNumber(exec);
    94102    if (d >= 0.0 && d < D32)
    95103        return static_cast<uint32_t>(d);
     
    99107        return 0;
    100108    }
    101     double d32 = fmod(d, D32);
    102109
     110    double d32 = fmod(trunc(d), D32);
    103111    if (d32 < 0)
    104112        d32 += D32;
    105 
    106113    return static_cast<uint32_t>(d32);
    107 }
    108 
    109 uint16_t JSValue::toUInt16(ExecState *exec) const
    110 {
    111     uint32_t i;
    112     if (getTruncatedUInt32(i))
    113         return static_cast<uint16_t>(i);
    114 
    115     double d = roundValue(exec, const_cast<JSValue*>(this));
    116     if (d >= 0.0 && d < D16)
    117         return static_cast<uint16_t>(d);
    118 
    119     if (isNaN(d) || isInf(d))
    120         return 0;
    121     double d16 = fmod(d, D16);
    122 
    123     if (d16 < 0)
    124         d16 += D16;
    125 
    126     return static_cast<uint16_t>(d16);
    127114}
    128115
Note: See TracChangeset for help on using the changeset viewer.