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 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 37 | Context* Context::declaration_context() { |
| 38 | Context* current = this; |
| 39 | while (!current->IsFunctionContext() && !current->IsGlobalContext()) { |
| 40 | current = current->previous(); |
| 41 | ASSERT(current->closure() == closure()); |
| 42 | } |
| 43 | return current; |
| 44 | } |
| 45 | |
| 46 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 47 | JSBuiltinsObject* Context::builtins() { |
| 48 | GlobalObject* object = global(); |
| 49 | if (object->IsJSGlobalObject()) { |
| 50 | return JSGlobalObject::cast(object)->builtins(); |
| 51 | } else { |
| 52 | ASSERT(object->IsJSBuiltinsObject()); |
| 53 | return JSBuiltinsObject::cast(object); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | |
| 58 | Context* Context::global_context() { |
| 59 | // Fast case: the global object for this context has been set. In |
| 60 | // that case, the global object has a direct pointer to the global |
| 61 | // context. |
| 62 | if (global()->IsGlobalObject()) { |
| 63 | return global()->global_context(); |
| 64 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 65 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 66 | // During bootstrapping, the global object might not be set and we |
| 67 | // have to search the context chain to find the global context. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 68 | ASSERT(Isolate::Current()->bootstrapper()->IsActive()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 69 | Context* current = this; |
| 70 | while (!current->IsGlobalContext()) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 71 | JSFunction* closure = JSFunction::cast(current->closure()); |
| 72 | current = Context::cast(closure->context()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 73 | } |
| 74 | return current; |
| 75 | } |
| 76 | |
| 77 | |
| 78 | JSObject* Context::global_proxy() { |
| 79 | return global_context()->global_proxy_object(); |
| 80 | } |
| 81 | |
| 82 | void Context::set_global_proxy(JSObject* object) { |
| 83 | global_context()->set_global_proxy_object(object); |
| 84 | } |
| 85 | |
| 86 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 87 | Handle<Object> Context::Lookup(Handle<String> name, |
| 88 | ContextLookupFlags flags, |
| 89 | int* index_, |
| 90 | PropertyAttributes* attributes) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 91 | Isolate* isolate = GetIsolate(); |
| 92 | Handle<Context> context(this, isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 93 | |
| 94 | bool follow_context_chain = (flags & FOLLOW_CONTEXT_CHAIN) != 0; |
| 95 | *index_ = -1; |
| 96 | *attributes = ABSENT; |
| 97 | |
| 98 | if (FLAG_trace_contexts) { |
| 99 | PrintF("Context::Lookup("); |
| 100 | name->ShortPrint(); |
| 101 | PrintF(")\n"); |
| 102 | } |
| 103 | |
| 104 | do { |
| 105 | if (FLAG_trace_contexts) { |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 106 | PrintF(" - looking in context %p", reinterpret_cast<void*>(*context)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 107 | if (context->IsGlobalContext()) PrintF(" (global context)"); |
| 108 | PrintF("\n"); |
| 109 | } |
| 110 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 111 | // Check extension/with/global object. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 112 | if (context->has_extension()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 113 | if (context->IsCatchContext()) { |
| 114 | // Catch contexts have the variable name in the extension slot. |
| 115 | if (name->Equals(String::cast(context->extension()))) { |
| 116 | if (FLAG_trace_contexts) { |
| 117 | PrintF("=> found in catch context\n"); |
| 118 | } |
| 119 | *index_ = Context::THROWN_OBJECT_INDEX; |
| 120 | *attributes = NONE; |
| 121 | return context; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 122 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 123 | } else { |
| 124 | // Global, function, and with contexts may have an object in the |
| 125 | // extension slot. |
| 126 | Handle<JSObject> extension(JSObject::cast(context->extension()), |
| 127 | isolate); |
| 128 | // Context extension objects needs to behave as if they have no |
| 129 | // prototype. So even if we want to follow prototype chains, we |
| 130 | // need to only do a local lookup for context extension objects. |
| 131 | if ((flags & FOLLOW_PROTOTYPE_CHAIN) == 0 || |
| 132 | extension->IsJSContextExtensionObject()) { |
| 133 | *attributes = extension->GetLocalPropertyAttribute(*name); |
| 134 | } else { |
| 135 | *attributes = extension->GetPropertyAttribute(*name); |
| 136 | } |
| 137 | if (*attributes != ABSENT) { |
| 138 | // property found |
| 139 | if (FLAG_trace_contexts) { |
| 140 | PrintF("=> found property in context object %p\n", |
| 141 | reinterpret_cast<void*>(*extension)); |
| 142 | } |
| 143 | return extension; |
| 144 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 148 | // Only functions can have locals, parameters, and a function name. |
| 149 | if (context->IsFunctionContext()) { |
| 150 | // We may have context-local slots. Check locals in the context. |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 151 | Handle<SerializedScopeInfo> scope_info( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 152 | context->closure()->shared()->scope_info(), isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 153 | Variable::Mode mode; |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 154 | int index = scope_info->ContextSlotIndex(*name, &mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 155 | ASSERT(index < 0 || index >= MIN_CONTEXT_SLOTS); |
| 156 | if (index >= 0) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 157 | if (FLAG_trace_contexts) { |
| 158 | PrintF("=> found local in context slot %d (mode = %d)\n", |
| 159 | index, mode); |
| 160 | } |
| 161 | *index_ = index; |
| 162 | // Note: Fixed context slots are statically allocated by the compiler. |
| 163 | // Statically allocated variables always have a statically known mode, |
| 164 | // which is the mode with which they were declared when added to the |
| 165 | // scope. Thus, the DYNAMIC mode (which corresponds to dynamically |
| 166 | // declared variables that were introduced through declaration nodes) |
| 167 | // must not appear here. |
| 168 | switch (mode) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 169 | case Variable::INTERNAL: // Fall through. |
| 170 | case Variable::VAR: |
| 171 | *attributes = NONE; |
| 172 | break; |
| 173 | case Variable::CONST: |
| 174 | *attributes = READ_ONLY; |
| 175 | break; |
| 176 | case Variable::DYNAMIC: |
| 177 | case Variable::DYNAMIC_GLOBAL: |
| 178 | case Variable::DYNAMIC_LOCAL: |
| 179 | case Variable::TEMPORARY: |
| 180 | UNREACHABLE(); |
| 181 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 182 | } |
| 183 | return context; |
| 184 | } |
| 185 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 186 | // Check the slot corresponding to the intermediate context holding |
| 187 | // only the function name variable. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 188 | if (follow_context_chain) { |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 189 | int index = scope_info->FunctionContextSlotIndex(*name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 190 | if (index >= 0) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 191 | if (FLAG_trace_contexts) { |
| 192 | PrintF("=> found intermediate function in context slot %d\n", |
| 193 | index); |
| 194 | } |
| 195 | *index_ = index; |
| 196 | *attributes = READ_ONLY; |
| 197 | return context; |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 202 | // Proceed with the previous context. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 203 | if (context->IsGlobalContext()) { |
| 204 | follow_context_chain = false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 205 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 206 | context = Handle<Context>(context->previous(), isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 207 | } |
| 208 | } while (follow_context_chain); |
| 209 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 210 | if (FLAG_trace_contexts) { |
| 211 | PrintF("=> no property/slot found\n"); |
| 212 | } |
| 213 | return Handle<Object>::null(); |
| 214 | } |
| 215 | |
| 216 | |
| 217 | bool Context::GlobalIfNotShadowedByEval(Handle<String> name) { |
| 218 | Context* context = this; |
| 219 | |
| 220 | // Check that there is no local with the given name in contexts |
| 221 | // before the global context and check that there are no context |
| 222 | // extension objects (conservative check for with statements). |
| 223 | while (!context->IsGlobalContext()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 224 | // Check if the context is a catch or with context, or has introduced |
| 225 | // bindings by calling non-strict eval. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 226 | if (context->has_extension()) return false; |
| 227 | |
| 228 | // Not a with context so it must be a function context. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 229 | ASSERT(context->IsFunctionContext()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 230 | |
| 231 | // Check non-parameter locals. |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 232 | Handle<SerializedScopeInfo> scope_info( |
| 233 | context->closure()->shared()->scope_info()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 234 | Variable::Mode mode; |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 235 | int index = scope_info->ContextSlotIndex(*name, &mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 236 | ASSERT(index < 0 || index >= MIN_CONTEXT_SLOTS); |
| 237 | if (index >= 0) return false; |
| 238 | |
| 239 | // Check parameter locals. |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 240 | int param_index = scope_info->ParameterIndex(*name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 241 | if (param_index >= 0) return false; |
| 242 | |
| 243 | // Check context only holding the function name variable. |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 244 | index = scope_info->FunctionContextSlotIndex(*name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 245 | if (index >= 0) return false; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 246 | context = context->previous(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | // No local or potential with statement found so the variable is |
| 250 | // global unless it is shadowed by an eval-introduced variable. |
| 251 | return true; |
| 252 | } |
| 253 | |
| 254 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 255 | void Context::ComputeEvalScopeInfo(bool* outer_scope_calls_eval, |
| 256 | bool* outer_scope_calls_non_strict_eval) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 257 | // Skip up the context chain checking all the function contexts to see |
| 258 | // whether they call eval. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 259 | Context* context = this; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 260 | while (!context->IsGlobalContext()) { |
| 261 | if (context->IsFunctionContext()) { |
| 262 | Handle<SerializedScopeInfo> scope_info( |
| 263 | context->closure()->shared()->scope_info()); |
| 264 | if (scope_info->CallsEval()) { |
| 265 | *outer_scope_calls_eval = true; |
| 266 | if (!scope_info->IsStrictMode()) { |
| 267 | // No need to go further since the answers will not change from |
| 268 | // here. |
| 269 | *outer_scope_calls_non_strict_eval = true; |
| 270 | return; |
| 271 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 272 | } |
| 273 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 274 | context = context->previous(); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | |
| 278 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 279 | void Context::AddOptimizedFunction(JSFunction* function) { |
| 280 | ASSERT(IsGlobalContext()); |
| 281 | #ifdef DEBUG |
| 282 | Object* element = get(OPTIMIZED_FUNCTIONS_LIST); |
| 283 | while (!element->IsUndefined()) { |
| 284 | CHECK(element != function); |
| 285 | element = JSFunction::cast(element)->next_function_link(); |
| 286 | } |
| 287 | |
| 288 | CHECK(function->next_function_link()->IsUndefined()); |
| 289 | |
| 290 | // Check that the context belongs to the weak global contexts list. |
| 291 | bool found = false; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 292 | Object* context = GetHeap()->global_contexts_list(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 293 | while (!context->IsUndefined()) { |
| 294 | if (context == this) { |
| 295 | found = true; |
| 296 | break; |
| 297 | } |
| 298 | context = Context::cast(context)->get(Context::NEXT_CONTEXT_LINK); |
| 299 | } |
| 300 | CHECK(found); |
| 301 | #endif |
| 302 | function->set_next_function_link(get(OPTIMIZED_FUNCTIONS_LIST)); |
| 303 | set(OPTIMIZED_FUNCTIONS_LIST, function); |
| 304 | } |
| 305 | |
| 306 | |
| 307 | void Context::RemoveOptimizedFunction(JSFunction* function) { |
| 308 | ASSERT(IsGlobalContext()); |
| 309 | Object* element = get(OPTIMIZED_FUNCTIONS_LIST); |
| 310 | JSFunction* prev = NULL; |
| 311 | while (!element->IsUndefined()) { |
| 312 | JSFunction* element_function = JSFunction::cast(element); |
| 313 | ASSERT(element_function->next_function_link()->IsUndefined() || |
| 314 | element_function->next_function_link()->IsJSFunction()); |
| 315 | if (element_function == function) { |
| 316 | if (prev == NULL) { |
| 317 | set(OPTIMIZED_FUNCTIONS_LIST, element_function->next_function_link()); |
| 318 | } else { |
| 319 | prev->set_next_function_link(element_function->next_function_link()); |
| 320 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 321 | element_function->set_next_function_link(GetHeap()->undefined_value()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 322 | return; |
| 323 | } |
| 324 | prev = element_function; |
| 325 | element = element_function->next_function_link(); |
| 326 | } |
| 327 | UNREACHABLE(); |
| 328 | } |
| 329 | |
| 330 | |
| 331 | Object* Context::OptimizedFunctionsListHead() { |
| 332 | ASSERT(IsGlobalContext()); |
| 333 | return get(OPTIMIZED_FUNCTIONS_LIST); |
| 334 | } |
| 335 | |
| 336 | |
| 337 | void Context::ClearOptimizedFunctions() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 338 | set(OPTIMIZED_FUNCTIONS_LIST, GetHeap()->undefined_value()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 342 | #ifdef DEBUG |
| 343 | bool Context::IsBootstrappingOrContext(Object* object) { |
| 344 | // During bootstrapping we allow all objects to pass as |
| 345 | // contexts. This is necessary to fix circular dependencies. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 346 | return Isolate::Current()->bootstrapper()->IsActive() || object->IsContext(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | |
| 350 | bool Context::IsBootstrappingOrGlobalObject(Object* object) { |
| 351 | // During bootstrapping we allow all objects to pass as global |
| 352 | // objects. This is necessary to fix circular dependencies. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 353 | Isolate* isolate = Isolate::Current(); |
| 354 | return isolate->heap()->gc_state() != Heap::NOT_IN_GC || |
| 355 | isolate->bootstrapper()->IsActive() || |
| 356 | object->IsGlobalObject(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 357 | } |
| 358 | #endif |
| 359 | |
| 360 | } } // namespace v8::internal |