NumberPrototype.cpp [plain text]
#include "config.h"
#include "NumberPrototype.h"
#include "BigInteger.h"
#include "Error.h"
#include "JSFunction.h"
#include "JSGlobalObject.h"
#include "JSString.h"
#include "Operations.h"
#include "Uint16WithFraction.h"
#include <wtf/dtoa.h>
#include <wtf/Assertions.h>
#include <wtf/MathExtras.h>
#include <wtf/Vector.h>
#include <wtf/dtoa/double-conversion.h>
using namespace WTF::double_conversion;
typedef WTF::double_conversion::StringBuilder DoubleConversionStringBuilder;
namespace JSC {
static EncodedJSValue JSC_HOST_CALL numberProtoFuncToString(ExecState*);
static EncodedJSValue JSC_HOST_CALL numberProtoFuncToLocaleString(ExecState*);
static EncodedJSValue JSC_HOST_CALL numberProtoFuncValueOf(ExecState*);
static EncodedJSValue JSC_HOST_CALL numberProtoFuncToFixed(ExecState*);
static EncodedJSValue JSC_HOST_CALL numberProtoFuncToExponential(ExecState*);
static EncodedJSValue JSC_HOST_CALL numberProtoFuncToPrecision(ExecState*);
}
#include "NumberPrototype.lut.h"
namespace JSC {
const ClassInfo NumberPrototype::s_info = { "Number", &NumberObject::s_info, 0, ExecState::numberPrototypeTable, CREATE_METHOD_TABLE(NumberPrototype) };
ASSERT_HAS_TRIVIAL_DESTRUCTOR(NumberPrototype);
NumberPrototype::NumberPrototype(ExecState* exec, Structure* structure)
: NumberObject(exec->vm(), structure)
{
}
void NumberPrototype::finishCreation(ExecState* exec, JSGlobalObject*)
{
Base::finishCreation(exec->vm());
setInternalValue(exec->vm(), jsNumber(0));
ASSERT(inherits(&s_info));
}
bool NumberPrototype::getOwnPropertySlot(JSCell* cell, ExecState* exec, PropertyName propertyName, PropertySlot &slot)
{
return getStaticFunctionSlot<NumberObject>(exec, ExecState::numberPrototypeTable(exec), jsCast<NumberPrototype*>(cell), propertyName, slot);
}
bool NumberPrototype::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, PropertyName propertyName, PropertyDescriptor& descriptor)
{
return getStaticFunctionDescriptor<NumberObject>(exec, ExecState::numberPrototypeTable(exec), jsCast<NumberPrototype*>(object), propertyName, descriptor);
}
static ALWAYS_INLINE bool toThisNumber(JSValue thisValue, double& x)
{
if (thisValue.isInt32()) {
x = thisValue.asInt32();
return true;
}
if (thisValue.isDouble()) {
x = thisValue.asDouble();
return true;
}
if (thisValue.isCell() && thisValue.asCell()->structure()->typeInfo().isNumberObject()) {
x = static_cast<const NumberObject*>(thisValue.asCell())->internalValue().asNumber();
return true;
}
return false;
}
static ALWAYS_INLINE bool getIntegerArgumentInRange(ExecState* exec, int low, int high, int& result, bool& isUndefined)
{
result = 0;
isUndefined = false;
JSValue argument0 = exec->argument(0);
if (argument0.isUndefined()) {
isUndefined = true;
return true;
}
double asDouble = argument0.toInteger(exec);
if (asDouble < low || asDouble > high)
return false;
result = static_cast<int>(asDouble);
return true;
}
typedef char RadixBuffer[2180];
static const char radixDigits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
static char* toStringWithRadix(RadixBuffer& buffer, double number, unsigned radix)
{
ASSERT(std::isfinite(number));
ASSERT(radix >= 2 && radix <= 36);
char* decimalPoint = buffer + sizeof(buffer) / 2;
char* startOfResultString = decimalPoint;
bool isNegative = number < 0;
if (std::signbit(number))
number = -number;
double integerPart = floor(number);
bool integerPartIsOdd = integerPart <= static_cast<double>(0x1FFFFFFFFFFFFFull) && static_cast<int64_t>(integerPart) & 1;
ASSERT(integerPartIsOdd == static_cast<bool>(fmod(integerPart, 2)));
bool isOddInOddRadix = integerPartIsOdd;
uint32_t digit = integerPartIsOdd;
double fractionPart = number - integerPart;
if (fractionPart) {
*decimalPoint = '.';
Uint16WithFraction fraction(fractionPart);
bool needsRoundingUp = false;
char* endOfResultString = decimalPoint + 1;
double nextNumber = nextafter(number, std::numeric_limits<double>::infinity());
double lastNumber = nextafter(number, -std::numeric_limits<double>::infinity());
ASSERT(std::isfinite(nextNumber) && !std::signbit(nextNumber));
ASSERT(std::isfinite(lastNumber) && !std::signbit(lastNumber));
double deltaNextDouble = nextNumber - number;
double deltaLastDouble = number - lastNumber;
ASSERT(std::isfinite(deltaNextDouble) && !std::signbit(deltaNextDouble));
ASSERT(std::isfinite(deltaLastDouble) && !std::signbit(deltaLastDouble));
if (deltaNextDouble != deltaLastDouble) {
Uint16WithFraction halfDeltaNext(deltaNextDouble, 1);
Uint16WithFraction halfDeltaLast(deltaLastDouble, 1);
while (true) {
int dComparePoint5 = fraction.comparePoint5();
if (dComparePoint5 > 0 || (!dComparePoint5 && (radix & 1 ? isOddInOddRadix : digit & 1))) {
if (fraction.sumGreaterThanOne(halfDeltaNext)) {
needsRoundingUp = true;
break;
}
} else {
if (fraction < halfDeltaLast)
break;
}
ASSERT(endOfResultString < (buffer + sizeof(buffer) - 1));
fraction *= radix;
digit = fraction.floorAndSubtract();
*endOfResultString++ = radixDigits[digit];
if (digit & 1)
isOddInOddRadix = !isOddInOddRadix;
halfDeltaNext *= radix;
halfDeltaLast *= radix;
}
} else {
Uint16WithFraction halfDelta(deltaNextDouble, 1);
while (true) {
int dComparePoint5 = fraction.comparePoint5();
if (dComparePoint5 > 0 || (!dComparePoint5 && (radix & 1 ? isOddInOddRadix : digit & 1))) {
if (fraction.sumGreaterThanOne(halfDelta)) {
needsRoundingUp = true;
break;
}
} else if (fraction < halfDelta)
break;
ASSERT(endOfResultString < (buffer + sizeof(buffer) - 1));
fraction *= radix;
digit = fraction.floorAndSubtract();
if (digit & 1)
isOddInOddRadix = !isOddInOddRadix;
*endOfResultString++ = radixDigits[digit];
halfDelta *= radix;
}
}
if (needsRoundingUp) {
while (endOfResultString[-1] == radixDigits[radix - 1])
--endOfResultString;
if (endOfResultString[-1] == '9')
endOfResultString[-1] = 'a';
else if (endOfResultString[-1] != '.')
++endOfResultString[-1];
else {
--endOfResultString;
ASSERT((integerPart + 1) - integerPart == 1);
++integerPart;
}
} else {
while (endOfResultString[-1] == '0')
--endOfResultString;
}
*endOfResultString = '\0';
ASSERT(endOfResultString < buffer + sizeof(buffer));
} else
*decimalPoint = '\0';
BigInteger units(integerPart);
do {
ASSERT(buffer < startOfResultString);
digit = units.divide(radix);
*--startOfResultString = radixDigits[digit];
} while (!!units);
if (isNegative)
*--startOfResultString = '-';
ASSERT(buffer <= startOfResultString);
return startOfResultString;
}
static String toStringWithRadix(int32_t number, unsigned radix)
{
LChar buf[1 + 32]; LChar* end = buf + WTF_ARRAY_LENGTH(buf);
LChar* p = end;
bool negative = false;
uint32_t positiveNumber = number;
if (number < 0) {
negative = true;
positiveNumber = -number;
}
while (positiveNumber) {
uint32_t index = positiveNumber % radix;
ASSERT(index < sizeof(radixDigits));
*--p = static_cast<LChar>(radixDigits[index]);
positiveNumber /= radix;
}
if (negative)
*--p = '-';
return String(p, static_cast<unsigned>(end - p));
}
EncodedJSValue JSC_HOST_CALL numberProtoFuncToExponential(ExecState* exec)
{
double x;
if (!toThisNumber(exec->hostThisValue(), x))
return throwVMTypeError(exec);
int decimalPlacesInExponent;
bool isUndefined;
if (!getIntegerArgumentInRange(exec, 0, 20, decimalPlacesInExponent, isUndefined))
return throwVMError(exec, createRangeError(exec, ASCIILiteral("toExponential() argument must be between 0 and 20")));
if (!std::isfinite(x))
return JSValue::encode(jsString(exec, String::numberToStringECMAScript(x)));
char buffer[WTF::NumberToStringBufferLength];
DoubleConversionStringBuilder builder(buffer, WTF::NumberToStringBufferLength);
const DoubleToStringConverter& converter = DoubleToStringConverter::EcmaScriptConverter();
builder.Reset();
isUndefined
? converter.ToExponential(x, -1, &builder)
: converter.ToExponential(x, decimalPlacesInExponent, &builder);
return JSValue::encode(jsString(exec, String(builder.Finalize())));
}
EncodedJSValue JSC_HOST_CALL numberProtoFuncToFixed(ExecState* exec)
{
double x;
if (!toThisNumber(exec->hostThisValue(), x))
return throwVMTypeError(exec);
int decimalPlaces;
bool isUndefined; if (!getIntegerArgumentInRange(exec, 0, 20, decimalPlaces, isUndefined))
return throwVMError(exec, createRangeError(exec, ASCIILiteral("toFixed() argument must be between 0 and 20")));
if (!(fabs(x) < 1e+21))
return JSValue::encode(jsString(exec, String::numberToStringECMAScript(x)));
ASSERT(std::isfinite(x));
NumberToStringBuffer buffer;
return JSValue::encode(jsString(exec, String(numberToFixedWidthString(x, decimalPlaces, buffer))));
}
EncodedJSValue JSC_HOST_CALL numberProtoFuncToPrecision(ExecState* exec)
{
double x;
if (!toThisNumber(exec->hostThisValue(), x))
return throwVMTypeError(exec);
int significantFigures;
bool isUndefined;
if (!getIntegerArgumentInRange(exec, 1, 21, significantFigures, isUndefined))
return throwVMError(exec, createRangeError(exec, ASCIILiteral("toPrecision() argument must be between 1 and 21")));
if (isUndefined)
return JSValue::encode(jsString(exec, String::numberToStringECMAScript(x)));
if (!std::isfinite(x))
return JSValue::encode(jsString(exec, String::numberToStringECMAScript(x)));
NumberToStringBuffer buffer;
return JSValue::encode(jsString(exec, String(numberToFixedPrecisionString(x, significantFigures, buffer))));
}
static inline int32_t extractRadixFromArgs(ExecState* exec)
{
JSValue radixValue = exec->argument(0);
int32_t radix;
if (radixValue.isInt32())
radix = radixValue.asInt32();
else if (radixValue.isUndefined())
radix = 10;
else
radix = static_cast<int32_t>(radixValue.toInteger(exec));
return radix;
}
static inline EncodedJSValue integerValueToString(ExecState* exec, int32_t radix, int32_t value)
{
if (static_cast<unsigned>(value) < static_cast<unsigned>(radix)) {
ASSERT(value <= 36);
ASSERT(value >= 0);
VM* vm = &exec->vm();
return JSValue::encode(vm->smallStrings.singleCharacterString(vm, radixDigits[value]));
}
if (radix == 10) {
VM* vm = &exec->vm();
return JSValue::encode(jsString(vm, vm->numericStrings.add(value)));
}
return JSValue::encode(jsString(exec, toStringWithRadix(value, radix)));
}
EncodedJSValue JSC_HOST_CALL numberProtoFuncToString(ExecState* exec)
{
double doubleValue;
if (!toThisNumber(exec->hostThisValue(), doubleValue))
return throwVMTypeError(exec);
int32_t radix = extractRadixFromArgs(exec);
if (radix < 2 || radix > 36)
return throwVMError(exec, createRangeError(exec, ASCIILiteral("toString() radix argument must be between 2 and 36")));
int32_t integerValue = static_cast<int32_t>(doubleValue);
if (integerValue == doubleValue)
return integerValueToString(exec, radix, integerValue);
if (radix == 10) {
VM* vm = &exec->vm();
return JSValue::encode(jsString(vm, vm->numericStrings.add(doubleValue)));
}
if (!std::isfinite(doubleValue))
return JSValue::encode(jsString(exec, String::numberToStringECMAScript(doubleValue)));
RadixBuffer s;
return JSValue::encode(jsString(exec, toStringWithRadix(s, doubleValue, radix)));
}
EncodedJSValue JSC_HOST_CALL numberProtoFuncToLocaleString(ExecState* exec)
{
double x;
if (!toThisNumber(exec->hostThisValue(), x))
return throwVMTypeError(exec);
return JSValue::encode(jsNumber(x).toString(exec));
}
EncodedJSValue JSC_HOST_CALL numberProtoFuncValueOf(ExecState* exec)
{
double x;
if (!toThisNumber(exec->hostThisValue(), x))
return throwVMTypeError(exec);
return JSValue::encode(jsNumber(x));
}
}