X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/6fe7ccc865dc7d7541b93c5bcaf6368d2c98a174..81345200c95645a1b0d2635520f96ad55dfde63f:/runtime/Identifier.h?ds=sidebyside diff --git a/runtime/Identifier.h b/runtime/Identifier.h index 1496087..50a253f 100644 --- a/runtime/Identifier.h +++ b/runtime/Identifier.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved. + * Copyright (C) 2003, 2006, 2007, 2008, 2009, 2012 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 @@ -21,11 +21,11 @@ #ifndef Identifier_h #define Identifier_h -#include "JSGlobalData.h" +#include "VM.h" #include -#include "UString.h" #include #include +#include namespace JSC { @@ -35,37 +35,46 @@ namespace JSC { friend class Structure; public: Identifier() { } + enum EmptyIdentifierFlag { EmptyIdentifier }; + Identifier(EmptyIdentifierFlag) : m_string(StringImpl::empty()) { ASSERT(m_string.impl()->isAtomic()); } - Identifier(ExecState* exec, const char* s) : m_string(add(exec, s)) { } // Only to be used with string literals. - Identifier(ExecState* exec, StringImpl* rep) : m_string(add(exec, rep)) { } - Identifier(ExecState* exec, const UString& s) : m_string(add(exec, s.impl())) { } + // Only to be used with string literals. + template + Identifier(ExecState* exec, const char (&characters)[charactersCount]) : m_string(add(exec, characters)) { ASSERT(m_string.impl()->isAtomic()); } + template + Identifier(VM* vm, const char (&characters)[charactersCount]) : m_string(add(vm, characters)) { ASSERT(m_string.impl()->isAtomic()); } - Identifier(JSGlobalData* globalData, const char* s) : m_string(add(globalData, s)) { } // Only to be used with string literals. - Identifier(JSGlobalData* globalData, const LChar* s, int length) : m_string(add(globalData, s, length)) { } - Identifier(JSGlobalData* globalData, const UChar* s, int length) : m_string(add(globalData, s, length)) { } - Identifier(JSGlobalData* globalData, StringImpl* rep) : m_string(add(globalData, rep)) { } - Identifier(JSGlobalData* globalData, const UString& s) : m_string(add(globalData, s.impl())) { } + Identifier(ExecState* exec, StringImpl* rep) : m_string(add(exec, rep)) { ASSERT(m_string.impl()->isAtomic()); } + Identifier(ExecState* exec, const String& s) : m_string(add(exec, s.impl())) { ASSERT(m_string.impl()->isAtomic()); } - const UString& ustring() const { return m_string; } + Identifier(VM* vm, const LChar* s, int length) : m_string(add(vm, s, length)) { ASSERT(m_string.impl()->isAtomic()); } + Identifier(VM* vm, const UChar* s, int length) : m_string(add(vm, s, length)) { ASSERT(m_string.impl()->isAtomic()); } + Identifier(VM* vm, StringImpl* rep) : m_string(add(vm, rep)) { ASSERT(m_string.impl()->isAtomic()); } + Identifier(VM* vm, const String& s) : m_string(add(vm, s.impl())) { ASSERT(m_string.impl()->isAtomic()); } + + const String& string() const { return m_string; } StringImpl* impl() const { return m_string.impl(); } - const UChar* characters() const { return m_string.characters(); } int length() const { return m_string.length(); } CString ascii() const { return m_string.ascii(); } + CString utf8() const { return m_string.utf8(); } + + static Identifier from(const PrivateName& name) + { + Identifier result; + result.m_string = name.uid(); + return result; + } - static Identifier createLCharFromUChar(JSGlobalData* globalData, const UChar* s, int length) { return Identifier(globalData, add8(globalData, s, length)); } + static Identifier createLCharFromUChar(VM* vm, const UChar* s, int length) { return Identifier(vm, add8(vm, s, length)); } JS_EXPORT_PRIVATE static Identifier from(ExecState* exec, unsigned y); JS_EXPORT_PRIVATE static Identifier from(ExecState* exec, int y); static Identifier from(ExecState* exec, double y); - static Identifier from(JSGlobalData*, unsigned y); - static Identifier from(JSGlobalData*, int y); - static Identifier from(JSGlobalData*, double y); - - JS_EXPORT_PRIVATE static uint32_t toUInt32(const UString&, bool& ok); - uint32_t toUInt32(bool& ok) const { return toUInt32(m_string, ok); } - unsigned toArrayIndex(bool& ok) const; + static Identifier from(VM*, unsigned y); + static Identifier from(VM*, int y); + static Identifier from(VM*, double y); bool isNull() const { return m_string.isNull(); } bool isEmpty() const { return m_string.isEmpty(); } @@ -84,11 +93,12 @@ namespace JSC { static bool equal(const StringImpl*, const UChar*, unsigned length); static bool equal(const StringImpl* a, const StringImpl* b) { return ::equal(a, b); } - JS_EXPORT_PRIVATE static PassRefPtr add(ExecState*, const char*); // Only to be used with string literals. - static PassRefPtr add(JSGlobalData*, const char*); // Only to be used with string literals. + // Only to be used with string literals. + JS_EXPORT_PRIVATE static PassRef add(VM*, const char*); + JS_EXPORT_PRIVATE static PassRef add(ExecState*, const char*); private: - UString m_string; + String m_string; template ALWAYS_INLINE static uint32_t toUInt32FromCharacters(const CharType* characters, unsigned length, bool& ok); @@ -96,34 +106,15 @@ namespace JSC { static bool equal(const Identifier& a, const Identifier& b) { return a.m_string.impl() == b.m_string.impl(); } static bool equal(const Identifier& a, const LChar* b) { return equal(a.m_string.impl(), b); } - template static PassRefPtr add(JSGlobalData*, const T*, int length); - static PassRefPtr add8(JSGlobalData*, const UChar*, int length); + template static PassRef add(VM*, const T*, int length); + static PassRef add8(VM*, const UChar*, int length); template ALWAYS_INLINE static bool canUseSingleCharacterString(T); - static PassRefPtr add(ExecState* exec, StringImpl* r) - { -#ifndef NDEBUG - checkCurrentIdentifierTable(exec); -#endif - if (r->isIdentifier()) - return r; - return addSlowCase(exec, r); - } - static PassRefPtr add(JSGlobalData* globalData, StringImpl* r) - { -#ifndef NDEBUG - checkCurrentIdentifierTable(globalData); -#endif - if (r->isIdentifier()) - return r; - return addSlowCase(globalData, r); - } - - JS_EXPORT_PRIVATE static PassRefPtr addSlowCase(ExecState*, StringImpl* r); - JS_EXPORT_PRIVATE static PassRefPtr addSlowCase(JSGlobalData*, StringImpl* r); + static PassRef add(ExecState*, StringImpl*); + static PassRef add(VM*, StringImpl*); - JS_EXPORT_PRIVATE static void checkCurrentIdentifierTable(ExecState*); - JS_EXPORT_PRIVATE static void checkCurrentIdentifierTable(JSGlobalData*); + JS_EXPORT_PRIVATE static void checkCurrentAtomicStringTable(ExecState*); + JS_EXPORT_PRIVATE static void checkCurrentAtomicStringTable(VM*); }; template <> ALWAYS_INLINE bool Identifier::canUseSingleCharacterString(LChar) @@ -138,51 +129,17 @@ namespace JSC { } template - struct CharBuffer { - const T* s; - unsigned int length; - }; - - template - struct IdentifierCharBufferTranslator { - static unsigned hash(const CharBuffer& buf) - { - return StringHasher::computeHash(buf.s, buf.length); - } - - static bool equal(StringImpl* str, const CharBuffer& buf) - { - return Identifier::equal(str, buf.s, buf.length); - } - - static void translate(StringImpl*& location, const CharBuffer& buf, unsigned hash) - { - T* d; - StringImpl* r = StringImpl::createUninitialized(buf.length, d).leakRef(); - for (unsigned i = 0; i != buf.length; i++) - d[i] = buf.s[i]; - r->setHash(hash); - location = r; - } - }; - - template - PassRefPtr Identifier::add(JSGlobalData* globalData, const T* s, int length) + PassRef Identifier::add(VM* vm, const T* s, int length) { if (length == 1) { T c = s[0]; if (canUseSingleCharacterString(c)) - return add(globalData, globalData->smallStrings.singleCharacterStringRep(c)); + return *vm->smallStrings.singleCharacterStringRep(c); } - if (!length) - return StringImpl::empty(); - CharBuffer buf = {s, length}; - HashSet::AddResult addResult = globalData->identifierTable->add, IdentifierCharBufferTranslator >(buf); + return *StringImpl::empty(); - // If the string is newly-translated, then we need to adopt it. - // The boolean in the pair tells us if that is so. - return addResult.isNewEntry ? adoptRef(*addResult.iterator) : *addResult.iterator; + return *AtomicString::add(s, length); } inline bool operator==(const Identifier& a, const Identifier& b) @@ -230,10 +187,7 @@ namespace JSC { return WTF::equal(r, s, length); } - IdentifierTable* createIdentifierTable(); - void deleteIdentifierTable(IdentifierTable*); - - struct IdentifierRepHash : PtrHash > { + struct IdentifierRepHash : PtrHash> { static unsigned hash(const RefPtr& key) { return key->existingHash(); } static unsigned hash(StringImpl* key) { return key->existingHash(); } }; @@ -243,16 +197,15 @@ namespace JSC { static const bool emptyValueIsZero = false; }; - typedef HashMap, int, IdentifierRepHash, HashTraits >, IdentifierMapIndexHashTraits> IdentifierMap; - - template - HashSet::AddResult IdentifierTable::add(U value) - { - HashSet::AddResult result = m_table.add(value); - (*result.iterator)->setIsIdentifier(true); - return result; - } + typedef HashMap, int, IdentifierRepHash, HashTraits>, IdentifierMapIndexHashTraits> IdentifierMap; + typedef HashMap, IdentifierMapIndexHashTraits> BorrowedIdentifierMap; } // namespace JSC +namespace WTF { + +template <> struct VectorTraits : SimpleClassVectorTraits { }; + +} // namespace WTF + #endif // Identifier_h