ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +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 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 30 | #include "bootstrapper.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 31 | #include "debug.h" |
| 32 | #include "scopeinfo.h" |
| 33 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 34 | namespace v8 { |
| 35 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 36 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | } |
kasperl@chromium.org | 7b9eafd | 2009-12-21 15:20:30 +0000 | [diff] [blame] | 55 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +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. |
kasperl@chromium.org | 7b9eafd | 2009-12-21 15:20:30 +0000 | [diff] [blame] | 58 | ASSERT(Bootstrapper::IsActive()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 59 | Context* current = this; |
| 60 | while (!current->IsGlobalContext()) { |
kasperl@chromium.org | 7b9eafd | 2009-12-21 15:20:30 +0000 | [diff] [blame] | 61 | JSFunction* closure = JSFunction::cast(current->closure()); |
| 62 | current = Context::cast(closure->context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 63 | } |
| 64 | return current; |
| 65 | } |
| 66 | |
| 67 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 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 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 77 | Handle<Object> Context::Lookup(Handle<String> name, ContextLookupFlags flags, |
| 78 | int* index_, PropertyAttributes* attributes) { |
| 79 | Handle<Context> context(this); |
| 80 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 81 | bool follow_context_chain = (flags & FOLLOW_CONTEXT_CHAIN) != 0; |
| 82 | *index_ = -1; |
| 83 | *attributes = ABSENT; |
| 84 | |
| 85 | if (FLAG_trace_contexts) { |
| 86 | PrintF("Context::Lookup("); |
| 87 | name->ShortPrint(); |
| 88 | PrintF(")\n"); |
| 89 | } |
| 90 | |
| 91 | do { |
| 92 | if (FLAG_trace_contexts) { |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 93 | PrintF(" - looking in context %p", reinterpret_cast<void*>(*context)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 94 | if (context->IsGlobalContext()) PrintF(" (global context)"); |
| 95 | PrintF("\n"); |
| 96 | } |
| 97 | |
| 98 | // check extension/with object |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 99 | if (context->has_extension()) { |
| 100 | Handle<JSObject> extension = Handle<JSObject>(context->extension()); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 101 | // Context extension objects needs to behave as if they have no |
| 102 | // prototype. So even if we want to follow prototype chains, we |
| 103 | // need to only do a local lookup for context extension objects. |
| 104 | if ((flags & FOLLOW_PROTOTYPE_CHAIN) == 0 || |
| 105 | extension->IsJSContextExtensionObject()) { |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 106 | *attributes = extension->GetLocalPropertyAttribute(*name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 107 | } else { |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 108 | *attributes = extension->GetPropertyAttribute(*name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 109 | } |
| 110 | if (*attributes != ABSENT) { |
| 111 | // property found |
| 112 | if (FLAG_trace_contexts) { |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 113 | PrintF("=> found property in context object %p\n", |
| 114 | reinterpret_cast<void*>(*extension)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 115 | } |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 116 | return extension; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | if (context->is_function_context()) { |
| 121 | // we have context-local slots |
| 122 | |
| 123 | // check non-parameter locals in context |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 124 | Handle<SerializedScopeInfo> scope_info( |
| 125 | context->closure()->shared()->scope_info()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 126 | Variable::Mode mode; |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 127 | int index = scope_info->ContextSlotIndex(*name, &mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 128 | ASSERT(index < 0 || index >= MIN_CONTEXT_SLOTS); |
| 129 | if (index >= 0) { |
| 130 | // slot found |
| 131 | if (FLAG_trace_contexts) { |
| 132 | PrintF("=> found local in context slot %d (mode = %d)\n", |
| 133 | index, mode); |
| 134 | } |
| 135 | *index_ = index; |
| 136 | // Note: Fixed context slots are statically allocated by the compiler. |
| 137 | // Statically allocated variables always have a statically known mode, |
| 138 | // which is the mode with which they were declared when added to the |
| 139 | // scope. Thus, the DYNAMIC mode (which corresponds to dynamically |
| 140 | // declared variables that were introduced through declaration nodes) |
| 141 | // must not appear here. |
| 142 | switch (mode) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 143 | case Variable::INTERNAL: // fall through |
| 144 | case Variable::VAR: *attributes = NONE; break; |
| 145 | case Variable::CONST: *attributes = READ_ONLY; break; |
| 146 | case Variable::DYNAMIC: UNREACHABLE(); break; |
| 147 | case Variable::DYNAMIC_GLOBAL: UNREACHABLE(); break; |
| 148 | case Variable::DYNAMIC_LOCAL: UNREACHABLE(); break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 149 | case Variable::TEMPORARY: UNREACHABLE(); break; |
| 150 | } |
| 151 | return context; |
| 152 | } |
| 153 | |
| 154 | // check parameter locals in context |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 155 | int param_index = scope_info->ParameterIndex(*name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 156 | if (param_index >= 0) { |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 157 | // slot found. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 158 | int index = |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 159 | scope_info->ContextSlotIndex(Heap::arguments_shadow_symbol(), NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 160 | ASSERT(index >= 0); // arguments must exist and be in the heap context |
| 161 | Handle<JSObject> arguments(JSObject::cast(context->get(index))); |
| 162 | ASSERT(arguments->HasLocalProperty(Heap::length_symbol())); |
| 163 | if (FLAG_trace_contexts) { |
| 164 | PrintF("=> found parameter %d in arguments object\n", param_index); |
| 165 | } |
| 166 | *index_ = param_index; |
| 167 | *attributes = NONE; |
| 168 | return arguments; |
| 169 | } |
| 170 | |
| 171 | // check intermediate context (holding only the function name variable) |
| 172 | if (follow_context_chain) { |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 173 | int index = scope_info->FunctionContextSlotIndex(*name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 174 | if (index >= 0) { |
| 175 | // slot found |
| 176 | if (FLAG_trace_contexts) { |
| 177 | PrintF("=> found intermediate function in context slot %d\n", |
| 178 | index); |
| 179 | } |
| 180 | *index_ = index; |
| 181 | *attributes = READ_ONLY; |
| 182 | return context; |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | // proceed with enclosing context |
| 188 | if (context->IsGlobalContext()) { |
| 189 | follow_context_chain = false; |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 190 | } else if (context->is_function_context()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 191 | context = Handle<Context>(Context::cast(context->closure()->context())); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 192 | } else { |
| 193 | context = Handle<Context>(context->previous()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 194 | } |
| 195 | } while (follow_context_chain); |
| 196 | |
| 197 | // slot not found |
| 198 | if (FLAG_trace_contexts) { |
| 199 | PrintF("=> no property/slot found\n"); |
| 200 | } |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 201 | return Handle<Object>::null(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 205 | bool Context::GlobalIfNotShadowedByEval(Handle<String> name) { |
| 206 | Context* context = this; |
| 207 | |
| 208 | // Check that there is no local with the given name in contexts |
| 209 | // before the global context and check that there are no context |
| 210 | // extension objects (conservative check for with statements). |
| 211 | while (!context->IsGlobalContext()) { |
| 212 | // Check if the context is a potentially a with context. |
| 213 | if (context->has_extension()) return false; |
| 214 | |
| 215 | // Not a with context so it must be a function context. |
| 216 | ASSERT(context->is_function_context()); |
| 217 | |
| 218 | // Check non-parameter locals. |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 219 | Handle<SerializedScopeInfo> scope_info( |
| 220 | context->closure()->shared()->scope_info()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 221 | Variable::Mode mode; |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 222 | int index = scope_info->ContextSlotIndex(*name, &mode); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 223 | ASSERT(index < 0 || index >= MIN_CONTEXT_SLOTS); |
| 224 | if (index >= 0) return false; |
| 225 | |
| 226 | // Check parameter locals. |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 227 | int param_index = scope_info->ParameterIndex(*name); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 228 | if (param_index >= 0) return false; |
| 229 | |
| 230 | // Check context only holding the function name variable. |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 231 | index = scope_info->FunctionContextSlotIndex(*name); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 232 | if (index >= 0) return false; |
| 233 | context = Context::cast(context->closure()->context()); |
| 234 | } |
| 235 | |
| 236 | // No local or potential with statement found so the variable is |
| 237 | // global unless it is shadowed by an eval-introduced variable. |
| 238 | return true; |
| 239 | } |
| 240 | |
| 241 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 242 | #ifdef DEBUG |
| 243 | bool Context::IsBootstrappingOrContext(Object* object) { |
| 244 | // During bootstrapping we allow all objects to pass as |
| 245 | // contexts. This is necessary to fix circular dependencies. |
| 246 | return Bootstrapper::IsActive() || object->IsContext(); |
| 247 | } |
| 248 | |
| 249 | |
| 250 | bool Context::IsBootstrappingOrGlobalObject(Object* object) { |
| 251 | // During bootstrapping we allow all objects to pass as global |
| 252 | // objects. This is necessary to fix circular dependencies. |
| 253 | return Bootstrapper::IsActive() || object->IsGlobalObject(); |
| 254 | } |
| 255 | #endif |
| 256 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 257 | } } // namespace v8::internal |