2 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2012 Apple Inc. All rights reserved.
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.
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.
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.
22 #include "Identifier.h"
24 #include "CallFrame.h"
27 #include "NumericStrings.h"
28 #include "Operations.h"
31 #include <wtf/Assertions.h>
32 #include <wtf/FastMalloc.h>
33 #include <wtf/HashSet.h>
34 #include <wtf/text/ASCIIFastPath.h>
35 #include <wtf/text/StringHash.h>
37 using WTF::ThreadSpecific
;
41 IdentifierTable
* createIdentifierTable()
43 return new IdentifierTable
;
46 void deleteIdentifierTable(IdentifierTable
* table
)
51 struct IdentifierASCIIStringTranslator
{
52 static unsigned hash(const LChar
* c
)
54 return StringHasher::computeHashAndMaskTop8Bits(c
);
57 static bool equal(StringImpl
* r
, const LChar
* s
)
59 return Identifier::equal(r
, s
);
62 static void translate(StringImpl
*& location
, const LChar
* c
, unsigned hash
)
64 size_t length
= strlen(reinterpret_cast<const char*>(c
));
65 location
= StringImpl::createFromLiteral(reinterpret_cast<const char*>(c
), length
).leakRef();
66 location
->setHash(hash
);
70 struct IdentifierLCharFromUCharTranslator
{
71 static unsigned hash(const CharBuffer
<UChar
>& buf
)
73 return StringHasher::computeHashAndMaskTop8Bits(buf
.s
, buf
.length
);
76 static bool equal(StringImpl
* str
, const CharBuffer
<UChar
>& buf
)
78 return Identifier::equal(str
, buf
.s
, buf
.length
);
81 static void translate(StringImpl
*& location
, const CharBuffer
<UChar
>& buf
, unsigned hash
)
84 StringImpl
* r
= StringImpl::createUninitialized(buf
.length
, d
).leakRef();
85 WTF::copyLCharsFromUCharSource(d
, buf
.s
, buf
.length
);
91 PassRefPtr
<StringImpl
> Identifier::add(VM
* vm
, const char* c
)
96 return add(vm
, vm
->smallStrings
.singleCharacterStringRep(c
[0]));
98 IdentifierTable
& identifierTable
= *vm
->identifierTable
;
100 HashSet
<StringImpl
*>::AddResult addResult
= identifierTable
.add
<const LChar
*, IdentifierASCIIStringTranslator
>(reinterpret_cast<const LChar
*>(c
));
102 // If the string is newly-translated, then we need to adopt it.
103 // The boolean in the pair tells us if that is so.
104 RefPtr
<StringImpl
> addedString
= addResult
.isNewEntry
? adoptRef(*addResult
.iterator
) : *addResult
.iterator
;
106 return addedString
.release();
109 PassRefPtr
<StringImpl
> Identifier::add(ExecState
* exec
, const char* c
)
111 return add(&exec
->vm(), c
);
114 PassRefPtr
<StringImpl
> Identifier::add8(VM
* vm
, const UChar
* s
, int length
)
119 if (canUseSingleCharacterString(c
))
120 return add(vm
, vm
->smallStrings
.singleCharacterStringRep(c
));
124 return StringImpl::empty();
125 CharBuffer
<UChar
> buf
= { s
, static_cast<unsigned>(length
) };
126 HashSet
<StringImpl
*>::AddResult addResult
= vm
->identifierTable
->add
<CharBuffer
<UChar
>, IdentifierLCharFromUCharTranslator
>(buf
);
128 // If the string is newly-translated, then we need to adopt it.
129 // The boolean in the pair tells us if that is so.
130 return addResult
.isNewEntry
? adoptRef(*addResult
.iterator
) : *addResult
.iterator
;
133 PassRefPtr
<StringImpl
> Identifier::addSlowCase(VM
* vm
, StringImpl
* r
)
135 ASSERT(!r
->isIdentifier());
136 // The empty & null strings are static singletons, and static strings are handled
137 // in ::add() in the header, so we should never get here with a zero length string.
140 if (r
->length() == 1) {
142 if (c
<= maxSingleCharacterString
)
143 r
= vm
->smallStrings
.singleCharacterStringRep(c
);
144 if (r
->isIdentifier())
148 return *vm
->identifierTable
->add(r
).iterator
;
151 PassRefPtr
<StringImpl
> Identifier::addSlowCase(ExecState
* exec
, StringImpl
* r
)
153 return addSlowCase(&exec
->vm(), r
);
156 Identifier
Identifier::from(ExecState
* exec
, unsigned value
)
158 return Identifier(exec
, exec
->vm().numericStrings
.add(value
));
161 Identifier
Identifier::from(ExecState
* exec
, int value
)
163 return Identifier(exec
, exec
->vm().numericStrings
.add(value
));
166 Identifier
Identifier::from(ExecState
* exec
, double value
)
168 return Identifier(exec
, exec
->vm().numericStrings
.add(value
));
171 Identifier
Identifier::from(VM
* vm
, unsigned value
)
173 return Identifier(vm
, vm
->numericStrings
.add(value
));
176 Identifier
Identifier::from(VM
* vm
, int value
)
178 return Identifier(vm
, vm
->numericStrings
.add(value
));
181 Identifier
Identifier::from(VM
* vm
, double value
)
183 return Identifier(vm
, vm
->numericStrings
.add(value
));
188 void Identifier::checkCurrentIdentifierTable(VM
* vm
)
190 // Check the identifier table accessible through the threadspecific matches the
191 // vm's identifier table.
192 ASSERT_UNUSED(vm
, vm
->identifierTable
== wtfThreadData().currentIdentifierTable());
195 void Identifier::checkCurrentIdentifierTable(ExecState
* exec
)
197 checkCurrentIdentifierTable(&exec
->vm());
202 // These only exists so that our exports are the same for debug and release builds.
203 // This would be an RELEASE_ASSERT_NOT_REACHED(), but we're in NDEBUG only code here!
204 NO_RETURN_DUE_TO_CRASH
void Identifier::checkCurrentIdentifierTable(VM
*) { CRASH(); }
205 NO_RETURN_DUE_TO_CRASH
void Identifier::checkCurrentIdentifierTable(ExecState
*) { CRASH(); }