]>
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" | |
25 | #include <new> // for placement new | |
26 | #include <string.h> // for strlen | |
27 | #include <wtf/Assertions.h> | |
28 | #include <wtf/FastMalloc.h> | |
29 | #include <wtf/HashSet.h> | |
30 | ||
f9bf01c6 A |
31 | using WTF::ThreadSpecific; |
32 | ||
9dae56ea A |
33 | namespace JSC { |
34 | ||
35 | typedef HashMap<const char*, RefPtr<UString::Rep>, PtrHash<const char*> > LiteralIdentifierTable; | |
36 | ||
f9bf01c6 | 37 | class IdentifierTable : public FastAllocBase { |
9dae56ea A |
38 | public: |
39 | ~IdentifierTable() | |
40 | { | |
41 | HashSet<UString::Rep*>::iterator end = m_table.end(); | |
42 | for (HashSet<UString::Rep*>::iterator iter = m_table.begin(); iter != end; ++iter) | |
f9bf01c6 | 43 | (*iter)->setIsIdentifier(false); |
9dae56ea A |
44 | } |
45 | ||
46 | std::pair<HashSet<UString::Rep*>::iterator, bool> add(UString::Rep* value) | |
47 | { | |
48 | std::pair<HashSet<UString::Rep*>::iterator, bool> result = m_table.add(value); | |
f9bf01c6 | 49 | (*result.first)->setIsIdentifier(true); |
9dae56ea A |
50 | return result; |
51 | } | |
52 | ||
53 | template<typename U, typename V> | |
54 | std::pair<HashSet<UString::Rep*>::iterator, bool> add(U value) | |
55 | { | |
56 | std::pair<HashSet<UString::Rep*>::iterator, bool> result = m_table.add<U, V>(value); | |
f9bf01c6 | 57 | (*result.first)->setIsIdentifier(true); |
9dae56ea A |
58 | return result; |
59 | } | |
60 | ||
61 | void remove(UString::Rep* r) { m_table.remove(r); } | |
62 | ||
63 | LiteralIdentifierTable& literalTable() { return m_literalTable; } | |
64 | ||
65 | private: | |
66 | HashSet<UString::Rep*> m_table; | |
67 | LiteralIdentifierTable m_literalTable; | |
68 | }; | |
69 | ||
70 | IdentifierTable* createIdentifierTable() | |
71 | { | |
72 | return new IdentifierTable; | |
73 | } | |
74 | ||
75 | void deleteIdentifierTable(IdentifierTable* table) | |
76 | { | |
77 | delete table; | |
78 | } | |
79 | ||
80 | bool Identifier::equal(const UString::Rep* r, const char* s) | |
81 | { | |
f9bf01c6 | 82 | int length = r->size(); |
9dae56ea A |
83 | const UChar* d = r->data(); |
84 | for (int i = 0; i != length; ++i) | |
85 | if (d[i] != (unsigned char)s[i]) | |
86 | return false; | |
87 | return s[length] == 0; | |
88 | } | |
89 | ||
90 | bool Identifier::equal(const UString::Rep* r, const UChar* s, int length) | |
91 | { | |
f9bf01c6 | 92 | if (r->size() != length) |
9dae56ea A |
93 | return false; |
94 | const UChar* d = r->data(); | |
95 | for (int i = 0; i != length; ++i) | |
96 | if (d[i] != s[i]) | |
97 | return false; | |
98 | return true; | |
99 | } | |
100 | ||
101 | struct CStringTranslator { | |
102 | static unsigned hash(const char* c) | |
103 | { | |
104 | return UString::Rep::computeHash(c); | |
105 | } | |
106 | ||
107 | static bool equal(UString::Rep* r, const char* s) | |
108 | { | |
109 | return Identifier::equal(r, s); | |
110 | } | |
111 | ||
112 | static void translate(UString::Rep*& location, const char* c, unsigned hash) | |
113 | { | |
114 | size_t length = strlen(c); | |
f9bf01c6 A |
115 | UChar* d; |
116 | UString::Rep* r = UString::Rep::createUninitialized(length, d).releaseRef(); | |
9dae56ea A |
117 | for (size_t i = 0; i != length; i++) |
118 | d[i] = static_cast<unsigned char>(c[i]); // use unsigned char to zero-extend instead of sign-extend | |
f9bf01c6 | 119 | r->setHash(hash); |
9dae56ea A |
120 | location = r; |
121 | } | |
122 | }; | |
123 | ||
124 | PassRefPtr<UString::Rep> Identifier::add(JSGlobalData* globalData, const char* c) | |
125 | { | |
126 | if (!c) { | |
127 | UString::Rep::null().hash(); | |
128 | return &UString::Rep::null(); | |
129 | } | |
130 | if (!c[0]) { | |
131 | UString::Rep::empty().hash(); | |
132 | return &UString::Rep::empty(); | |
133 | } | |
134 | if (!c[1]) | |
135 | return add(globalData, globalData->smallStrings.singleCharacterStringRep(static_cast<unsigned char>(c[0]))); | |
136 | ||
137 | IdentifierTable& identifierTable = *globalData->identifierTable; | |
138 | LiteralIdentifierTable& literalIdentifierTable = identifierTable.literalTable(); | |
139 | ||
140 | const LiteralIdentifierTable::iterator& iter = literalIdentifierTable.find(c); | |
141 | if (iter != literalIdentifierTable.end()) | |
142 | return iter->second; | |
143 | ||
144 | pair<HashSet<UString::Rep*>::iterator, bool> addResult = identifierTable.add<const char*, CStringTranslator>(c); | |
145 | ||
146 | // If the string is newly-translated, then we need to adopt it. | |
147 | // The boolean in the pair tells us if that is so. | |
148 | RefPtr<UString::Rep> addedString = addResult.second ? adoptRef(*addResult.first) : *addResult.first; | |
149 | ||
150 | literalIdentifierTable.add(c, addedString.get()); | |
151 | ||
152 | return addedString.release(); | |
153 | } | |
154 | ||
155 | PassRefPtr<UString::Rep> Identifier::add(ExecState* exec, const char* c) | |
156 | { | |
157 | return add(&exec->globalData(), c); | |
158 | } | |
159 | ||
160 | struct UCharBuffer { | |
161 | const UChar* s; | |
162 | unsigned int length; | |
163 | }; | |
164 | ||
165 | struct UCharBufferTranslator { | |
166 | static unsigned hash(const UCharBuffer& buf) | |
167 | { | |
168 | return UString::Rep::computeHash(buf.s, buf.length); | |
169 | } | |
170 | ||
171 | static bool equal(UString::Rep* str, const UCharBuffer& buf) | |
172 | { | |
173 | return Identifier::equal(str, buf.s, buf.length); | |
174 | } | |
175 | ||
176 | static void translate(UString::Rep*& location, const UCharBuffer& buf, unsigned hash) | |
177 | { | |
f9bf01c6 A |
178 | UChar* d; |
179 | UString::Rep* r = UString::Rep::createUninitialized(buf.length, d).releaseRef(); | |
9dae56ea A |
180 | for (unsigned i = 0; i != buf.length; i++) |
181 | d[i] = buf.s[i]; | |
f9bf01c6 | 182 | r->setHash(hash); |
9dae56ea A |
183 | location = r; |
184 | } | |
185 | }; | |
186 | ||
187 | PassRefPtr<UString::Rep> Identifier::add(JSGlobalData* globalData, const UChar* s, int length) | |
188 | { | |
189 | if (length == 1) { | |
190 | UChar c = s[0]; | |
191 | if (c <= 0xFF) | |
192 | return add(globalData, globalData->smallStrings.singleCharacterStringRep(c)); | |
193 | } | |
194 | if (!length) { | |
195 | UString::Rep::empty().hash(); | |
196 | return &UString::Rep::empty(); | |
197 | } | |
198 | UCharBuffer buf = {s, length}; | |
199 | pair<HashSet<UString::Rep*>::iterator, bool> addResult = globalData->identifierTable->add<UCharBuffer, UCharBufferTranslator>(buf); | |
200 | ||
201 | // If the string is newly-translated, then we need to adopt it. | |
202 | // The boolean in the pair tells us if that is so. | |
203 | return addResult.second ? adoptRef(*addResult.first) : *addResult.first; | |
204 | } | |
205 | ||
206 | PassRefPtr<UString::Rep> Identifier::add(ExecState* exec, const UChar* s, int length) | |
207 | { | |
208 | return add(&exec->globalData(), s, length); | |
209 | } | |
210 | ||
211 | PassRefPtr<UString::Rep> Identifier::addSlowCase(JSGlobalData* globalData, UString::Rep* r) | |
212 | { | |
f9bf01c6 A |
213 | ASSERT(!r->isIdentifier()); |
214 | if (r->size() == 1) { | |
9dae56ea A |
215 | UChar c = r->data()[0]; |
216 | if (c <= 0xFF) | |
217 | r = globalData->smallStrings.singleCharacterStringRep(c); | |
f9bf01c6 | 218 | if (r->isIdentifier()) { |
9dae56ea A |
219 | #ifndef NDEBUG |
220 | checkSameIdentifierTable(globalData, r); | |
221 | #endif | |
222 | return r; | |
223 | } | |
224 | } | |
f9bf01c6 | 225 | if (!r->size()) { |
9dae56ea A |
226 | UString::Rep::empty().hash(); |
227 | return &UString::Rep::empty(); | |
228 | } | |
229 | return *globalData->identifierTable->add(r).first; | |
230 | } | |
231 | ||
232 | PassRefPtr<UString::Rep> Identifier::addSlowCase(ExecState* exec, UString::Rep* r) | |
233 | { | |
234 | return addSlowCase(&exec->globalData(), r); | |
235 | } | |
236 | ||
237 | void Identifier::remove(UString::Rep* r) | |
238 | { | |
f9bf01c6 | 239 | currentIdentifierTable()->remove(r); |
9dae56ea A |
240 | } |
241 | ||
242 | #ifndef NDEBUG | |
243 | ||
f9bf01c6 | 244 | void Identifier::checkSameIdentifierTable(ExecState* exec, UString::Rep*) |
9dae56ea | 245 | { |
f9bf01c6 | 246 | ASSERT_UNUSED(exec, exec->globalData().identifierTable == currentIdentifierTable()); |
9dae56ea A |
247 | } |
248 | ||
f9bf01c6 | 249 | void Identifier::checkSameIdentifierTable(JSGlobalData* globalData, UString::Rep*) |
9dae56ea | 250 | { |
f9bf01c6 | 251 | ASSERT_UNUSED(globalData, globalData->identifierTable == currentIdentifierTable()); |
9dae56ea A |
252 | } |
253 | ||
254 | #else | |
255 | ||
256 | void Identifier::checkSameIdentifierTable(ExecState*, UString::Rep*) | |
257 | { | |
258 | } | |
259 | ||
260 | void Identifier::checkSameIdentifierTable(JSGlobalData*, UString::Rep*) | |
261 | { | |
262 | } | |
263 | ||
264 | #endif | |
265 | ||
f9bf01c6 A |
266 | ThreadSpecific<ThreadIdentifierTableData>* g_identifierTableSpecific = 0; |
267 | ThreadIdentifierTableData* g_identifierTableMain = 0; | |
268 | ||
269 | #if ENABLE(JSC_MULTIPLE_THREADS) | |
270 | ||
271 | pthread_once_t createIdentifierTableSpecificOnce = PTHREAD_ONCE_INIT; | |
272 | static void createIdentifierTableSpecificCallback() | |
273 | { | |
274 | ASSERT(!g_identifierTableSpecific); | |
275 | g_identifierTableSpecific = new ThreadSpecific<ThreadIdentifierTableData>(); | |
276 | ASSERT(!g_identifierTableMain); | |
277 | g_identifierTableMain = new ThreadIdentifierTableData(); | |
278 | } | |
279 | void createIdentifierTableSpecific() | |
280 | { | |
281 | pthread_once(&createIdentifierTableSpecificOnce, createIdentifierTableSpecificCallback); | |
282 | ASSERT(g_identifierTableSpecific); | |
283 | ASSERT(g_identifierTableMain); | |
284 | } | |
285 | ||
286 | #else | |
287 | ||
288 | void createIdentifierTableSpecific() | |
289 | { | |
290 | ASSERT(!g_identifierTableSpecific); | |
291 | g_identifierTableSpecific = new ThreadSpecific<ThreadIdentifierTableData>(); | |
292 | ASSERT(!g_identifierTableMain); | |
293 | g_identifierTableMain = new ThreadIdentifierTableData(); | |
294 | } | |
295 | ||
296 | #endif | |
297 | ||
9dae56ea | 298 | } // namespace JSC |