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 "ExecState.h"
|
---|
25 | #include <new> // for placement new
|
---|
26 | #include <string.h> // for strlen
|
---|
27 | #include <wtf/Assertions.h>
|
---|
28 | #include <wtf/FastMalloc.h>
|
---|
29 | #include <wtf/HashSet.h>
|
---|
30 |
|
---|
31 | namespace JSC {
|
---|
32 |
|
---|
33 | typedef HashMap<const char*, RefPtr<UString::Rep>, PtrHash<const char*> > LiteralIdentifierTable;
|
---|
34 |
|
---|
35 | class IdentifierTable {
|
---|
36 | public:
|
---|
37 | ~IdentifierTable()
|
---|
38 | {
|
---|
39 | HashSet<UString::Rep*>::iterator end = m_table.end();
|
---|
40 | for (HashSet<UString::Rep*>::iterator iter = m_table.begin(); iter != end; ++iter)
|
---|
41 | (*iter)->setIdentifierTable(0);
|
---|
42 | }
|
---|
43 |
|
---|
44 | std::pair<HashSet<UString::Rep*>::iterator, bool> add(UString::Rep* value)
|
---|
45 | {
|
---|
46 | std::pair<HashSet<UString::Rep*>::iterator, bool> result = m_table.add(value);
|
---|
47 | (*result.first)->setIdentifierTable(this);
|
---|
48 | return result;
|
---|
49 | }
|
---|
50 |
|
---|
51 | template<typename U, typename V>
|
---|
52 | std::pair<HashSet<UString::Rep*>::iterator, bool> add(U value)
|
---|
53 | {
|
---|
54 | std::pair<HashSet<UString::Rep*>::iterator, bool> result = m_table.add<U, V>(value);
|
---|
55 | (*result.first)->setIdentifierTable(this);
|
---|
56 | return result;
|
---|
57 | }
|
---|
58 |
|
---|
59 | void remove(UString::Rep* r) { m_table.remove(r); }
|
---|
60 |
|
---|
61 | LiteralIdentifierTable& literalTable() { return m_literalTable; }
|
---|
62 |
|
---|
63 | private:
|
---|
64 | HashSet<UString::Rep*> m_table;
|
---|
65 | LiteralIdentifierTable m_literalTable;
|
---|
66 | };
|
---|
67 |
|
---|
68 | IdentifierTable* createIdentifierTable()
|
---|
69 | {
|
---|
70 | return new IdentifierTable;
|
---|
71 | }
|
---|
72 |
|
---|
73 | void deleteIdentifierTable(IdentifierTable* table)
|
---|
74 | {
|
---|
75 | delete table;
|
---|
76 | }
|
---|
77 |
|
---|
78 | bool Identifier::equal(const UString::Rep* r, const char* s)
|
---|
79 | {
|
---|
80 | int length = r->len;
|
---|
81 | const UChar* d = r->data();
|
---|
82 | for (int i = 0; i != length; ++i)
|
---|
83 | if (d[i] != (unsigned char)s[i])
|
---|
84 | return false;
|
---|
85 | return s[length] == 0;
|
---|
86 | }
|
---|
87 |
|
---|
88 | bool Identifier::equal(const UString::Rep* r, const UChar* s, int length)
|
---|
89 | {
|
---|
90 | if (r->len != length)
|
---|
91 | return false;
|
---|
92 | const UChar* d = r->data();
|
---|
93 | for (int i = 0; i != length; ++i)
|
---|
94 | if (d[i] != s[i])
|
---|
95 | return false;
|
---|
96 | return true;
|
---|
97 | }
|
---|
98 |
|
---|
99 | struct CStringTranslator
|
---|
100 | {
|
---|
101 | static unsigned hash(const char* c)
|
---|
102 | {
|
---|
103 | return UString::Rep::computeHash(c);
|
---|
104 | }
|
---|
105 |
|
---|
106 | static bool equal(UString::Rep* r, const char* s)
|
---|
107 | {
|
---|
108 | return Identifier::equal(r, s);
|
---|
109 | }
|
---|
110 |
|
---|
111 | static void translate(UString::Rep*& location, const char* c, unsigned hash)
|
---|
112 | {
|
---|
113 | size_t length = strlen(c);
|
---|
114 | UChar* d = static_cast<UChar*>(fastMalloc(sizeof(UChar) * length));
|
---|
115 | for (size_t i = 0; i != length; i++)
|
---|
116 | d[i] = static_cast<unsigned char>(c[i]); // use unsigned char to zero-extend instead of sign-extend
|
---|
117 |
|
---|
118 | UString::Rep* r = UString::Rep::create(d, static_cast<int>(length)).releaseRef();
|
---|
119 | r->rc = 0;
|
---|
120 | r->_hash = hash;
|
---|
121 |
|
---|
122 | location = r;
|
---|
123 | }
|
---|
124 | };
|
---|
125 |
|
---|
126 | PassRefPtr<UString::Rep> Identifier::add(JSGlobalData* globalData, const char* c)
|
---|
127 | {
|
---|
128 | if (!c) {
|
---|
129 | UString::Rep::null.hash();
|
---|
130 | return &UString::Rep::null;
|
---|
131 | }
|
---|
132 | if (!c[0]) {
|
---|
133 | UString::Rep::empty.hash();
|
---|
134 | return &UString::Rep::empty;
|
---|
135 | }
|
---|
136 | if (!c[1])
|
---|
137 | return add(globalData, globalData->smallStrings.singleCharacterStringRep(static_cast<unsigned char>(c[0])));
|
---|
138 |
|
---|
139 | IdentifierTable& identifierTable = *globalData->identifierTable;
|
---|
140 | LiteralIdentifierTable& literalIdentifierTable = identifierTable.literalTable();
|
---|
141 |
|
---|
142 | const LiteralIdentifierTable::iterator& iter = literalIdentifierTable.find(c);
|
---|
143 | if (iter != literalIdentifierTable.end())
|
---|
144 | return iter->second;
|
---|
145 |
|
---|
146 | UString::Rep* addedString = *identifierTable.add<const char*, CStringTranslator>(c).first;
|
---|
147 | literalIdentifierTable.add(c, addedString);
|
---|
148 |
|
---|
149 | return addedString;
|
---|
150 | }
|
---|
151 |
|
---|
152 | PassRefPtr<UString::Rep> Identifier::add(ExecState* exec, const char* c)
|
---|
153 | {
|
---|
154 | return add(&exec->globalData(), c);
|
---|
155 | }
|
---|
156 |
|
---|
157 | struct UCharBuffer {
|
---|
158 | const UChar* s;
|
---|
159 | unsigned int length;
|
---|
160 | };
|
---|
161 |
|
---|
162 | struct UCharBufferTranslator
|
---|
163 | {
|
---|
164 | static unsigned hash(const UCharBuffer& buf)
|
---|
165 | {
|
---|
166 | return UString::Rep::computeHash(buf.s, buf.length);
|
---|
167 | }
|
---|
168 |
|
---|
169 | static bool equal(UString::Rep* str, const UCharBuffer& buf)
|
---|
170 | {
|
---|
171 | return Identifier::equal(str, buf.s, buf.length);
|
---|
172 | }
|
---|
173 |
|
---|
174 | static void translate(UString::Rep*& location, const UCharBuffer& buf, unsigned hash)
|
---|
175 | {
|
---|
176 | UChar* d = static_cast<UChar*>(fastMalloc(sizeof(UChar) * buf.length));
|
---|
177 | for (unsigned i = 0; i != buf.length; i++)
|
---|
178 | d[i] = buf.s[i];
|
---|
179 |
|
---|
180 | UString::Rep* r = UString::Rep::create(d, buf.length).releaseRef();
|
---|
181 | r->rc = 0;
|
---|
182 | r->_hash = hash;
|
---|
183 |
|
---|
184 | location = r;
|
---|
185 | }
|
---|
186 | };
|
---|
187 |
|
---|
188 | PassRefPtr<UString::Rep> Identifier::add(JSGlobalData* globalData, const UChar* s, int length)
|
---|
189 | {
|
---|
190 | if (length == 1) {
|
---|
191 | UChar c = s[0];
|
---|
192 | if (c <= 0xFF)
|
---|
193 | return add(globalData, globalData->smallStrings.singleCharacterStringRep(c));
|
---|
194 | }
|
---|
195 | if (!length) {
|
---|
196 | UString::Rep::empty.hash();
|
---|
197 | return &UString::Rep::empty;
|
---|
198 | }
|
---|
199 | UCharBuffer buf = {s, length};
|
---|
200 | return *globalData->identifierTable->add<UCharBuffer, UCharBufferTranslator>(buf).first;
|
---|
201 | }
|
---|
202 |
|
---|
203 | PassRefPtr<UString::Rep> Identifier::add(ExecState* exec, const UChar* s, int length)
|
---|
204 | {
|
---|
205 | return add(&exec->globalData(), s, length);
|
---|
206 | }
|
---|
207 |
|
---|
208 | PassRefPtr<UString::Rep> Identifier::addSlowCase(JSGlobalData* globalData, UString::Rep* r)
|
---|
209 | {
|
---|
210 | ASSERT(!r->identifierTable());
|
---|
211 | if (r->len == 1) {
|
---|
212 | UChar c = r->data()[0];
|
---|
213 | if (c <= 0xFF)
|
---|
214 | r = globalData->smallStrings.singleCharacterStringRep(c);
|
---|
215 | if (r->identifierTable()) {
|
---|
216 | #ifndef NDEBUG
|
---|
217 | checkSameIdentifierTable(globalData, r);
|
---|
218 | #endif
|
---|
219 | return r;
|
---|
220 | }
|
---|
221 | }
|
---|
222 | if (!r->len) {
|
---|
223 | UString::Rep::empty.hash();
|
---|
224 | return &UString::Rep::empty;
|
---|
225 | }
|
---|
226 | return *globalData->identifierTable->add(r).first;
|
---|
227 | }
|
---|
228 |
|
---|
229 | PassRefPtr<UString::Rep> Identifier::addSlowCase(ExecState* exec, UString::Rep* r)
|
---|
230 | {
|
---|
231 | return addSlowCase(&exec->globalData(), r);
|
---|
232 | }
|
---|
233 |
|
---|
234 | void Identifier::remove(UString::Rep* r)
|
---|
235 | {
|
---|
236 | r->identifierTable()->remove(r);
|
---|
237 | }
|
---|
238 |
|
---|
239 | #ifndef NDEBUG
|
---|
240 |
|
---|
241 | void Identifier::checkSameIdentifierTable(ExecState* exec, UString::Rep* rep)
|
---|
242 | {
|
---|
243 | ASSERT(rep->identifierTable() == exec->globalData().identifierTable);
|
---|
244 | }
|
---|
245 |
|
---|
246 | void Identifier::checkSameIdentifierTable(JSGlobalData* globalData, UString::Rep* rep)
|
---|
247 | {
|
---|
248 | ASSERT(rep->identifierTable() == globalData->identifierTable);
|
---|
249 | }
|
---|
250 |
|
---|
251 | #else
|
---|
252 |
|
---|
253 | void Identifier::checkSameIdentifierTable(ExecState*, UString::Rep*)
|
---|
254 | {
|
---|
255 | }
|
---|
256 |
|
---|
257 | void Identifier::checkSameIdentifierTable(JSGlobalData*, UString::Rep*)
|
---|
258 | {
|
---|
259 | }
|
---|
260 |
|
---|
261 | #endif
|
---|
262 |
|
---|
263 | } // namespace JSC
|
---|