OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 | 67 |
68 JSObject* Context::global_proxy() { | 68 JSObject* Context::global_proxy() { |
69 return global_context()->global_proxy_object(); | 69 return global_context()->global_proxy_object(); |
70 } | 70 } |
71 | 71 |
72 void Context::set_global_proxy(JSObject* object) { | 72 void Context::set_global_proxy(JSObject* object) { |
73 global_context()->set_global_proxy_object(object); | 73 global_context()->set_global_proxy_object(object); |
74 } | 74 } |
75 | 75 |
76 | 76 |
77 Handle<Object> Context::Lookup(Handle<String> name, ContextLookupFlags flags, | 77 Handle<Object> Context::Lookup(Handle<String> name, |
78 int* index_, PropertyAttributes* attributes) { | 78 ContextLookupFlags flags, |
| 79 int* index_, |
| 80 PropertyAttributes* attributes) { |
79 Isolate* isolate = GetIsolate(); | 81 Isolate* isolate = GetIsolate(); |
80 Handle<Context> context(this, isolate); | 82 Handle<Context> context(this, isolate); |
81 | 83 |
82 bool follow_context_chain = (flags & FOLLOW_CONTEXT_CHAIN) != 0; | 84 bool follow_context_chain = (flags & FOLLOW_CONTEXT_CHAIN) != 0; |
83 *index_ = -1; | 85 *index_ = -1; |
84 *attributes = ABSENT; | 86 *attributes = ABSENT; |
85 | 87 |
86 if (FLAG_trace_contexts) { | 88 if (FLAG_trace_contexts) { |
87 PrintF("Context::Lookup("); | 89 PrintF("Context::Lookup("); |
88 name->ShortPrint(); | 90 name->ShortPrint(); |
89 PrintF(")\n"); | 91 PrintF(")\n"); |
90 } | 92 } |
91 | 93 |
92 do { | 94 do { |
93 if (FLAG_trace_contexts) { | 95 if (FLAG_trace_contexts) { |
94 PrintF(" - looking in context %p", reinterpret_cast<void*>(*context)); | 96 PrintF(" - looking in context %p", reinterpret_cast<void*>(*context)); |
95 if (context->IsGlobalContext()) PrintF(" (global context)"); | 97 if (context->IsGlobalContext()) PrintF(" (global context)"); |
96 PrintF("\n"); | 98 PrintF("\n"); |
97 } | 99 } |
98 | 100 |
99 // Check extension/with/global object. | 101 // Check extension/with/global object. |
100 if (context->has_extension()) { | 102 if (context->has_extension()) { |
101 if (context->IsCatchContext()) { | 103 Handle<JSObject> extension = Handle<JSObject>(context->extension(), |
102 // Catch contexts have the variable name in the extension slot. | 104 isolate); |
103 if (name->Equals(String::cast(context->extension()))) { | 105 // Context extension objects needs to behave as if they have no |
104 if (FLAG_trace_contexts) { | 106 // prototype. So even if we want to follow prototype chains, we |
105 PrintF("=> found in catch context\n"); | 107 // need to only do a local lookup for context extension objects. |
106 } | 108 if ((flags & FOLLOW_PROTOTYPE_CHAIN) == 0 || |
107 *index_ = Context::THROWN_OBJECT_INDEX; | 109 extension->IsJSContextExtensionObject()) { |
108 *attributes = NONE; | 110 *attributes = extension->GetLocalPropertyAttribute(*name); |
109 return context; | 111 } else { |
| 112 *attributes = extension->GetPropertyAttribute(*name); |
| 113 } |
| 114 if (*attributes != ABSENT) { |
| 115 if (FLAG_trace_contexts) { |
| 116 PrintF("=> found property in context object %p\n", |
| 117 reinterpret_cast<void*>(*extension)); |
110 } | 118 } |
111 } else { | 119 return extension; |
112 // Global, function, and with contexts may have an object in the | |
113 // extension slot. | |
114 Handle<JSObject> extension(JSObject::cast(context->extension()), | |
115 isolate); | |
116 // Context extension objects needs to behave as if they have no | |
117 // prototype. So even if we want to follow prototype chains, we | |
118 // need to only do a local lookup for context extension objects. | |
119 if ((flags & FOLLOW_PROTOTYPE_CHAIN) == 0 || | |
120 extension->IsJSContextExtensionObject()) { | |
121 *attributes = extension->GetLocalPropertyAttribute(*name); | |
122 } else { | |
123 *attributes = extension->GetPropertyAttribute(*name); | |
124 } | |
125 if (*attributes != ABSENT) { | |
126 // property found | |
127 if (FLAG_trace_contexts) { | |
128 PrintF("=> found property in context object %p\n", | |
129 reinterpret_cast<void*>(*extension)); | |
130 } | |
131 return extension; | |
132 } | |
133 } | 120 } |
134 } | 121 } |
135 | 122 |
136 // Only functions can have locals, parameters, and a function name. | 123 // Only functions can have locals, parameters, and a function name. |
137 if (context->IsFunctionContext()) { | 124 if (context->IsFunctionContext()) { |
138 // we have context-local slots | 125 // We may have context-local slots. Check locals in the context. |
139 | |
140 // check non-parameter locals in context | |
141 Handle<SerializedScopeInfo> scope_info( | 126 Handle<SerializedScopeInfo> scope_info( |
142 context->closure()->shared()->scope_info(), isolate); | 127 context->closure()->shared()->scope_info(), isolate); |
143 Variable::Mode mode; | 128 Variable::Mode mode; |
144 int index = scope_info->ContextSlotIndex(*name, &mode); | 129 int index = scope_info->ContextSlotIndex(*name, &mode); |
145 ASSERT(index < 0 || index >= MIN_CONTEXT_SLOTS); | 130 ASSERT(index < 0 || index >= MIN_CONTEXT_SLOTS); |
146 if (index >= 0) { | 131 if (index >= 0) { |
147 // slot found | |
148 if (FLAG_trace_contexts) { | 132 if (FLAG_trace_contexts) { |
149 PrintF("=> found local in context slot %d (mode = %d)\n", | 133 PrintF("=> found local in context slot %d (mode = %d)\n", |
150 index, mode); | 134 index, mode); |
151 } | 135 } |
152 *index_ = index; | 136 *index_ = index; |
153 // Note: Fixed context slots are statically allocated by the compiler. | 137 // Note: Fixed context slots are statically allocated by the compiler. |
154 // Statically allocated variables always have a statically known mode, | 138 // Statically allocated variables always have a statically known mode, |
155 // which is the mode with which they were declared when added to the | 139 // which is the mode with which they were declared when added to the |
156 // scope. Thus, the DYNAMIC mode (which corresponds to dynamically | 140 // scope. Thus, the DYNAMIC mode (which corresponds to dynamically |
157 // declared variables that were introduced through declaration nodes) | 141 // declared variables that were introduced through declaration nodes) |
158 // must not appear here. | 142 // must not appear here. |
159 switch (mode) { | 143 switch (mode) { |
160 case Variable::INTERNAL: // fall through | 144 case Variable::INTERNAL: // Fall through. |
161 case Variable::VAR: *attributes = NONE; break; | 145 case Variable::VAR: |
162 case Variable::CONST: *attributes = READ_ONLY; break; | 146 *attributes = NONE; |
163 case Variable::DYNAMIC: UNREACHABLE(); break; | 147 break; |
164 case Variable::DYNAMIC_GLOBAL: UNREACHABLE(); break; | 148 case Variable::CONST: |
165 case Variable::DYNAMIC_LOCAL: UNREACHABLE(); break; | 149 *attributes = READ_ONLY; |
166 case Variable::TEMPORARY: UNREACHABLE(); break; | 150 break; |
| 151 case Variable::DYNAMIC: |
| 152 case Variable::DYNAMIC_GLOBAL: |
| 153 case Variable::DYNAMIC_LOCAL: |
| 154 case Variable::TEMPORARY: |
| 155 UNREACHABLE(); |
| 156 break; |
167 } | 157 } |
168 return context; | 158 return context; |
169 } | 159 } |
170 | 160 |
171 // check parameter locals in context | 161 // Check the slot corresponding to the intermediate context holding |
172 int param_index = scope_info->ParameterIndex(*name); | 162 // only the function name variable. |
173 if (param_index >= 0) { | |
174 // slot found. | |
175 int index = scope_info->ContextSlotIndex( | |
176 isolate->heap()->arguments_shadow_symbol(), NULL); | |
177 ASSERT(index >= 0); // arguments must exist and be in the heap context | |
178 Handle<JSObject> arguments(JSObject::cast(context->get(index)), | |
179 isolate); | |
180 if (FLAG_trace_contexts) { | |
181 PrintF("=> found parameter %d in arguments object\n", param_index); | |
182 } | |
183 *index_ = param_index; | |
184 *attributes = NONE; | |
185 return arguments; | |
186 } | |
187 | |
188 // check intermediate context (holding only the function name variable) | |
189 if (follow_context_chain) { | 163 if (follow_context_chain) { |
190 int index = scope_info->FunctionContextSlotIndex(*name); | 164 int index = scope_info->FunctionContextSlotIndex(*name); |
191 if (index >= 0) { | 165 if (index >= 0) { |
192 // slot found | |
193 if (FLAG_trace_contexts) { | 166 if (FLAG_trace_contexts) { |
194 PrintF("=> found intermediate function in context slot %d\n", | 167 PrintF("=> found intermediate function in context slot %d\n", |
195 index); | 168 index); |
196 } | 169 } |
197 *index_ = index; | 170 *index_ = index; |
198 *attributes = READ_ONLY; | 171 *attributes = READ_ONLY; |
199 return context; | 172 return context; |
200 } | 173 } |
201 } | 174 } |
202 } | 175 } |
203 | 176 |
204 // Proceed with the previous context. | 177 // Proceed with the previous context. |
205 if (context->IsGlobalContext()) { | 178 if (context->IsGlobalContext()) { |
206 follow_context_chain = false; | 179 follow_context_chain = false; |
207 } else { | 180 } else { |
208 context = Handle<Context>(context->previous(), isolate); | 181 context = Handle<Context>(context->previous(), isolate); |
209 } | 182 } |
210 } while (follow_context_chain); | 183 } while (follow_context_chain); |
211 | 184 |
212 // slot not found | |
213 if (FLAG_trace_contexts) { | 185 if (FLAG_trace_contexts) { |
214 PrintF("=> no property/slot found\n"); | 186 PrintF("=> no property/slot found\n"); |
215 } | 187 } |
216 return Handle<Object>::null(); | 188 return Handle<Object>::null(); |
217 } | 189 } |
218 | 190 |
219 | 191 |
220 bool Context::GlobalIfNotShadowedByEval(Handle<String> name) { | 192 bool Context::GlobalIfNotShadowedByEval(Handle<String> name) { |
221 Context* context = this; | 193 Context* context = this; |
222 | 194 |
223 // Check that there is no local with the given name in contexts | 195 // Check that there is no local with the given name in contexts |
224 // before the global context and check that there are no context | 196 // before the global context and check that there are no context |
225 // extension objects (conservative check for with statements). | 197 // extension objects (conservative check for with statements). |
226 while (!context->IsGlobalContext()) { | 198 while (!context->IsGlobalContext()) { |
227 // Check if the context is a catch or with context, or has introduced | 199 // Check if the context is a catch or with context, or has called |
228 // bindings by calling non-strict eval. | 200 // non-strict eval. |
229 if (context->has_extension()) return false; | 201 if (context->has_extension()) return false; |
230 | 202 |
231 // Not a with context so it must be a function context. | 203 // Not a with context so it must be a function context. |
232 ASSERT(context->IsFunctionContext()); | 204 ASSERT(context->IsFunctionContext()); |
233 | 205 |
234 // Check non-parameter locals. | 206 // Check non-parameter locals. |
235 Handle<SerializedScopeInfo> scope_info( | 207 Handle<SerializedScopeInfo> scope_info( |
236 context->closure()->shared()->scope_info()); | 208 context->closure()->shared()->scope_info()); |
237 Variable::Mode mode; | 209 Variable::Mode mode; |
238 int index = scope_info->ContextSlotIndex(*name, &mode); | 210 int index = scope_info->ContextSlotIndex(*name, &mode); |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 // During bootstrapping we allow all objects to pass as global | 324 // During bootstrapping we allow all objects to pass as global |
353 // objects. This is necessary to fix circular dependencies. | 325 // objects. This is necessary to fix circular dependencies. |
354 Isolate* isolate = Isolate::Current(); | 326 Isolate* isolate = Isolate::Current(); |
355 return isolate->heap()->gc_state() != Heap::NOT_IN_GC || | 327 return isolate->heap()->gc_state() != Heap::NOT_IN_GC || |
356 isolate->bootstrapper()->IsActive() || | 328 isolate->bootstrapper()->IsActive() || |
357 object->IsGlobalObject(); | 329 object->IsGlobalObject(); |
358 } | 330 } |
359 #endif | 331 #endif |
360 | 332 |
361 } } // namespace v8::internal | 333 } } // namespace v8::internal |
OLD | NEW |