#include "config.h"
#include "JSValue.h"
#include "BooleanConstructor.h"
#include "BooleanPrototype.h"
#include "Error.h"
#include "ExceptionHelpers.h"
#include "JSGlobalObject.h"
#include "JSFunction.h"
#include "JSNotAnObject.h"
#include "NumberObject.h"
#include <wtf/MathExtras.h>
#include <wtf/StringExtras.h>
namespace JSC {
static const double D32 = 4294967296.0;
double JSValue::toInteger(ExecState* exec) const
{
if (isInt32())
return asInt32();
double d = toNumber(exec);
return isnan(d) ? 0.0 : trunc(d);
}
double JSValue::toIntegerPreserveNaN(ExecState* exec) const
{
if (isInt32())
return asInt32();
return trunc(toNumber(exec));
}
double JSValue::toNumberSlowCase(ExecState* exec) const
{
ASSERT(!isInt32() && !isDouble());
if (isCell())
return asCell()->toNumber(exec);
if (isTrue())
return 1.0;
return isUndefined() ? std::numeric_limits<double>::quiet_NaN() : 0; }
JSObject* JSValue::toObjectSlowCase(ExecState* exec, JSGlobalObject* globalObject) const
{
ASSERT(!isCell());
if (isInt32() || isDouble())
return constructNumber(exec, globalObject, asValue());
if (isTrue() || isFalse())
return constructBooleanFromImmediateBoolean(exec, globalObject, asValue());
ASSERT(isUndefinedOrNull());
throwError(exec, createNotAnObjectError(exec, *this));
return JSNotAnObject::create(exec);
}
JSObject* JSValue::toThisObjectSlowCase(ExecState* exec) const
{
ASSERT(!isCell());
if (isInt32() || isDouble())
return constructNumber(exec, exec->lexicalGlobalObject(), asValue());
if (isTrue() || isFalse())
return constructBooleanFromImmediateBoolean(exec, exec->lexicalGlobalObject(), asValue());
ASSERT(isUndefinedOrNull());
return exec->globalThisValue();
}
JSObject* JSValue::synthesizeObject(ExecState* exec) const
{
ASSERT(!isCell());
if (isNumber())
return constructNumber(exec, exec->lexicalGlobalObject(), asValue());
if (isBoolean())
return constructBooleanFromImmediateBoolean(exec, exec->lexicalGlobalObject(), asValue());
ASSERT(isUndefinedOrNull());
throwError(exec, createNotAnObjectError(exec, *this));
return JSNotAnObject::create(exec);
}
JSObject* JSValue::synthesizePrototype(ExecState* exec) const
{
ASSERT(!isCell());
if (isNumber())
return exec->lexicalGlobalObject()->numberPrototype();
if (isBoolean())
return exec->lexicalGlobalObject()->booleanPrototype();
ASSERT(isUndefinedOrNull());
throwError(exec, createNotAnObjectError(exec, *this));
return JSNotAnObject::create(exec);
}
#ifndef NDEBUG
char* JSValue::description()
{
static const size_t size = 32;
static char description[size];
if (!*this)
snprintf(description, size, "<JSValue()>");
else if (isInt32())
snprintf(description, size, "Int32: %d", asInt32());
else if (isDouble())
snprintf(description, size, "Double: %lf", asDouble());
else if (isCell())
snprintf(description, size, "Cell: %p", asCell());
else if (isTrue())
snprintf(description, size, "True");
else if (isFalse())
snprintf(description, size, "False");
else if (isNull())
snprintf(description, size, "Null");
else if (isUndefined())
snprintf(description, size, "Undefined");
else
snprintf(description, size, "INVALID");
return description;
}
#endif
int32_t toInt32(double number)
{
int64_t bits = WTF::bitwise_cast<int64_t>(number);
int32_t exp = (static_cast<int32_t>(bits >> 52) & 0x7ff) - 0x3ff;
if (exp < 0 || exp > 83)
return 0;
int32_t result = (exp > 52)
? static_cast<int32_t>(bits << (exp - 52))
: static_cast<int32_t>(bits >> (52 - exp));
if (exp < 32) {
int32_t missingOne = 1 << exp;
result &= missingOne - 1;
result += missingOne;
}
return bits < 0 ? -result : result;
}
bool JSValue::isValidCallee()
{
return asObject(asObject(asCell())->getAnonymousValue(0))->isGlobalObject();
}
}