]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/Identifier.cpp
JavaScriptCore-1097.3.3.tar.gz
[apple/javascriptcore.git] / runtime / Identifier.cpp
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 "JSObject.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/text/StringHash.h>
34
35 using WTF::ThreadSpecific;
36
37 namespace JSC {
38
39 IdentifierTable* createIdentifierTable()
40 {
41 return new IdentifierTable;
42 }
43
44 void deleteIdentifierTable(IdentifierTable* table)
45 {
46 delete table;
47 }
48
49 struct IdentifierCStringTranslator {
50 static unsigned hash(const LChar* c)
51 {
52 return StringHasher::computeHash<LChar>(c);
53 }
54
55 static bool equal(StringImpl* r, const LChar* s)
56 {
57 return Identifier::equal(r, s);
58 }
59
60 static void translate(StringImpl*& location, const LChar* c, unsigned hash)
61 {
62 size_t length = strlen(reinterpret_cast<const char*>(c));
63 LChar* d;
64 StringImpl* r = StringImpl::createUninitialized(length, d).leakRef();
65 for (size_t i = 0; i != length; i++)
66 d[i] = c[i];
67 r->setHash(hash);
68 location = r;
69 }
70 };
71
72 struct IdentifierLCharFromUCharTranslator {
73 static unsigned hash(const CharBuffer<UChar>& buf)
74 {
75 return StringHasher::computeHash<UChar>(buf.s, buf.length);
76 }
77
78 static bool equal(StringImpl* str, const CharBuffer<UChar>& buf)
79 {
80 return Identifier::equal(str, buf.s, buf.length);
81 }
82
83 static void translate(StringImpl*& location, const CharBuffer<UChar>& buf, unsigned hash)
84 {
85 LChar* d;
86 StringImpl* r = StringImpl::createUninitialized(buf.length, d).leakRef();
87 for (unsigned i = 0; i != buf.length; i++) {
88 UChar c = buf.s[i];
89 ASSERT(c <= 0xff);
90 d[i] = c;
91 }
92 r->setHash(hash);
93 location = r;
94 }
95 };
96
97 PassRefPtr<StringImpl> Identifier::add(JSGlobalData* globalData, const char* c)
98 {
99 if (!c)
100 return 0;
101 if (!c[0])
102 return StringImpl::empty();
103 if (!c[1])
104 return add(globalData, globalData->smallStrings.singleCharacterStringRep(c[0]));
105
106 IdentifierTable& identifierTable = *globalData->identifierTable;
107 LiteralIdentifierTable& literalIdentifierTable = identifierTable.literalTable();
108
109 const LiteralIdentifierTable::iterator& iter = literalIdentifierTable.find(c);
110 if (iter != literalIdentifierTable.end())
111 return iter->second;
112
113 HashSet<StringImpl*>::AddResult addResult = identifierTable.add<const LChar*, IdentifierCStringTranslator>(reinterpret_cast<const LChar*>(c));
114
115 // If the string is newly-translated, then we need to adopt it.
116 // The boolean in the pair tells us if that is so.
117 RefPtr<StringImpl> addedString = addResult.isNewEntry ? adoptRef(*addResult.iterator) : *addResult.iterator;
118
119 literalIdentifierTable.add(c, addedString.get());
120
121 return addedString.release();
122 }
123
124 PassRefPtr<StringImpl> Identifier::add(ExecState* exec, const char* c)
125 {
126 return add(&exec->globalData(), c);
127 }
128
129 PassRefPtr<StringImpl> Identifier::add8(JSGlobalData* globalData, const UChar* s, int length)
130 {
131 if (length == 1) {
132 UChar c = s[0];
133 ASSERT(c <= 0xff);
134 if (canUseSingleCharacterString(c))
135 return add(globalData, globalData->smallStrings.singleCharacterStringRep(c));
136 }
137
138 if (!length)
139 return StringImpl::empty();
140 CharBuffer<UChar> buf = {s, length};
141 HashSet<StringImpl*>::AddResult addResult = globalData->identifierTable->add<CharBuffer<UChar>, IdentifierLCharFromUCharTranslator >(buf);
142
143 // If the string is newly-translated, then we need to adopt it.
144 // The boolean in the pair tells us if that is so.
145 return addResult.isNewEntry ? adoptRef(*addResult.iterator) : *addResult.iterator;
146 }
147
148 template <typename CharType>
149 ALWAYS_INLINE uint32_t Identifier::toUInt32FromCharacters(const CharType* characters, unsigned length, bool& ok)
150 {
151 // Get the first character, turning it into a digit.
152 uint32_t value = characters[0] - '0';
153 if (value > 9)
154 return 0;
155
156 // Check for leading zeros. If the first characher is 0, then the
157 // length of the string must be one - e.g. "042" is not equal to "42".
158 if (!value && length > 1)
159 return 0;
160
161 while (--length) {
162 // Multiply value by 10, checking for overflow out of 32 bits.
163 if (value > 0xFFFFFFFFU / 10)
164 return 0;
165 value *= 10;
166
167 // Get the next character, turning it into a digit.
168 uint32_t newValue = *(++characters) - '0';
169 if (newValue > 9)
170 return 0;
171
172 // Add in the old value, checking for overflow out of 32 bits.
173 newValue += value;
174 if (newValue < value)
175 return 0;
176 value = newValue;
177 }
178
179 ok = true;
180 return value;
181 }
182
183 uint32_t Identifier::toUInt32(const UString& string, bool& ok)
184 {
185 ok = false;
186
187 unsigned length = string.length();
188
189 // An empty string is not a number.
190 if (!length)
191 return 0;
192
193 if (string.is8Bit())
194 return toUInt32FromCharacters(string.characters8(), length, ok);
195 return toUInt32FromCharacters(string.characters16(), length, ok);
196 }
197
198 PassRefPtr<StringImpl> Identifier::addSlowCase(JSGlobalData* globalData, StringImpl* r)
199 {
200 ASSERT(!r->isIdentifier());
201 // The empty & null strings are static singletons, and static strings are handled
202 // in ::add() in the header, so we should never get here with a zero length string.
203 ASSERT(r->length());
204
205 if (r->length() == 1) {
206 UChar c = (*r)[0];
207 if (c <= maxSingleCharacterString)
208 r = globalData->smallStrings.singleCharacterStringRep(c);
209 if (r->isIdentifier())
210 return r;
211 }
212
213 return *globalData->identifierTable->add(r).iterator;
214 }
215
216 PassRefPtr<StringImpl> Identifier::addSlowCase(ExecState* exec, StringImpl* r)
217 {
218 return addSlowCase(&exec->globalData(), r);
219 }
220
221 Identifier Identifier::from(ExecState* exec, unsigned value)
222 {
223 return Identifier(exec, exec->globalData().numericStrings.add(value));
224 }
225
226 Identifier Identifier::from(ExecState* exec, int value)
227 {
228 return Identifier(exec, exec->globalData().numericStrings.add(value));
229 }
230
231 Identifier Identifier::from(ExecState* exec, double value)
232 {
233 return Identifier(exec, exec->globalData().numericStrings.add(value));
234 }
235
236 Identifier Identifier::from(JSGlobalData* globalData, unsigned value)
237 {
238 return Identifier(globalData, globalData->numericStrings.add(value));
239 }
240
241 Identifier Identifier::from(JSGlobalData* globalData, int value)
242 {
243 return Identifier(globalData, globalData->numericStrings.add(value));
244 }
245
246 Identifier Identifier::from(JSGlobalData* globalData, double value)
247 {
248 return Identifier(globalData, globalData->numericStrings.add(value));
249 }
250
251 #ifndef NDEBUG
252
253 void Identifier::checkCurrentIdentifierTable(JSGlobalData* globalData)
254 {
255 // Check the identifier table accessible through the threadspecific matches the
256 // globalData's identifier table.
257 ASSERT_UNUSED(globalData, globalData->identifierTable == wtfThreadData().currentIdentifierTable());
258 }
259
260 void Identifier::checkCurrentIdentifierTable(ExecState* exec)
261 {
262 checkCurrentIdentifierTable(&exec->globalData());
263 }
264
265 #else
266
267 // These only exists so that our exports are the same for debug and release builds.
268 // This would be an ASSERT_NOT_REACHED(), but we're in NDEBUG only code here!
269 NO_RETURN_DUE_TO_CRASH void Identifier::checkCurrentIdentifierTable(JSGlobalData*) { CRASH(); }
270 NO_RETURN_DUE_TO_CRASH void Identifier::checkCurrentIdentifierTable(ExecState*) { CRASH(); }
271
272 #endif
273
274 } // namespace JSC