JavaScriptCore:
Reviewed by Maciej.
- http://bugs.webkit.org/show_bug.cgi?id=15703
fix numeric functions -- improve correctness and speed
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.
- test changes for http://bugs.webkit.org/show_bug.cgi?id=15703
fix numeric functions -- improve correctness and speed
* 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.
git-svn-id: http://svn.webkit.org/repository/webkit/trunk@27095 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/JavaScriptCore/kjs/string_object.cpp b/JavaScriptCore/kjs/string_object.cpp
index 0698e11..c76f894 100644
--- a/JavaScriptCore/kjs/string_object.cpp
+++ b/JavaScriptCore/kjs/string_object.cpp
@@ -457,20 +457,16 @@
// handled above
break;
case CharAt:
- // Other browsers treat an omitted parameter as 0 rather than NaN.
- // That doesn't match the ECMA standard, but is needed for site compatibility.
- dpos = a0->isUndefined() ? 0 : a0->toInteger(exec);
- if (dpos >= 0 && dpos < len) // false for NaN
+ dpos = a0->toInteger(exec);
+ if (dpos >= 0 && dpos < len)
u = s.substr(static_cast<int>(dpos), 1);
else
u = "";
result = jsString(u);
break;
case CharCodeAt:
- // Other browsers treat an omitted parameter as 0 rather than NaN.
- // That doesn't match the ECMA standard, but is needed for site compatibility.
- dpos = a0->isUndefined() ? 0 : a0->toInteger(exec);
- if (dpos >= 0 && dpos < len) // false for NaN
+ dpos = a0->toInteger(exec);
+ if (dpos >= 0 && dpos < len)
result = jsNumber(s[static_cast<int>(dpos)].unicode());
else
result = jsNaN();
@@ -485,31 +481,21 @@
}
case IndexOf:
u2 = a0->toString(exec);
- if (a1->isUndefined())
+ dpos = a1->toInteger(exec);
+ if (dpos < 0)
dpos = 0;
- else {
- dpos = a1->toInteger(exec);
- if (dpos >= 0) { // false for NaN
- if (dpos > len)
- dpos = len;
- } else
- dpos = 0;
- }
+ else if (dpos > len)
+ dpos = len;
result = jsNumber(s.find(u2, static_cast<int>(dpos)));
break;
case LastIndexOf:
u2 = a0->toString(exec);
d = a1->toNumber(exec);
- if (a1->isUndefined() || KJS::isNaN(d))
+ dpos = a1->toIntegerPreserveNaN(exec);
+ if (dpos < 0)
+ dpos = 0;
+ else if (!(dpos <= len)) // true for NaN
dpos = len;
- else {
- dpos = a1->toInteger(exec);
- if (dpos >= 0) { // false for NaN
- if (dpos > len)
- dpos = len;
- } else
- dpos = 0;
- }
result = jsNumber(s.rfind(u2, static_cast<int>(dpos)));
break;
case Match:
@@ -658,22 +644,20 @@
}
break;
case Substr: {
- double d = a0->toInteger(exec);
- double d2 = a1->toInteger(exec);
- if (!(d >= 0)) { // true for NaN
- d += len;
- if (!(d >= 0)) // true for NaN
- d = 0;
+ double start = a0->toInteger(exec);
+ double length = a1->isUndefined() ? len : a1->toInteger(exec);
+ if (start >= len)
+ return jsString("");
+ if (length < 0)
+ return jsString("");
+ if (start < 0) {
+ start += len;
+ if (start < 0)
+ start = 0;
}
- if (isNaN(d2))
- d2 = len - d;
- else {
- if (d2 < 0)
- d2 = 0;
- if (d2 > len - d)
- d2 = len - d;
- }
- result = jsString(s.substr(static_cast<int>(d), static_cast<int>(d2)));
+ if (length > len - d)
+ length = len - d;
+ result = jsString(s.substr(static_cast<int>(start), static_cast<int>(length)));
break;
}
case Substring: {
@@ -852,7 +836,7 @@
UChar *p = buf;
ListIterator it = args.begin();
while (it != args.end()) {
- unsigned short u = it->toUInt16(exec);
+ unsigned short u = it->toUInt32(exec);
*p++ = UChar(u);
it++;
}