Changeset 58712 in webkit for trunk/JavaScriptCore/runtime/Identifier.cpp
- Timestamp:
- May 3, 2010, 4:03:37 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/runtime/Identifier.cpp
r58114 r58712 30 30 #include <wtf/HashSet.h> 31 31 #include <wtf/WTFThreadData.h> 32 #include <wtf/text/AtomicStringTable.h>33 32 #include <wtf/text/StringHash.h> 34 33 … … 37 36 namespace JSC { 38 37 39 class LiteralTable : public HashMap<const char*, RefPtr<StringImpl>, PtrHash<const char*> > {}; 40 41 LiteralTable* createLiteralTable() 42 { 43 return new LiteralTable; 44 } 45 46 void deleteLiteralTable(LiteralTable* table) 47 { 48 delete table; 38 IdentifierTable::~IdentifierTable() 39 { 40 HashSet<StringImpl*>::iterator end = m_table.end(); 41 for (HashSet<StringImpl*>::iterator iter = m_table.begin(); iter != end; ++iter) 42 (*iter)->setIsIdentifier(false); 43 } 44 std::pair<HashSet<StringImpl*>::iterator, bool> IdentifierTable::add(StringImpl* value) 45 { 46 std::pair<HashSet<StringImpl*>::iterator, bool> result = m_table.add(value); 47 (*result.first)->setIsIdentifier(true); 48 return result; 49 } 50 template<typename U, typename V> 51 std::pair<HashSet<StringImpl*>::iterator, bool> IdentifierTable::add(U value) 52 { 53 std::pair<HashSet<StringImpl*>::iterator, bool> result = m_table.add<U, V>(value); 54 (*result.first)->setIsIdentifier(true); 55 return result; 49 56 } 50 57 … … 80 87 } 81 88 89 struct IdentifierCStringTranslator { 90 static unsigned hash(const char* c) 91 { 92 return UString::Rep::computeHash(c); 93 } 94 95 static bool equal(UString::Rep* r, const char* s) 96 { 97 return Identifier::equal(r, s); 98 } 99 100 static void translate(UString::Rep*& location, const char* c, unsigned hash) 101 { 102 size_t length = strlen(c); 103 UChar* d; 104 UString::Rep* r = UString::Rep::createUninitialized(length, d).releaseRef(); 105 for (size_t i = 0; i != length; i++) 106 d[i] = static_cast<unsigned char>(c[i]); // use unsigned char to zero-extend instead of sign-extend 107 r->setHash(hash); 108 location = r; 109 } 110 }; 111 82 112 PassRefPtr<UString::Rep> Identifier::add(JSGlobalData* globalData, const char* c) 83 113 { … … 89 119 return add(globalData, globalData->smallStrings.singleCharacterStringRep(static_cast<unsigned char>(c[0]))); 90 120 91 LiteralTable* literalTable = globalData->literalTable; 92 pair<LiteralTable::iterator, bool> result = literalTable->add(c, 0); 93 if (!result.second) // pre-existing entry 94 return result.first->second; 95 96 RefPtr<StringImpl> addedString = globalData->identifierTable->add(c); 97 result.first->second = addedString.get(); 121 IdentifierTable& identifierTable = *globalData->identifierTable; 122 LiteralIdentifierTable& literalIdentifierTable = identifierTable.literalTable(); 123 124 const LiteralIdentifierTable::iterator& iter = literalIdentifierTable.find(c); 125 if (iter != literalIdentifierTable.end()) 126 return iter->second; 127 128 pair<HashSet<UString::Rep*>::iterator, bool> addResult = identifierTable.add<const char*, IdentifierCStringTranslator>(c); 129 130 // If the string is newly-translated, then we need to adopt it. 131 // The boolean in the pair tells us if that is so. 132 RefPtr<UString::Rep> addedString = addResult.second ? adoptRef(*addResult.first) : *addResult.first; 133 134 literalIdentifierTable.add(c, addedString.get()); 98 135 99 136 return addedString.release(); … … 104 141 return add(&exec->globalData(), c); 105 142 } 143 144 struct UCharBuffer { 145 const UChar* s; 146 unsigned int length; 147 }; 148 149 struct IdentifierUCharBufferTranslator { 150 static unsigned hash(const UCharBuffer& buf) 151 { 152 return UString::Rep::computeHash(buf.s, buf.length); 153 } 154 155 static bool equal(UString::Rep* str, const UCharBuffer& buf) 156 { 157 return Identifier::equal(str, buf.s, buf.length); 158 } 159 160 static void translate(UString::Rep*& location, const UCharBuffer& buf, unsigned hash) 161 { 162 UChar* d; 163 UString::Rep* r = UString::Rep::createUninitialized(buf.length, d).releaseRef(); 164 for (unsigned i = 0; i != buf.length; i++) 165 d[i] = buf.s[i]; 166 r->setHash(hash); 167 location = r; 168 } 169 }; 106 170 107 171 PassRefPtr<UString::Rep> Identifier::add(JSGlobalData* globalData, const UChar* s, int length) … … 114 178 if (!length) 115 179 return UString::Rep::empty(); 116 117 return globalData->identifierTable->add(s, length); 180 UCharBuffer buf = {s, length}; 181 pair<HashSet<UString::Rep*>::iterator, bool> addResult = globalData->identifierTable->add<UCharBuffer, IdentifierUCharBufferTranslator>(buf); 182 183 // If the string is newly-translated, then we need to adopt it. 184 // The boolean in the pair tells us if that is so. 185 return addResult.second ? adoptRef(*addResult.first) : *addResult.first; 118 186 } 119 187 … … 138 206 } 139 207 140 return globalData->identifierTable->add(r);208 return *globalData->identifierTable->add(r).first; 141 209 } 142 210
Note:
See TracChangeset
for help on using the changeset viewer.