1 | /*
|
---|
2 | * Copyright (C) 2015-2019 Apple Inc. All Rights Reserved.
|
---|
3 | *
|
---|
4 | * Redistribution and use in source and binary forms, with or without
|
---|
5 | * modification, are permitted provided that the following conditions
|
---|
6 | * are met:
|
---|
7 | * 1. Redistributions of source code must retain the above copyright
|
---|
8 | * notice, this list of conditions and the following disclaimer.
|
---|
9 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer in the
|
---|
11 | * documentation and/or other materials provided with the distribution.
|
---|
12 | *
|
---|
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
|
---|
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
|
---|
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
---|
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
---|
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
---|
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #pragma once
|
---|
27 |
|
---|
28 | #include "Identifier.h"
|
---|
29 | #include <variant>
|
---|
30 | #include <wtf/HashMap.h>
|
---|
31 | #include <wtf/HashSet.h>
|
---|
32 | #include <wtf/IteratorRange.h>
|
---|
33 |
|
---|
34 | namespace JSC {
|
---|
35 |
|
---|
36 | struct VariableEnvironmentEntry {
|
---|
37 | public:
|
---|
38 | ALWAYS_INLINE bool isCaptured() const { return m_bits & IsCaptured; }
|
---|
39 | ALWAYS_INLINE bool isConst() const { return m_bits & IsConst; }
|
---|
40 | ALWAYS_INLINE bool isVar() const { return m_bits & IsVar; }
|
---|
41 | ALWAYS_INLINE bool isLet() const { return m_bits & IsLet; }
|
---|
42 | ALWAYS_INLINE bool isExported() const { return m_bits & IsExported; }
|
---|
43 | ALWAYS_INLINE bool isImported() const { return m_bits & IsImported; }
|
---|
44 | ALWAYS_INLINE bool isImportedNamespace() const { return m_bits & IsImportedNamespace; }
|
---|
45 | ALWAYS_INLINE bool isFunction() const { return m_bits & IsFunction; }
|
---|
46 | ALWAYS_INLINE bool isParameter() const { return m_bits & IsParameter; }
|
---|
47 | ALWAYS_INLINE bool isSloppyModeHoistingCandidate() const { return m_bits & IsSloppyModeHoistingCandidate; }
|
---|
48 | ALWAYS_INLINE bool isPrivateField() const { return m_bits & IsPrivateField; }
|
---|
49 | ALWAYS_INLINE bool isPrivateMethod() const { return m_bits & IsPrivateMethod; }
|
---|
50 | ALWAYS_INLINE bool isPrivateSetter() const { return m_bits & IsPrivateSetter; }
|
---|
51 | ALWAYS_INLINE bool isPrivateGetter() const { return m_bits & IsPrivateGetter; }
|
---|
52 |
|
---|
53 | ALWAYS_INLINE void setIsCaptured() { m_bits |= IsCaptured; }
|
---|
54 | ALWAYS_INLINE void setIsConst() { m_bits |= IsConst; }
|
---|
55 | ALWAYS_INLINE void setIsVar() { m_bits |= IsVar; }
|
---|
56 | ALWAYS_INLINE void setIsLet() { m_bits |= IsLet; }
|
---|
57 | ALWAYS_INLINE void setIsExported() { m_bits |= IsExported; }
|
---|
58 | ALWAYS_INLINE void setIsImported() { m_bits |= IsImported; }
|
---|
59 | ALWAYS_INLINE void setIsImportedNamespace() { m_bits |= IsImportedNamespace; }
|
---|
60 | ALWAYS_INLINE void setIsFunction() { m_bits |= IsFunction; }
|
---|
61 | ALWAYS_INLINE void setIsParameter() { m_bits |= IsParameter; }
|
---|
62 | ALWAYS_INLINE void setIsSloppyModeHoistingCandidate() { m_bits |= IsSloppyModeHoistingCandidate; }
|
---|
63 | ALWAYS_INLINE void setIsPrivateField() { m_bits |= IsPrivateField; }
|
---|
64 | ALWAYS_INLINE void setIsPrivateMethod() { m_bits |= IsPrivateMethod; }
|
---|
65 | ALWAYS_INLINE void setIsPrivateSetter() { m_bits |= IsPrivateSetter; }
|
---|
66 | ALWAYS_INLINE void setIsPrivateGetter() { m_bits |= IsPrivateGetter; }
|
---|
67 |
|
---|
68 | ALWAYS_INLINE void clearIsVar() { m_bits &= ~IsVar; }
|
---|
69 |
|
---|
70 | uint16_t bits() const { return m_bits; }
|
---|
71 |
|
---|
72 | bool operator==(const VariableEnvironmentEntry& other) const
|
---|
73 | {
|
---|
74 | return m_bits == other.m_bits;
|
---|
75 | }
|
---|
76 |
|
---|
77 | void dump(PrintStream&) const;
|
---|
78 |
|
---|
79 | private:
|
---|
80 | enum Traits : uint16_t {
|
---|
81 | IsCaptured = 1 << 0,
|
---|
82 | IsConst = 1 << 1,
|
---|
83 | IsVar = 1 << 2,
|
---|
84 | IsLet = 1 << 3,
|
---|
85 | IsExported = 1 << 4,
|
---|
86 | IsImported = 1 << 5,
|
---|
87 | IsImportedNamespace = 1 << 6,
|
---|
88 | IsFunction = 1 << 7,
|
---|
89 | IsParameter = 1 << 8,
|
---|
90 | IsSloppyModeHoistingCandidate = 1 << 9,
|
---|
91 | IsPrivateField = 1 << 10,
|
---|
92 | IsPrivateMethod = 1 << 11,
|
---|
93 | IsPrivateGetter = 1 << 12,
|
---|
94 | IsPrivateSetter = 1 << 13,
|
---|
95 | };
|
---|
96 | uint16_t m_bits { 0 };
|
---|
97 | };
|
---|
98 |
|
---|
99 | struct VariableEnvironmentEntryHashTraits : HashTraits<VariableEnvironmentEntry> {
|
---|
100 | static constexpr bool needsDestruction = false;
|
---|
101 | };
|
---|
102 |
|
---|
103 | struct PrivateNameEntry {
|
---|
104 | friend class CachedPrivateNameEntry;
|
---|
105 |
|
---|
106 | static constexpr unsigned privateClassBrandOffset = 0;
|
---|
107 | static constexpr unsigned privateBrandOffset = 1;
|
---|
108 |
|
---|
109 | public:
|
---|
110 | PrivateNameEntry(uint16_t traits = 0) { m_bits = traits; }
|
---|
111 |
|
---|
112 | ALWAYS_INLINE bool isMethod() const { return m_bits & IsMethod; }
|
---|
113 | ALWAYS_INLINE bool isSetter() const { return m_bits & IsSetter; }
|
---|
114 | ALWAYS_INLINE bool isGetter() const { return m_bits & IsGetter; }
|
---|
115 | ALWAYS_INLINE bool isField() const { return !isPrivateMethodOrAccessor(); }
|
---|
116 | ALWAYS_INLINE bool isStatic() const { return m_bits & IsStatic; }
|
---|
117 |
|
---|
118 | bool isPrivateMethodOrAccessor() const { return isMethod() || isSetter() || isGetter(); }
|
---|
119 |
|
---|
120 | uint16_t bits() const { return m_bits; }
|
---|
121 |
|
---|
122 | bool operator==(const PrivateNameEntry& other) const
|
---|
123 | {
|
---|
124 | return m_bits == other.m_bits;
|
---|
125 | }
|
---|
126 |
|
---|
127 | enum Traits : uint16_t {
|
---|
128 | None = 0,
|
---|
129 | IsMethod = 1 << 0,
|
---|
130 | IsGetter = 1 << 1,
|
---|
131 | IsSetter = 1 << 2,
|
---|
132 | IsStatic = 1 << 3,
|
---|
133 | };
|
---|
134 |
|
---|
135 | private:
|
---|
136 | uint16_t m_bits { 0 };
|
---|
137 | };
|
---|
138 |
|
---|
139 | struct PrivateNameEntryHashTraits : HashTraits<PrivateNameEntry> {
|
---|
140 | static constexpr bool needsDestruction = false;
|
---|
141 | };
|
---|
142 |
|
---|
143 | typedef HashMap<PackedRefPtr<UniquedStringImpl>, PrivateNameEntry, IdentifierRepHash, HashTraits<RefPtr<UniquedStringImpl>>, PrivateNameEntryHashTraits> PrivateNameEnvironment;
|
---|
144 |
|
---|
145 | class VariableEnvironment {
|
---|
146 | WTF_MAKE_FAST_ALLOCATED;
|
---|
147 | private:
|
---|
148 | typedef HashMap<PackedRefPtr<UniquedStringImpl>, VariableEnvironmentEntry, IdentifierRepHash, HashTraits<RefPtr<UniquedStringImpl>>, VariableEnvironmentEntryHashTraits> Map;
|
---|
149 |
|
---|
150 | public:
|
---|
151 |
|
---|
152 | VariableEnvironment() { }
|
---|
153 | VariableEnvironment(VariableEnvironment&& other)
|
---|
154 | : m_map(WTFMove(other.m_map))
|
---|
155 | , m_isEverythingCaptured(other.m_isEverythingCaptured)
|
---|
156 | , m_rareData(WTFMove(other.m_rareData))
|
---|
157 | {
|
---|
158 | }
|
---|
159 | VariableEnvironment(const VariableEnvironment& other)
|
---|
160 | : m_map(other.m_map)
|
---|
161 | , m_isEverythingCaptured(other.m_isEverythingCaptured)
|
---|
162 | , m_rareData(other.m_rareData ? WTF::makeUnique<VariableEnvironment::RareData>(*other.m_rareData) : nullptr)
|
---|
163 | {
|
---|
164 | }
|
---|
165 | VariableEnvironment& operator=(const VariableEnvironment& other);
|
---|
166 |
|
---|
167 | ALWAYS_INLINE Map::iterator begin() { return m_map.begin(); }
|
---|
168 | ALWAYS_INLINE Map::iterator end() { return m_map.end(); }
|
---|
169 | ALWAYS_INLINE Map::const_iterator begin() const { return m_map.begin(); }
|
---|
170 | ALWAYS_INLINE Map::const_iterator end() const { return m_map.end(); }
|
---|
171 | ALWAYS_INLINE Map::AddResult add(const RefPtr<UniquedStringImpl>& identifier) { return m_map.add(identifier, VariableEnvironmentEntry()); }
|
---|
172 | ALWAYS_INLINE Map::AddResult add(const Identifier& identifier) { return add(identifier.impl()); }
|
---|
173 |
|
---|
174 | ALWAYS_INLINE PrivateNameEnvironment::AddResult addPrivateName(const Identifier& identifier) { return addPrivateName(identifier.impl()); }
|
---|
175 | ALWAYS_INLINE PrivateNameEnvironment::AddResult addPrivateName(const RefPtr<UniquedStringImpl>& identifier)
|
---|
176 | {
|
---|
177 | if (!m_rareData)
|
---|
178 | m_rareData = makeUnique<VariableEnvironment::RareData>();
|
---|
179 |
|
---|
180 | return m_rareData->m_privateNames.add(identifier, PrivateNameEntry());
|
---|
181 | }
|
---|
182 |
|
---|
183 | ALWAYS_INLINE unsigned size() const { return m_map.size() + privateNamesSize(); }
|
---|
184 | ALWAYS_INLINE unsigned mapSize() const { return m_map.size(); }
|
---|
185 | ALWAYS_INLINE bool contains(const RefPtr<UniquedStringImpl>& identifier) const { return m_map.contains(identifier); }
|
---|
186 | ALWAYS_INLINE bool remove(const RefPtr<UniquedStringImpl>& identifier) { return m_map.remove(identifier); }
|
---|
187 | ALWAYS_INLINE Map::iterator find(const RefPtr<UniquedStringImpl>& identifier) { return m_map.find(identifier); }
|
---|
188 | ALWAYS_INLINE Map::const_iterator find(const RefPtr<UniquedStringImpl>& identifier) const { return m_map.find(identifier); }
|
---|
189 | void swap(VariableEnvironment& other);
|
---|
190 | void markVariableAsCapturedIfDefined(const RefPtr<UniquedStringImpl>& identifier);
|
---|
191 | void markVariableAsCaptured(const RefPtr<UniquedStringImpl>& identifier);
|
---|
192 | void markAllVariablesAsCaptured();
|
---|
193 | bool hasCapturedVariables() const;
|
---|
194 | bool captures(UniquedStringImpl* identifier) const;
|
---|
195 | void markVariableAsImported(const RefPtr<UniquedStringImpl>& identifier);
|
---|
196 | void markVariableAsExported(const RefPtr<UniquedStringImpl>& identifier);
|
---|
197 |
|
---|
198 | bool isEverythingCaptured() const { return m_isEverythingCaptured; }
|
---|
199 | bool isEmpty() const { return !m_map.size() && !privateNamesSize(); }
|
---|
200 |
|
---|
201 | using PrivateNamesRange = WTF::IteratorRange<PrivateNameEnvironment::iterator>;
|
---|
202 |
|
---|
203 | ALWAYS_INLINE Map::AddResult declarePrivateField(const Identifier& identifier) { return declarePrivateField(identifier.impl()); }
|
---|
204 |
|
---|
205 | bool declarePrivateMethod(const Identifier& identifier) { return declarePrivateMethod(identifier.impl()); }
|
---|
206 | bool declarePrivateMethod(const RefPtr<UniquedStringImpl>& identifier, PrivateNameEntry::Traits addionalTraits = PrivateNameEntry::Traits::None);
|
---|
207 |
|
---|
208 | enum class PrivateDeclarationResult {
|
---|
209 | Success,
|
---|
210 | DuplicatedName,
|
---|
211 | InvalidStaticNonStatic
|
---|
212 | };
|
---|
213 |
|
---|
214 | PrivateDeclarationResult declarePrivateAccessor(const RefPtr<UniquedStringImpl>&, PrivateNameEntry accessorTraits);
|
---|
215 |
|
---|
216 | bool declareStaticPrivateMethod(const Identifier& identifier)
|
---|
217 | {
|
---|
218 | return declarePrivateMethod(identifier.impl(), static_cast<PrivateNameEntry::Traits>(PrivateNameEntry::Traits::IsMethod | PrivateNameEntry::Traits::IsStatic));
|
---|
219 | }
|
---|
220 |
|
---|
221 | PrivateDeclarationResult declarePrivateSetter(const Identifier& identifier) { return declarePrivateSetter(identifier.impl()); }
|
---|
222 | PrivateDeclarationResult declareStaticPrivateSetter(const Identifier& identifier) { return declarePrivateSetter(identifier.impl(), PrivateNameEntry::Traits::IsStatic); }
|
---|
223 | PrivateDeclarationResult declarePrivateSetter(const RefPtr<UniquedStringImpl>& identifier, PrivateNameEntry::Traits modifierTraits = PrivateNameEntry::Traits::None);
|
---|
224 |
|
---|
225 | PrivateDeclarationResult declarePrivateGetter(const Identifier& identifier) { return declarePrivateGetter(identifier.impl()); }
|
---|
226 | PrivateDeclarationResult declareStaticPrivateGetter(const Identifier& identifier) { return declarePrivateGetter(identifier.impl(), PrivateNameEntry::Traits::IsStatic); }
|
---|
227 | PrivateDeclarationResult declarePrivateGetter(const RefPtr<UniquedStringImpl>& identifier, PrivateNameEntry::Traits modifierTraits = PrivateNameEntry::Traits::None);
|
---|
228 |
|
---|
229 | Map::AddResult declarePrivateField(const RefPtr<UniquedStringImpl>&);
|
---|
230 |
|
---|
231 | ALWAYS_INLINE PrivateNamesRange privateNames() const
|
---|
232 | {
|
---|
233 | // Use of the IteratorRange must be guarded to prevent ASSERT failures in checkValidity().
|
---|
234 | ASSERT(privateNamesSize() > 0);
|
---|
235 | return makeIteratorRange(m_rareData->m_privateNames.begin(), m_rareData->m_privateNames.end());
|
---|
236 | }
|
---|
237 |
|
---|
238 | ALWAYS_INLINE unsigned privateNamesSize() const
|
---|
239 | {
|
---|
240 | if (!m_rareData)
|
---|
241 | return 0;
|
---|
242 | return m_rareData->m_privateNames.size();
|
---|
243 | }
|
---|
244 |
|
---|
245 | ALWAYS_INLINE PrivateNameEnvironment* privateNameEnvironment()
|
---|
246 | {
|
---|
247 | if (!m_rareData)
|
---|
248 | return nullptr;
|
---|
249 | return &m_rareData->m_privateNames;
|
---|
250 | }
|
---|
251 |
|
---|
252 | ALWAYS_INLINE const PrivateNameEnvironment* privateNameEnvironment() const
|
---|
253 | {
|
---|
254 | if (!m_rareData)
|
---|
255 | return nullptr;
|
---|
256 | return &m_rareData->m_privateNames;
|
---|
257 | }
|
---|
258 |
|
---|
259 | ALWAYS_INLINE bool hasStaticPrivateMethodOrAccessor() const
|
---|
260 | {
|
---|
261 | if (!m_rareData)
|
---|
262 | return false;
|
---|
263 |
|
---|
264 | for (auto entry : privateNames()) {
|
---|
265 | if (entry.value.isPrivateMethodOrAccessor() && entry.value.isStatic())
|
---|
266 | return true;
|
---|
267 | }
|
---|
268 |
|
---|
269 | return false;
|
---|
270 | }
|
---|
271 |
|
---|
272 | ALWAYS_INLINE bool hasInstancePrivateMethodOrAccessor() const
|
---|
273 | {
|
---|
274 | if (!m_rareData)
|
---|
275 | return false;
|
---|
276 |
|
---|
277 | for (auto entry : privateNames()) {
|
---|
278 | if (entry.value.isPrivateMethodOrAccessor() && !entry.value.isStatic())
|
---|
279 | return true;
|
---|
280 | }
|
---|
281 |
|
---|
282 | return false;
|
---|
283 | }
|
---|
284 |
|
---|
285 | ALWAYS_INLINE bool hasPrivateName(const Identifier& identifier)
|
---|
286 | {
|
---|
287 | if (!m_rareData)
|
---|
288 | return false;
|
---|
289 | return m_rareData->m_privateNames.contains(identifier.impl());
|
---|
290 | }
|
---|
291 |
|
---|
292 | ALWAYS_INLINE void addPrivateNamesFrom(const PrivateNameEnvironment* privateNameEnvironment)
|
---|
293 | {
|
---|
294 | if (!privateNameEnvironment)
|
---|
295 | return;
|
---|
296 |
|
---|
297 | if (!m_rareData)
|
---|
298 | m_rareData = makeUnique<VariableEnvironment::RareData>();
|
---|
299 |
|
---|
300 | for (auto entry : *privateNameEnvironment)
|
---|
301 | m_rareData->m_privateNames.add(entry.key, entry.value);
|
---|
302 | }
|
---|
303 |
|
---|
304 | struct RareData {
|
---|
305 | WTF_MAKE_STRUCT_FAST_ALLOCATED;
|
---|
306 |
|
---|
307 | RareData() { }
|
---|
308 | RareData(RareData&& other)
|
---|
309 | : m_privateNames(WTFMove(other.m_privateNames))
|
---|
310 | {
|
---|
311 | }
|
---|
312 | RareData(const RareData&) = default;
|
---|
313 | RareData& operator=(const RareData&) = default;
|
---|
314 | PrivateNameEnvironment m_privateNames;
|
---|
315 | };
|
---|
316 |
|
---|
317 | void dump(PrintStream&) const;
|
---|
318 |
|
---|
319 | private:
|
---|
320 | friend class CachedVariableEnvironment;
|
---|
321 |
|
---|
322 | Map m_map;
|
---|
323 | bool m_isEverythingCaptured { false };
|
---|
324 |
|
---|
325 | PrivateNameEntry& getOrAddPrivateName(UniquedStringImpl* impl)
|
---|
326 | {
|
---|
327 | if (!m_rareData)
|
---|
328 | m_rareData = WTF::makeUnique<VariableEnvironment::RareData>();
|
---|
329 |
|
---|
330 | return m_rareData->m_privateNames.add(impl, PrivateNameEntry()).iterator->value;
|
---|
331 | }
|
---|
332 |
|
---|
333 | std::unique_ptr<VariableEnvironment::RareData> m_rareData;
|
---|
334 | };
|
---|
335 |
|
---|
336 | using TDZEnvironment = HashSet<RefPtr<UniquedStringImpl>, IdentifierRepHash>;
|
---|
337 |
|
---|
338 | class CompactTDZEnvironment {
|
---|
339 | WTF_MAKE_FAST_ALLOCATED;
|
---|
340 | WTF_MAKE_NONCOPYABLE(CompactTDZEnvironment);
|
---|
341 |
|
---|
342 | friend class CachedCompactTDZEnvironment;
|
---|
343 |
|
---|
344 | using Compact = Vector<PackedRefPtr<UniquedStringImpl>>;
|
---|
345 | using Inflated = TDZEnvironment;
|
---|
346 | using Variables = std::variant<Compact, Inflated>;
|
---|
347 |
|
---|
348 | public:
|
---|
349 | CompactTDZEnvironment(const TDZEnvironment&);
|
---|
350 |
|
---|
351 | bool operator==(const CompactTDZEnvironment&) const;
|
---|
352 | unsigned hash() const { return m_hash; }
|
---|
353 |
|
---|
354 | static void sortCompact(Compact&);
|
---|
355 |
|
---|
356 | TDZEnvironment& toTDZEnvironment() const
|
---|
357 | {
|
---|
358 | if (std::holds_alternative<Inflated>(m_variables))
|
---|
359 | return const_cast<TDZEnvironment&>(std::get<Inflated>(m_variables));
|
---|
360 | return toTDZEnvironmentSlow();
|
---|
361 | }
|
---|
362 |
|
---|
363 | private:
|
---|
364 | CompactTDZEnvironment() = default;
|
---|
365 | TDZEnvironment& toTDZEnvironmentSlow() const;
|
---|
366 |
|
---|
367 | mutable Variables m_variables;
|
---|
368 | unsigned m_hash;
|
---|
369 | };
|
---|
370 |
|
---|
371 | struct CompactTDZEnvironmentKey {
|
---|
372 | CompactTDZEnvironmentKey()
|
---|
373 | : m_environment(nullptr)
|
---|
374 | {
|
---|
375 | ASSERT(isHashTableEmptyValue());
|
---|
376 | }
|
---|
377 |
|
---|
378 | CompactTDZEnvironmentKey(CompactTDZEnvironment& environment)
|
---|
379 | : m_environment(&environment)
|
---|
380 | { }
|
---|
381 |
|
---|
382 | static unsigned hash(const CompactTDZEnvironmentKey& key) { return key.m_environment->hash(); }
|
---|
383 | static bool equal(const CompactTDZEnvironmentKey& a, const CompactTDZEnvironmentKey& b) { return *a.m_environment == *b.m_environment; }
|
---|
384 | static constexpr bool safeToCompareToEmptyOrDeleted = false;
|
---|
385 | static void makeDeletedValue(CompactTDZEnvironmentKey& key)
|
---|
386 | {
|
---|
387 | key.m_environment = reinterpret_cast<CompactTDZEnvironment*>(1);
|
---|
388 | }
|
---|
389 | bool isHashTableDeletedValue() const
|
---|
390 | {
|
---|
391 | return m_environment == reinterpret_cast<CompactTDZEnvironment*>(1);
|
---|
392 | }
|
---|
393 | bool isHashTableEmptyValue() const
|
---|
394 | {
|
---|
395 | return !m_environment;
|
---|
396 | }
|
---|
397 |
|
---|
398 | CompactTDZEnvironment& environment()
|
---|
399 | {
|
---|
400 | RELEASE_ASSERT(!isHashTableDeletedValue());
|
---|
401 | RELEASE_ASSERT(!isHashTableEmptyValue());
|
---|
402 | return *m_environment;
|
---|
403 | }
|
---|
404 |
|
---|
405 | private:
|
---|
406 | CompactTDZEnvironment* m_environment;
|
---|
407 | };
|
---|
408 |
|
---|
409 | } // namespace JSC
|
---|
410 |
|
---|
411 | namespace WTF {
|
---|
412 |
|
---|
413 | template<typename T> struct DefaultHash;
|
---|
414 | template<> struct DefaultHash<JSC::CompactTDZEnvironmentKey> : JSC::CompactTDZEnvironmentKey { };
|
---|
415 |
|
---|
416 | template<> struct HashTraits<JSC::CompactTDZEnvironmentKey> : GenericHashTraits<JSC::CompactTDZEnvironmentKey> {
|
---|
417 | static constexpr bool emptyValueIsZero = true;
|
---|
418 | static JSC::CompactTDZEnvironmentKey emptyValue() { return JSC::CompactTDZEnvironmentKey(); }
|
---|
419 |
|
---|
420 | static constexpr bool hasIsEmptyValueFunction = true;
|
---|
421 | static bool isEmptyValue(JSC::CompactTDZEnvironmentKey key) { return key.isHashTableEmptyValue(); }
|
---|
422 |
|
---|
423 | static void constructDeletedValue(JSC::CompactTDZEnvironmentKey& key) { JSC::CompactTDZEnvironmentKey::makeDeletedValue(key); }
|
---|
424 | static bool isDeletedValue(JSC::CompactTDZEnvironmentKey key) { return key.isHashTableDeletedValue(); }
|
---|
425 | };
|
---|
426 |
|
---|
427 | } // namespace WTF
|
---|
428 |
|
---|
429 | namespace JSC {
|
---|
430 |
|
---|
431 | class CompactTDZEnvironmentMap : public RefCounted<CompactTDZEnvironmentMap> {
|
---|
432 | public:
|
---|
433 | class Handle {
|
---|
434 | friend class CachedCompactTDZEnvironmentMapHandle;
|
---|
435 |
|
---|
436 | public:
|
---|
437 | Handle() = default;
|
---|
438 |
|
---|
439 | Handle(CompactTDZEnvironment&, CompactTDZEnvironmentMap&);
|
---|
440 |
|
---|
441 | Handle(Handle&& other)
|
---|
442 | {
|
---|
443 | swap(other);
|
---|
444 | }
|
---|
445 | Handle& operator=(Handle&& other)
|
---|
446 | {
|
---|
447 | Handle handle(WTFMove(other));
|
---|
448 | swap(handle);
|
---|
449 | return *this;
|
---|
450 | }
|
---|
451 |
|
---|
452 | Handle(const Handle&);
|
---|
453 | Handle& operator=(const Handle& other)
|
---|
454 | {
|
---|
455 | Handle handle(other);
|
---|
456 | swap(handle);
|
---|
457 | return *this;
|
---|
458 | }
|
---|
459 |
|
---|
460 | ~Handle();
|
---|
461 |
|
---|
462 | explicit operator bool() const { return !!m_map; }
|
---|
463 |
|
---|
464 | const CompactTDZEnvironment& environment() const
|
---|
465 | {
|
---|
466 | return *m_environment;
|
---|
467 | }
|
---|
468 |
|
---|
469 | private:
|
---|
470 | void swap(Handle& other)
|
---|
471 | {
|
---|
472 | std::swap(other.m_environment, m_environment);
|
---|
473 | std::swap(other.m_map, m_map);
|
---|
474 | }
|
---|
475 |
|
---|
476 | CompactTDZEnvironment* m_environment { nullptr };
|
---|
477 | RefPtr<CompactTDZEnvironmentMap> m_map;
|
---|
478 | };
|
---|
479 |
|
---|
480 | Handle get(const TDZEnvironment&);
|
---|
481 |
|
---|
482 | private:
|
---|
483 | friend class Handle;
|
---|
484 | friend class CachedCompactTDZEnvironmentMapHandle;
|
---|
485 |
|
---|
486 | Handle get(CompactTDZEnvironment*, bool& isNewEntry);
|
---|
487 |
|
---|
488 | HashMap<CompactTDZEnvironmentKey, unsigned> m_map;
|
---|
489 | };
|
---|
490 |
|
---|
491 | class TDZEnvironmentLink : public RefCounted<TDZEnvironmentLink> {
|
---|
492 | TDZEnvironmentLink(CompactTDZEnvironmentMap::Handle handle, RefPtr<TDZEnvironmentLink> parent)
|
---|
493 | : m_handle(WTFMove(handle))
|
---|
494 | , m_parent(WTFMove(parent))
|
---|
495 | { }
|
---|
496 |
|
---|
497 | public:
|
---|
498 | static RefPtr<TDZEnvironmentLink> create(CompactTDZEnvironmentMap::Handle handle, RefPtr<TDZEnvironmentLink> parent)
|
---|
499 | {
|
---|
500 | return adoptRef(new TDZEnvironmentLink(WTFMove(handle), WTFMove(parent)));
|
---|
501 | }
|
---|
502 |
|
---|
503 | bool contains(UniquedStringImpl* impl) const { return m_handle.environment().toTDZEnvironment().contains(impl); }
|
---|
504 | TDZEnvironmentLink* parent() { return m_parent.get(); }
|
---|
505 |
|
---|
506 | private:
|
---|
507 | friend class CachedTDZEnvironmentLink;
|
---|
508 |
|
---|
509 | CompactTDZEnvironmentMap::Handle m_handle;
|
---|
510 | RefPtr<TDZEnvironmentLink> m_parent;
|
---|
511 | };
|
---|
512 |
|
---|
513 | } // namespace JSC
|
---|