Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame^] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
| 30 | #include "bootstrapper.h" |
| 31 | #include "debug.h" |
| 32 | #include "scopeinfo.h" |
| 33 | |
| 34 | namespace v8 { |
| 35 | namespace internal { |
| 36 | |
| 37 | JSBuiltinsObject* Context::builtins() { |
| 38 | GlobalObject* object = global(); |
| 39 | if (object->IsJSGlobalObject()) { |
| 40 | return JSGlobalObject::cast(object)->builtins(); |
| 41 | } else { |
| 42 | ASSERT(object->IsJSBuiltinsObject()); |
| 43 | return JSBuiltinsObject::cast(object); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | |
| 48 | Context* Context::global_context() { |
| 49 | // Fast case: the global object for this context has been set. In |
| 50 | // that case, the global object has a direct pointer to the global |
| 51 | // context. |
| 52 | if (global()->IsGlobalObject()) { |
| 53 | return global()->global_context(); |
| 54 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 55 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 56 | // During bootstrapping, the global object might not be set and we |
| 57 | // have to search the context chain to find the global context. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 58 | ASSERT(Isolate::Current()->bootstrapper()->IsActive()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 59 | Context* current = this; |
| 60 | while (!current->IsGlobalContext()) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 61 | JSFunction* closure = JSFunction::cast(current->closure()); |
| 62 | current = Context::cast(closure->context()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 63 | } |
| 64 | return current; |
| 65 | } |
| 66 | |
| 67 | |
| 68 | JSObject* Context::global_proxy() { |
| 69 | return global_context()->global_proxy_object(); |
| 70 | } |
| 71 | |
| 72 | void Context::set_global_proxy(JSObject* object) { |
| 73 | global_context()->set_global_proxy_object(object); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | Handle<Object> Context::Lookup(Handle<String> name, ContextLookupFlags flags, |
| 78 | int* index_, PropertyAttributes* attributes) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 79 | Isolate* isolate = GetIsolate(); |
| 80 | Handle<Context> context(this, isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 81 | |
| 82 | bool follow_context_chain = (flags & FOLLOW_CONTEXT_CHAIN) != 0; |
| 83 | *index_ = -1; |
| 84 | *attributes = ABSENT; |
| 85 | |
| 86 | if (FLAG_trace_contexts) { |
| 87 | PrintF("Context::Lookup("); |
| 88 | name->ShortPrint(); |
| 89 | PrintF(")\n"); |
| 90 | } |
| 91 | |
| 92 | do { |
| 93 | if (FLAG_trace_contexts) { |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 94 | PrintF(" - looking in context %p", reinterpret_cast<void*>(*context)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 95 | if (context->IsGlobalContext()) PrintF(" (global context)"); |
| 96 | PrintF("\n"); |
| 97 | } |
| 98 | |
| 99 | // check extension/with object |
| 100 | if (context->has_extension()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 101 | Handle<JSObject> extension = Handle<JSObject>(context->extension(), |
| 102 | isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 103 | // Context extension objects needs to behave as if they have no |
| 104 | // prototype. So even if we want to follow prototype chains, we |
| 105 | // need to only do a local lookup for context extension objects. |
| 106 | if ((flags & FOLLOW_PROTOTYPE_CHAIN) == 0 || |
| 107 | extension->IsJSContextExtensionObject()) { |
| 108 | *attributes = extension->GetLocalPropertyAttribute(*name); |
| 109 | } else { |
| 110 | *attributes = extension->GetPropertyAttribute(*name); |
| 111 | } |
| 112 | if (*attributes != ABSENT) { |
| 113 | // property found |
| 114 | if (FLAG_trace_contexts) { |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 115 | PrintF("=> found property in context object %p\n", |
| 116 | reinterpret_cast<void*>(*extension)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 117 | } |
| 118 | return extension; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | if (context->is_function_context()) { |
| 123 | // we have context-local slots |
| 124 | |
| 125 | // check non-parameter locals in context |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 126 | Handle<SerializedScopeInfo> scope_info( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 127 | context->closure()->shared()->scope_info(), isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 128 | Variable::Mode mode; |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 129 | int index = scope_info->ContextSlotIndex(*name, &mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 130 | ASSERT(index < 0 || index >= MIN_CONTEXT_SLOTS); |
| 131 | if (index >= 0) { |
| 132 | // slot found |
| 133 | if (FLAG_trace_contexts) { |
| 134 | PrintF("=> found local in context slot %d (mode = %d)\n", |
| 135 | index, mode); |
| 136 | } |
| 137 | *index_ = index; |
| 138 | // Note: Fixed context slots are statically allocated by the compiler. |
| 139 | // Statically allocated variables always have a statically known mode, |
| 140 | // which is the mode with which they were declared when added to the |
| 141 | // scope. Thus, the DYNAMIC mode (which corresponds to dynamically |
| 142 | // declared variables that were introduced through declaration nodes) |
| 143 | // must not appear here. |
| 144 | switch (mode) { |
| 145 | case Variable::INTERNAL: // fall through |
| 146 | case Variable::VAR: *attributes = NONE; break; |
| 147 | case Variable::CONST: *attributes = READ_ONLY; break; |
| 148 | case Variable::DYNAMIC: UNREACHABLE(); break; |
| 149 | case Variable::DYNAMIC_GLOBAL: UNREACHABLE(); break; |
| 150 | case Variable::DYNAMIC_LOCAL: UNREACHABLE(); break; |
| 151 | case Variable::TEMPORARY: UNREACHABLE(); break; |
| 152 | } |
| 153 | return context; |
| 154 | } |
| 155 | |
| 156 | // check parameter locals in context |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 157 | int param_index = scope_info->ParameterIndex(*name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 158 | if (param_index >= 0) { |
| 159 | // slot found. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 160 | int index = scope_info->ContextSlotIndex( |
| 161 | isolate->heap()->arguments_shadow_symbol(), NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 162 | ASSERT(index >= 0); // arguments must exist and be in the heap context |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 163 | Handle<JSObject> arguments(JSObject::cast(context->get(index)), |
| 164 | isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 165 | if (FLAG_trace_contexts) { |
| 166 | PrintF("=> found parameter %d in arguments object\n", param_index); |
| 167 | } |
| 168 | *index_ = param_index; |
| 169 | *attributes = NONE; |
| 170 | return arguments; |
| 171 | } |
| 172 | |
| 173 | // check intermediate context (holding only the function name variable) |
| 174 | if (follow_context_chain) { |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 175 | int index = scope_info->FunctionContextSlotIndex(*name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 176 | if (index >= 0) { |
| 177 | // slot found |
| 178 | if (FLAG_trace_contexts) { |
| 179 | PrintF("=> found intermediate function in context slot %d\n", |
| 180 | index); |
| 181 | } |
| 182 | *index_ = index; |
| 183 | *attributes = READ_ONLY; |
| 184 | return context; |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | // proceed with enclosing context |
| 190 | if (context->IsGlobalContext()) { |
| 191 | follow_context_chain = false; |
| 192 | } else if (context->is_function_context()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 193 | context = Handle<Context>(Context::cast(context->closure()->context()), |
| 194 | isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 195 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 196 | context = Handle<Context>(context->previous(), isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 197 | } |
| 198 | } while (follow_context_chain); |
| 199 | |
| 200 | // slot not found |
| 201 | if (FLAG_trace_contexts) { |
| 202 | PrintF("=> no property/slot found\n"); |
| 203 | } |
| 204 | return Handle<Object>::null(); |
| 205 | } |
| 206 | |
| 207 | |
| 208 | bool Context::GlobalIfNotShadowedByEval(Handle<String> name) { |
| 209 | Context* context = this; |
| 210 | |
| 211 | // Check that there is no local with the given name in contexts |
| 212 | // before the global context and check that there are no context |
| 213 | // extension objects (conservative check for with statements). |
| 214 | while (!context->IsGlobalContext()) { |
| 215 | // Check if the context is a potentially a with context. |
| 216 | if (context->has_extension()) return false; |
| 217 | |
| 218 | // Not a with context so it must be a function context. |
| 219 | ASSERT(context->is_function_context()); |
| 220 | |
| 221 | // Check non-parameter locals. |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 222 | Handle<SerializedScopeInfo> scope_info( |
| 223 | context->closure()->shared()->scope_info()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 224 | Variable::Mode mode; |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 225 | int index = scope_info->ContextSlotIndex(*name, &mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 226 | ASSERT(index < 0 || index >= MIN_CONTEXT_SLOTS); |
| 227 | if (index >= 0) return false; |
| 228 | |
| 229 | // Check parameter locals. |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 230 | int param_index = scope_info->ParameterIndex(*name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 231 | if (param_index >= 0) return false; |
| 232 | |
| 233 | // Check context only holding the function name variable. |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 234 | index = scope_info->FunctionContextSlotIndex(*name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 235 | if (index >= 0) return false; |
| 236 | context = Context::cast(context->closure()->context()); |
| 237 | } |
| 238 | |
| 239 | // No local or potential with statement found so the variable is |
| 240 | // global unless it is shadowed by an eval-introduced variable. |
| 241 | return true; |
| 242 | } |
| 243 | |
| 244 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame^] | 245 | void Context::ComputeEvalScopeInfo(bool* outer_scope_calls_eval, |
| 246 | bool* outer_scope_calls_non_strict_eval) { |
| 247 | Context* context = this; |
| 248 | while (true) { |
| 249 | Handle<SerializedScopeInfo> scope_info( |
| 250 | context->closure()->shared()->scope_info()); |
| 251 | if (scope_info->CallsEval()) { |
| 252 | *outer_scope_calls_eval = true; |
| 253 | if (!scope_info->IsStrictMode()) { |
| 254 | // No need to go further since the answers will not change |
| 255 | // from here. |
| 256 | *outer_scope_calls_non_strict_eval = true; |
| 257 | return; |
| 258 | } |
| 259 | } |
| 260 | if (context->IsGlobalContext()) break; |
| 261 | context = Context::cast(context->closure()->context()); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 266 | void Context::AddOptimizedFunction(JSFunction* function) { |
| 267 | ASSERT(IsGlobalContext()); |
| 268 | #ifdef DEBUG |
| 269 | Object* element = get(OPTIMIZED_FUNCTIONS_LIST); |
| 270 | while (!element->IsUndefined()) { |
| 271 | CHECK(element != function); |
| 272 | element = JSFunction::cast(element)->next_function_link(); |
| 273 | } |
| 274 | |
| 275 | CHECK(function->next_function_link()->IsUndefined()); |
| 276 | |
| 277 | // Check that the context belongs to the weak global contexts list. |
| 278 | bool found = false; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 279 | Object* context = GetHeap()->global_contexts_list(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 280 | while (!context->IsUndefined()) { |
| 281 | if (context == this) { |
| 282 | found = true; |
| 283 | break; |
| 284 | } |
| 285 | context = Context::cast(context)->get(Context::NEXT_CONTEXT_LINK); |
| 286 | } |
| 287 | CHECK(found); |
| 288 | #endif |
| 289 | function->set_next_function_link(get(OPTIMIZED_FUNCTIONS_LIST)); |
| 290 | set(OPTIMIZED_FUNCTIONS_LIST, function); |
| 291 | } |
| 292 | |
| 293 | |
| 294 | void Context::RemoveOptimizedFunction(JSFunction* function) { |
| 295 | ASSERT(IsGlobalContext()); |
| 296 | Object* element = get(OPTIMIZED_FUNCTIONS_LIST); |
| 297 | JSFunction* prev = NULL; |
| 298 | while (!element->IsUndefined()) { |
| 299 | JSFunction* element_function = JSFunction::cast(element); |
| 300 | ASSERT(element_function->next_function_link()->IsUndefined() || |
| 301 | element_function->next_function_link()->IsJSFunction()); |
| 302 | if (element_function == function) { |
| 303 | if (prev == NULL) { |
| 304 | set(OPTIMIZED_FUNCTIONS_LIST, element_function->next_function_link()); |
| 305 | } else { |
| 306 | prev->set_next_function_link(element_function->next_function_link()); |
| 307 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 308 | element_function->set_next_function_link(GetHeap()->undefined_value()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 309 | return; |
| 310 | } |
| 311 | prev = element_function; |
| 312 | element = element_function->next_function_link(); |
| 313 | } |
| 314 | UNREACHABLE(); |
| 315 | } |
| 316 | |
| 317 | |
| 318 | Object* Context::OptimizedFunctionsListHead() { |
| 319 | ASSERT(IsGlobalContext()); |
| 320 | return get(OPTIMIZED_FUNCTIONS_LIST); |
| 321 | } |
| 322 | |
| 323 | |
| 324 | void Context::ClearOptimizedFunctions() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 325 | set(OPTIMIZED_FUNCTIONS_LIST, GetHeap()->undefined_value()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 329 | #ifdef DEBUG |
| 330 | bool Context::IsBootstrappingOrContext(Object* object) { |
| 331 | // During bootstrapping we allow all objects to pass as |
| 332 | // contexts. This is necessary to fix circular dependencies. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 333 | return Isolate::Current()->bootstrapper()->IsActive() || object->IsContext(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | |
| 337 | bool Context::IsBootstrappingOrGlobalObject(Object* object) { |
| 338 | // During bootstrapping we allow all objects to pass as global |
| 339 | // objects. This is necessary to fix circular dependencies. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 340 | Isolate* isolate = Isolate::Current(); |
| 341 | return isolate->heap()->gc_state() != Heap::NOT_IN_GC || |
| 342 | isolate->bootstrapper()->IsActive() || |
| 343 | object->IsGlobalObject(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 344 | } |
| 345 | #endif |
| 346 | |
| 347 | } } // namespace v8::internal |