]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/JSScope.cpp
JavaScriptCore-7600.1.4.17.5.tar.gz
[apple/javascriptcore.git] / runtime / JSScope.cpp
1 /*
2 * Copyright (C) 2012, 2013, 2014 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 #include "config.h"
27 #include "JSScope.h"
28
29 #include "JSActivation.h"
30 #include "JSGlobalObject.h"
31 #include "JSNameScope.h"
32 #include "JSWithScope.h"
33 #include "JSCInlines.h"
34
35 namespace JSC {
36
37 STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(JSScope);
38
39 void JSScope::visitChildren(JSCell* cell, SlotVisitor& visitor)
40 {
41 JSScope* thisObject = jsCast<JSScope*>(cell);
42 ASSERT_GC_OBJECT_INHERITS(thisObject, info());
43 COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
44 ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
45
46 Base::visitChildren(thisObject, visitor);
47 visitor.append(&thisObject->m_next);
48 }
49
50 // Returns true if we found enough information to terminate optimization.
51 static inline bool abstractAccess(ExecState* exec, JSScope* scope, const Identifier& ident, GetOrPut getOrPut, size_t depth, bool& needsVarInjectionChecks, ResolveOp& op)
52 {
53 if (JSActivation* activation = jsDynamicCast<JSActivation*>(scope)) {
54 if (ident == exec->propertyNames().arguments) {
55 // We know the property will be at this activation scope, but we don't know how to cache it.
56 op = ResolveOp(Dynamic, 0, 0, 0, 0, 0);
57 return true;
58 }
59
60 SymbolTableEntry entry = activation->symbolTable()->get(ident.impl());
61 if (entry.isReadOnly() && getOrPut == Put) {
62 // We know the property will be at this activation scope, but we don't know how to cache it.
63 op = ResolveOp(Dynamic, 0, 0, 0, 0, 0);
64 return true;
65 }
66
67 if (!entry.isNull()) {
68 op = ResolveOp(makeType(ClosureVar, needsVarInjectionChecks), depth, 0, activation, entry.watchpointSet(), entry.getIndex());
69 return true;
70 }
71
72 if (activation->symbolTable()->usesNonStrictEval())
73 needsVarInjectionChecks = true;
74 return false;
75 }
76
77 if (JSGlobalObject* globalObject = jsDynamicCast<JSGlobalObject*>(scope)) {
78 SymbolTableEntry entry = globalObject->symbolTable()->get(ident.impl());
79 if (!entry.isNull()) {
80 if (getOrPut == Put && entry.isReadOnly()) {
81 // We know the property will be at global scope, but we don't know how to cache it.
82 op = ResolveOp(Dynamic, 0, 0, 0, 0, 0);
83 return true;
84 }
85
86 op = ResolveOp(
87 makeType(GlobalVar, needsVarInjectionChecks), depth, 0, 0, entry.watchpointSet(),
88 reinterpret_cast<uintptr_t>(globalObject->registerAt(entry.getIndex()).slot()));
89 return true;
90 }
91
92 PropertySlot slot(globalObject);
93 if (!globalObject->getOwnPropertySlot(globalObject, exec, ident, slot)
94 || !slot.isCacheableValue()
95 || !globalObject->structure()->propertyAccessesAreCacheable()
96 || (globalObject->structure()->hasReadOnlyOrGetterSetterPropertiesExcludingProto() && getOrPut == Put)) {
97 // We know the property will be at global scope, but we don't know how to cache it.
98 ASSERT(!scope->next());
99 op = ResolveOp(makeType(GlobalProperty, needsVarInjectionChecks), depth, 0, 0, 0, 0);
100 return true;
101 }
102
103 op = ResolveOp(makeType(GlobalProperty, needsVarInjectionChecks), depth, globalObject->structure(), 0, 0, slot.cachedOffset());
104 return true;
105 }
106
107 op = ResolveOp(Dynamic, 0, 0, 0, 0, 0);
108 return true;
109 }
110
111 JSObject* JSScope::objectAtScope(JSScope* scope)
112 {
113 JSObject* object = scope;
114 if (object->type() == WithScopeType)
115 return jsCast<JSWithScope*>(object)->object();
116
117 return object;
118 }
119
120 int JSScope::depth()
121 {
122 int depth = 0;
123 for (JSScope* scope = this; scope; scope = scope->next())
124 ++depth;
125 return depth;
126 }
127
128 JSValue JSScope::resolve(ExecState* exec, JSScope* scope, const Identifier& ident)
129 {
130 ScopeChainIterator end = scope->end();
131 ScopeChainIterator it = scope->begin();
132 while (1) {
133 JSObject* object = it.get();
134
135 if (++it == end) // Global scope.
136 return object;
137
138 if (object->hasProperty(exec, ident))
139 return object;
140 }
141 }
142
143 ResolveOp JSScope::abstractResolve(ExecState* exec, JSScope* scope, const Identifier& ident, GetOrPut getOrPut, ResolveType unlinkedType)
144 {
145 ResolveOp op(Dynamic, 0, 0, 0, 0, 0);
146 if (unlinkedType == Dynamic)
147 return op;
148
149 size_t depth = 0;
150 bool needsVarInjectionChecks = JSC::needsVarInjectionChecks(unlinkedType);
151 for (; scope; scope = scope->next()) {
152 if (abstractAccess(exec, scope, ident, getOrPut, depth, needsVarInjectionChecks, op))
153 break;
154 ++depth;
155 }
156
157 return op;
158 }
159
160 const char* resolveModeName(ResolveMode mode)
161 {
162 static const char* const names[] = {
163 "ThrowIfNotFound",
164 "DoNotThrowIfNotFound"
165 };
166 return names[mode];
167 }
168
169 const char* resolveTypeName(ResolveType type)
170 {
171 static const char* const names[] = {
172 "GlobalProperty",
173 "GlobalVar",
174 "ClosureVar",
175 "GlobalPropertyWithVarInjectionChecks",
176 "GlobalVarWithVarInjectionChecks",
177 "ClosureVarWithVarInjectionChecks",
178 "Dynamic"
179 };
180 ASSERT(type < sizeof(names) / sizeof(names[0]));
181 return names[type];
182 }
183
184 } // namespace JSC