source: webkit/trunk/JavaScriptCore/runtime/Identifier.cpp@ 58001

Last change on this file since 58001 was 58001, checked in by [email protected], 15 years ago

JavaScriptCore: Bug 37906 - Remove JSC::UStringImpl; unify with StringImpl.

Reviewed by Oliver Hunt, Darin Adler.

JSC::UStringImpl and WebCore::StringImpl (soon to be renamed to
WTF::StringImpl) are almost identical. Remove duplication of code by unifying
the two, move missing features from UStringImpl into StringImpl & delete the
class UStringImpl.

  • API/JSClassRef.cpp:
  • API/JSContextRef.cpp:
  • GNUmakefile.am:
  • JavaScriptCore.exp:
  • JavaScriptCore.pro:
  • JavaScriptCore.xcodeproj/project.pbxproj:
  • bytecode/EvalCodeCache.h:
  • bytecode/JumpTable.cpp:
  • profiler/ProfileNode.cpp:
  • runtime/Identifier.cpp:

(JSC::Identifier::add):

  • runtime/Identifier.h:

(JSC::Identifier::equal):

  • runtime/UString.cpp:
  • runtime/UString.h:

(WTF::):

  • runtime/UStringImpl.cpp: Removed.
  • runtime/UStringImpl.h:
  • wtf/text/StringHash.h:

(WebCore::StringHash::equal):
(WebCore::CaseFoldingHash::equal):

  • wtf/text/StringImpl.cpp:

(WebCore::StringImpl::~StringImpl):
(WebCore::StringImpl::empty):
(WebCore::StringImpl::sharedBuffer):
(WebCore::equal):

  • wtf/text/StringImpl.h:

(WebCore::StringImpl::StringImpl):
(WebCore::StringImpl::create):
(WebCore::StringImpl::tryCreateUninitialized):
(WebCore::StringImpl::cost):
(WebCore::StringImpl::isIdentifier):
(WebCore::StringImpl::setIsIdentifier):
(WebCore::StringImpl::computeHash):
(WebCore::StringImpl::copyChars):
(WebCore::StringImpl::):

JavaScriptGlue: Bug 37906 - Remove JSC::UStringImpl; unify with StringImpl.
Add forwarding header.

Reviewed by Oliver Hunt, Darin Adler.

  • ForwardingHeaders/wtf/ASCIICType.h: Added.
  • ForwardingHeaders/wtf/text/StringImpl.h: Added.

WebCore: Bug 37906 - Remove JSC::UStringImpl; unify with StringImpl.
Add include for StringHash.h.

Reviewed by Oliver Hunt, Darin Adler.

  • WebCore.xcodeproj/project.pbxproj:
  • bridge/c/c_class.cpp:
  • Property svn:eol-style set to native
File size: 8.4 KB
Line 
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 "NumericStrings.h"
26#include <new> // for placement new
27#include <string.h> // for strlen
28#include <wtf/Assertions.h>
29#include <wtf/FastMalloc.h>
30#include <wtf/HashSet.h>
31#include <wtf/WTFThreadData.h>
32#include <wtf/text/StringHash.h>
33
34using WTF::ThreadSpecific;
35
36namespace JSC {
37
38typedef HashMap<const char*, RefPtr<UString::Rep>, PtrHash<const char*> > LiteralIdentifierTable;
39
40class IdentifierTable : public FastAllocBase {
41public:
42 ~IdentifierTable()
43 {
44 HashSet<UString::Rep*>::iterator end = m_table.end();
45 for (HashSet<UString::Rep*>::iterator iter = m_table.begin(); iter != end; ++iter)
46 (*iter)->setIsIdentifier(false);
47 }
48
49 std::pair<HashSet<UString::Rep*>::iterator, bool> add(UString::Rep* value)
50 {
51 std::pair<HashSet<UString::Rep*>::iterator, bool> result = m_table.add(value);
52 (*result.first)->setIsIdentifier(true);
53 return result;
54 }
55
56 template<typename U, typename V>
57 std::pair<HashSet<UString::Rep*>::iterator, bool> add(U value)
58 {
59 std::pair<HashSet<UString::Rep*>::iterator, bool> result = m_table.add<U, V>(value);
60 (*result.first)->setIsIdentifier(true);
61 return result;
62 }
63
64 void remove(UString::Rep* r) { m_table.remove(r); }
65
66 LiteralIdentifierTable& literalTable() { return m_literalTable; }
67
68private:
69 HashSet<UString::Rep*> m_table;
70 LiteralIdentifierTable m_literalTable;
71};
72
73IdentifierTable* createIdentifierTable()
74{
75 return new IdentifierTable;
76}
77
78void deleteIdentifierTable(IdentifierTable* table)
79{
80 delete table;
81}
82
83bool Identifier::equal(const UString::Rep* r, const char* s)
84{
85 int length = r->length();
86 const UChar* d = r->characters();
87 for (int i = 0; i != length; ++i)
88 if (d[i] != (unsigned char)s[i])
89 return false;
90 return s[length] == 0;
91}
92
93bool Identifier::equal(const UString::Rep* r, const UChar* s, unsigned length)
94{
95 if (r->length() != length)
96 return false;
97 const UChar* d = r->characters();
98 for (unsigned i = 0; i != length; ++i)
99 if (d[i] != s[i])
100 return false;
101 return true;
102}
103
104struct IdentifierCStringTranslator {
105 static unsigned hash(const char* c)
106 {
107 return UString::Rep::computeHash(c);
108 }
109
110 static bool equal(UString::Rep* r, const char* s)
111 {
112 return Identifier::equal(r, s);
113 }
114
115 static void translate(UString::Rep*& location, const char* c, unsigned hash)
116 {
117 size_t length = strlen(c);
118 UChar* d;
119 UString::Rep* r = UString::Rep::createUninitialized(length, d).releaseRef();
120 for (size_t i = 0; i != length; i++)
121 d[i] = static_cast<unsigned char>(c[i]); // use unsigned char to zero-extend instead of sign-extend
122 r->setHash(hash);
123 location = r;
124 }
125};
126
127PassRefPtr<UString::Rep> Identifier::add(JSGlobalData* globalData, const char* c)
128{
129 if (!c)
130 return UString::null().rep();
131 if (!c[0])
132 return UString::Rep::empty();
133 if (!c[1])
134 return add(globalData, globalData->smallStrings.singleCharacterStringRep(static_cast<unsigned char>(c[0])));
135
136 IdentifierTable& identifierTable = *globalData->identifierTable;
137 LiteralIdentifierTable& literalIdentifierTable = identifierTable.literalTable();
138
139 const LiteralIdentifierTable::iterator& iter = literalIdentifierTable.find(c);
140 if (iter != literalIdentifierTable.end())
141 return iter->second;
142
143 pair<HashSet<UString::Rep*>::iterator, bool> addResult = identifierTable.add<const char*, IdentifierCStringTranslator>(c);
144
145 // If the string is newly-translated, then we need to adopt it.
146 // The boolean in the pair tells us if that is so.
147 RefPtr<UString::Rep> addedString = addResult.second ? adoptRef(*addResult.first) : *addResult.first;
148
149 literalIdentifierTable.add(c, addedString.get());
150
151 return addedString.release();
152}
153
154PassRefPtr<UString::Rep> Identifier::add(ExecState* exec, const char* c)
155{
156 return add(&exec->globalData(), c);
157}
158
159struct UCharBuffer {
160 const UChar* s;
161 unsigned int length;
162};
163
164struct IdentifierUCharBufferTranslator {
165 static unsigned hash(const UCharBuffer& buf)
166 {
167 return UString::Rep::computeHash(buf.s, buf.length);
168 }
169
170 static bool equal(UString::Rep* str, const UCharBuffer& buf)
171 {
172 return Identifier::equal(str, buf.s, buf.length);
173 }
174
175 static void translate(UString::Rep*& location, const UCharBuffer& buf, unsigned hash)
176 {
177 UChar* d;
178 UString::Rep* r = UString::Rep::createUninitialized(buf.length, d).releaseRef();
179 for (unsigned i = 0; i != buf.length; i++)
180 d[i] = buf.s[i];
181 r->setHash(hash);
182 location = r;
183 }
184};
185
186PassRefPtr<UString::Rep> Identifier::add(JSGlobalData* globalData, const UChar* s, int length)
187{
188 if (length == 1) {
189 UChar c = s[0];
190 if (c <= 0xFF)
191 return add(globalData, globalData->smallStrings.singleCharacterStringRep(c));
192 }
193 if (!length)
194 return UString::Rep::empty();
195 UCharBuffer buf = {s, length};
196 pair<HashSet<UString::Rep*>::iterator, bool> addResult = globalData->identifierTable->add<UCharBuffer, IdentifierUCharBufferTranslator>(buf);
197
198 // If the string is newly-translated, then we need to adopt it.
199 // The boolean in the pair tells us if that is so.
200 return addResult.second ? adoptRef(*addResult.first) : *addResult.first;
201}
202
203PassRefPtr<UString::Rep> Identifier::add(ExecState* exec, const UChar* s, int length)
204{
205 return add(&exec->globalData(), s, length);
206}
207
208PassRefPtr<UString::Rep> Identifier::addSlowCase(JSGlobalData* globalData, UString::Rep* r)
209{
210 ASSERT(!r->isIdentifier());
211 // The empty & null strings are static singletons, and static strings are handled
212 // in ::add() in the header, so we should never get here with a zero length string.
213 ASSERT(r->length());
214
215 if (r->length() == 1) {
216 UChar c = r->characters()[0];
217 if (c <= 0xFF)
218 r = globalData->smallStrings.singleCharacterStringRep(c);
219 if (r->isIdentifier())
220 return r;
221 }
222
223 return *globalData->identifierTable->add(r).first;
224}
225
226PassRefPtr<UString::Rep> Identifier::addSlowCase(ExecState* exec, UString::Rep* r)
227{
228 return addSlowCase(&exec->globalData(), r);
229}
230
231void Identifier::remove(UString::Rep* r)
232{
233 wtfThreadData().currentIdentifierTable()->remove(r);
234}
235
236Identifier Identifier::from(ExecState* exec, unsigned value)
237{
238 return Identifier(exec, exec->globalData().numericStrings.add(value));
239}
240
241Identifier Identifier::from(ExecState* exec, int value)
242{
243 return Identifier(exec, exec->globalData().numericStrings.add(value));
244}
245
246Identifier Identifier::from(ExecState* exec, double value)
247{
248 return Identifier(exec, exec->globalData().numericStrings.add(value));
249}
250
251#ifndef NDEBUG
252
253void Identifier::checkCurrentIdentifierTable(JSGlobalData* globalData)
254{
255 // Check the identifier table accessible through the threadspecific matches the
256 // globalData's identifier table.
257 ASSERT_UNUSED(globalData, globalData->identifierTable == wtfThreadData().currentIdentifierTable());
258}
259
260void Identifier::checkCurrentIdentifierTable(ExecState* exec)
261{
262 checkCurrentIdentifierTable(&exec->globalData());
263}
264
265#else
266
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!
269void Identifier::checkCurrentIdentifierTable(JSGlobalData*) { CRASH(); }
270void Identifier::checkCurrentIdentifierTable(ExecState*) { CRASH(); }
271
272#endif
273
274} // namespace JSC
Note: See TracBrowser for help on using the repository browser.