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