1/*
2 * Copyright (C) 2015 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#ifndef VariableEnvironment_h
27#define VariableEnvironment_h
28
29#include "Identifier.h"
30#include <wtf/HashMap.h>
31
32namespace JSC {
33
34struct VariableEnvironmentEntry {
35public:
36 ALWAYS_INLINE bool isCaptured() const { return m_bits & IsCaptured; }
37 ALWAYS_INLINE bool isConst() const { return m_bits & IsConst; }
38 ALWAYS_INLINE bool isVar() const { return m_bits & IsVar; }
39 ALWAYS_INLINE bool isLet() const { return m_bits & IsLet; }
40 ALWAYS_INLINE bool isExported() const { return m_bits & IsExported; }
41 ALWAYS_INLINE bool isImported() const { return m_bits & IsImported; }
42 ALWAYS_INLINE bool isImportedNamespace() const { return m_bits & IsImportedNamespace; }
43
44 ALWAYS_INLINE void setIsCaptured() { m_bits |= IsCaptured; }
45 ALWAYS_INLINE void setIsConst() { m_bits |= IsConst; }
46 ALWAYS_INLINE void setIsVar() { m_bits |= IsVar; }
47 ALWAYS_INLINE void setIsLet() { m_bits |= IsLet; }
48 ALWAYS_INLINE void setIsExported() { m_bits |= IsExported; }
49 ALWAYS_INLINE void setIsImported() { m_bits |= IsImported; }
50 ALWAYS_INLINE void setIsImportedNamespace() { m_bits |= IsImportedNamespace; }
51
52 ALWAYS_INLINE void clearIsVar() { m_bits &= ~IsVar; }
53
54private:
55 enum Traits {
56 IsCaptured = 1 << 0,
57 IsConst = 1 << 1,
58 IsVar = 1 << 2,
59 IsLet = 1 << 3,
60 IsExported = 1 << 4,
61 IsImported = 1 << 5,
62 IsImportedNamespace = 1 << 6
63 };
64 uint8_t m_bits { 0 };
65};
66
67struct VariableEnvironmentEntryHashTraits : HashTraits<VariableEnvironmentEntry> {
68 static const bool needsDestruction = false;
69};
70
71class VariableEnvironment {
72private:
73 typedef HashMap<RefPtr<UniquedStringImpl>, VariableEnvironmentEntry, IdentifierRepHash, HashTraits<RefPtr<UniquedStringImpl>>, VariableEnvironmentEntryHashTraits> Map;
74public:
75 ALWAYS_INLINE Map::iterator begin() { return m_map.begin(); }
76 ALWAYS_INLINE Map::iterator end() { return m_map.end(); }
77 ALWAYS_INLINE Map::const_iterator begin() const { return m_map.begin(); }
78 ALWAYS_INLINE Map::const_iterator end() const { return m_map.end(); }
79 ALWAYS_INLINE Map::AddResult add(const RefPtr<UniquedStringImpl>& identifier) { return m_map.add(identifier, VariableEnvironmentEntry()); }
80 ALWAYS_INLINE Map::AddResult add(const Identifier& identifier) { return add(identifier.impl()); }
81 ALWAYS_INLINE unsigned size() const { return m_map.size(); }
82 ALWAYS_INLINE bool contains(const RefPtr<UniquedStringImpl>& identifier) const { return m_map.contains(identifier); }
83 ALWAYS_INLINE bool remove(const RefPtr<UniquedStringImpl>& identifier) { return m_map.remove(identifier); }
84 ALWAYS_INLINE Map::iterator find(const RefPtr<UniquedStringImpl>& identifier) { return m_map.find(identifier); }
85 ALWAYS_INLINE Map::const_iterator find(const RefPtr<UniquedStringImpl>& identifier) const { return m_map.find(identifier); }
86 void swap(VariableEnvironment& other);
87 void markVariableAsCapturedIfDefined(const RefPtr<UniquedStringImpl>& identifier);
88 void markVariableAsCaptured(const RefPtr<UniquedStringImpl>& identifier);
89 void markAllVariablesAsCaptured();
90 bool hasCapturedVariables() const;
91 bool captures(UniquedStringImpl* identifier) const;
92 void markVariableAsImported(const RefPtr<UniquedStringImpl>& identifier);
93 void markVariableAsExported(const RefPtr<UniquedStringImpl>& identifier);
94
95private:
96 Map m_map;
97 bool m_isEverythingCaptured { false };
98};
99
100} // namespace JSC
101
102#endif // VariableEnvironment_h
103