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