]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/Identifier.h
JavaScriptCore-584.tar.gz
[apple/javascriptcore.git] / runtime / Identifier.h
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"
25 #include "ThreadSpecific.h"
26 #include "UString.h"
27
28 namespace JSC {
29
30 class ExecState;
31
32 class Identifier {
33 friend class Structure;
34 public:
35 Identifier() { }
36
37 Identifier(ExecState* exec, const char* s) : _ustring(add(exec, s)) { } // Only to be used with string literals.
38 Identifier(ExecState* exec, const UChar* s, int length) : _ustring(add(exec, s, length)) { }
39 Identifier(ExecState* exec, UString::Rep* rep) : _ustring(add(exec, rep)) { }
40 Identifier(ExecState* exec, const UString& s) : _ustring(add(exec, s.rep())) { }
41
42 Identifier(JSGlobalData* globalData, const char* s) : _ustring(add(globalData, s)) { } // Only to be used with string literals.
43 Identifier(JSGlobalData* globalData, const UChar* s, int length) : _ustring(add(globalData, s, length)) { }
44 Identifier(JSGlobalData* globalData, UString::Rep* rep) : _ustring(add(globalData, rep)) { }
45 Identifier(JSGlobalData* globalData, const UString& s) : _ustring(add(globalData, s.rep())) { }
46
47 // Special constructor for cases where we overwrite an object in place.
48 Identifier(PlacementNewAdoptType) : _ustring(PlacementNewAdopt) { }
49
50 const UString& ustring() const { return _ustring; }
51
52 const UChar* data() const { return _ustring.data(); }
53 int size() const { return _ustring.size(); }
54
55 const char* ascii() const { return _ustring.ascii(); }
56
57 static Identifier from(ExecState* exec, unsigned y) { return Identifier(exec, UString::from(y)); }
58 static Identifier from(ExecState* exec, int y) { return Identifier(exec, UString::from(y)); }
59 static Identifier from(ExecState* exec, double y) { return Identifier(exec, UString::from(y)); }
60
61 bool isNull() const { return _ustring.isNull(); }
62 bool isEmpty() const { return _ustring.isEmpty(); }
63
64 uint32_t toUInt32(bool* ok) const { return _ustring.toUInt32(ok); }
65 uint32_t toUInt32(bool* ok, bool tolerateEmptyString) const { return _ustring.toUInt32(ok, tolerateEmptyString); };
66 uint32_t toStrictUInt32(bool* ok) const { return _ustring.toStrictUInt32(ok); }
67 unsigned toArrayIndex(bool* ok) const { return _ustring.toArrayIndex(ok); }
68 double toDouble() const { return _ustring.toDouble(); }
69
70 friend bool operator==(const Identifier&, const Identifier&);
71 friend bool operator!=(const Identifier&, const Identifier&);
72
73 friend bool operator==(const Identifier&, const char*);
74 friend bool operator!=(const Identifier&, const char*);
75
76 static void remove(UString::Rep*);
77
78 static bool equal(const UString::Rep*, const char*);
79 static bool equal(const UString::Rep*, const UChar*, int length);
80 static bool equal(const UString::Rep* a, const UString::Rep* b) { return JSC::equal(a, b); }
81
82 static PassRefPtr<UString::Rep> add(ExecState*, const char*); // Only to be used with string literals.
83 static PassRefPtr<UString::Rep> add(JSGlobalData*, const char*); // Only to be used with string literals.
84
85 private:
86 UString _ustring;
87
88 static bool equal(const Identifier& a, const Identifier& b) { return a._ustring.rep() == b._ustring.rep(); }
89 static bool equal(const Identifier& a, const char* b) { return equal(a._ustring.rep(), b); }
90
91 static PassRefPtr<UString::Rep> add(ExecState*, const UChar*, int length);
92 static PassRefPtr<UString::Rep> add(JSGlobalData*, const UChar*, int length);
93
94 static PassRefPtr<UString::Rep> add(ExecState* exec, UString::Rep* r)
95 {
96 if (r->isIdentifier()) {
97 #ifndef NDEBUG
98 checkSameIdentifierTable(exec, r);
99 #endif
100 return r;
101 }
102 return addSlowCase(exec, r);
103 }
104 static PassRefPtr<UString::Rep> add(JSGlobalData* globalData, UString::Rep* r)
105 {
106 if (r->isIdentifier()) {
107 #ifndef NDEBUG
108 checkSameIdentifierTable(globalData, r);
109 #endif
110 return r;
111 }
112 return addSlowCase(globalData, r);
113 }
114
115 static PassRefPtr<UString::Rep> addSlowCase(ExecState*, UString::Rep* r);
116 static PassRefPtr<UString::Rep> addSlowCase(JSGlobalData*, UString::Rep* r);
117
118 static void checkSameIdentifierTable(ExecState*, UString::Rep*);
119 static void checkSameIdentifierTable(JSGlobalData*, UString::Rep*);
120 };
121
122 inline bool operator==(const Identifier& a, const Identifier& b)
123 {
124 return Identifier::equal(a, b);
125 }
126
127 inline bool operator!=(const Identifier& a, const Identifier& b)
128 {
129 return !Identifier::equal(a, b);
130 }
131
132 inline bool operator==(const Identifier& a, const char* b)
133 {
134 return Identifier::equal(a, b);
135 }
136
137 inline bool operator!=(const Identifier& a, const char* b)
138 {
139 return !Identifier::equal(a, b);
140 }
141
142 IdentifierTable* createIdentifierTable();
143 void deleteIdentifierTable(IdentifierTable*);
144
145 struct ThreadIdentifierTableData {
146 ThreadIdentifierTableData()
147 : defaultIdentifierTable(0)
148 , currentIdentifierTable(0)
149 {
150 }
151
152 IdentifierTable* defaultIdentifierTable;
153 IdentifierTable* currentIdentifierTable;
154 };
155
156 extern WTF::ThreadSpecific<ThreadIdentifierTableData>* g_identifierTableSpecific;
157 extern ThreadIdentifierTableData* g_identifierTableMain;
158 void createIdentifierTableSpecific();
159
160 inline IdentifierTable* defaultIdentifierTable()
161 {
162 if (isMainThread() || pthread_main_np()) {
163 if (!g_identifierTableMain)
164 createIdentifierTableSpecific();
165 return g_identifierTableMain->defaultIdentifierTable;
166 }
167 if (!g_identifierTableSpecific)
168 createIdentifierTableSpecific();
169 ThreadIdentifierTableData& data = **g_identifierTableSpecific;
170
171 return data.defaultIdentifierTable;
172 }
173
174 inline void setDefaultIdentifierTable(IdentifierTable* identifierTable)
175 {
176 if (isMainThread() || pthread_main_np()) {
177 if (!g_identifierTableMain)
178 createIdentifierTableSpecific();
179 g_identifierTableMain->defaultIdentifierTable = identifierTable;
180 return;
181 }
182 if (!g_identifierTableSpecific)
183 createIdentifierTableSpecific();
184 ThreadIdentifierTableData& data = **g_identifierTableSpecific;
185
186 data.defaultIdentifierTable = identifierTable;
187 }
188
189 inline IdentifierTable* currentIdentifierTable()
190 {
191 if (isMainThread() || pthread_main_np()) {
192 if (!g_identifierTableMain)
193 createIdentifierTableSpecific();
194 return g_identifierTableMain->currentIdentifierTable;
195 }
196 if (!g_identifierTableSpecific)
197 createIdentifierTableSpecific();
198 ThreadIdentifierTableData& data = **g_identifierTableSpecific;
199
200 return data.currentIdentifierTable;
201 }
202
203 inline IdentifierTable* setCurrentIdentifierTable(IdentifierTable* identifierTable)
204 {
205 if (isMainThread() || pthread_main_np()) {
206 if (!g_identifierTableMain)
207 createIdentifierTableSpecific();
208 IdentifierTable* oldIdentifierTable = g_identifierTableMain->currentIdentifierTable;
209 g_identifierTableMain->currentIdentifierTable = identifierTable;
210 return oldIdentifierTable;
211 }
212 if (!g_identifierTableSpecific)
213 createIdentifierTableSpecific();
214 ThreadIdentifierTableData& data = **g_identifierTableSpecific;
215
216 IdentifierTable* oldIdentifierTable = data.currentIdentifierTable;
217 data.currentIdentifierTable = identifierTable;
218 return oldIdentifierTable;
219 }
220
221 inline void resetCurrentIdentifierTable()
222 {
223 if (isMainThread() || pthread_main_np()) {
224 if (!g_identifierTableMain)
225 createIdentifierTableSpecific();
226 g_identifierTableMain->currentIdentifierTable = g_identifierTableMain->defaultIdentifierTable;
227 return;
228 }
229 if (!g_identifierTableSpecific)
230 createIdentifierTableSpecific();
231 ThreadIdentifierTableData& data = **g_identifierTableSpecific;
232
233 data.currentIdentifierTable = data.defaultIdentifierTable;
234 }
235
236 } // namespace JSC
237
238 #endif // Identifier_h