diff options
Diffstat (limited to 'src/3rdparty/javascriptcore/JavaScriptCore/runtime/JSValue.h')
-rw-r--r-- | src/3rdparty/javascriptcore/JavaScriptCore/runtime/JSValue.h | 545 |
1 files changed, 464 insertions, 81 deletions
diff --git a/src/3rdparty/javascriptcore/JavaScriptCore/runtime/JSValue.h b/src/3rdparty/javascriptcore/JavaScriptCore/runtime/JSValue.h index 391425c752..58e74b18c7 100644 --- a/src/3rdparty/javascriptcore/JavaScriptCore/runtime/JSValue.h +++ b/src/3rdparty/javascriptcore/JavaScriptCore/runtime/JSValue.h @@ -1,7 +1,7 @@ /* * Copyright (C) 1999-2001 Harri Porten (porten@kde.org) * Copyright (C) 2001 Peter Kelly (pmk@post.com) - * Copyright (C) 2003, 2004, 2005, 2007, 2008 Apple Inc. All rights reserved. + * Copyright (C) 2003, 2004, 2005, 2007, 2008, 2009 Apple Inc. All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -20,16 +20,18 @@ * */ -#include <stddef.h> // for size_t -#include <stdint.h> - #ifndef JSValue_h #define JSValue_h #include "CallData.h" #include "ConstructData.h" -#include <wtf/HashTraits.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> namespace JSC { @@ -48,31 +50,37 @@ namespace JSC { enum PreferredPrimitiveType { NoPreference, PreferNumber, PreferString }; +#if USE(JSVALUE32_64) + typedef int64_t EncodedJSValue; +#else typedef void* EncodedJSValue; +#endif + + double nonInlineNaN(); + int32_t toInt32SlowCase(double, bool& ok); + uint32_t toUInt32SlowCase(double, bool& ok); class JSValue { friend class JSImmediate; - friend struct JSValueHashTraits; - - static JSValue makeImmediate(intptr_t value) - { - return JSValue(reinterpret_cast<JSCell*>(value)); - } + friend struct EncodedJSValueHashTraits; + friend class JIT; + friend class JITStubs; + friend class JITStubCall; - intptr_t immediateValue() - { - return reinterpret_cast<intptr_t>(m_ptr); - } - public: + static EncodedJSValue encode(JSValue value); + static JSValue decode(EncodedJSValue ptr); +#if !USE(JSVALUE32_64) + private: + static JSValue makeImmediate(intptr_t value); + intptr_t immediateValue(); + public: +#endif enum JSNullTag { JSNull }; enum JSUndefinedTag { JSUndefined }; enum JSTrueTag { JSTrue }; enum JSFalseTag { JSFalse }; - static EncodedJSValue encode(JSValue value); - static JSValue decode(EncodedJSValue ptr); - JSValue(); JSValue(JSNullTag); JSValue(JSUndefinedTag); @@ -94,20 +102,22 @@ namespace JSC { JSValue(ExecState*, long long); JSValue(ExecState*, unsigned long long); JSValue(JSGlobalData*, double); - JSValue(JSGlobalData*, char); - JSValue(JSGlobalData*, unsigned char); - JSValue(JSGlobalData*, short); - JSValue(JSGlobalData*, unsigned short); JSValue(JSGlobalData*, int); JSValue(JSGlobalData*, unsigned); - JSValue(JSGlobalData*, long); - JSValue(JSGlobalData*, unsigned long); - JSValue(JSGlobalData*, long long); - JSValue(JSGlobalData*, unsigned long long); operator bool() const; - bool operator==(const JSValue other) const; - bool operator!=(const JSValue other) 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; // Querying the type. bool isUndefined() const; @@ -118,7 +128,7 @@ namespace JSC { bool isString() const; bool isGetterSetter() const; bool isObject() const; - bool isObject(const ClassInfo*) const; + bool inherits(const ClassInfo*) const; // Extracting the value. bool getBoolean(bool&) const; @@ -134,8 +144,6 @@ namespace JSC { // Extracting integer values. bool getUInt32(uint32_t&) const; - bool getTruncatedInt32(int32_t&) const; - bool getTruncatedUInt32(uint32_t&) const; // Basic conversions. JSValue toPrimitive(ExecState*, PreferredPrimitiveType = NoPreference) const; @@ -151,38 +159,17 @@ namespace JSC { JSObject* toObject(ExecState*) const; // Integer conversions. - // 'x.numberToInt32(output)' is equivalent to 'x.isNumber() && x.toInt32(output)' double toInteger(ExecState*) const; double toIntegerPreserveNaN(ExecState*) const; int32_t toInt32(ExecState*) const; int32_t toInt32(ExecState*, bool& ok) const; - bool numberToInt32(int32_t& arg); uint32_t toUInt32(ExecState*) const; uint32_t toUInt32(ExecState*, bool& ok) const; - bool numberToUInt32(uint32_t& arg); - - // Fast integer operations; these values return results where the value is trivially available - // in a convenient form, for use in optimizations. No assumptions should be made based on the - // results of these operations, for example !isInt32Fast() does not necessarily indicate the - // result of getNumber will not be 0. - bool isInt32Fast() const; - int32_t getInt32Fast() const; - bool isUInt32Fast() const; - uint32_t getUInt32Fast() const; - static JSValue makeInt32Fast(int32_t); - static bool areBothInt32Fast(JSValue, JSValue); // Floating point conversions (this is a convenience method for webcore; // signle precision float is not a representation used in JS or JSC). float toFloat(ExecState* exec) const { return static_cast<float>(toNumber(exec)); } - // API Mangled Numbers - bool isAPIMangledNumber(); - - // Garbage collection. - void mark(); - bool marked() const; - // Object operations, with the toObject operation included. JSValue get(ExecState*, const Identifier& propertyName) const; JSValue get(ExecState*, const Identifier& propertyName, PropertySlot&) const; @@ -208,22 +195,72 @@ namespace JSC { bool isCell() const; JSCell* asCell() const; +#ifndef NDEBUG + char* description(); +#endif + private: enum HashTableDeletedValueTag { HashTableDeletedValue }; JSValue(HashTableDeletedValueTag); inline const JSValue asValue() const { return *this; } + JSObject* toObjectSlowCase(ExecState*) const; + JSObject* toThisObjectSlowCase(ExecState*) const; + + enum { Int32Tag = 0xffffffff }; + enum { CellTag = 0xfffffffe }; + enum { TrueTag = 0xfffffffd }; + enum { FalseTag = 0xfffffffc }; + enum { NullTag = 0xfffffffb }; + enum { UndefinedTag = 0xfffffffa }; + enum { DeletedValueTag = 0xfffffff9 }; + + enum { LowestTag = DeletedValueTag }; + + uint32_t tag() const; + int32_t payload() const; + + JSObject* synthesizePrototype(ExecState*) const; + JSObject* synthesizeObject(ExecState*) const; + +#if USE(JSVALUE32_64) + union { + EncodedJSValue asEncodedJSValue; + double asDouble; +#if PLATFORM(BIG_ENDIAN) + struct { + int32_t tag; + int32_t payload; + } asBits; +#else + struct { + int32_t payload; + int32_t tag; + } asBits; +#endif + } u; +#else // USE(JSVALUE32_64) + JSCell* m_ptr; +#endif // USE(JSVALUE32_64) + }; - bool isDoubleNumber() const; - double getDoubleNumber() const; +#if USE(JSVALUE32_64) + typedef IntHash<EncodedJSValue> EncodedJSValueHash; - JSCell* m_ptr; + 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 JSValueHashTraits : HashTraits<EncodedJSValue> { + 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 // Stand-alone helper functions. inline JSValue jsNull() @@ -301,61 +338,396 @@ namespace JSC { return JSValue(globalData, d); } - ALWAYS_INLINE JSValue jsNumber(JSGlobalData* globalData, char i) + ALWAYS_INLINE JSValue jsNumber(JSGlobalData* globalData, int i) { return JSValue(globalData, i); } - ALWAYS_INLINE JSValue jsNumber(JSGlobalData* globalData, unsigned char i) + ALWAYS_INLINE JSValue jsNumber(JSGlobalData* globalData, unsigned i) { return JSValue(globalData, i); } - ALWAYS_INLINE JSValue jsNumber(JSGlobalData* globalData, short 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; } + + inline int32_t toInt32(double val) { - return JSValue(globalData, i); + if (!(val >= -2147483648.0 && val < 2147483648.0)) { + bool ignored; + return toInt32SlowCase(val, ignored); + } + return static_cast<int32_t>(val); } - ALWAYS_INLINE JSValue jsNumber(JSGlobalData* globalData, unsigned short i) + inline uint32_t toUInt32(double val) { - return JSValue(globalData, i); + if (!(val >= 0.0 && val < 4294967296.0)) { + bool ignored; + return toUInt32SlowCase(val, ignored); + } + return static_cast<uint32_t>(val); } - ALWAYS_INLINE JSValue jsNumber(JSGlobalData* globalData, int i) + ALWAYS_INLINE int32_t JSValue::toInt32(ExecState* exec) const { - return JSValue(globalData, i); + if (isInt32()) + return asInt32(); + bool ignored; + return toInt32SlowCase(toNumber(exec), ignored); } - ALWAYS_INLINE JSValue jsNumber(JSGlobalData* globalData, unsigned i) + inline uint32_t JSValue::toUInt32(ExecState* exec) const { - return JSValue(globalData, i); + if (isUInt32()) + return asInt32(); + bool ignored; + return toUInt32SlowCase(toNumber(exec), ignored); } - ALWAYS_INLINE JSValue jsNumber(JSGlobalData* globalData, long i) + inline int32_t JSValue::toInt32(ExecState* exec, bool& ok) const { - return JSValue(globalData, i); + if (isInt32()) { + ok = true; + return asInt32(); + } + return toInt32SlowCase(toNumber(exec), ok); } - ALWAYS_INLINE JSValue jsNumber(JSGlobalData* globalData, unsigned long i) + inline uint32_t JSValue::toUInt32(ExecState* exec, bool& ok) const { - return JSValue(globalData, i); + if (isUInt32()) { + ok = true; + return asInt32(); + } + return toUInt32SlowCase(toNumber(exec), ok); } - ALWAYS_INLINE JSValue jsNumber(JSGlobalData* globalData, long long i) +#if USE(JSVALUE32_64) + inline JSValue jsNaN(ExecState* exec) { - return JSValue(globalData, i); + return JSValue(exec, nonInlineNaN()); } - ALWAYS_INLINE JSValue jsNumber(JSGlobalData* globalData, unsigned long long i) + // JSValue member functions. + inline EncodedJSValue JSValue::encode(JSValue value) { - return JSValue(globalData, i); + return value.u.asEncodedJSValue; } - 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 JSValue JSValue::decode(EncodedJSValue encodedJSValue) + { + JSValue v; + v.u.asEncodedJSValue = encodedJSValue; + return v; + } - 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 JSValue::JSValue() + { + u.asBits.tag = CellTag; + u.asBits.payload = 0; + } + + inline JSValue::JSValue(JSNullTag) + { + u.asBits.tag = NullTag; + u.asBits.payload = 0; + } + + inline JSValue::JSValue(JSUndefinedTag) + { + u.asBits.tag = UndefinedTag; + u.asBits.payload = 0; + } + + inline JSValue::JSValue(JSTrueTag) + { + u.asBits.tag = TrueTag; + u.asBits.payload = 0; + } + + inline JSValue::JSValue(JSFalseTag) + { + u.asBits.tag = FalseTag; + u.asBits.payload = 0; + } + + inline JSValue::JSValue(HashTableDeletedValueTag) + { + u.asBits.tag = DeletedValueTag; + u.asBits.payload = 0; + } + + inline JSValue::JSValue(JSCell* ptr) + { + u.asBits.tag = CellTag; + u.asBits.payload = reinterpret_cast<int32_t>(ptr); + } + + inline JSValue::JSValue(const JSCell* ptr) + { + u.asBits.tag = CellTag; + u.asBits.payload = reinterpret_cast<int32_t>(const_cast<JSCell*>(ptr)); + } + + inline JSValue::operator bool() const + { + return u.asBits.payload || tag() != CellTag; + } + + inline bool JSValue::operator==(const JSValue& other) const + { + return u.asEncodedJSValue == other.u.asEncodedJSValue; + } + + inline bool JSValue::operator!=(const JSValue& other) const + { + return u.asEncodedJSValue != other.u.asEncodedJSValue; + } + + inline bool JSValue::isUndefined() const + { + return tag() == UndefinedTag; + } + + inline bool JSValue::isNull() const + { + return tag() == NullTag; + } + + inline bool JSValue::isUndefinedOrNull() const + { + return isUndefined() || isNull(); + } + + inline bool JSValue::isCell() const + { + return tag() == CellTag; + } + + inline bool JSValue::isInt32() const + { + return tag() == Int32Tag; + } + + inline bool JSValue::isUInt32() const + { + return tag() == Int32Tag && asInt32() > -1; + } + + inline bool JSValue::isDouble() const + { + return tag() < LowestTag; + } + + inline bool JSValue::isTrue() const + { + return tag() == TrueTag; + } + + inline bool JSValue::isFalse() const + { + return tag() == FalseTag; + } + + inline uint32_t JSValue::tag() const + { + return u.asBits.tag; + } + + inline int32_t JSValue::payload() const + { + return u.asBits.payload; + } + + inline int32_t JSValue::asInt32() const + { + ASSERT(isInt32()); + return u.asBits.payload; + } + + inline uint32_t JSValue::asUInt32() const + { + ASSERT(isUInt32()); + return u.asBits.payload; + } + + inline double JSValue::asDouble() const + { + ASSERT(isDouble()); + return u.asDouble; + } + + ALWAYS_INLINE JSCell* JSValue::asCell() const + { + ASSERT(isCell()); + return reinterpret_cast<JSCell*>(u.asBits.payload); + } + + inline JSValue::JSValue(ExecState* exec, double d) + { + const int32_t asInt32 = static_cast<int32_t>(d); + if (asInt32 != d || (!asInt32 && signbit(d))) { // true for -0.0 + u.asDouble = d; + return; + } + *this = JSValue(exec, static_cast<int32_t>(d)); + } + + inline JSValue::JSValue(ExecState* exec, char i) + { + *this = JSValue(exec, static_cast<int32_t>(i)); + } + + inline JSValue::JSValue(ExecState* exec, unsigned char i) + { + *this = JSValue(exec, static_cast<int32_t>(i)); + } + + inline JSValue::JSValue(ExecState* exec, short i) + { + *this = JSValue(exec, static_cast<int32_t>(i)); + } + + inline JSValue::JSValue(ExecState* exec, unsigned short i) + { + *this = JSValue(exec, static_cast<int32_t>(i)); + } + + inline JSValue::JSValue(ExecState*, int i) + { + u.asBits.tag = Int32Tag; + u.asBits.payload = i; + } + + inline JSValue::JSValue(ExecState* exec, unsigned i) + { + if (static_cast<int32_t>(i) < 0) { + *this = JSValue(exec, static_cast<double>(i)); + return; + } + *this = JSValue(exec, static_cast<int32_t>(i)); + } + + inline JSValue::JSValue(ExecState* exec, long i) + { + if (static_cast<int32_t>(i) != i) { + *this = JSValue(exec, static_cast<double>(i)); + return; + } + *this = JSValue(exec, static_cast<int32_t>(i)); + } + + inline JSValue::JSValue(ExecState* exec, unsigned long i) + { + if (static_cast<uint32_t>(i) != i) { + *this = JSValue(exec, static_cast<double>(i)); + return; + } + *this = JSValue(exec, static_cast<uint32_t>(i)); + } + + inline JSValue::JSValue(ExecState* exec, long long i) + { + if (static_cast<int32_t>(i) != i) { + *this = JSValue(exec, static_cast<double>(i)); + return; + } + *this = JSValue(exec, static_cast<int32_t>(i)); + } + + inline JSValue::JSValue(ExecState* exec, unsigned long long i) + { + if (static_cast<uint32_t>(i) != i) { + *this = JSValue(exec, static_cast<double>(i)); + return; + } + *this = JSValue(exec, static_cast<uint32_t>(i)); + } + + inline JSValue::JSValue(JSGlobalData* globalData, double d) + { + const int32_t asInt32 = static_cast<int32_t>(d); + if (asInt32 != d || (!asInt32 && signbit(d))) { // true for -0.0 + u.asDouble = d; + return; + } + *this = JSValue(globalData, static_cast<int32_t>(d)); + } + + inline JSValue::JSValue(JSGlobalData*, int i) + { + u.asBits.tag = Int32Tag; + u.asBits.payload = i; + } + + inline JSValue::JSValue(JSGlobalData* globalData, unsigned i) + { + if (static_cast<int32_t>(i) < 0) { + *this = JSValue(globalData, static_cast<double>(i)); + return; + } + *this = JSValue(globalData, static_cast<int32_t>(i)); + } + + inline bool JSValue::isNumber() const + { + return isInt32() || isDouble(); + } + + inline bool JSValue::isBoolean() const + { + return isTrue() || isFalse(); + } + + inline bool JSValue::getBoolean(bool& v) const + { + if (isTrue()) { + v = true; + return true; + } + if (isFalse()) { + v = false; + return true; + } + + return false; + } + + inline bool JSValue::getBoolean() const + { + ASSERT(isBoolean()); + return tag() == TrueTag; + } + + inline double JSValue::uncheckedGetNumber() const + { + ASSERT(isNumber()); + return isInt32() ? asInt32() : asDouble(); + } + + ALWAYS_INLINE JSValue JSValue::toJSNumber(ExecState* exec) const + { + return isNumber() ? asValue() : jsNumber(exec, this->toNumber(exec)); + } + + inline bool JSValue::getNumber(double& result) const + { + if (isInt32()) { + result = asInt32(); + return true; + } + if (isDouble()) { + result = asDouble(); + return true; + } + return false; + } + +#else // USE(JSVALUE32_64) // JSValue member functions. inline EncodedJSValue JSValue::encode(JSValue value) @@ -368,6 +740,16 @@ namespace JSC { return JSValue(reinterpret_cast<JSCell*>(ptr)); } + inline JSValue JSValue::makeImmediate(intptr_t value) + { + return JSValue(reinterpret_cast<JSCell*>(value)); + } + + inline intptr_t JSValue::immediateValue() + { + return reinterpret_cast<intptr_t>(m_ptr); + } + // 0x0 can never occur naturally because it has a tag of 00, indicating a pointer value, but a payload of 0x0, which is in the (invalid) zero page. inline JSValue::JSValue() : m_ptr(0) @@ -395,12 +777,12 @@ namespace JSC { return m_ptr; } - inline bool JSValue::operator==(const JSValue other) const + inline bool JSValue::operator==(const JSValue& other) const { return m_ptr == other.m_ptr; } - inline bool JSValue::operator!=(const JSValue other) const + inline bool JSValue::operator!=(const JSValue& other) const { return m_ptr != other.m_ptr; } @@ -414,6 +796,7 @@ namespace JSC { { return asValue() == jsNull(); } +#endif // USE(JSVALUE32_64) } // namespace JSC |