diff options
Diffstat (limited to 'Source/JavaScriptCore/runtime/PropertyName.h')
-rw-r--r-- | Source/JavaScriptCore/runtime/PropertyName.h | 99 |
1 files changed, 40 insertions, 59 deletions
diff --git a/Source/JavaScriptCore/runtime/PropertyName.h b/Source/JavaScriptCore/runtime/PropertyName.h index 7253756fa..9d3cc2b51 100644 --- a/Source/JavaScriptCore/runtime/PropertyName.h +++ b/Source/JavaScriptCore/runtime/PropertyName.h @@ -28,90 +28,56 @@ #include "Identifier.h" #include "PrivateName.h" +#include <wtf/Optional.h> namespace JSC { -template <typename CharType> -ALWAYS_INLINE uint32_t toUInt32FromCharacters(const CharType* characters, unsigned length) -{ - // An empty string is not a number. - if (!length) - return UINT_MAX; - - // Get the first character, turning it into a digit. - uint32_t value = characters[0] - '0'; - if (value > 9) - return UINT_MAX; - - // Check for leading zeros. If the first characher is 0, then the - // length of the string must be one - e.g. "042" is not equal to "42". - if (!value && length > 1) - return UINT_MAX; - - while (--length) { - // Multiply value by 10, checking for overflow out of 32 bits. - if (value > 0xFFFFFFFFU / 10) - return UINT_MAX; - value *= 10; - - // Get the next character, turning it into a digit. - uint32_t newValue = *(++characters) - '0'; - if (newValue > 9) - return UINT_MAX; - - // Add in the old value, checking for overflow out of 32 bits. - newValue += value; - if (newValue < value) - return UINT_MAX; - value = newValue; - } - - return value; -} - -ALWAYS_INLINE uint32_t toUInt32FromStringImpl(StringImpl* impl) -{ - if (impl->is8Bit()) - return toUInt32FromCharacters(impl->characters8(), impl->length()); - return toUInt32FromCharacters(impl->characters16(), impl->length()); -} - class PropertyName { public: + PropertyName(UniquedStringImpl* propertyName) + : m_impl(propertyName) + { + } + PropertyName(const Identifier& propertyName) - : m_impl(propertyName.impl()) + : PropertyName(propertyName.impl()) { - ASSERT(!m_impl || m_impl->isIdentifier()); } PropertyName(const PrivateName& propertyName) : m_impl(propertyName.uid()) { - ASSERT(m_impl && m_impl->isEmptyUnique()); + ASSERT(m_impl); + ASSERT(m_impl->isSymbol()); } - StringImpl* uid() const + bool isNull() const { return !m_impl; } + + bool isSymbol() { - ASSERT(!m_impl || (m_impl->isIdentifier() == !m_impl->isEmptyUnique())); - return m_impl; + return m_impl && m_impl->isSymbol(); } - StringImpl* publicName() const + UniquedStringImpl* uid() const { - ASSERT(!m_impl || (m_impl->isIdentifier() == !m_impl->isEmptyUnique())); - return m_impl->isIdentifier() ? m_impl : 0; + return m_impl; } - static const uint32_t NotAnIndex = UINT_MAX; + AtomicStringImpl* publicName() const + { + return (!m_impl || m_impl->isSymbol()) ? nullptr : static_cast<AtomicStringImpl*>(m_impl); + } - uint32_t asIndex() + void dump(PrintStream& out) const { - ASSERT(!m_impl || (m_impl->isIdentifier() == !m_impl->isEmptyUnique())); - return m_impl ? toUInt32FromStringImpl(m_impl) : NotAnIndex; + if (m_impl) + out.print(m_impl); + else + out.print("<null property name>"); } private: - StringImpl* m_impl; + UniquedStringImpl* m_impl; }; inline bool operator==(PropertyName a, const Identifier& b) @@ -129,6 +95,11 @@ inline bool operator==(PropertyName a, PropertyName b) return a.uid() == b.uid(); } +inline bool operator==(PropertyName a, const char* b) +{ + return equal(a.uid(), b); +} + inline bool operator!=(PropertyName a, const Identifier& b) { return a.uid() != b.impl(); @@ -144,6 +115,16 @@ inline bool operator!=(PropertyName a, PropertyName b) return a.uid() != b.uid(); } +ALWAYS_INLINE Optional<uint32_t> parseIndex(PropertyName propertyName) +{ + auto uid = propertyName.uid(); + if (!uid) + return Nullopt; + if (uid->isSymbol()) + return Nullopt; + return parseIndex(*uid); +} + } #endif |