#include "config.h"
#include "JSValue.h"
#include "JSFunction.h"
#include <wtf/MathExtras.h>
namespace JSC {
static const double D32 = 4294967296.0;
double JSValue::toInteger(ExecState* exec) const
{
if (isInt32Fast())
return getInt32Fast();
double d = toNumber(exec);
return isnan(d) ? 0.0 : trunc(d);
}
double JSValue::toIntegerPreserveNaN(ExecState* exec) const
{
if (isInt32Fast())
return getInt32Fast();
return trunc(toNumber(exec));
}
int32_t toInt32SlowCase(double d, bool& ok)
{
ok = true;
if (d >= -D32 / 2 && d < D32 / 2)
return static_cast<int32_t>(d);
if (isnan(d) || isinf(d)) {
ok = false;
return 0;
}
double d32 = fmod(trunc(d), D32);
if (d32 >= D32 / 2)
d32 -= D32;
else if (d32 < -D32 / 2)
d32 += D32;
return static_cast<int32_t>(d32);
}
uint32_t toUInt32SlowCase(double d, bool& ok)
{
ok = true;
if (d >= 0.0 && d < D32)
return static_cast<uint32_t>(d);
if (isnan(d) || isinf(d)) {
ok = false;
return 0;
}
double d32 = fmod(trunc(d), D32);
if (d32 < 0)
d32 += D32;
return static_cast<uint32_t>(d32);
}
}