1/*
2 * Copyright (C) 2012-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 "GetPutInfo.h"
29#include "JSObject.h"
30
31namespace JSC {
32
33class ScopeChainIterator;
34class SymbolTable;
35class VariableEnvironment;
36class WatchpointSet;
37
38class JSScope : public JSNonFinalObject {
39public:
40 using Base = JSNonFinalObject;
41 static constexpr unsigned StructureFlags = Base::StructureFlags | OverridesToThis;
42
43 DECLARE_EXPORT_INFO;
44
45 friend class LLIntOffsetsExtractor;
46 static size_t offsetOfNext();
47
48 static JSObject* objectAtScope(JSScope*);
49
50 static JSObject* resolve(JSGlobalObject*, JSScope*, const Identifier&);
51 static JSValue resolveScopeForHoistingFuncDeclInEval(JSGlobalObject*, JSScope*, const Identifier&);
52 static ResolveOp abstractResolve(JSGlobalObject*, size_t depthOffset, JSScope*, const Identifier&, GetOrPut, ResolveType, InitializationMode);
53
54 static bool hasConstantScope(ResolveType);
55 static JSScope* constantScopeForCodeBlock(ResolveType, CodeBlock*);
56
57 static void collectClosureVariablesUnderTDZ(JSScope*, VariableEnvironment& result);
58
59 static void visitChildren(JSCell*, SlotVisitor&);
60
61 bool isVarScope();
62 bool isLexicalScope();
63 bool isModuleScope();
64 bool isCatchScope();
65 bool isFunctionNameScopeObject();
66
67 bool isNestedLexicalScope();
68
69 ScopeChainIterator begin();
70 ScopeChainIterator end();
71 JSScope* next();
72
73 JSObject* globalThis();
74
75 SymbolTable* symbolTable(VM&);
76
77 JS_EXPORT_PRIVATE static JSValue toThis(JSCell*, JSGlobalObject*, ECMAMode);
78
79protected:
80 JSScope(VM&, Structure*, JSScope* next);
81
82 template<typename ReturnPredicateFunctor, typename SkipPredicateFunctor>
83 static JSObject* resolve(JSGlobalObject*, JSScope*, const Identifier&, ReturnPredicateFunctor, SkipPredicateFunctor);
84
85private:
86 WriteBarrier<JSScope> m_next;
87};
88
89inline JSScope::JSScope(VM& vm, Structure* structure, JSScope* next)
90 : Base(vm, structure)
91 , m_next(vm, this, next, WriteBarrier<JSScope>::MayBeNull)
92{
93}
94
95class ScopeChainIterator {
96public:
97 ScopeChainIterator(JSScope* node)
98 : m_node(node)
99 {
100 }
101
102 JSObject* get() const { return JSScope::objectAtScope(m_node); }
103 JSObject* operator->() const { return JSScope::objectAtScope(m_node); }
104 JSScope* scope() const { return m_node; }
105
106 ScopeChainIterator& operator++() { m_node = m_node->next(); return *this; }
107
108 // postfix ++ intentionally omitted
109
110 bool operator==(const ScopeChainIterator& other) const { return m_node == other.m_node; }
111 bool operator!=(const ScopeChainIterator& other) const { return m_node != other.m_node; }
112
113private:
114 JSScope* m_node;
115};
116
117inline ScopeChainIterator JSScope::begin()
118{
119 return ScopeChainIterator(this);
120}
121
122inline ScopeChainIterator JSScope::end()
123{
124 return ScopeChainIterator(0);
125}
126
127inline JSScope* JSScope::next()
128{
129 return m_next.get();
130}
131
132inline size_t JSScope::offsetOfNext()
133{
134 return OBJECT_OFFSETOF(JSScope, m_next);
135}
136
137} // namespace JSC
138