]> git.saurik.com Git - apple/javascriptcore.git/blame - runtime/Identifier.cpp
JavaScriptCore-1097.3.tar.gz
[apple/javascriptcore.git] / runtime / Identifier.cpp
CommitLineData
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 33#include <wtf/text/StringHash.h>
9dae56ea 34
f9bf01c6
A
35using WTF::ThreadSpecific;
36
9dae56ea
A
37namespace JSC {
38
9dae56ea
A
39IdentifierTable* createIdentifierTable()
40{
41 return new IdentifierTable;
42}
43
44void deleteIdentifierTable(IdentifierTable* table)
45{
46 delete table;
47}
48
4e4e5a6f 49struct IdentifierCStringTranslator {
6fe7ccc8 50 static unsigned hash(const LChar* c)
9dae56ea 51 {
6fe7ccc8 52 return StringHasher::computeHash<LChar>(c);
9dae56ea
A
53 }
54
6fe7ccc8 55 static bool equal(StringImpl* r, const LChar* s)
9dae56ea
A
56 {
57 return Identifier::equal(r, s);
58 }
59
6fe7ccc8 60 static void translate(StringImpl*& location, const LChar* c, unsigned hash)
9dae56ea 61 {
6fe7ccc8
A
62 size_t length = strlen(reinterpret_cast<const char*>(c));
63 LChar* d;
14957cd0 64 StringImpl* r = StringImpl::createUninitialized(length, d).leakRef();
9dae56ea 65 for (size_t i = 0; i != length; i++)
6fe7ccc8 66 d[i] = c[i];
f9bf01c6 67 r->setHash(hash);
9dae56ea
A
68 location = r;
69 }
70};
71
6fe7ccc8
A
72struct 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
14957cd0 97PassRefPtr<StringImpl> Identifier::add(JSGlobalData* globalData, const char* c)
9dae56ea 98{
4e4e5a6f 99 if (!c)
14957cd0 100 return 0;
4e4e5a6f 101 if (!c[0])
14957cd0 102 return StringImpl::empty();
9dae56ea 103 if (!c[1])
6fe7ccc8 104 return add(globalData, globalData->smallStrings.singleCharacterStringRep(c[0]));
9dae56ea
A
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
6fe7ccc8 113 HashSet<StringImpl*>::AddResult addResult = identifierTable.add<const LChar*, IdentifierCStringTranslator>(reinterpret_cast<const LChar*>(c));
9dae56ea
A
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.
6fe7ccc8 117 RefPtr<StringImpl> addedString = addResult.isNewEntry ? adoptRef(*addResult.iterator) : *addResult.iterator;
9dae56ea
A
118
119 literalIdentifierTable.add(c, addedString.get());
120
121 return addedString.release();
122}
123
14957cd0 124PassRefPtr<StringImpl> Identifier::add(ExecState* exec, const char* c)
9dae56ea
A
125{
126 return add(&exec->globalData(), c);
127}
128
6fe7ccc8 129PassRefPtr<StringImpl> Identifier::add8(JSGlobalData* globalData, const UChar* s, int length)
14957cd0 130{
6fe7ccc8
A
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
14957cd0 138 if (!length)
6fe7ccc8
A
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}
14957cd0 147
6fe7ccc8
A
148template <typename CharType>
149ALWAYS_INLINE uint32_t Identifier::toUInt32FromCharacters(const CharType* characters, unsigned length, bool& ok)
150{
14957cd0
A
151 // Get the first character, turning it into a digit.
152 uint32_t value = characters[0] - '0';
153 if (value > 9)
154 return 0;
6fe7ccc8 155
14957cd0
A
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;
6fe7ccc8 160
14957cd0
A
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;
6fe7ccc8 166
14957cd0
A
167 // Get the next character, turning it into a digit.
168 uint32_t newValue = *(++characters) - '0';
169 if (newValue > 9)
170 return 0;
6fe7ccc8 171
14957cd0
A
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 }
6fe7ccc8 178
14957cd0
A
179 ok = true;
180 return value;
181}
182
6fe7ccc8 183uint32_t Identifier::toUInt32(const UString& string, bool& ok)
9dae56ea 184{
6fe7ccc8 185 ok = false;
9dae56ea 186
6fe7ccc8 187 unsigned length = string.length();
9dae56ea 188
6fe7ccc8
A
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);
9dae56ea
A
196}
197
14957cd0 198PassRefPtr<StringImpl> Identifier::addSlowCase(JSGlobalData* globalData, StringImpl* r)
9dae56ea 199{
f9bf01c6 200 ASSERT(!r->isIdentifier());
4e4e5a6f
A
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) {
6fe7ccc8 206 UChar c = (*r)[0];
14957cd0 207 if (c <= maxSingleCharacterString)
9dae56ea 208 r = globalData->smallStrings.singleCharacterStringRep(c);
4e4e5a6f 209 if (r->isIdentifier())
9dae56ea 210 return r;
9dae56ea 211 }
4e4e5a6f 212
6fe7ccc8 213 return *globalData->identifierTable->add(r).iterator;
9dae56ea
A
214}
215
14957cd0 216PassRefPtr<StringImpl> Identifier::addSlowCase(ExecState* exec, StringImpl* r)
9dae56ea
A
217{
218 return addSlowCase(&exec->globalData(), r);
219}
220
4e4e5a6f 221Identifier Identifier::from(ExecState* exec, unsigned value)
9dae56ea 222{
4e4e5a6f 223 return Identifier(exec, exec->globalData().numericStrings.add(value));
9dae56ea
A
224}
225
4e4e5a6f 226Identifier Identifier::from(ExecState* exec, int value)
9dae56ea 227{
4e4e5a6f 228 return Identifier(exec, exec->globalData().numericStrings.add(value));
9dae56ea
A
229}
230
4e4e5a6f 231Identifier Identifier::from(ExecState* exec, double value)
9dae56ea 232{
4e4e5a6f 233 return Identifier(exec, exec->globalData().numericStrings.add(value));
9dae56ea
A
234}
235
14957cd0
A
236Identifier Identifier::from(JSGlobalData* globalData, unsigned value)
237{
238 return Identifier(globalData, globalData->numericStrings.add(value));
239}
240
241Identifier Identifier::from(JSGlobalData* globalData, int value)
242{
243 return Identifier(globalData, globalData->numericStrings.add(value));
244}
245
246Identifier Identifier::from(JSGlobalData* globalData, double value)
247{
248 return Identifier(globalData, globalData->numericStrings.add(value));
249}
250
4e4e5a6f 251#ifndef NDEBUG
9dae56ea 252
4e4e5a6f 253void Identifier::checkCurrentIdentifierTable(JSGlobalData* globalData)
9dae56ea 254{
4e4e5a6f
A
255 // Check the identifier table accessible through the threadspecific matches the
256 // globalData's identifier table.
257 ASSERT_UNUSED(globalData, globalData->identifierTable == wtfThreadData().currentIdentifierTable());
9dae56ea
A
258}
259
4e4e5a6f 260void Identifier::checkCurrentIdentifierTable(ExecState* exec)
f9bf01c6 261{
4e4e5a6f 262 checkCurrentIdentifierTable(&exec->globalData());
f9bf01c6
A
263}
264
4e4e5a6f 265#else
f9bf01c6 266
4e4e5a6f
A
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!
6fe7ccc8
A
269NO_RETURN_DUE_TO_CRASH void Identifier::checkCurrentIdentifierTable(JSGlobalData*) { CRASH(); }
270NO_RETURN_DUE_TO_CRASH void Identifier::checkCurrentIdentifierTable(ExecState*) { CRASH(); }
f9bf01c6
A
271
272#endif
273
9dae56ea 274} // namespace JSC