#ifndef JSValue_h
#define JSValue_h
#include <math.h>
#include <stddef.h> // for size_t
#include <stdint.h>
#include <wtf/AlwaysInline.h>
#include <wtf/Assertions.h>
#include <wtf/HashTraits.h>
#include <wtf/MathExtras.h>
#include <wtf/StdLibExtras.h>
namespace JSC {
class ExecState;
class Identifier;
class JSCell;
class JSGlobalData;
class JSGlobalObject;
class JSObject;
class JSString;
class PropertySlot;
class PutPropertySlot;
class UString;
struct ClassInfo;
struct Instruction;
template <class T> class WriteBarrierBase;
enum PreferredPrimitiveType { NoPreference, PreferNumber, PreferString };
#if USE(JSVALUE32_64)
typedef int64_t EncodedJSValue;
#else
typedef void* EncodedJSValue;
#endif
union EncodedValueDescriptor {
int64_t asInt64;
#if USE(JSVALUE32_64)
double asDouble;
#elif USE(JSVALUE64)
JSCell* ptr;
#endif
#if CPU(BIG_ENDIAN)
struct {
int32_t tag;
int32_t payload;
} asBits;
#else
struct {
int32_t payload;
int32_t tag;
} asBits;
#endif
};
int32_t toInt32(double);
inline uint32_t toUInt32(double number)
{
return toInt32(number);
}
class JSValue {
friend struct EncodedJSValueHashTraits;
friend class JIT;
friend class JITStubs;
friend class JITStubCall;
friend class JSInterfaceJIT;
friend class SpecializedThunkJIT;
public:
static EncodedJSValue encode(JSValue);
static JSValue decode(EncodedJSValue);
enum JSNullTag { JSNull };
enum JSUndefinedTag { JSUndefined };
enum JSTrueTag { JSTrue };
enum JSFalseTag { JSFalse };
enum EncodeAsDoubleTag { EncodeAsDouble };
JSValue();
JSValue(JSNullTag);
JSValue(JSUndefinedTag);
JSValue(JSTrueTag);
JSValue(JSFalseTag);
JSValue(JSCell* ptr);
JSValue(const JSCell* ptr);
JSValue(EncodeAsDoubleTag, double);
explicit JSValue(double);
explicit JSValue(char);
explicit JSValue(unsigned char);
explicit JSValue(short);
explicit JSValue(unsigned short);
explicit JSValue(int);
explicit JSValue(unsigned);
explicit JSValue(long);
explicit JSValue(unsigned long);
explicit JSValue(long long);
explicit JSValue(unsigned long long);
operator bool() const;
bool operator==(const JSValue& other) const;
bool operator!=(const JSValue& other) const;
bool isInt32() const;
bool isUInt32() const;
bool isDouble() const;
bool isTrue() const;
bool isFalse() const;
int32_t asInt32() const;
uint32_t asUInt32() const;
double asDouble() const;
bool isUndefined() const;
bool isNull() const;
bool isUndefinedOrNull() const;
bool isBoolean() const;
bool isNumber() const;
bool isString() const;
bool isPrimitive() const;
bool isGetterSetter() const;
bool isObject() const;
bool inherits(const ClassInfo*) const;
bool getBoolean(bool&) const;
bool getBoolean() const; bool getNumber(double&) const;
double uncheckedGetNumber() const;
bool getString(ExecState* exec, UString&) const;
UString getString(ExecState* exec) const; JSObject* getObject() const;
bool getUInt32(uint32_t&) const;
JSValue toPrimitive(ExecState*, PreferredPrimitiveType = NoPreference) const;
bool getPrimitiveNumber(ExecState*, double& number, JSValue&);
bool toBoolean(ExecState*) const;
double toNumber(ExecState*) const;
JSValue toJSNumber(ExecState*) const; UString toString(ExecState*) const;
UString toPrimitiveString(ExecState*) const;
JSObject* toObject(ExecState*) const;
JSObject* toObject(ExecState*, JSGlobalObject*) const;
double toInteger(ExecState*) const;
double toIntegerPreserveNaN(ExecState*) const;
int32_t toInt32(ExecState*) const;
uint32_t toUInt32(ExecState*) const;
float toFloat(ExecState* exec) const { return static_cast<float>(toNumber(exec)); }
JSValue get(ExecState*, const Identifier& propertyName) const;
JSValue get(ExecState*, const Identifier& propertyName, PropertySlot&) const;
JSValue get(ExecState*, unsigned propertyName) const;
JSValue get(ExecState*, unsigned propertyName, PropertySlot&) const;
void put(ExecState*, const Identifier& propertyName, JSValue, PutPropertySlot&);
void putDirect(ExecState*, const Identifier& propertyName, JSValue, PutPropertySlot&);
void put(ExecState*, unsigned propertyName, JSValue);
JSObject* toThisObject(ExecState*) const;
JSValue toStrictThisObject(ExecState*) const;
UString toThisString(ExecState*) const;
JSString* toThisJSString(ExecState*) const;
static bool equal(ExecState* exec, JSValue v1, JSValue v2);
static bool equalSlowCase(ExecState* exec, JSValue v1, JSValue v2);
static bool equalSlowCaseInline(ExecState* exec, JSValue v1, JSValue v2);
static bool strictEqual(ExecState* exec, JSValue v1, JSValue v2);
static bool strictEqualSlowCase(ExecState* exec, JSValue v1, JSValue v2);
static bool strictEqualSlowCaseInline(ExecState* exec, JSValue v1, JSValue v2);
JSValue getJSNumber();
bool isCell() const;
JSCell* asCell() const;
bool isValidCallee();
#ifndef NDEBUG
char* description();
#endif
private:
template <class T> JSValue(WriteBarrierBase<T>);
enum HashTableDeletedValueTag { HashTableDeletedValue };
JSValue(HashTableDeletedValueTag);
inline const JSValue asValue() const { return *this; }
double toNumberSlowCase(ExecState*) const;
JSObject* toObjectSlowCase(ExecState*, JSGlobalObject*) const;
JSObject* toThisObjectSlowCase(ExecState*) const;
JSObject* synthesizePrototype(ExecState*) const;
JSObject* synthesizeObject(ExecState*) const;
#if USE(JSVALUE32_64)
enum { Int32Tag = 0xffffffff };
enum { BooleanTag = 0xfffffffe };
enum { NullTag = 0xfffffffd };
enum { UndefinedTag = 0xfffffffc };
enum { CellTag = 0xfffffffb };
enum { EmptyValueTag = 0xfffffffa };
enum { DeletedValueTag = 0xfffffff9 };
enum { LowestTag = DeletedValueTag };
uint32_t tag() const;
int32_t payload() const;
#elif USE(JSVALUE64)
#define DoubleEncodeOffset 0x1000000000000ll
#define TagTypeNumber 0xffff000000000000ll
#define TagBitTypeOther 0x2ll
#define TagBitBool 0x4ll
#define TagBitUndefined 0x8ll
#define ValueFalse (TagBitTypeOther | TagBitBool | false)
#define ValueTrue (TagBitTypeOther | TagBitBool | true)
#define ValueUndefined (TagBitTypeOther | TagBitUndefined)
#define ValueNull (TagBitTypeOther)
#define TagMask (TagTypeNumber | TagBitTypeOther)
#define ValueEmpty 0x0ll
#define ValueDeleted 0x4ll
#endif
EncodedValueDescriptor u;
};
#if USE(JSVALUE32_64)
typedef IntHash<EncodedJSValue> EncodedJSValueHash;
struct EncodedJSValueHashTraits : HashTraits<EncodedJSValue> {
static const bool emptyValueIsZero = false;
static EncodedJSValue emptyValue() { return JSValue::encode(JSValue()); }
static void constructDeletedValue(EncodedJSValue& slot) { slot = JSValue::encode(JSValue(JSValue::HashTableDeletedValue)); }
static bool isDeletedValue(EncodedJSValue value) { return value == JSValue::encode(JSValue(JSValue::HashTableDeletedValue)); }
};
#else
typedef PtrHash<EncodedJSValue> EncodedJSValueHash;
struct EncodedJSValueHashTraits : HashTraits<EncodedJSValue> {
static void constructDeletedValue(EncodedJSValue& slot) { slot = JSValue::encode(JSValue(JSValue::HashTableDeletedValue)); }
static bool isDeletedValue(EncodedJSValue value) { return value == JSValue::encode(JSValue(JSValue::HashTableDeletedValue)); }
};
#endif
inline JSValue jsNull()
{
return JSValue(JSValue::JSNull);
}
inline JSValue jsUndefined()
{
return JSValue(JSValue::JSUndefined);
}
inline JSValue jsBoolean(bool b)
{
return b ? JSValue(JSValue::JSTrue) : JSValue(JSValue::JSFalse);
}
ALWAYS_INLINE JSValue jsDoubleNumber(double d)
{
return JSValue(JSValue::EncodeAsDouble, d);
}
ALWAYS_INLINE JSValue jsNumber(double d)
{
return JSValue(d);
}
ALWAYS_INLINE JSValue jsNumber(char i)
{
return JSValue(i);
}
ALWAYS_INLINE JSValue jsNumber(unsigned char i)
{
return JSValue(i);
}
ALWAYS_INLINE JSValue jsNumber(short i)
{
return JSValue(i);
}
ALWAYS_INLINE JSValue jsNumber(unsigned short i)
{
return JSValue(i);
}
ALWAYS_INLINE JSValue jsNumber(int i)
{
return JSValue(i);
}
ALWAYS_INLINE JSValue jsNumber(unsigned i)
{
return JSValue(i);
}
ALWAYS_INLINE JSValue jsNumber(long i)
{
return JSValue(i);
}
ALWAYS_INLINE JSValue jsNumber(unsigned long i)
{
return JSValue(i);
}
ALWAYS_INLINE JSValue jsNumber(long long i)
{
return JSValue(i);
}
ALWAYS_INLINE JSValue jsNumber(unsigned long long i)
{
return JSValue(i);
}
inline bool operator==(const JSValue a, const JSCell* b) { return a == JSValue(b); }
inline bool operator==(const JSCell* a, const JSValue b) { return JSValue(a) == b; }
inline bool operator!=(const JSValue a, const JSCell* b) { return a != JSValue(b); }
inline bool operator!=(const JSCell* a, const JSValue b) { return JSValue(a) != b; }
bool isZombie(const JSCell*);
}
#endif // JSValue_h