2 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
22 #include "Identifier.h"
24 #include "CallFrame.h"
26 #include "NumericStrings.h"
27 #include "ScopeChain.h"
28 #include <new> // for placement new
29 #include <string.h> // for strlen
30 #include <wtf/Assertions.h>
31 #include <wtf/FastMalloc.h>
32 #include <wtf/HashSet.h>
33 #include <wtf/WTFThreadData.h>
34 #include <wtf/text/StringHash.h>
36 using WTF::ThreadSpecific;
40 IdentifierTable::~IdentifierTable()
42 HashSet<StringImpl*>::iterator end = m_table.end();
43 for (HashSet<StringImpl*>::iterator iter = m_table.begin(); iter != end; ++iter)
44 (*iter)->setIsIdentifier(false);
46 std::pair<HashSet<StringImpl*>::iterator, bool> IdentifierTable::add(StringImpl* value)
48 std::pair<HashSet<StringImpl*>::iterator, bool> result = m_table.add(value);
49 (*result.first)->setIsIdentifier(true);
52 template<typename U, typename V>
53 std::pair<HashSet<StringImpl*>::iterator, bool> IdentifierTable::add(U value)
55 std::pair<HashSet<StringImpl*>::iterator, bool> result = m_table.add<U, V>(value);
56 (*result.first)->setIsIdentifier(true);
60 IdentifierTable* createIdentifierTable()
62 return new IdentifierTable;
65 void deleteIdentifierTable(IdentifierTable* table)
70 struct IdentifierCStringTranslator {
71 static unsigned hash(const char* c)
73 return StringHasher::computeHash<char>(c);
76 static bool equal(StringImpl* r, const char* s)
78 return Identifier::equal(r, s);
81 static void translate(StringImpl*& location, const char* c, unsigned hash)
83 size_t length = strlen(c);
85 StringImpl* r = StringImpl::createUninitialized(length, d).leakRef();
86 for (size_t i = 0; i != length; i++)
87 d[i] = static_cast<unsigned char>(c[i]); // use unsigned char to zero-extend instead of sign-extend
93 PassRefPtr<StringImpl> Identifier::add(JSGlobalData* globalData, const char* c)
98 return StringImpl::empty();
100 return add(globalData, globalData->smallStrings.singleCharacterStringRep(static_cast<unsigned char>(c[0])));
102 IdentifierTable& identifierTable = *globalData->identifierTable;
103 LiteralIdentifierTable& literalIdentifierTable = identifierTable.literalTable();
105 const LiteralIdentifierTable::iterator& iter = literalIdentifierTable.find(c);
106 if (iter != literalIdentifierTable.end())
109 pair<HashSet<StringImpl*>::iterator, bool> addResult = identifierTable.add<const char*, IdentifierCStringTranslator>(c);
111 // If the string is newly-translated, then we need to adopt it.
112 // The boolean in the pair tells us if that is so.
113 RefPtr<StringImpl> addedString = addResult.second ? adoptRef(*addResult.first) : *addResult.first;
115 literalIdentifierTable.add(c, addedString.get());
117 return addedString.release();
120 PassRefPtr<StringImpl> Identifier::add(ExecState* exec, const char* c)
122 return add(&exec->globalData(), c);
130 struct IdentifierUCharBufferTranslator {
131 static unsigned hash(const UCharBuffer& buf)
133 return StringHasher::computeHash<UChar>(buf.s, buf.length);
136 static bool equal(StringImpl* str, const UCharBuffer& buf)
138 return Identifier::equal(str, buf.s, buf.length);
141 static void translate(StringImpl*& location, const UCharBuffer& buf, unsigned hash)
144 StringImpl* r = StringImpl::createUninitialized(buf.length, d).leakRef();
145 for (unsigned i = 0; i != buf.length; i++)
152 uint32_t Identifier::toUInt32(const UString& string, bool& ok)
156 unsigned length = string.length();
157 const UChar* characters = string.characters();
159 // An empty string is not a number.
163 // Get the first character, turning it into a digit.
164 uint32_t value = characters[0] - '0';
168 // Check for leading zeros. If the first characher is 0, then the
169 // length of the string must be one - e.g. "042" is not equal to "42".
170 if (!value && length > 1)
174 // Multiply value by 10, checking for overflow out of 32 bits.
175 if (value > 0xFFFFFFFFU / 10)
179 // Get the next character, turning it into a digit.
180 uint32_t newValue = *(++characters) - '0';
184 // Add in the old value, checking for overflow out of 32 bits.
186 if (newValue < value)
195 PassRefPtr<StringImpl> Identifier::add(JSGlobalData* globalData, const UChar* s, int length)
199 if (c <= maxSingleCharacterString)
200 return add(globalData, globalData->smallStrings.singleCharacterStringRep(c));
203 return StringImpl::empty();
204 UCharBuffer buf = {s, length};
205 pair<HashSet<StringImpl*>::iterator, bool> addResult = globalData->identifierTable->add<UCharBuffer, IdentifierUCharBufferTranslator>(buf);
207 // If the string is newly-translated, then we need to adopt it.
208 // The boolean in the pair tells us if that is so.
209 return addResult.second ? adoptRef(*addResult.first) : *addResult.first;
212 PassRefPtr<StringImpl> Identifier::add(ExecState* exec, const UChar* s, int length)
214 return add(&exec->globalData(), s, length);
217 PassRefPtr<StringImpl> Identifier::addSlowCase(JSGlobalData* globalData, StringImpl* r)
219 ASSERT(!r->isIdentifier());
220 // The empty & null strings are static singletons, and static strings are handled
221 // in ::add() in the header, so we should never get here with a zero length string.
224 if (r->length() == 1) {
226 if (c <= maxSingleCharacterString)
227 r = globalData->smallStrings.singleCharacterStringRep(c);
228 if (r->isIdentifier())
232 return *globalData->identifierTable->add(r).first;
235 PassRefPtr<StringImpl> Identifier::addSlowCase(ExecState* exec, StringImpl* r)
237 return addSlowCase(&exec->globalData(), r);
240 Identifier Identifier::from(ExecState* exec, unsigned value)
242 return Identifier(exec, exec->globalData().numericStrings.add(value));
245 Identifier Identifier::from(ExecState* exec, int value)
247 return Identifier(exec, exec->globalData().numericStrings.add(value));
250 Identifier Identifier::from(ExecState* exec, double value)
252 return Identifier(exec, exec->globalData().numericStrings.add(value));
255 Identifier Identifier::from(JSGlobalData* globalData, unsigned value)
257 return Identifier(globalData, globalData->numericStrings.add(value));
260 Identifier Identifier::from(JSGlobalData* globalData, int value)
262 return Identifier(globalData, globalData->numericStrings.add(value));
265 Identifier Identifier::from(JSGlobalData* globalData, double value)
267 return Identifier(globalData, globalData->numericStrings.add(value));
272 void Identifier::checkCurrentIdentifierTable(JSGlobalData* globalData)
274 // Check the identifier table accessible through the threadspecific matches the
275 // globalData's identifier table.
276 ASSERT_UNUSED(globalData, globalData->identifierTable == wtfThreadData().currentIdentifierTable());
279 void Identifier::checkCurrentIdentifierTable(ExecState* exec)
281 checkCurrentIdentifierTable(&exec->globalData());
286 // These only exists so that our exports are the same for debug and release builds.
287 // This would be an ASSERT_NOT_REACHED(), but we're in NDEBUG only code here!
288 NO_RETURN_DUE_TO_CRASH void Identifier::checkCurrentIdentifierTable(JSGlobalData*) { CRASH(); }
289 NO_RETURN_DUE_TO_CRASH void Identifier::checkCurrentIdentifierTable(ExecState*) { CRASH(); }