]>
Commit | Line | Data |
---|---|---|
9dae56ea A |
1 | /* |
2 | * Copyright (C) 2003, 2006, 2007, 2008, 2009 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 | #ifndef Identifier_h | |
22 | #define Identifier_h | |
23 | ||
24 | #include "JSGlobalData.h" | |
6fe7ccc8 | 25 | #include <wtf/ThreadSpecific.h> |
9dae56ea | 26 | #include "UString.h" |
6fe7ccc8 | 27 | #include <wtf/WTFThreadData.h> |
14957cd0 | 28 | #include <wtf/text/CString.h> |
9dae56ea A |
29 | |
30 | namespace JSC { | |
31 | ||
32 | class ExecState; | |
33 | ||
34 | class Identifier { | |
35 | friend class Structure; | |
36 | public: | |
37 | Identifier() { } | |
38 | ||
14957cd0 | 39 | Identifier(ExecState* exec, const char* s) : m_string(add(exec, s)) { } // Only to be used with string literals. |
14957cd0 A |
40 | Identifier(ExecState* exec, StringImpl* rep) : m_string(add(exec, rep)) { } |
41 | Identifier(ExecState* exec, const UString& s) : m_string(add(exec, s.impl())) { } | |
9dae56ea | 42 | |
14957cd0 | 43 | Identifier(JSGlobalData* globalData, const char* s) : m_string(add(globalData, s)) { } // Only to be used with string literals. |
6fe7ccc8 | 44 | Identifier(JSGlobalData* globalData, const LChar* s, int length) : m_string(add(globalData, s, length)) { } |
14957cd0 A |
45 | Identifier(JSGlobalData* globalData, const UChar* s, int length) : m_string(add(globalData, s, length)) { } |
46 | Identifier(JSGlobalData* globalData, StringImpl* rep) : m_string(add(globalData, rep)) { } | |
47 | Identifier(JSGlobalData* globalData, const UString& s) : m_string(add(globalData, s.impl())) { } | |
9dae56ea | 48 | |
14957cd0 A |
49 | const UString& ustring() const { return m_string; } |
50 | StringImpl* impl() const { return m_string.impl(); } | |
9dae56ea | 51 | |
14957cd0 A |
52 | const UChar* characters() const { return m_string.characters(); } |
53 | int length() const { return m_string.length(); } | |
9dae56ea | 54 | |
14957cd0 | 55 | CString ascii() const { return m_string.ascii(); } |
6fe7ccc8 A |
56 | |
57 | static Identifier createLCharFromUChar(JSGlobalData* globalData, const UChar* s, int length) { return Identifier(globalData, add8(globalData, s, length)); } | |
58 | ||
59 | JS_EXPORT_PRIVATE static Identifier from(ExecState* exec, unsigned y); | |
60 | JS_EXPORT_PRIVATE static Identifier from(ExecState* exec, int y); | |
4e4e5a6f | 61 | static Identifier from(ExecState* exec, double y); |
14957cd0 A |
62 | static Identifier from(JSGlobalData*, unsigned y); |
63 | static Identifier from(JSGlobalData*, int y); | |
64 | static Identifier from(JSGlobalData*, double y); | |
65 | ||
6fe7ccc8 | 66 | JS_EXPORT_PRIVATE static uint32_t toUInt32(const UString&, bool& ok); |
14957cd0 A |
67 | uint32_t toUInt32(bool& ok) const { return toUInt32(m_string, ok); } |
68 | unsigned toArrayIndex(bool& ok) const; | |
69 | ||
70 | bool isNull() const { return m_string.isNull(); } | |
71 | bool isEmpty() const { return m_string.isEmpty(); } | |
9dae56ea A |
72 | |
73 | friend bool operator==(const Identifier&, const Identifier&); | |
74 | friend bool operator!=(const Identifier&, const Identifier&); | |
75 | ||
6fe7ccc8 | 76 | friend bool operator==(const Identifier&, const LChar*); |
9dae56ea | 77 | friend bool operator==(const Identifier&, const char*); |
6fe7ccc8 | 78 | friend bool operator!=(const Identifier&, const LChar*); |
9dae56ea A |
79 | friend bool operator!=(const Identifier&, const char*); |
80 | ||
6fe7ccc8 A |
81 | static bool equal(const StringImpl*, const LChar*); |
82 | static inline bool equal(const StringImpl*a, const char*b) { return Identifier::equal(a, reinterpret_cast<const LChar*>(b)); }; | |
83 | static bool equal(const StringImpl*, const LChar*, unsigned length); | |
14957cd0 A |
84 | static bool equal(const StringImpl*, const UChar*, unsigned length); |
85 | static bool equal(const StringImpl* a, const StringImpl* b) { return ::equal(a, b); } | |
9dae56ea | 86 | |
6fe7ccc8 | 87 | JS_EXPORT_PRIVATE static PassRefPtr<StringImpl> add(ExecState*, const char*); // Only to be used with string literals. |
14957cd0 | 88 | static PassRefPtr<StringImpl> add(JSGlobalData*, const char*); // Only to be used with string literals. |
9dae56ea A |
89 | |
90 | private: | |
14957cd0 | 91 | UString m_string; |
6fe7ccc8 A |
92 | |
93 | template <typename CharType> | |
94 | ALWAYS_INLINE static uint32_t toUInt32FromCharacters(const CharType* characters, unsigned length, bool& ok); | |
95 | ||
14957cd0 | 96 | static bool equal(const Identifier& a, const Identifier& b) { return a.m_string.impl() == b.m_string.impl(); } |
6fe7ccc8 | 97 | static bool equal(const Identifier& a, const LChar* b) { return equal(a.m_string.impl(), b); } |
9dae56ea | 98 | |
6fe7ccc8 A |
99 | template <typename T> static PassRefPtr<StringImpl> add(JSGlobalData*, const T*, int length); |
100 | static PassRefPtr<StringImpl> add8(JSGlobalData*, const UChar*, int length); | |
101 | template <typename T> ALWAYS_INLINE static bool canUseSingleCharacterString(T); | |
9dae56ea | 102 | |
14957cd0 | 103 | static PassRefPtr<StringImpl> add(ExecState* exec, StringImpl* r) |
9dae56ea | 104 | { |
9dae56ea | 105 | #ifndef NDEBUG |
4e4e5a6f | 106 | checkCurrentIdentifierTable(exec); |
9dae56ea | 107 | #endif |
4e4e5a6f | 108 | if (r->isIdentifier()) |
9dae56ea | 109 | return r; |
9dae56ea A |
110 | return addSlowCase(exec, r); |
111 | } | |
14957cd0 | 112 | static PassRefPtr<StringImpl> add(JSGlobalData* globalData, StringImpl* r) |
9dae56ea | 113 | { |
9dae56ea | 114 | #ifndef NDEBUG |
4e4e5a6f | 115 | checkCurrentIdentifierTable(globalData); |
9dae56ea | 116 | #endif |
4e4e5a6f | 117 | if (r->isIdentifier()) |
9dae56ea | 118 | return r; |
9dae56ea A |
119 | return addSlowCase(globalData, r); |
120 | } | |
121 | ||
6fe7ccc8 A |
122 | JS_EXPORT_PRIVATE static PassRefPtr<StringImpl> addSlowCase(ExecState*, StringImpl* r); |
123 | JS_EXPORT_PRIVATE static PassRefPtr<StringImpl> addSlowCase(JSGlobalData*, StringImpl* r); | |
9dae56ea | 124 | |
6fe7ccc8 A |
125 | JS_EXPORT_PRIVATE static void checkCurrentIdentifierTable(ExecState*); |
126 | JS_EXPORT_PRIVATE static void checkCurrentIdentifierTable(JSGlobalData*); | |
127 | }; | |
128 | ||
129 | template <> ALWAYS_INLINE bool Identifier::canUseSingleCharacterString(LChar) | |
130 | { | |
131 | ASSERT(maxSingleCharacterString == 0xff); | |
132 | return true; | |
133 | } | |
134 | ||
135 | template <> ALWAYS_INLINE bool Identifier::canUseSingleCharacterString(UChar c) | |
136 | { | |
137 | return (c <= maxSingleCharacterString); | |
138 | } | |
139 | ||
140 | template <typename T> | |
141 | struct CharBuffer { | |
142 | const T* s; | |
143 | unsigned int length; | |
9dae56ea A |
144 | }; |
145 | ||
6fe7ccc8 A |
146 | template <typename T> |
147 | struct IdentifierCharBufferTranslator { | |
148 | static unsigned hash(const CharBuffer<T>& buf) | |
149 | { | |
150 | return StringHasher::computeHash<T>(buf.s, buf.length); | |
151 | } | |
152 | ||
153 | static bool equal(StringImpl* str, const CharBuffer<T>& buf) | |
154 | { | |
155 | return Identifier::equal(str, buf.s, buf.length); | |
156 | } | |
157 | ||
158 | static void translate(StringImpl*& location, const CharBuffer<T>& buf, unsigned hash) | |
159 | { | |
160 | T* d; | |
161 | StringImpl* r = StringImpl::createUninitialized(buf.length, d).leakRef(); | |
162 | for (unsigned i = 0; i != buf.length; i++) | |
163 | d[i] = buf.s[i]; | |
164 | r->setHash(hash); | |
165 | location = r; | |
166 | } | |
167 | }; | |
168 | ||
169 | template <typename T> | |
170 | PassRefPtr<StringImpl> Identifier::add(JSGlobalData* globalData, const T* s, int length) | |
171 | { | |
172 | if (length == 1) { | |
173 | T c = s[0]; | |
174 | if (canUseSingleCharacterString(c)) | |
175 | return add(globalData, globalData->smallStrings.singleCharacterStringRep(c)); | |
176 | } | |
177 | ||
178 | if (!length) | |
179 | return StringImpl::empty(); | |
180 | CharBuffer<T> buf = {s, length}; | |
181 | HashSet<StringImpl*>::AddResult addResult = globalData->identifierTable->add<CharBuffer<T>, IdentifierCharBufferTranslator<T> >(buf); | |
182 | ||
183 | // If the string is newly-translated, then we need to adopt it. | |
184 | // The boolean in the pair tells us if that is so. | |
185 | return addResult.isNewEntry ? adoptRef(*addResult.iterator) : *addResult.iterator; | |
186 | } | |
187 | ||
9dae56ea A |
188 | inline bool operator==(const Identifier& a, const Identifier& b) |
189 | { | |
190 | return Identifier::equal(a, b); | |
191 | } | |
192 | ||
193 | inline bool operator!=(const Identifier& a, const Identifier& b) | |
194 | { | |
195 | return !Identifier::equal(a, b); | |
196 | } | |
197 | ||
6fe7ccc8 | 198 | inline bool operator==(const Identifier& a, const LChar* b) |
9dae56ea A |
199 | { |
200 | return Identifier::equal(a, b); | |
201 | } | |
202 | ||
6fe7ccc8 A |
203 | inline bool operator==(const Identifier& a, const char* b) |
204 | { | |
205 | return Identifier::equal(a, reinterpret_cast<const LChar*>(b)); | |
206 | } | |
207 | ||
208 | inline bool operator!=(const Identifier& a, const LChar* b) | |
9dae56ea A |
209 | { |
210 | return !Identifier::equal(a, b); | |
211 | } | |
212 | ||
6fe7ccc8 A |
213 | inline bool operator!=(const Identifier& a, const char* b) |
214 | { | |
215 | return !Identifier::equal(a, reinterpret_cast<const LChar*>(b)); | |
216 | } | |
217 | ||
218 | inline bool Identifier::equal(const StringImpl* r, const LChar* s) | |
219 | { | |
220 | return WTF::equal(r, s); | |
221 | } | |
222 | ||
223 | inline bool Identifier::equal(const StringImpl* r, const LChar* s, unsigned length) | |
224 | { | |
225 | return WTF::equal(r, s, length); | |
226 | } | |
227 | ||
14957cd0 A |
228 | inline bool Identifier::equal(const StringImpl* r, const UChar* s, unsigned length) |
229 | { | |
6fe7ccc8 | 230 | return WTF::equal(r, s, length); |
14957cd0 A |
231 | } |
232 | ||
9dae56ea A |
233 | IdentifierTable* createIdentifierTable(); |
234 | void deleteIdentifierTable(IdentifierTable*); | |
235 | ||
14957cd0 A |
236 | struct IdentifierRepHash : PtrHash<RefPtr<StringImpl> > { |
237 | static unsigned hash(const RefPtr<StringImpl>& key) { return key->existingHash(); } | |
238 | static unsigned hash(StringImpl* key) { return key->existingHash(); } | |
239 | }; | |
240 | ||
6fe7ccc8 A |
241 | struct IdentifierMapIndexHashTraits : HashTraits<int> { |
242 | static int emptyValue() { return std::numeric_limits<int>::max(); } | |
243 | static const bool emptyValueIsZero = false; | |
244 | }; | |
245 | ||
246 | typedef HashMap<RefPtr<StringImpl>, int, IdentifierRepHash, HashTraits<RefPtr<StringImpl> >, IdentifierMapIndexHashTraits> IdentifierMap; | |
247 | ||
248 | template<typename U, typename V> | |
249 | HashSet<StringImpl*>::AddResult IdentifierTable::add(U value) | |
250 | { | |
251 | HashSet<StringImpl*>::AddResult result = m_table.add<U, V>(value); | |
252 | (*result.iterator)->setIsIdentifier(true); | |
253 | return result; | |
254 | } | |
255 | ||
9dae56ea A |
256 | } // namespace JSC |
257 | ||
258 | #endif // Identifier_h |