1/*
2 * Copyright (C) 2003, 2006, 2007, 2008, 2009, 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#pragma once
22
23#include "PrivateName.h"
24#include "VM.h"
25#include <wtf/Optional.h>
26#include <wtf/text/CString.h>
27#include <wtf/text/UniquedStringImpl.h>
28#include <wtf/text/WTFString.h>
29
30namespace JSC {
31
32class ExecState;
33
34ALWAYS_INLINE bool isIndex(uint32_t index)
35{
36 return index != 0xFFFFFFFFU;
37}
38
39template <typename CharType>
40ALWAYS_INLINE Optional<uint32_t> parseIndex(const CharType* characters, unsigned length)
41{
42 // An empty string is not a number.
43 if (!length)
44 return WTF::nullopt;
45
46 // Get the first character, turning it into a digit.
47 uint32_t value = characters[0] - '0';
48 if (value > 9)
49 return WTF::nullopt;
50
51 // Check for leading zeros. If the first characher is 0, then the
52 // length of the string must be one - e.g. "042" is not equal to "42".
53 if (!value && length > 1)
54 return WTF::nullopt;
55
56 while (--length) {
57 // Multiply value by 10, checking for overflow out of 32 bits.
58 if (value > 0xFFFFFFFFU / 10)
59 return WTF::nullopt;
60 value *= 10;
61
62 // Get the next character, turning it into a digit.
63 uint32_t newValue = *(++characters) - '0';
64 if (newValue > 9)
65 return WTF::nullopt;
66
67 // Add in the old value, checking for overflow out of 32 bits.
68 newValue += value;
69 if (newValue < value)
70 return WTF::nullopt;
71 value = newValue;
72 }
73
74 if (!isIndex(value))
75 return WTF::nullopt;
76 return value;
77}
78
79ALWAYS_INLINE Optional<uint32_t> parseIndex(StringImpl& impl)
80{
81 if (impl.is8Bit())
82 return parseIndex(impl.characters8(), impl.length());
83 return parseIndex(impl.characters16(), impl.length());
84}
85
86class Identifier {
87 friend class Structure;
88public:
89 Identifier() { }
90 enum EmptyIdentifierFlag { EmptyIdentifier };
91 Identifier(EmptyIdentifierFlag) : m_string(StringImpl::empty()) { ASSERT(m_string.impl()->isAtom()); }
92
93 const String& string() const { return m_string; }
94 UniquedStringImpl* impl() const { return static_cast<UniquedStringImpl*>(m_string.impl()); }
95
96 int length() const { return m_string.length(); }
97
98 CString ascii() const { return m_string.ascii(); }
99 CString utf8() const { return m_string.utf8(); }
100
101 // There's 2 functions to construct Identifier from string, (1) fromString and (2) fromUid.
102 // They have different meanings in keeping or discarding symbol-ness of strings.
103 // (1): fromString
104 // Just construct Identifier from string. String held by Identifier is always atomized.
105 // Symbol-ness of StringImpl*, which represents that the string is inteded to be used for ES6 Symbols, is discarded.
106 // So a constructed Identifier never represents a symbol.
107 // (2): fromUid
108 // `StringImpl* uid` represents ether String or Symbol property.
109 // fromUid keeps symbol-ness of provided StringImpl* while fromString discards it.
110 // Use fromUid when constructing Identifier from StringImpl* which may represent symbols.
111
112 // Only to be used with string literals.
113 template<unsigned charactersCount>
114 static Identifier fromString(VM*, const char (&characters)[charactersCount]);
115 template<unsigned charactersCount>
116 static Identifier fromString(ExecState*, const char (&characters)[charactersCount]);
117 static Identifier fromString(VM*, const LChar*, int length);
118 static Identifier fromString(VM*, const UChar*, int length);
119 static Identifier fromString(VM*, const String&);
120 static Identifier fromString(ExecState*, AtomStringImpl*);
121 static Identifier fromString(ExecState*, const AtomString&);
122 static Identifier fromString(ExecState*, const String&);
123 static Identifier fromString(ExecState*, const char*);
124 static Identifier fromString(VM* vm, const Vector<LChar>& characters) { return fromString(vm, characters.data(), characters.size()); }
125
126 static Identifier fromUid(VM*, UniquedStringImpl* uid);
127 static Identifier fromUid(ExecState*, UniquedStringImpl* uid);
128 static Identifier fromUid(const PrivateName&);
129 static Identifier fromUid(SymbolImpl&);
130
131 static Identifier createLCharFromUChar(VM* vm, const UChar* s, int length) { return Identifier(vm, add8(vm, s, length)); }
132
133 JS_EXPORT_PRIVATE static Identifier from(ExecState*, unsigned y);
134 JS_EXPORT_PRIVATE static Identifier from(ExecState*, int y);
135 static Identifier from(ExecState*, double y);
136 static Identifier from(VM*, unsigned y);
137 static Identifier from(VM*, int y);
138 static Identifier from(VM*, double y);
139
140 bool isNull() const { return m_string.isNull(); }
141 bool isEmpty() const { return m_string.isEmpty(); }
142 bool isSymbol() const { return !isNull() && impl()->isSymbol(); }
143 bool isPrivateName() const { return isSymbol() && static_cast<const SymbolImpl*>(impl())->isPrivate(); }
144
145 friend bool operator==(const Identifier&, const Identifier&);
146 friend bool operator!=(const Identifier&, const Identifier&);
147
148 friend bool operator==(const Identifier&, const LChar*);
149 friend bool operator==(const Identifier&, const char*);
150 friend bool operator!=(const Identifier&, const LChar*);
151 friend bool operator!=(const Identifier&, const char*);
152
153 static bool equal(const StringImpl*, const LChar*);
154 static inline bool equal(const StringImpl*a, const char*b) { return Identifier::equal(a, reinterpret_cast<const LChar*>(b)); };
155 static bool equal(const StringImpl*, const LChar*, unsigned length);
156 static bool equal(const StringImpl*, const UChar*, unsigned length);
157 static bool equal(const StringImpl* a, const StringImpl* b) { return ::equal(a, b); }
158
159 // Only to be used with string literals.
160 JS_EXPORT_PRIVATE static Ref<StringImpl> add(VM*, const char*);
161 JS_EXPORT_PRIVATE static Ref<StringImpl> add(ExecState*, const char*);
162
163 void dump(PrintStream&) const;
164
165private:
166 String m_string;
167
168 // Only to be used with string literals.
169 template<unsigned charactersCount>
170 Identifier(VM* vm, const char (&characters)[charactersCount]) : m_string(add(vm, characters)) { ASSERT(m_string.impl()->isAtom()); }
171
172 Identifier(VM* vm, const LChar* s, int length) : m_string(add(vm, s, length)) { ASSERT(m_string.impl()->isAtom()); }
173 Identifier(VM* vm, const UChar* s, int length) : m_string(add(vm, s, length)) { ASSERT(m_string.impl()->isAtom()); }
174 Identifier(ExecState*, AtomStringImpl*);
175 Identifier(ExecState*, const AtomString&);
176 Identifier(VM* vm, const String& string) : m_string(add(vm, string.impl())) { ASSERT(m_string.impl()->isAtom()); }
177 Identifier(VM* vm, StringImpl* rep) : m_string(add(vm, rep)) { ASSERT(m_string.impl()->isAtom()); }
178
179 Identifier(SymbolImpl& uid)
180 : m_string(&uid)
181 {
182 }
183
184 template <typename CharType>
185 ALWAYS_INLINE static uint32_t toUInt32FromCharacters(const CharType* characters, unsigned length, bool& ok);
186
187 static bool equal(const Identifier& a, const Identifier& b) { return a.m_string.impl() == b.m_string.impl(); }
188 static bool equal(const Identifier& a, const LChar* b) { return equal(a.m_string.impl(), b); }
189
190 template <typename T> static Ref<StringImpl> add(VM*, const T*, int length);
191 static Ref<StringImpl> add8(VM*, const UChar*, int length);
192 template <typename T> ALWAYS_INLINE static bool canUseSingleCharacterString(T);
193
194 static Ref<StringImpl> add(ExecState*, StringImpl*);
195 static Ref<StringImpl> add(VM*, StringImpl*);
196
197#ifndef NDEBUG
198 JS_EXPORT_PRIVATE static void checkCurrentAtomStringTable(ExecState*);
199 JS_EXPORT_PRIVATE static void checkCurrentAtomStringTable(VM*);
200#else
201 JS_EXPORT_PRIVATE NO_RETURN_DUE_TO_CRASH static void checkCurrentAtomStringTable(ExecState*);
202 JS_EXPORT_PRIVATE NO_RETURN_DUE_TO_CRASH static void checkCurrentAtomStringTable(VM*);
203#endif
204};
205
206template <> ALWAYS_INLINE bool Identifier::canUseSingleCharacterString(LChar)
207{
208 ASSERT(maxSingleCharacterString == 0xff);
209 return true;
210}
211
212template <> ALWAYS_INLINE bool Identifier::canUseSingleCharacterString(UChar c)
213{
214 return (c <= maxSingleCharacterString);
215}
216
217template <typename T>
218Ref<StringImpl> Identifier::add(VM* vm, const T* s, int length)
219{
220 if (length == 1) {
221 T c = s[0];
222 if (canUseSingleCharacterString(c))
223 return vm->smallStrings.singleCharacterStringRep(c);
224 }
225 if (!length)
226 return *StringImpl::empty();
227
228 return *AtomStringImpl::add(s, length);
229}
230
231inline bool operator==(const Identifier& a, const Identifier& b)
232{
233 return Identifier::equal(a, b);
234}
235
236inline bool operator!=(const Identifier& a, const Identifier& b)
237{
238 return !Identifier::equal(a, b);
239}
240
241inline bool operator==(const Identifier& a, const LChar* b)
242{
243 return Identifier::equal(a, b);
244}
245
246inline bool operator==(const Identifier& a, const char* b)
247{
248 return Identifier::equal(a, reinterpret_cast<const LChar*>(b));
249}
250
251inline bool operator!=(const Identifier& a, const LChar* b)
252{
253 return !Identifier::equal(a, b);
254}
255
256inline bool operator!=(const Identifier& a, const char* b)
257{
258 return !Identifier::equal(a, reinterpret_cast<const LChar*>(b));
259}
260
261inline bool Identifier::equal(const StringImpl* r, const LChar* s)
262{
263 return WTF::equal(r, s);
264}
265
266inline bool Identifier::equal(const StringImpl* r, const LChar* s, unsigned length)
267{
268 return WTF::equal(r, s, length);
269}
270
271inline bool Identifier::equal(const StringImpl* r, const UChar* s, unsigned length)
272{
273 return WTF::equal(r, s, length);
274}
275
276ALWAYS_INLINE Optional<uint32_t> parseIndex(const Identifier& identifier)
277{
278 auto uid = identifier.impl();
279 if (!uid)
280 return WTF::nullopt;
281 if (uid->isSymbol())
282 return WTF::nullopt;
283 return parseIndex(*uid);
284}
285
286JSValue identifierToJSValue(VM&, const Identifier&);
287// This will stringify private symbols. When leaking JSValues to
288// non-internal code, make sure to use this function and not the above one.
289JSValue identifierToSafePublicJSValue(VM&, const Identifier&);
290
291// FIXME: It may be better for this to just be a typedef for PtrHash, since PtrHash may be cheaper to
292// compute than loading the StringImpl's hash from memory. That change would also reduce the likelihood of
293// crashes in code that somehow dangled a StringImpl.
294// https://bugs.webkit.org/show_bug.cgi?id=150137
295struct IdentifierRepHash : PtrHash<RefPtr<UniquedStringImpl>> {
296 static unsigned hash(const RefPtr<UniquedStringImpl>& key) { return key->existingSymbolAwareHash(); }
297 static unsigned hash(UniquedStringImpl* key) { return key->existingSymbolAwareHash(); }
298};
299
300struct IdentifierMapIndexHashTraits : HashTraits<int> {
301 static int emptyValue() { return std::numeric_limits<int>::max(); }
302 static const bool emptyValueIsZero = false;
303};
304
305typedef HashSet<RefPtr<UniquedStringImpl>, IdentifierRepHash> IdentifierSet;
306typedef HashMap<RefPtr<UniquedStringImpl>, int, IdentifierRepHash, HashTraits<RefPtr<UniquedStringImpl>>, IdentifierMapIndexHashTraits> IdentifierMap;
307typedef HashMap<UniquedStringImpl*, int, IdentifierRepHash, HashTraits<UniquedStringImpl*>, IdentifierMapIndexHashTraits> BorrowedIdentifierMap;
308
309} // namespace JSC
310
311namespace WTF {
312
313template <> struct VectorTraits<JSC::Identifier> : SimpleClassVectorTraits { };
314
315} // namespace WTF
316