]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/Identifier.cpp
JavaScriptCore-1218.34.tar.gz
[apple/javascriptcore.git] / runtime / Identifier.cpp
1 /*
2 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2012 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 "JSObject.h"
26 #include "JSScope.h"
27 #include "NumericStrings.h"
28 #include "Operations.h"
29 #include <new>
30 #include <string.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>
36
37 using WTF::ThreadSpecific;
38
39 namespace JSC {
40
41 IdentifierTable* createIdentifierTable()
42 {
43 return new IdentifierTable;
44 }
45
46 void deleteIdentifierTable(IdentifierTable* table)
47 {
48 delete table;
49 }
50
51 struct IdentifierASCIIStringTranslator {
52 static unsigned hash(const LChar* c)
53 {
54 return StringHasher::computeHashAndMaskTop8Bits(c);
55 }
56
57 static bool equal(StringImpl* r, const LChar* s)
58 {
59 return Identifier::equal(r, s);
60 }
61
62 static void translate(StringImpl*& location, const LChar* c, unsigned hash)
63 {
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);
67 }
68 };
69
70 struct IdentifierLCharFromUCharTranslator {
71 static unsigned hash(const CharBuffer<UChar>& buf)
72 {
73 return StringHasher::computeHashAndMaskTop8Bits(buf.s, buf.length);
74 }
75
76 static bool equal(StringImpl* str, const CharBuffer<UChar>& buf)
77 {
78 return Identifier::equal(str, buf.s, buf.length);
79 }
80
81 static void translate(StringImpl*& location, const CharBuffer<UChar>& buf, unsigned hash)
82 {
83 LChar* d;
84 StringImpl* r = StringImpl::createUninitialized(buf.length, d).leakRef();
85 WTF::copyLCharsFromUCharSource(d, buf.s, buf.length);
86 r->setHash(hash);
87 location = r;
88 }
89 };
90
91 PassRefPtr<StringImpl> Identifier::add(VM* vm, const char* c)
92 {
93 ASSERT(c);
94 ASSERT(c[0]);
95 if (!c[1])
96 return add(vm, vm->smallStrings.singleCharacterStringRep(c[0]));
97
98 IdentifierTable& identifierTable = *vm->identifierTable;
99
100 HashSet<StringImpl*>::AddResult addResult = identifierTable.add<const LChar*, IdentifierASCIIStringTranslator>(reinterpret_cast<const LChar*>(c));
101
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;
105
106 return addedString.release();
107 }
108
109 PassRefPtr<StringImpl> Identifier::add(ExecState* exec, const char* c)
110 {
111 return add(&exec->vm(), c);
112 }
113
114 PassRefPtr<StringImpl> Identifier::add8(VM* vm, const UChar* s, int length)
115 {
116 if (length == 1) {
117 UChar c = s[0];
118 ASSERT(c <= 0xff);
119 if (canUseSingleCharacterString(c))
120 return add(vm, vm->smallStrings.singleCharacterStringRep(c));
121 }
122
123 if (!length)
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);
127
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;
131 }
132
133 PassRefPtr<StringImpl> Identifier::addSlowCase(VM* vm, StringImpl* r)
134 {
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.
138 ASSERT(r->length());
139
140 if (r->length() == 1) {
141 UChar c = (*r)[0];
142 if (c <= maxSingleCharacterString)
143 r = vm->smallStrings.singleCharacterStringRep(c);
144 if (r->isIdentifier())
145 return r;
146 }
147
148 return *vm->identifierTable->add(r).iterator;
149 }
150
151 PassRefPtr<StringImpl> Identifier::addSlowCase(ExecState* exec, StringImpl* r)
152 {
153 return addSlowCase(&exec->vm(), r);
154 }
155
156 Identifier Identifier::from(ExecState* exec, unsigned value)
157 {
158 return Identifier(exec, exec->vm().numericStrings.add(value));
159 }
160
161 Identifier Identifier::from(ExecState* exec, int value)
162 {
163 return Identifier(exec, exec->vm().numericStrings.add(value));
164 }
165
166 Identifier Identifier::from(ExecState* exec, double value)
167 {
168 return Identifier(exec, exec->vm().numericStrings.add(value));
169 }
170
171 Identifier Identifier::from(VM* vm, unsigned value)
172 {
173 return Identifier(vm, vm->numericStrings.add(value));
174 }
175
176 Identifier Identifier::from(VM* vm, int value)
177 {
178 return Identifier(vm, vm->numericStrings.add(value));
179 }
180
181 Identifier Identifier::from(VM* vm, double value)
182 {
183 return Identifier(vm, vm->numericStrings.add(value));
184 }
185
186 #ifndef NDEBUG
187
188 void Identifier::checkCurrentIdentifierTable(VM* vm)
189 {
190 // Check the identifier table accessible through the threadspecific matches the
191 // vm's identifier table.
192 ASSERT_UNUSED(vm, vm->identifierTable == wtfThreadData().currentIdentifierTable());
193 }
194
195 void Identifier::checkCurrentIdentifierTable(ExecState* exec)
196 {
197 checkCurrentIdentifierTable(&exec->vm());
198 }
199
200 #else
201
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(); }
206
207 #endif
208
209 } // namespace JSC