]> git.saurik.com Git - apple/javascriptcore.git/blame - runtime/JSScope.cpp
JavaScriptCore-7600.1.4.16.1.tar.gz
[apple/javascriptcore.git] / runtime / JSScope.cpp
CommitLineData
93a37866 1/*
81345200 2 * Copyright (C) 2012, 2013, 2014 Apple Inc. All Rights Reserved.
93a37866
A
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"
81345200 33#include "JSCInlines.h"
93a37866
A
34
35namespace JSC {
36
81345200 37STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(JSScope);
93a37866
A
38
39void JSScope::visitChildren(JSCell* cell, SlotVisitor& visitor)
40{
41 JSScope* thisObject = jsCast<JSScope*>(cell);
81345200 42 ASSERT_GC_OBJECT_INHERITS(thisObject, info());
93a37866
A
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
81345200
A
50// Returns true if we found enough information to terminate optimization.
51static inline bool abstractAccess(ExecState* exec, JSScope* scope, const Identifier& ident, GetOrPut getOrPut, size_t depth, bool& needsVarInjectionChecks, ResolveOp& op)
93a37866 52{
81345200
A
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);
93a37866 57 return true;
93a37866 58 }
93a37866 59
81345200
A
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 }
93a37866 66
81345200
A
67 if (!entry.isNull()) {
68 op = ResolveOp(makeType(ClosureVar, needsVarInjectionChecks), depth, 0, activation, entry.watchpointSet(), entry.getIndex());
69 return true;
70 }
93a37866 71
81345200
A
72 if (activation->symbolTable()->usesNonStrictEval())
73 needsVarInjectionChecks = true;
74 return false;
75 }
93a37866 76
81345200
A
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);
93a37866
A
83 return true;
84 }
85
81345200
A
86 op = ResolveOp(
87 makeType(GlobalVar, needsVarInjectionChecks), depth, 0, 0, entry.watchpointSet(),
88 reinterpret_cast<uintptr_t>(globalObject->registerAt(entry.getIndex()).slot()));
93a37866
A
89 return true;
90 }
93a37866 91
81345200
A
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;
93a37866 101 }
93a37866 102
81345200
A
103 op = ResolveOp(makeType(GlobalProperty, needsVarInjectionChecks), depth, globalObject->structure(), 0, 0, slot.cachedOffset());
104 return true;
93a37866 105 }
93a37866 106
81345200
A
107 op = ResolveOp(Dynamic, 0, 0, 0, 0, 0);
108 return true;
93a37866
A
109}
110
81345200 111JSObject* JSScope::objectAtScope(JSScope* scope)
93a37866 112{
81345200
A
113 JSObject* object = scope;
114 if (object->type() == WithScopeType)
115 return jsCast<JSWithScope*>(object)->object();
93a37866 116
81345200 117 return object;
93a37866
A
118}
119
81345200 120int JSScope::depth()
93a37866 121{
81345200
A
122 int depth = 0;
123 for (JSScope* scope = this; scope; scope = scope->next())
124 ++depth;
125 return depth;
93a37866
A
126}
127
81345200 128JSValue JSScope::resolve(ExecState* exec, JSScope* scope, const Identifier& ident)
93a37866 129{
81345200
A
130 ScopeChainIterator end = scope->end();
131 ScopeChainIterator it = scope->begin();
132 while (1) {
133 JSObject* object = it.get();
93a37866 134
81345200
A
135 if (++it == end) // Global scope.
136 return object;
93a37866 137
81345200
A
138 if (object->hasProperty(exec, ident))
139 return object;
93a37866 140 }
93a37866
A
141}
142
81345200 143ResolveOp JSScope::abstractResolve(ExecState* exec, JSScope* scope, const Identifier& ident, GetOrPut getOrPut, ResolveType unlinkedType)
93a37866 144{
81345200
A
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;
93a37866 155 }
93a37866 156
81345200 157 return op;
93a37866
A
158}
159
81345200 160const char* resolveModeName(ResolveMode mode)
93a37866 161{
81345200
A
162 static const char* const names[] = {
163 "ThrowIfNotFound",
164 "DoNotThrowIfNotFound"
165 };
166 return names[mode];
93a37866
A
167}
168
81345200 169const char* resolveTypeName(ResolveType type)
93a37866 170{
81345200
A
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];
93a37866
A
182}
183
93a37866 184} // namespace JSC