initial import
[vuplus_webkit] / Source / JavaScriptCore / 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/WTFThreadData.h>
34 #include <wtf/text/StringHash.h>
35
36 using WTF::ThreadSpecific;
37
38 namespace JSC {
39
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 }
59
60 IdentifierTable* createIdentifierTable()
61 {
62     return new IdentifierTable;
63 }
64
65 void deleteIdentifierTable(IdentifierTable* table)
66 {
67     delete table;
68 }
69
70 struct IdentifierCStringTranslator {
71     static unsigned hash(const char* c)
72     {
73         return StringHasher::computeHash<char>(c);
74     }
75
76     static bool equal(StringImpl* r, const char* s)
77     {
78         return Identifier::equal(r, s);
79     }
80
81     static void translate(StringImpl*& location, const char* c, unsigned hash)
82     {
83         size_t length = strlen(c);
84         UChar* d;
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
88         r->setHash(hash);
89         location = r;
90     }
91 };
92
93 PassRefPtr<StringImpl> Identifier::add(JSGlobalData* globalData, const char* c)
94 {
95     if (!c)
96         return 0;
97     if (!c[0])
98         return StringImpl::empty();
99     if (!c[1])
100         return add(globalData, globalData->smallStrings.singleCharacterStringRep(static_cast<unsigned char>(c[0])));
101
102     IdentifierTable& identifierTable = *globalData->identifierTable;
103     LiteralIdentifierTable& literalIdentifierTable = identifierTable.literalTable();
104
105     const LiteralIdentifierTable::iterator& iter = literalIdentifierTable.find(c);
106     if (iter != literalIdentifierTable.end())
107         return iter->second;
108
109     pair<HashSet<StringImpl*>::iterator, bool> addResult = identifierTable.add<const char*, IdentifierCStringTranslator>(c);
110
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;
114
115     literalIdentifierTable.add(c, addedString.get());
116
117     return addedString.release();
118 }
119
120 PassRefPtr<StringImpl> Identifier::add(ExecState* exec, const char* c)
121 {
122     return add(&exec->globalData(), c);
123 }
124
125 struct UCharBuffer {
126     const UChar* s;
127     unsigned int length;
128 };
129
130 struct IdentifierUCharBufferTranslator {
131     static unsigned hash(const UCharBuffer& buf)
132     {
133         return StringHasher::computeHash<UChar>(buf.s, buf.length);
134     }
135
136     static bool equal(StringImpl* str, const UCharBuffer& buf)
137     {
138         return Identifier::equal(str, buf.s, buf.length);
139     }
140
141     static void translate(StringImpl*& location, const UCharBuffer& buf, unsigned hash)
142     {
143         UChar* d;
144         StringImpl* r = StringImpl::createUninitialized(buf.length, d).leakRef();
145         for (unsigned i = 0; i != buf.length; i++)
146             d[i] = buf.s[i];
147         r->setHash(hash);
148         location = r; 
149     }
150 };
151
152 uint32_t Identifier::toUInt32(const UString& string, bool& ok)
153 {
154     ok = false;
155
156     unsigned length = string.length();
157     const UChar* characters = string.characters();
158
159     // An empty string is not a number.
160     if (!length)
161         return 0;
162
163     // Get the first character, turning it into a digit.
164     uint32_t value = characters[0] - '0';
165     if (value > 9)
166         return 0;
167
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)
171         return 0;
172
173     while (--length) {
174         // Multiply value by 10, checking for overflow out of 32 bits.
175         if (value > 0xFFFFFFFFU / 10)
176             return 0;
177         value *= 10;
178
179         // Get the next character, turning it into a digit.
180         uint32_t newValue = *(++characters) - '0';
181         if (newValue > 9)
182             return 0;
183
184         // Add in the old value, checking for overflow out of 32 bits.
185         newValue += value;
186         if (newValue < value)
187             return 0;
188         value = newValue;
189     }
190
191     ok = true;
192     return value;
193 }
194
195 PassRefPtr<StringImpl> Identifier::add(JSGlobalData* globalData, const UChar* s, int length)
196 {
197     if (length == 1) {
198         UChar c = s[0];
199         if (c <= maxSingleCharacterString)
200             return add(globalData, globalData->smallStrings.singleCharacterStringRep(c));
201     }
202     if (!length)
203         return StringImpl::empty();
204     UCharBuffer buf = {s, length}; 
205     pair<HashSet<StringImpl*>::iterator, bool> addResult = globalData->identifierTable->add<UCharBuffer, IdentifierUCharBufferTranslator>(buf);
206
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;
210 }
211
212 PassRefPtr<StringImpl> Identifier::add(ExecState* exec, const UChar* s, int length)
213 {
214     return add(&exec->globalData(), s, length);
215 }
216
217 PassRefPtr<StringImpl> Identifier::addSlowCase(JSGlobalData* globalData, StringImpl* r)
218 {
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.
222     ASSERT(r->length());
223
224     if (r->length() == 1) {
225         UChar c = (*r)[0];
226         if (c <= maxSingleCharacterString)
227             r = globalData->smallStrings.singleCharacterStringRep(c);
228             if (r->isIdentifier())
229                 return r;
230     }
231
232     return *globalData->identifierTable->add(r).first;
233 }
234
235 PassRefPtr<StringImpl> Identifier::addSlowCase(ExecState* exec, StringImpl* r)
236 {
237     return addSlowCase(&exec->globalData(), r);
238 }
239
240 Identifier Identifier::from(ExecState* exec, unsigned value)
241 {
242     return Identifier(exec, exec->globalData().numericStrings.add(value));
243 }
244
245 Identifier Identifier::from(ExecState* exec, int value)
246 {
247     return Identifier(exec, exec->globalData().numericStrings.add(value));
248 }
249
250 Identifier Identifier::from(ExecState* exec, double value)
251 {
252     return Identifier(exec, exec->globalData().numericStrings.add(value));
253 }
254
255 Identifier Identifier::from(JSGlobalData* globalData, unsigned value)
256 {
257     return Identifier(globalData, globalData->numericStrings.add(value));
258 }
259
260 Identifier Identifier::from(JSGlobalData* globalData, int value)
261 {
262     return Identifier(globalData, globalData->numericStrings.add(value));
263 }
264
265 Identifier Identifier::from(JSGlobalData* globalData, double value)
266 {
267     return Identifier(globalData, globalData->numericStrings.add(value));
268 }
269
270 #ifndef NDEBUG
271
272 void Identifier::checkCurrentIdentifierTable(JSGlobalData* globalData)
273 {
274     // Check the identifier table accessible through the threadspecific matches the
275     // globalData's identifier table.
276     ASSERT_UNUSED(globalData, globalData->identifierTable == wtfThreadData().currentIdentifierTable());
277 }
278
279 void Identifier::checkCurrentIdentifierTable(ExecState* exec)
280 {
281     checkCurrentIdentifierTable(&exec->globalData());
282 }
283
284 #else
285
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(); }
290
291 #endif
292
293 } // namespace JSC