]>
Commit | Line | Data |
---|---|---|
9dae56ea A |
1 | /* |
2 | * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. | |
3 | * | |
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. | |
8 | * | |
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. | |
13 | * | |
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. | |
18 | * | |
19 | */ | |
20 | ||
21 | #include "config.h" | |
22 | #include "Identifier.h" | |
23 | ||
24 | #include "CallFrame.h" | |
14957cd0 | 25 | #include "JSObject.h" |
4e4e5a6f | 26 | #include "NumericStrings.h" |
14957cd0 | 27 | #include "ScopeChain.h" |
9dae56ea A |
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> | |
4e4e5a6f A |
33 | #include <wtf/WTFThreadData.h> |
34 | #include <wtf/text/StringHash.h> | |
9dae56ea | 35 | |
f9bf01c6 A |
36 | using WTF::ThreadSpecific; |
37 | ||
9dae56ea A |
38 | namespace JSC { |
39 | ||
4e4e5a6f A |
40 | IdentifierTable::~IdentifierTable() |
41 | { | |
42 | HashSet<StringImpl*>::iterator end = m_table.end(); | |
43 | for (HashSet<StringImpl*>::iterator iter = m_table.begin(); iter != end; ++iter) | |
44 | (*iter)->setIsIdentifier(false); | |
45 | } | |
46 | std::pair<HashSet<StringImpl*>::iterator, bool> IdentifierTable::add(StringImpl* value) | |
47 | { | |
48 | std::pair<HashSet<StringImpl*>::iterator, bool> result = m_table.add(value); | |
49 | (*result.first)->setIsIdentifier(true); | |
50 | return result; | |
51 | } | |
52 | template<typename U, typename V> | |
53 | std::pair<HashSet<StringImpl*>::iterator, bool> IdentifierTable::add(U value) | |
54 | { | |
55 | std::pair<HashSet<StringImpl*>::iterator, bool> result = m_table.add<U, V>(value); | |
56 | (*result.first)->setIsIdentifier(true); | |
57 | return result; | |
58 | } | |
9dae56ea A |
59 | |
60 | IdentifierTable* createIdentifierTable() | |
61 | { | |
62 | return new IdentifierTable; | |
63 | } | |
64 | ||
65 | void deleteIdentifierTable(IdentifierTable* table) | |
66 | { | |
67 | delete table; | |
68 | } | |
69 | ||
14957cd0 | 70 | bool Identifier::equal(const StringImpl* r, const char* s) |
9dae56ea | 71 | { |
4e4e5a6f A |
72 | int length = r->length(); |
73 | const UChar* d = r->characters(); | |
9dae56ea A |
74 | for (int i = 0; i != length; ++i) |
75 | if (d[i] != (unsigned char)s[i]) | |
76 | return false; | |
77 | return s[length] == 0; | |
78 | } | |
79 | ||
4e4e5a6f | 80 | struct IdentifierCStringTranslator { |
9dae56ea A |
81 | static unsigned hash(const char* c) |
82 | { | |
14957cd0 | 83 | return StringHasher::computeHash<char>(c); |
9dae56ea A |
84 | } |
85 | ||
14957cd0 | 86 | static bool equal(StringImpl* r, const char* s) |
9dae56ea A |
87 | { |
88 | return Identifier::equal(r, s); | |
89 | } | |
90 | ||
14957cd0 | 91 | static void translate(StringImpl*& location, const char* c, unsigned hash) |
9dae56ea A |
92 | { |
93 | size_t length = strlen(c); | |
f9bf01c6 | 94 | UChar* d; |
14957cd0 | 95 | StringImpl* r = StringImpl::createUninitialized(length, d).leakRef(); |
9dae56ea A |
96 | for (size_t i = 0; i != length; i++) |
97 | d[i] = static_cast<unsigned char>(c[i]); // use unsigned char to zero-extend instead of sign-extend | |
f9bf01c6 | 98 | r->setHash(hash); |
9dae56ea A |
99 | location = r; |
100 | } | |
101 | }; | |
102 | ||
14957cd0 | 103 | PassRefPtr<StringImpl> Identifier::add(JSGlobalData* globalData, const char* c) |
9dae56ea | 104 | { |
4e4e5a6f | 105 | if (!c) |
14957cd0 | 106 | return 0; |
4e4e5a6f | 107 | if (!c[0]) |
14957cd0 | 108 | return StringImpl::empty(); |
9dae56ea A |
109 | if (!c[1]) |
110 | return add(globalData, globalData->smallStrings.singleCharacterStringRep(static_cast<unsigned char>(c[0]))); | |
111 | ||
112 | IdentifierTable& identifierTable = *globalData->identifierTable; | |
113 | LiteralIdentifierTable& literalIdentifierTable = identifierTable.literalTable(); | |
114 | ||
115 | const LiteralIdentifierTable::iterator& iter = literalIdentifierTable.find(c); | |
116 | if (iter != literalIdentifierTable.end()) | |
117 | return iter->second; | |
118 | ||
14957cd0 | 119 | pair<HashSet<StringImpl*>::iterator, bool> addResult = identifierTable.add<const char*, IdentifierCStringTranslator>(c); |
9dae56ea A |
120 | |
121 | // If the string is newly-translated, then we need to adopt it. | |
122 | // The boolean in the pair tells us if that is so. | |
14957cd0 | 123 | RefPtr<StringImpl> addedString = addResult.second ? adoptRef(*addResult.first) : *addResult.first; |
9dae56ea A |
124 | |
125 | literalIdentifierTable.add(c, addedString.get()); | |
126 | ||
127 | return addedString.release(); | |
128 | } | |
129 | ||
14957cd0 | 130 | PassRefPtr<StringImpl> Identifier::add(ExecState* exec, const char* c) |
9dae56ea A |
131 | { |
132 | return add(&exec->globalData(), c); | |
133 | } | |
134 | ||
135 | struct UCharBuffer { | |
136 | const UChar* s; | |
137 | unsigned int length; | |
138 | }; | |
139 | ||
4e4e5a6f | 140 | struct IdentifierUCharBufferTranslator { |
9dae56ea A |
141 | static unsigned hash(const UCharBuffer& buf) |
142 | { | |
14957cd0 | 143 | return StringHasher::computeHash<UChar>(buf.s, buf.length); |
9dae56ea A |
144 | } |
145 | ||
14957cd0 | 146 | static bool equal(StringImpl* str, const UCharBuffer& buf) |
9dae56ea A |
147 | { |
148 | return Identifier::equal(str, buf.s, buf.length); | |
149 | } | |
150 | ||
14957cd0 | 151 | static void translate(StringImpl*& location, const UCharBuffer& buf, unsigned hash) |
9dae56ea | 152 | { |
f9bf01c6 | 153 | UChar* d; |
14957cd0 | 154 | StringImpl* r = StringImpl::createUninitialized(buf.length, d).leakRef(); |
9dae56ea A |
155 | for (unsigned i = 0; i != buf.length; i++) |
156 | d[i] = buf.s[i]; | |
f9bf01c6 | 157 | r->setHash(hash); |
9dae56ea A |
158 | location = r; |
159 | } | |
160 | }; | |
161 | ||
14957cd0 A |
162 | uint32_t Identifier::toUInt32(const UString& string, bool& ok) |
163 | { | |
164 | ok = false; | |
165 | ||
166 | unsigned length = string.length(); | |
167 | const UChar* characters = string.characters(); | |
168 | ||
169 | // An empty string is not a number. | |
170 | if (!length) | |
171 | return 0; | |
172 | ||
173 | // Get the first character, turning it into a digit. | |
174 | uint32_t value = characters[0] - '0'; | |
175 | if (value > 9) | |
176 | return 0; | |
177 | ||
178 | // Check for leading zeros. If the first characher is 0, then the | |
179 | // length of the string must be one - e.g. "042" is not equal to "42". | |
180 | if (!value && length > 1) | |
181 | return 0; | |
182 | ||
183 | while (--length) { | |
184 | // Multiply value by 10, checking for overflow out of 32 bits. | |
185 | if (value > 0xFFFFFFFFU / 10) | |
186 | return 0; | |
187 | value *= 10; | |
188 | ||
189 | // Get the next character, turning it into a digit. | |
190 | uint32_t newValue = *(++characters) - '0'; | |
191 | if (newValue > 9) | |
192 | return 0; | |
193 | ||
194 | // Add in the old value, checking for overflow out of 32 bits. | |
195 | newValue += value; | |
196 | if (newValue < value) | |
197 | return 0; | |
198 | value = newValue; | |
199 | } | |
200 | ||
201 | ok = true; | |
202 | return value; | |
203 | } | |
204 | ||
205 | PassRefPtr<StringImpl> Identifier::add(JSGlobalData* globalData, const UChar* s, int length) | |
9dae56ea A |
206 | { |
207 | if (length == 1) { | |
208 | UChar c = s[0]; | |
14957cd0 | 209 | if (c <= maxSingleCharacterString) |
9dae56ea A |
210 | return add(globalData, globalData->smallStrings.singleCharacterStringRep(c)); |
211 | } | |
4e4e5a6f | 212 | if (!length) |
14957cd0 | 213 | return StringImpl::empty(); |
9dae56ea | 214 | UCharBuffer buf = {s, length}; |
14957cd0 | 215 | pair<HashSet<StringImpl*>::iterator, bool> addResult = globalData->identifierTable->add<UCharBuffer, IdentifierUCharBufferTranslator>(buf); |
9dae56ea A |
216 | |
217 | // If the string is newly-translated, then we need to adopt it. | |
218 | // The boolean in the pair tells us if that is so. | |
219 | return addResult.second ? adoptRef(*addResult.first) : *addResult.first; | |
220 | } | |
221 | ||
14957cd0 | 222 | PassRefPtr<StringImpl> Identifier::add(ExecState* exec, const UChar* s, int length) |
9dae56ea A |
223 | { |
224 | return add(&exec->globalData(), s, length); | |
225 | } | |
226 | ||
14957cd0 | 227 | PassRefPtr<StringImpl> Identifier::addSlowCase(JSGlobalData* globalData, StringImpl* r) |
9dae56ea | 228 | { |
f9bf01c6 | 229 | ASSERT(!r->isIdentifier()); |
4e4e5a6f A |
230 | // The empty & null strings are static singletons, and static strings are handled |
231 | // in ::add() in the header, so we should never get here with a zero length string. | |
232 | ASSERT(r->length()); | |
233 | ||
234 | if (r->length() == 1) { | |
235 | UChar c = r->characters()[0]; | |
14957cd0 | 236 | if (c <= maxSingleCharacterString) |
9dae56ea | 237 | r = globalData->smallStrings.singleCharacterStringRep(c); |
4e4e5a6f | 238 | if (r->isIdentifier()) |
9dae56ea | 239 | return r; |
9dae56ea | 240 | } |
4e4e5a6f | 241 | |
9dae56ea A |
242 | return *globalData->identifierTable->add(r).first; |
243 | } | |
244 | ||
14957cd0 | 245 | PassRefPtr<StringImpl> Identifier::addSlowCase(ExecState* exec, StringImpl* r) |
9dae56ea A |
246 | { |
247 | return addSlowCase(&exec->globalData(), r); | |
248 | } | |
249 | ||
4e4e5a6f | 250 | Identifier Identifier::from(ExecState* exec, unsigned value) |
9dae56ea | 251 | { |
4e4e5a6f | 252 | return Identifier(exec, exec->globalData().numericStrings.add(value)); |
9dae56ea A |
253 | } |
254 | ||
4e4e5a6f | 255 | Identifier Identifier::from(ExecState* exec, int value) |
9dae56ea | 256 | { |
4e4e5a6f | 257 | return Identifier(exec, exec->globalData().numericStrings.add(value)); |
9dae56ea A |
258 | } |
259 | ||
4e4e5a6f | 260 | Identifier Identifier::from(ExecState* exec, double value) |
9dae56ea | 261 | { |
4e4e5a6f | 262 | return Identifier(exec, exec->globalData().numericStrings.add(value)); |
9dae56ea A |
263 | } |
264 | ||
14957cd0 A |
265 | Identifier Identifier::from(JSGlobalData* globalData, unsigned value) |
266 | { | |
267 | return Identifier(globalData, globalData->numericStrings.add(value)); | |
268 | } | |
269 | ||
270 | Identifier Identifier::from(JSGlobalData* globalData, int value) | |
271 | { | |
272 | return Identifier(globalData, globalData->numericStrings.add(value)); | |
273 | } | |
274 | ||
275 | Identifier Identifier::from(JSGlobalData* globalData, double value) | |
276 | { | |
277 | return Identifier(globalData, globalData->numericStrings.add(value)); | |
278 | } | |
279 | ||
4e4e5a6f | 280 | #ifndef NDEBUG |
9dae56ea | 281 | |
4e4e5a6f | 282 | void Identifier::checkCurrentIdentifierTable(JSGlobalData* globalData) |
9dae56ea | 283 | { |
4e4e5a6f A |
284 | // Check the identifier table accessible through the threadspecific matches the |
285 | // globalData's identifier table. | |
286 | ASSERT_UNUSED(globalData, globalData->identifierTable == wtfThreadData().currentIdentifierTable()); | |
9dae56ea A |
287 | } |
288 | ||
4e4e5a6f | 289 | void Identifier::checkCurrentIdentifierTable(ExecState* exec) |
f9bf01c6 | 290 | { |
4e4e5a6f | 291 | checkCurrentIdentifierTable(&exec->globalData()); |
f9bf01c6 A |
292 | } |
293 | ||
4e4e5a6f | 294 | #else |
f9bf01c6 | 295 | |
4e4e5a6f A |
296 | // These only exists so that our exports are the same for debug and release builds. |
297 | // This would be an ASSERT_NOT_REACHED(), but we're in NDEBUG only code here! | |
298 | void Identifier::checkCurrentIdentifierTable(JSGlobalData*) { CRASH(); } | |
299 | void Identifier::checkCurrentIdentifierTable(ExecState*) { CRASH(); } | |
f9bf01c6 A |
300 | |
301 | #endif | |
302 | ||
9dae56ea | 303 | } // namespace JSC |