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