Infer one-time scopes
[WebKit-https.git] / Source / JavaScriptCore / runtime / JSScope.h
1 /*
2  * Copyright (C) 2012, 2013 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 "JSObject.h"
30
31 namespace JSC {
32
33 class ScopeChainIterator;
34 class VariableWatchpointSet;
35
36 enum ResolveMode {
37     ThrowIfNotFound,
38     DoNotThrowIfNotFound
39 };
40
41 enum ResolveType {
42     // Lexical scope guaranteed a certain type of variable access.
43     GlobalProperty,
44     GlobalVar,
45     ClosureVar,
46
47     // Ditto, but at least one intervening scope used non-strict eval, which
48     // can inject an intercepting var delcaration at runtime.
49     GlobalPropertyWithVarInjectionChecks,
50     GlobalVarWithVarInjectionChecks,
51     ClosureVarWithVarInjectionChecks,
52
53     // Lexical scope didn't prove anything -- probably because of a 'with' scope.
54     Dynamic
55 };
56
57 inline ResolveType makeType(ResolveType type, bool needsVarInjectionChecks)
58 {
59     if (!needsVarInjectionChecks)
60         return type;
61
62     switch (type) {
63     case GlobalProperty:
64         return GlobalPropertyWithVarInjectionChecks;
65     case GlobalVar:
66         return GlobalVarWithVarInjectionChecks;
67     case ClosureVar:
68         return ClosureVarWithVarInjectionChecks;
69     case GlobalPropertyWithVarInjectionChecks:
70     case GlobalVarWithVarInjectionChecks:
71     case ClosureVarWithVarInjectionChecks:
72     case Dynamic:
73         return type;
74     }
75
76     RELEASE_ASSERT_NOT_REACHED();
77     return type;
78 }
79
80 inline bool needsVarInjectionChecks(ResolveType type)
81 {
82     switch (type) {
83     case GlobalProperty:
84     case GlobalVar:
85     case ClosureVar:
86         return false;
87     case GlobalPropertyWithVarInjectionChecks:
88     case GlobalVarWithVarInjectionChecks:
89     case ClosureVarWithVarInjectionChecks:
90     case Dynamic:
91         return true;
92     default:
93         RELEASE_ASSERT_NOT_REACHED();
94         return true;
95     }
96 }
97
98 struct ResolveOp {
99     ResolveOp(ResolveType type, size_t depth, Structure* structure, JSActivation* activation, VariableWatchpointSet* watchpointSet, uintptr_t operand)
100         : type(type)
101         , depth(depth)
102         , structure(structure)
103         , activation(activation)
104         , watchpointSet(watchpointSet)
105         , operand(operand)
106     {
107     }
108
109     ResolveType type;
110     size_t depth;
111     Structure* structure;
112     JSActivation* activation;
113     VariableWatchpointSet* watchpointSet;
114     uintptr_t operand;
115 };
116
117 class ResolveModeAndType {
118     typedef unsigned Operand;
119 public:
120     static const size_t shift = sizeof(Operand) * 8 / 2;
121     static const unsigned mask = (1 << shift) - 1;
122
123     ResolveModeAndType(ResolveMode resolveMode, ResolveType resolveType)
124         : m_operand((resolveMode << shift) | resolveType)
125     {
126     }
127
128     explicit ResolveModeAndType(unsigned operand)
129         : m_operand(operand)
130     {
131     }
132
133     ResolveMode mode() { return static_cast<ResolveMode>(m_operand >> shift); }
134     ResolveType type() { return static_cast<ResolveType>(m_operand & mask); }
135     unsigned operand() { return m_operand; }
136
137 private:
138     Operand m_operand;
139 };
140
141 enum GetOrPut { Get, Put };
142
143 class JSScope : public JSNonFinalObject {
144 public:
145     typedef JSNonFinalObject Base;
146
147     friend class LLIntOffsetsExtractor;
148     static size_t offsetOfNext();
149
150     JS_EXPORT_PRIVATE static JSObject* objectAtScope(JSScope*);
151
152     static JSValue resolve(ExecState*, JSScope*, const Identifier&);
153     static ResolveOp abstractResolve(ExecState*, JSScope*, const Identifier&, GetOrPut, ResolveType);
154
155     static void visitChildren(JSCell*, SlotVisitor&);
156
157     ScopeChainIterator begin();
158     ScopeChainIterator end();
159     JSScope* next();
160     int depth();
161
162     JSGlobalObject* globalObject();
163     VM* vm();
164     JSObject* globalThis();
165
166 protected:
167     JSScope(VM&, Structure*, JSScope* next);
168     static const unsigned StructureFlags = OverridesVisitChildren | Base::StructureFlags;
169
170 private:
171     WriteBarrier<JSScope> m_next;
172 };
173
174 inline JSScope::JSScope(VM& vm, Structure* structure, JSScope* next)
175     : Base(vm, structure)
176     , m_next(vm, this, next, WriteBarrier<JSScope>::MayBeNull)
177 {
178 }
179
180 class ScopeChainIterator {
181 public:
182     ScopeChainIterator(JSScope* node)
183         : m_node(node)
184     {
185     }
186
187     JSObject* get() const { return JSScope::objectAtScope(m_node); }
188     JSObject* operator->() const { return JSScope::objectAtScope(m_node); }
189
190     ScopeChainIterator& operator++() { m_node = m_node->next(); return *this; }
191
192     // postfix ++ intentionally omitted
193
194     bool operator==(const ScopeChainIterator& other) const { return m_node == other.m_node; }
195     bool operator!=(const ScopeChainIterator& other) const { return m_node != other.m_node; }
196
197 private:
198     JSScope* m_node;
199 };
200
201 inline ScopeChainIterator JSScope::begin()
202 {
203     return ScopeChainIterator(this); 
204 }
205
206 inline ScopeChainIterator JSScope::end()
207
208     return ScopeChainIterator(0); 
209 }
210
211 inline JSScope* JSScope::next()
212
213     return m_next.get();
214 }
215
216 inline JSGlobalObject* JSScope::globalObject()
217
218     return structure()->globalObject();
219 }
220
221 inline VM* JSScope::vm()
222
223     return MarkedBlock::blockFor(this)->vm();
224 }
225
226 inline Register& Register::operator=(JSScope* scope)
227 {
228     *this = JSValue(scope);
229     return *this;
230 }
231
232 inline JSScope* Register::scope() const
233 {
234     return jsCast<JSScope*>(jsValue());
235 }
236
237 inline VM& ExecState::vm() const
238 {
239     ASSERT(scope()->vm());
240     return *scope()->vm();
241 }
242
243 inline JSGlobalObject* ExecState::lexicalGlobalObject() const
244 {
245     return scope()->globalObject();
246 }
247
248 inline JSObject* ExecState::globalThisValue() const
249 {
250     return scope()->globalThis();
251 }
252
253 inline size_t JSScope::offsetOfNext()
254 {
255     return OBJECT_OFFSETOF(JSScope, m_next);
256 }
257
258 } // namespace JSC
259
260 #endif // JSScope_h