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 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 return global_context()->global_proxy_object(); | 79 return global_context()->global_proxy_object(); |
80 } | 80 } |
81 | 81 |
82 void Context::set_global_proxy(JSObject* object) { | 82 void Context::set_global_proxy(JSObject* object) { |
83 global_context()->set_global_proxy_object(object); | 83 global_context()->set_global_proxy_object(object); |
84 } | 84 } |
85 | 85 |
86 | 86 |
87 Handle<Object> Context::Lookup(Handle<String> name, | 87 Handle<Object> Context::Lookup(Handle<String> name, |
88 ContextLookupFlags flags, | 88 ContextLookupFlags flags, |
89 int* index, | 89 int* index_, |
90 PropertyAttributes* attributes, | 90 PropertyAttributes* attributes, |
91 BindingFlags* binding_flags) { | 91 BindingFlags* binding_flags) { |
92 Isolate* isolate = GetIsolate(); | 92 Isolate* isolate = GetIsolate(); |
93 Handle<Context> context(this, isolate); | 93 Handle<Context> context(this, isolate); |
94 | 94 |
95 bool follow_context_chain = (flags & FOLLOW_CONTEXT_CHAIN) != 0; | 95 bool follow_context_chain = (flags & FOLLOW_CONTEXT_CHAIN) != 0; |
96 *index = -1; | 96 *index_ = -1; |
97 *attributes = ABSENT; | 97 *attributes = ABSENT; |
98 *binding_flags = MISSING_BINDING; | 98 *binding_flags = MISSING_BINDING; |
99 | 99 |
100 if (FLAG_trace_contexts) { | 100 if (FLAG_trace_contexts) { |
101 PrintF("Context::Lookup("); | 101 PrintF("Context::Lookup("); |
102 name->ShortPrint(); | 102 name->ShortPrint(); |
103 PrintF(")\n"); | 103 PrintF(")\n"); |
104 } | 104 } |
105 | 105 |
106 do { | 106 do { |
107 if (FLAG_trace_contexts) { | 107 if (FLAG_trace_contexts) { |
108 PrintF(" - looking in context %p", reinterpret_cast<void*>(*context)); | 108 PrintF(" - looking in context %p", reinterpret_cast<void*>(*context)); |
109 if (context->IsGlobalContext()) PrintF(" (global context)"); | 109 if (context->IsGlobalContext()) PrintF(" (global context)"); |
110 PrintF("\n"); | 110 PrintF("\n"); |
111 } | 111 } |
112 | 112 |
113 // 1. Check global objects, subjects of with, and extension objects. | 113 // Check extension/with/global object. |
114 if (context->IsGlobalContext() || | 114 if (!context->IsBlockContext() && context->has_extension()) { |
115 context->IsWithContext() || | 115 if (context->IsCatchContext()) { |
116 (context->IsFunctionContext() && context->has_extension())) { | 116 // Catch contexts have the variable name in the extension slot. |
117 Handle<JSObject> object(JSObject::cast(context->extension()), isolate); | 117 if (name->Equals(String::cast(context->extension()))) { |
118 // Context extension objects needs to behave as if they have no | 118 if (FLAG_trace_contexts) { |
119 // prototype. So even if we want to follow prototype chains, we need | 119 PrintF("=> found in catch context\n"); |
120 // to only do a local lookup for context extension objects. | 120 } |
121 if ((flags & FOLLOW_PROTOTYPE_CHAIN) == 0 || | 121 *index_ = Context::THROWN_OBJECT_INDEX; |
122 object->IsJSContextExtensionObject()) { | 122 *attributes = NONE; |
123 *attributes = object->GetLocalPropertyAttribute(*name); | 123 *binding_flags = MUTABLE_IS_INITIALIZED; |
| 124 return context; |
| 125 } |
124 } else { | 126 } else { |
125 *attributes = object->GetPropertyAttribute(*name); | 127 ASSERT(context->IsGlobalContext() || |
126 } | 128 context->IsFunctionContext() || |
127 if (*attributes != ABSENT) { | 129 context->IsWithContext()); |
128 if (FLAG_trace_contexts) { | 130 // Global, function, and with contexts may have an object in the |
129 PrintF("=> found property in context object %p\n", | 131 // extension slot. |
130 reinterpret_cast<void*>(*object)); | 132 Handle<JSObject> extension(JSObject::cast(context->extension()), |
| 133 isolate); |
| 134 // Context extension objects needs to behave as if they have no |
| 135 // prototype. So even if we want to follow prototype chains, we |
| 136 // need to only do a local lookup for context extension objects. |
| 137 if ((flags & FOLLOW_PROTOTYPE_CHAIN) == 0 || |
| 138 extension->IsJSContextExtensionObject()) { |
| 139 *attributes = extension->GetLocalPropertyAttribute(*name); |
| 140 } else { |
| 141 *attributes = extension->GetPropertyAttribute(*name); |
131 } | 142 } |
132 return object; | 143 if (*attributes != ABSENT) { |
| 144 // property found |
| 145 if (FLAG_trace_contexts) { |
| 146 PrintF("=> found property in context object %p\n", |
| 147 reinterpret_cast<void*>(*extension)); |
| 148 } |
| 149 return extension; |
| 150 } |
133 } | 151 } |
134 } | 152 } |
135 | 153 |
136 // 2. Check the context proper if it has slots. | 154 // Check serialized scope information of functions and blocks. Only |
| 155 // functions can have parameters, and a function name. |
137 if (context->IsFunctionContext() || context->IsBlockContext()) { | 156 if (context->IsFunctionContext() || context->IsBlockContext()) { |
138 // Use serialized scope information of functions and blocks to search | 157 // We may have context-local slots. Check locals in the context. |
139 // for the context index. | |
140 Handle<SerializedScopeInfo> scope_info; | 158 Handle<SerializedScopeInfo> scope_info; |
141 if (context->IsFunctionContext()) { | 159 if (context->IsFunctionContext()) { |
142 scope_info = Handle<SerializedScopeInfo>( | 160 scope_info = Handle<SerializedScopeInfo>( |
143 context->closure()->shared()->scope_info(), isolate); | 161 context->closure()->shared()->scope_info(), isolate); |
144 } else { | 162 } else { |
| 163 ASSERT(context->IsBlockContext()); |
145 scope_info = Handle<SerializedScopeInfo>( | 164 scope_info = Handle<SerializedScopeInfo>( |
146 SerializedScopeInfo::cast(context->extension()), isolate); | 165 SerializedScopeInfo::cast(context->extension()), isolate); |
147 } | 166 } |
| 167 |
148 Variable::Mode mode; | 168 Variable::Mode mode; |
149 int slot_index = scope_info->ContextSlotIndex(*name, &mode); | 169 int index = scope_info->ContextSlotIndex(*name, &mode); |
150 ASSERT(slot_index < 0 || slot_index >= MIN_CONTEXT_SLOTS); | 170 ASSERT(index < 0 || index >= MIN_CONTEXT_SLOTS); |
151 if (slot_index >= 0) { | 171 if (index >= 0) { |
152 if (FLAG_trace_contexts) { | 172 if (FLAG_trace_contexts) { |
153 PrintF("=> found local in context slot %d (mode = %d)\n", | 173 PrintF("=> found local in context slot %d (mode = %d)\n", |
154 slot_index, mode); | 174 index, mode); |
155 } | 175 } |
156 *index = slot_index; | 176 *index_ = index; |
157 // Note: Fixed context slots are statically allocated by the compiler. | 177 // Note: Fixed context slots are statically allocated by the compiler. |
158 // Statically allocated variables always have a statically known mode, | 178 // Statically allocated variables always have a statically known mode, |
159 // which is the mode with which they were declared when added to the | 179 // which is the mode with which they were declared when added to the |
160 // scope. Thus, the DYNAMIC mode (which corresponds to dynamically | 180 // scope. Thus, the DYNAMIC mode (which corresponds to dynamically |
161 // declared variables that were introduced through declaration nodes) | 181 // declared variables that were introduced through declaration nodes) |
162 // must not appear here. | 182 // must not appear here. |
163 switch (mode) { | 183 switch (mode) { |
164 case Variable::INTERNAL: // Fall through. | 184 case Variable::INTERNAL: // Fall through. |
165 case Variable::VAR: | 185 case Variable::VAR: |
166 *attributes = NONE; | 186 *attributes = NONE; |
(...skipping 12 matching lines...) Expand all Loading... |
179 case Variable::DYNAMIC_LOCAL: | 199 case Variable::DYNAMIC_LOCAL: |
180 case Variable::TEMPORARY: | 200 case Variable::TEMPORARY: |
181 UNREACHABLE(); | 201 UNREACHABLE(); |
182 break; | 202 break; |
183 } | 203 } |
184 return context; | 204 return context; |
185 } | 205 } |
186 | 206 |
187 // Check the slot corresponding to the intermediate context holding | 207 // Check the slot corresponding to the intermediate context holding |
188 // only the function name variable. | 208 // only the function name variable. |
189 if (follow_context_chain && context->IsFunctionContext()) { | 209 if (follow_context_chain) { |
190 int function_index = scope_info->FunctionContextSlotIndex(*name); | 210 int index = scope_info->FunctionContextSlotIndex(*name); |
191 if (function_index >= 0) { | 211 if (index >= 0) { |
192 if (FLAG_trace_contexts) { | 212 if (FLAG_trace_contexts) { |
193 PrintF("=> found intermediate function in context slot %d\n", | 213 PrintF("=> found intermediate function in context slot %d\n", |
194 function_index); | 214 index); |
195 } | 215 } |
196 *index = function_index; | 216 *index_ = index; |
197 *attributes = READ_ONLY; | 217 *attributes = READ_ONLY; |
198 *binding_flags = IMMUTABLE_IS_INITIALIZED; | 218 *binding_flags = IMMUTABLE_IS_INITIALIZED; |
199 return context; | 219 return context; |
200 } | 220 } |
201 } | 221 } |
202 | |
203 } else if (context->IsCatchContext()) { | |
204 // Catch contexts have the variable name in the extension slot. | |
205 if (name->Equals(String::cast(context->extension()))) { | |
206 if (FLAG_trace_contexts) { | |
207 PrintF("=> found in catch context\n"); | |
208 } | |
209 *index = Context::THROWN_OBJECT_INDEX; | |
210 *attributes = NONE; | |
211 *binding_flags = MUTABLE_IS_INITIALIZED; | |
212 return context; | |
213 } | |
214 } | 222 } |
215 | 223 |
216 // 3. Prepare to continue with the previous (next outermost) context. | 224 // Proceed with the previous context. |
217 if (context->IsGlobalContext()) { | 225 if (context->IsGlobalContext()) { |
218 follow_context_chain = false; | 226 follow_context_chain = false; |
219 } else { | 227 } else { |
220 context = Handle<Context>(context->previous(), isolate); | 228 context = Handle<Context>(context->previous(), isolate); |
221 } | 229 } |
222 } while (follow_context_chain); | 230 } while (follow_context_chain); |
223 | 231 |
224 if (FLAG_trace_contexts) { | 232 if (FLAG_trace_contexts) { |
225 PrintF("=> no property/slot found\n"); | 233 PrintF("=> no property/slot found\n"); |
226 } | 234 } |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
365 // During bootstrapping we allow all objects to pass as global | 373 // During bootstrapping we allow all objects to pass as global |
366 // objects. This is necessary to fix circular dependencies. | 374 // objects. This is necessary to fix circular dependencies. |
367 Isolate* isolate = Isolate::Current(); | 375 Isolate* isolate = Isolate::Current(); |
368 return isolate->heap()->gc_state() != Heap::NOT_IN_GC || | 376 return isolate->heap()->gc_state() != Heap::NOT_IN_GC || |
369 isolate->bootstrapper()->IsActive() || | 377 isolate->bootstrapper()->IsActive() || |
370 object->IsGlobalObject(); | 378 object->IsGlobalObject(); |
371 } | 379 } |
372 #endif | 380 #endif |
373 | 381 |
374 } } // namespace v8::internal | 382 } } // namespace v8::internal |
OLD | NEW |