]>
Commit | Line | Data |
---|---|---|
93a37866 | 1 | /* |
ed1e77d3 | 2 | * Copyright (C) 2012-2015 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 | ||
93a37866 | 29 | #include "JSGlobalObject.h" |
ed1e77d3 | 30 | #include "JSLexicalEnvironment.h" |
93a37866 A |
31 | #include "JSNameScope.h" |
32 | #include "JSWithScope.h" | |
81345200 | 33 | #include "JSCInlines.h" |
93a37866 A |
34 | |
35 | namespace JSC { | |
36 | ||
81345200 | 37 | STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(JSScope); |
93a37866 A |
38 | |
39 | void JSScope::visitChildren(JSCell* cell, SlotVisitor& visitor) | |
40 | { | |
41 | JSScope* thisObject = jsCast<JSScope*>(cell); | |
81345200 | 42 | ASSERT_GC_OBJECT_INHERITS(thisObject, info()); |
93a37866 A |
43 | Base::visitChildren(thisObject, visitor); |
44 | visitor.append(&thisObject->m_next); | |
45 | } | |
46 | ||
81345200 A |
47 | // Returns true if we found enough information to terminate optimization. |
48 | static inline bool abstractAccess(ExecState* exec, JSScope* scope, const Identifier& ident, GetOrPut getOrPut, size_t depth, bool& needsVarInjectionChecks, ResolveOp& op) | |
93a37866 | 49 | { |
ed1e77d3 | 50 | if (JSLexicalEnvironment* lexicalEnvironment = jsDynamicCast<JSLexicalEnvironment*>(scope)) { |
81345200 | 51 | if (ident == exec->propertyNames().arguments) { |
ed1e77d3 | 52 | // We know the property will be at this lexical environment scope, but we don't know how to cache it. |
81345200 | 53 | op = ResolveOp(Dynamic, 0, 0, 0, 0, 0); |
93a37866 | 54 | return true; |
93a37866 | 55 | } |
93a37866 | 56 | |
ed1e77d3 | 57 | SymbolTableEntry entry = lexicalEnvironment->symbolTable()->get(ident.impl()); |
81345200 | 58 | if (entry.isReadOnly() && getOrPut == Put) { |
ed1e77d3 | 59 | // We know the property will be at this lexical environment scope, but we don't know how to cache it. |
81345200 A |
60 | op = ResolveOp(Dynamic, 0, 0, 0, 0, 0); |
61 | return true; | |
62 | } | |
93a37866 | 63 | |
81345200 | 64 | if (!entry.isNull()) { |
ed1e77d3 | 65 | op = ResolveOp(makeType(ClosureVar, needsVarInjectionChecks), depth, 0, lexicalEnvironment, entry.watchpointSet(), entry.scopeOffset().offset()); |
81345200 A |
66 | return true; |
67 | } | |
93a37866 | 68 | |
ed1e77d3 | 69 | if (lexicalEnvironment->symbolTable()->usesNonStrictEval()) |
81345200 A |
70 | needsVarInjectionChecks = true; |
71 | return false; | |
72 | } | |
93a37866 | 73 | |
81345200 A |
74 | if (JSGlobalObject* globalObject = jsDynamicCast<JSGlobalObject*>(scope)) { |
75 | SymbolTableEntry entry = globalObject->symbolTable()->get(ident.impl()); | |
76 | if (!entry.isNull()) { | |
77 | if (getOrPut == Put && entry.isReadOnly()) { | |
78 | // We know the property will be at global scope, but we don't know how to cache it. | |
79 | op = ResolveOp(Dynamic, 0, 0, 0, 0, 0); | |
93a37866 A |
80 | return true; |
81 | } | |
82 | ||
81345200 A |
83 | op = ResolveOp( |
84 | makeType(GlobalVar, needsVarInjectionChecks), depth, 0, 0, entry.watchpointSet(), | |
ed1e77d3 | 85 | reinterpret_cast<uintptr_t>(globalObject->variableAt(entry.scopeOffset()).slot())); |
93a37866 A |
86 | return true; |
87 | } | |
93a37866 | 88 | |
81345200 A |
89 | PropertySlot slot(globalObject); |
90 | if (!globalObject->getOwnPropertySlot(globalObject, exec, ident, slot) | |
91 | || !slot.isCacheableValue() | |
92 | || !globalObject->structure()->propertyAccessesAreCacheable() | |
93 | || (globalObject->structure()->hasReadOnlyOrGetterSetterPropertiesExcludingProto() && getOrPut == Put)) { | |
94 | // We know the property will be at global scope, but we don't know how to cache it. | |
95 | ASSERT(!scope->next()); | |
96 | op = ResolveOp(makeType(GlobalProperty, needsVarInjectionChecks), depth, 0, 0, 0, 0); | |
97 | return true; | |
93a37866 | 98 | } |
ed1e77d3 A |
99 | |
100 | WatchpointState state = globalObject->structure()->ensurePropertyReplacementWatchpointSet(exec->vm(), slot.cachedOffset())->state(); | |
101 | if (state == IsWatched && getOrPut == Put) { | |
102 | // The field exists, but because the replacement watchpoint is still intact. This is | |
103 | // kind of dangerous. We have two options: | |
104 | // 1) Invalidate the watchpoint set. That would work, but it's possible that this code | |
105 | // path never executes - in which case this would be unwise. | |
106 | // 2) Have the invalidation happen at run-time. All we have to do is leave the code | |
107 | // uncached. The only downside is slightly more work when this does execute. | |
108 | // We go with option (2) here because it seems less evil. | |
109 | op = ResolveOp(makeType(GlobalProperty, needsVarInjectionChecks), depth, 0, 0, 0, 0); | |
110 | } else | |
111 | op = ResolveOp(makeType(GlobalProperty, needsVarInjectionChecks), depth, globalObject->structure(), 0, 0, slot.cachedOffset()); | |
81345200 | 112 | return true; |
93a37866 | 113 | } |
93a37866 | 114 | |
81345200 A |
115 | op = ResolveOp(Dynamic, 0, 0, 0, 0, 0); |
116 | return true; | |
93a37866 A |
117 | } |
118 | ||
81345200 | 119 | JSObject* JSScope::objectAtScope(JSScope* scope) |
93a37866 | 120 | { |
81345200 A |
121 | JSObject* object = scope; |
122 | if (object->type() == WithScopeType) | |
123 | return jsCast<JSWithScope*>(object)->object(); | |
93a37866 | 124 | |
81345200 | 125 | return object; |
93a37866 A |
126 | } |
127 | ||
81345200 | 128 | int JSScope::depth() |
93a37866 | 129 | { |
81345200 A |
130 | int depth = 0; |
131 | for (JSScope* scope = this; scope; scope = scope->next()) | |
132 | ++depth; | |
133 | return depth; | |
93a37866 A |
134 | } |
135 | ||
ed1e77d3 A |
136 | // When an exception occurs, the result of isUnscopable becomes false. |
137 | static inline bool isUnscopable(ExecState* exec, JSScope* scope, JSObject* object, const Identifier& ident) | |
138 | { | |
139 | if (scope->type() != WithScopeType) | |
140 | return false; | |
141 | ||
142 | JSValue unscopables = object->get(exec, exec->propertyNames().unscopablesSymbol); | |
143 | if (exec->hadException()) | |
144 | return false; | |
145 | if (!unscopables.isObject()) | |
146 | return false; | |
147 | JSValue blocked = jsCast<JSObject*>(unscopables)->get(exec, ident); | |
148 | if (exec->hadException()) | |
149 | return false; | |
150 | ||
151 | return blocked.toBoolean(exec); | |
152 | } | |
153 | ||
81345200 | 154 | JSValue JSScope::resolve(ExecState* exec, JSScope* scope, const Identifier& ident) |
93a37866 | 155 | { |
81345200 A |
156 | ScopeChainIterator end = scope->end(); |
157 | ScopeChainIterator it = scope->begin(); | |
158 | while (1) { | |
ed1e77d3 | 159 | JSScope* scope = it.scope(); |
81345200 | 160 | JSObject* object = it.get(); |
93a37866 | 161 | |
81345200 A |
162 | if (++it == end) // Global scope. |
163 | return object; | |
93a37866 | 164 | |
ed1e77d3 A |
165 | if (object->hasProperty(exec, ident)) { |
166 | if (!isUnscopable(exec, scope, object, ident)) | |
167 | return object; | |
168 | ASSERT_WITH_MESSAGE(!exec->hadException(), "When an exception occurs, the result of isUnscopable becomes false"); | |
169 | } | |
93a37866 | 170 | } |
93a37866 A |
171 | } |
172 | ||
ed1e77d3 | 173 | ResolveOp JSScope::abstractResolve(ExecState* exec, bool hasTopActivation, JSScope* scope, const Identifier& ident, GetOrPut getOrPut, ResolveType unlinkedType) |
93a37866 | 174 | { |
81345200 A |
175 | ResolveOp op(Dynamic, 0, 0, 0, 0, 0); |
176 | if (unlinkedType == Dynamic) | |
177 | return op; | |
178 | ||
ed1e77d3 | 179 | size_t depth = hasTopActivation ? 1 : 0; |
81345200 A |
180 | bool needsVarInjectionChecks = JSC::needsVarInjectionChecks(unlinkedType); |
181 | for (; scope; scope = scope->next()) { | |
182 | if (abstractAccess(exec, scope, ident, getOrPut, depth, needsVarInjectionChecks, op)) | |
183 | break; | |
184 | ++depth; | |
93a37866 | 185 | } |
93a37866 | 186 | |
81345200 | 187 | return op; |
93a37866 A |
188 | } |
189 | ||
81345200 | 190 | const char* resolveModeName(ResolveMode mode) |
93a37866 | 191 | { |
81345200 A |
192 | static const char* const names[] = { |
193 | "ThrowIfNotFound", | |
194 | "DoNotThrowIfNotFound" | |
195 | }; | |
196 | return names[mode]; | |
93a37866 A |
197 | } |
198 | ||
81345200 | 199 | const char* resolveTypeName(ResolveType type) |
93a37866 | 200 | { |
81345200 A |
201 | static const char* const names[] = { |
202 | "GlobalProperty", | |
203 | "GlobalVar", | |
204 | "ClosureVar", | |
ed1e77d3 | 205 | "LocalClosureVar", |
81345200 A |
206 | "GlobalPropertyWithVarInjectionChecks", |
207 | "GlobalVarWithVarInjectionChecks", | |
208 | "ClosureVarWithVarInjectionChecks", | |
209 | "Dynamic" | |
210 | }; | |
81345200 | 211 | return names[type]; |
93a37866 A |
212 | } |
213 | ||
93a37866 | 214 | } // namespace JSC |