]> git.saurik.com Git - apple/javascriptcore.git/blame - runtime/Identifier.h
JavaScriptCore-7600.1.4.11.8.tar.gz
[apple/javascriptcore.git] / runtime / Identifier.h
CommitLineData
9dae56ea 1/*
93a37866 2 * Copyright (C) 2003, 2006, 2007, 2008, 2009, 2012 Apple Inc. All rights reserved.
9dae56ea
A
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
93a37866 24#include "VM.h"
6fe7ccc8 25#include <wtf/ThreadSpecific.h>
6fe7ccc8 26#include <wtf/WTFThreadData.h>
14957cd0 27#include <wtf/text/CString.h>
93a37866 28#include <wtf/text/WTFString.h>
9dae56ea
A
29
30namespace JSC {
31
32 class ExecState;
33
34 class Identifier {
35 friend class Structure;
36 public:
37 Identifier() { }
93a37866 38 enum EmptyIdentifierFlag { EmptyIdentifier };
81345200 39 Identifier(EmptyIdentifierFlag) : m_string(StringImpl::empty()) { ASSERT(m_string.impl()->isAtomic()); }
9dae56ea 40
93a37866
A
41 // Only to be used with string literals.
42 template<unsigned charactersCount>
81345200 43 Identifier(ExecState* exec, const char (&characters)[charactersCount]) : m_string(add(exec, characters)) { ASSERT(m_string.impl()->isAtomic()); }
93a37866 44 template<unsigned charactersCount>
81345200 45 Identifier(VM* vm, const char (&characters)[charactersCount]) : m_string(add(vm, characters)) { ASSERT(m_string.impl()->isAtomic()); }
9dae56ea 46
81345200
A
47 Identifier(ExecState* exec, StringImpl* rep) : m_string(add(exec, rep)) { ASSERT(m_string.impl()->isAtomic()); }
48 Identifier(ExecState* exec, const String& s) : m_string(add(exec, s.impl())) { ASSERT(m_string.impl()->isAtomic()); }
9dae56ea 49
81345200
A
50 Identifier(VM* vm, const LChar* s, int length) : m_string(add(vm, s, length)) { ASSERT(m_string.impl()->isAtomic()); }
51 Identifier(VM* vm, const UChar* s, int length) : m_string(add(vm, s, length)) { ASSERT(m_string.impl()->isAtomic()); }
52 Identifier(VM* vm, StringImpl* rep) : m_string(add(vm, rep)) { ASSERT(m_string.impl()->isAtomic()); }
53 Identifier(VM* vm, const String& s) : m_string(add(vm, s.impl())) { ASSERT(m_string.impl()->isAtomic()); }
93a37866
A
54
55 const String& string() const { return m_string; }
14957cd0 56 StringImpl* impl() const { return m_string.impl(); }
9dae56ea 57
14957cd0 58 int length() const { return m_string.length(); }
9dae56ea 59
14957cd0 60 CString ascii() const { return m_string.ascii(); }
81345200
A
61 CString utf8() const { return m_string.utf8(); }
62
63 static Identifier from(const PrivateName& name)
64 {
65 Identifier result;
66 result.m_string = name.uid();
67 return result;
68 }
6fe7ccc8 69
93a37866 70 static Identifier createLCharFromUChar(VM* vm, const UChar* s, int length) { return Identifier(vm, add8(vm, s, length)); }
6fe7ccc8
A
71
72 JS_EXPORT_PRIVATE static Identifier from(ExecState* exec, unsigned y);
73 JS_EXPORT_PRIVATE static Identifier from(ExecState* exec, int y);
4e4e5a6f 74 static Identifier from(ExecState* exec, double y);
93a37866
A
75 static Identifier from(VM*, unsigned y);
76 static Identifier from(VM*, int y);
77 static Identifier from(VM*, double y);
14957cd0
A
78
79 bool isNull() const { return m_string.isNull(); }
80 bool isEmpty() const { return m_string.isEmpty(); }
9dae56ea
A
81
82 friend bool operator==(const Identifier&, const Identifier&);
83 friend bool operator!=(const Identifier&, const Identifier&);
84
6fe7ccc8 85 friend bool operator==(const Identifier&, const LChar*);
9dae56ea 86 friend bool operator==(const Identifier&, const char*);
6fe7ccc8 87 friend bool operator!=(const Identifier&, const LChar*);
9dae56ea
A
88 friend bool operator!=(const Identifier&, const char*);
89
6fe7ccc8
A
90 static bool equal(const StringImpl*, const LChar*);
91 static inline bool equal(const StringImpl*a, const char*b) { return Identifier::equal(a, reinterpret_cast<const LChar*>(b)); };
92 static bool equal(const StringImpl*, const LChar*, unsigned length);
14957cd0
A
93 static bool equal(const StringImpl*, const UChar*, unsigned length);
94 static bool equal(const StringImpl* a, const StringImpl* b) { return ::equal(a, b); }
9dae56ea 95
93a37866 96 // Only to be used with string literals.
81345200
A
97 JS_EXPORT_PRIVATE static PassRef<StringImpl> add(VM*, const char*);
98 JS_EXPORT_PRIVATE static PassRef<StringImpl> add(ExecState*, const char*);
9dae56ea
A
99
100 private:
93a37866 101 String m_string;
6fe7ccc8
A
102
103 template <typename CharType>
104 ALWAYS_INLINE static uint32_t toUInt32FromCharacters(const CharType* characters, unsigned length, bool& ok);
105
14957cd0 106 static bool equal(const Identifier& a, const Identifier& b) { return a.m_string.impl() == b.m_string.impl(); }
6fe7ccc8 107 static bool equal(const Identifier& a, const LChar* b) { return equal(a.m_string.impl(), b); }
9dae56ea 108
81345200
A
109 template <typename T> static PassRef<StringImpl> add(VM*, const T*, int length);
110 static PassRef<StringImpl> add8(VM*, const UChar*, int length);
6fe7ccc8 111 template <typename T> ALWAYS_INLINE static bool canUseSingleCharacterString(T);
9dae56ea 112
81345200
A
113 static PassRef<StringImpl> add(ExecState*, StringImpl*);
114 static PassRef<StringImpl> add(VM*, StringImpl*);
9dae56ea 115
81345200
A
116 JS_EXPORT_PRIVATE static void checkCurrentAtomicStringTable(ExecState*);
117 JS_EXPORT_PRIVATE static void checkCurrentAtomicStringTable(VM*);
6fe7ccc8
A
118 };
119
120 template <> ALWAYS_INLINE bool Identifier::canUseSingleCharacterString(LChar)
121 {
122 ASSERT(maxSingleCharacterString == 0xff);
123 return true;
124 }
125
126 template <> ALWAYS_INLINE bool Identifier::canUseSingleCharacterString(UChar c)
127 {
128 return (c <= maxSingleCharacterString);
129 }
130
131 template <typename T>
81345200 132 PassRef<StringImpl> Identifier::add(VM* vm, const T* s, int length)
6fe7ccc8
A
133 {
134 if (length == 1) {
135 T c = s[0];
136 if (canUseSingleCharacterString(c))
81345200 137 return *vm->smallStrings.singleCharacterStringRep(c);
6fe7ccc8 138 }
6fe7ccc8 139 if (!length)
81345200 140 return *StringImpl::empty();
6fe7ccc8 141
81345200 142 return *AtomicString::add(s, length);
6fe7ccc8
A
143 }
144
9dae56ea
A
145 inline bool operator==(const Identifier& a, const Identifier& b)
146 {
147 return Identifier::equal(a, b);
148 }
149
150 inline bool operator!=(const Identifier& a, const Identifier& b)
151 {
152 return !Identifier::equal(a, b);
153 }
154
6fe7ccc8 155 inline bool operator==(const Identifier& a, const LChar* b)
9dae56ea
A
156 {
157 return Identifier::equal(a, b);
158 }
159
6fe7ccc8
A
160 inline bool operator==(const Identifier& a, const char* b)
161 {
162 return Identifier::equal(a, reinterpret_cast<const LChar*>(b));
163 }
164
165 inline bool operator!=(const Identifier& a, const LChar* b)
9dae56ea
A
166 {
167 return !Identifier::equal(a, b);
168 }
169
6fe7ccc8
A
170 inline bool operator!=(const Identifier& a, const char* b)
171 {
172 return !Identifier::equal(a, reinterpret_cast<const LChar*>(b));
173 }
174
175 inline bool Identifier::equal(const StringImpl* r, const LChar* s)
176 {
177 return WTF::equal(r, s);
178 }
179
180 inline bool Identifier::equal(const StringImpl* r, const LChar* s, unsigned length)
181 {
182 return WTF::equal(r, s, length);
183 }
184
14957cd0
A
185 inline bool Identifier::equal(const StringImpl* r, const UChar* s, unsigned length)
186 {
6fe7ccc8 187 return WTF::equal(r, s, length);
14957cd0
A
188 }
189
81345200 190 struct IdentifierRepHash : PtrHash<RefPtr<StringImpl>> {
14957cd0
A
191 static unsigned hash(const RefPtr<StringImpl>& key) { return key->existingHash(); }
192 static unsigned hash(StringImpl* key) { return key->existingHash(); }
193 };
194
6fe7ccc8
A
195 struct IdentifierMapIndexHashTraits : HashTraits<int> {
196 static int emptyValue() { return std::numeric_limits<int>::max(); }
197 static const bool emptyValueIsZero = false;
198 };
199
81345200
A
200 typedef HashMap<RefPtr<StringImpl>, int, IdentifierRepHash, HashTraits<RefPtr<StringImpl>>, IdentifierMapIndexHashTraits> IdentifierMap;
201 typedef HashMap<StringImpl*, int, IdentifierRepHash, HashTraits<StringImpl*>, IdentifierMapIndexHashTraits> BorrowedIdentifierMap;
6fe7ccc8 202
9dae56ea
A
203} // namespace JSC
204
93a37866
A
205namespace WTF {
206
207template <> struct VectorTraits<JSC::Identifier> : SimpleClassVectorTraits { };
208
209} // namespace WTF
210
9dae56ea 211#endif // Identifier_h