JavaScriptCore: https://bugs.webkit.org/show_bug.cgi?id=44487
Reviewed by Oliver Hunt.
Number.toExponential/toFixed/toPrecision all contain a spaghetti of duplicated
code & unnecessary complexity. Add a new DecimalNumber class to encapsulate
double to string conversion, share the implementations of rounding &
decimal-fraction/exponential formatting.
Update exports.
- runtime/NumberPrototype.cpp:
(JSC::toThisNumber):
(JSC::getIntegerArgumentInRange):
Helper methods used in implementing toExponential/toFixed/toString.
(JSC::numberProtoFuncToExponential):
(JSC::numberProtoFuncToFixed):
(JSC::numberProtoFuncToPrecision):
Reimplemented using new DecimalNumber class.
(JSC::UString::number):
Updated to call numberToString.
- wtf/DecimalNumber.h: Added.
(WTF::):
(WTF::DecimalNumber::DecimalNumber):
(WTF::DecimalNumber::toStringDecimal):
(WTF::DecimalNumber::toStringExponential):
(WTF::DecimalNumber::sign):
(WTF::DecimalNumber::exponent):
(WTF::DecimalNumber::significand):
(WTF::DecimalNumber::precision):
(WTF::DecimalNumber::init):
(WTF::DecimalNumber::isZero):
(WTF::DecimalNumber::roundToPrecision):
New class to perform double to string conversion.
Has three constructors, which allow conversion with no rounding,
rounding to significant-figures, or rounding to decimal-places,
and two methods for formatting strings, either using decimal
fraction or exponential encoding. Internal implementation uses
pre-rounding of the values before calling dtoa rather than
relying on dtoa to correctly round, which does not produce
fully accurate results. Hopefully we can address this in the
near future.
(WTF::intPow10):
intPow10 is used internally by DecimalNumber.
(WTF::copyToString):
(WTF::nanOrInfToString):
Used internally in numberToString for NaN/Infinity handling.
(WTF::numberToString):
Added new method to convert doubles to strings.
Added declaration for numberToString. This is here because
we should switch over to using this for all double to string
conversion in WebCore (see section 2.4.4.3 of the HTML5 spec).
JavaScriptGlue: https://bugs.webkit.org/show_bug.cgi?id=44487
Reviewed by Oliver Hunt.
- ForwardingHeaders/wtf/text/WTFString.h: Added.
WebCore: https://bugs.webkit.org/show_bug.cgi?id=44487
Reviewed by Oliver Hunt.
- html/LegacyHTMLTreeBuilder.cpp:
(WebCore::serializeForNumberType):
Update function call to numberToString.
LayoutTests: Bug 44487 - Clean up NumberPrototype.cpp
Reviewed by Oliver Hunt.
This patch changes some layout test results - in all these cases we were
not previously accurate to spec defined behaviour, and we are still not
- but overall this changes reduces the overall magnitude of error due to
rounding differences. The underlying problem is that we should be using
dtoa to generate results to a specified accuracy, rather than relying on
pre-rounding the input values. We should look at reenabling our dtoa
implementation to work in this fashion as a separate change.
- fast/js/kde/Number-expected.txt:
- fast/js/kde/script-tests/Number.js:
- fast/js/number-toExponential-expected.txt:
- fast/js/number-tofixed-expected.txt:
- fast/js/number-toprecision-expected.txt:
- fast/js/script-tests/number-toExponential.js:
- fast/js/script-tests/number-tofixed.js:
- fast/js/script-tests/number-toprecision.js: