2 * Copyright (C) 2003, 2006, 2007, 2008, 2009 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.
24 #include "JSGlobalData.h"
25 #include "ThreadSpecific.h"
27 #include <wtf/text/CString.h>
34 friend class Structure;
38 Identifier(ExecState* exec, const char* s) : m_string(add(exec, s)) { } // Only to be used with string literals.
39 Identifier(ExecState* exec, const UChar* s, int length) : m_string(add(exec, s, length)) { }
40 Identifier(ExecState* exec, StringImpl* rep) : m_string(add(exec, rep)) { }
41 Identifier(ExecState* exec, const UString& s) : m_string(add(exec, s.impl())) { }
43 Identifier(JSGlobalData* globalData, const char* s) : m_string(add(globalData, s)) { } // Only to be used with string literals.
44 Identifier(JSGlobalData* globalData, const UChar* s, int length) : m_string(add(globalData, s, length)) { }
45 Identifier(JSGlobalData* globalData, StringImpl* rep) : m_string(add(globalData, rep)) { }
46 Identifier(JSGlobalData* globalData, const UString& s) : m_string(add(globalData, s.impl())) { }
48 const UString& ustring() const { return m_string; }
49 StringImpl* impl() const { return m_string.impl(); }
51 const UChar* characters() const { return m_string.characters(); }
52 int length() const { return m_string.length(); }
54 CString ascii() const { return m_string.ascii(); }
56 static Identifier from(ExecState* exec, unsigned y);
57 static Identifier from(ExecState* exec, int y);
58 static Identifier from(ExecState* exec, double y);
59 static Identifier from(JSGlobalData*, unsigned y);
60 static Identifier from(JSGlobalData*, int y);
61 static Identifier from(JSGlobalData*, double y);
63 static uint32_t toUInt32(const UString&, bool& ok);
64 uint32_t toUInt32(bool& ok) const { return toUInt32(m_string, ok); }
65 unsigned toArrayIndex(bool& ok) const;
67 bool isNull() const { return m_string.isNull(); }
68 bool isEmpty() const { return m_string.isEmpty(); }
70 friend bool operator==(const Identifier&, const Identifier&);
71 friend bool operator!=(const Identifier&, const Identifier&);
73 friend bool operator==(const Identifier&, const char*);
74 friend bool operator!=(const Identifier&, const char*);
76 static bool equal(const StringImpl*, const char*);
77 static bool equal(const StringImpl*, const UChar*, unsigned length);
78 static bool equal(const StringImpl* a, const StringImpl* b) { return ::equal(a, b); }
80 static PassRefPtr<StringImpl> add(ExecState*, const char*); // Only to be used with string literals.
81 static PassRefPtr<StringImpl> add(JSGlobalData*, const char*); // Only to be used with string literals.
86 static bool equal(const Identifier& a, const Identifier& b) { return a.m_string.impl() == b.m_string.impl(); }
87 static bool equal(const Identifier& a, const char* b) { return equal(a.m_string.impl(), b); }
89 static PassRefPtr<StringImpl> add(ExecState*, const UChar*, int length);
90 static PassRefPtr<StringImpl> add(JSGlobalData*, const UChar*, int length);
92 static PassRefPtr<StringImpl> add(ExecState* exec, StringImpl* r)
95 checkCurrentIdentifierTable(exec);
97 if (r->isIdentifier())
99 return addSlowCase(exec, r);
101 static PassRefPtr<StringImpl> add(JSGlobalData* globalData, StringImpl* r)
104 checkCurrentIdentifierTable(globalData);
106 if (r->isIdentifier())
108 return addSlowCase(globalData, r);
111 static PassRefPtr<StringImpl> addSlowCase(ExecState*, StringImpl* r);
112 static PassRefPtr<StringImpl> addSlowCase(JSGlobalData*, StringImpl* r);
114 static void checkCurrentIdentifierTable(ExecState*);
115 static void checkCurrentIdentifierTable(JSGlobalData*);
118 inline bool operator==(const Identifier& a, const Identifier& b)
120 return Identifier::equal(a, b);
123 inline bool operator!=(const Identifier& a, const Identifier& b)
125 return !Identifier::equal(a, b);
128 inline bool operator==(const Identifier& a, const char* b)
130 return Identifier::equal(a, b);
133 inline bool operator!=(const Identifier& a, const char* b)
135 return !Identifier::equal(a, b);
138 inline bool Identifier::equal(const StringImpl* r, const char* s)
140 return WTF::equal(r, s);
143 inline bool Identifier::equal(const StringImpl* r, const UChar* s, unsigned length)
145 return WTF::equal(r, s, length);
148 IdentifierTable* createIdentifierTable();
149 void deleteIdentifierTable(IdentifierTable*);
151 struct IdentifierRepHash : PtrHash<RefPtr<StringImpl> > {
152 static unsigned hash(const RefPtr<StringImpl>& key) { return key->existingHash(); }
153 static unsigned hash(StringImpl* key) { return key->existingHash(); }
158 #endif // Identifier_h