Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5 | #include "src/v8.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7 | #include "src/bootstrapper.h" |
| 8 | #include "src/debug.h" |
| 9 | #include "src/scopeinfo.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10 | |
| 11 | namespace v8 { |
| 12 | namespace internal { |
| 13 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 14 | |
| 15 | Handle<ScriptContextTable> ScriptContextTable::Extend( |
| 16 | Handle<ScriptContextTable> table, Handle<Context> script_context) { |
| 17 | Handle<ScriptContextTable> result; |
| 18 | int used = table->used(); |
| 19 | int length = table->length(); |
| 20 | CHECK(used >= 0 && length > 0 && used < length); |
| 21 | if (used + 1 == length) { |
| 22 | CHECK(length < Smi::kMaxValue / 2); |
| 23 | result = Handle<ScriptContextTable>::cast( |
| 24 | FixedArray::CopySize(table, length * 2)); |
| 25 | } else { |
| 26 | result = table; |
| 27 | } |
| 28 | result->set_used(used + 1); |
| 29 | |
| 30 | DCHECK(script_context->IsScriptContext()); |
| 31 | result->set(used + 1, *script_context); |
| 32 | return result; |
| 33 | } |
| 34 | |
| 35 | |
| 36 | bool ScriptContextTable::Lookup(Handle<ScriptContextTable> table, |
| 37 | Handle<String> name, LookupResult* result) { |
| 38 | for (int i = 0; i < table->used(); i++) { |
| 39 | Handle<Context> context = GetContext(table, i); |
| 40 | DCHECK(context->IsScriptContext()); |
| 41 | Handle<ScopeInfo> scope_info(ScopeInfo::cast(context->extension())); |
| 42 | int slot_index = ScopeInfo::ContextSlotIndex( |
| 43 | scope_info, name, &result->mode, &result->init_flag, |
| 44 | &result->maybe_assigned_flag); |
| 45 | |
| 46 | if (slot_index >= 0) { |
| 47 | result->context_index = i; |
| 48 | result->slot_index = slot_index; |
| 49 | return true; |
| 50 | } |
| 51 | } |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 56 | Context* Context::declaration_context() { |
| 57 | Context* current = this; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 58 | while (!current->IsFunctionContext() && !current->IsNativeContext() && |
| 59 | !current->IsScriptContext()) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 60 | current = current->previous(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 61 | DCHECK(current->closure() == closure()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 62 | } |
| 63 | return current; |
| 64 | } |
| 65 | |
| 66 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 67 | JSBuiltinsObject* Context::builtins() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 68 | GlobalObject* object = global_object(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 69 | if (object->IsJSGlobalObject()) { |
| 70 | return JSGlobalObject::cast(object)->builtins(); |
| 71 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 72 | DCHECK(object->IsJSBuiltinsObject()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 73 | return JSBuiltinsObject::cast(object); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 78 | Context* Context::script_context() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 79 | Context* current = this; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 80 | while (!current->IsScriptContext()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 81 | current = current->previous(); |
| 82 | } |
| 83 | return current; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | Context* Context::native_context() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 88 | // Fast case: the global object for this context has been set. In |
| 89 | // that case, the global object has a direct pointer to the global |
| 90 | // context. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 91 | if (global_object()->IsGlobalObject()) { |
| 92 | return global_object()->native_context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 93 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 94 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 95 | // During bootstrapping, the global object might not be set and we |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 96 | // have to search the context chain to find the native context. |
| 97 | DCHECK(this->GetIsolate()->bootstrapper()->IsActive()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 98 | Context* current = this; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 99 | while (!current->IsNativeContext()) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 100 | JSFunction* closure = JSFunction::cast(current->closure()); |
| 101 | current = Context::cast(closure->context()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 102 | } |
| 103 | return current; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | JSObject* Context::global_proxy() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 108 | return native_context()->global_proxy_object(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 111 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 112 | void Context::set_global_proxy(JSObject* object) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 113 | native_context()->set_global_proxy_object(object); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | /** |
| 118 | * Lookups a property in an object environment, taking the unscopables into |
| 119 | * account. This is used For HasBinding spec algorithms for ObjectEnvironment. |
| 120 | */ |
| 121 | static Maybe<PropertyAttributes> UnscopableLookup(LookupIterator* it) { |
| 122 | Isolate* isolate = it->isolate(); |
| 123 | |
| 124 | Maybe<PropertyAttributes> attrs = JSReceiver::GetPropertyAttributes(it); |
| 125 | DCHECK(attrs.has_value || isolate->has_pending_exception()); |
| 126 | if (!attrs.has_value || attrs.value == ABSENT) return attrs; |
| 127 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 128 | Handle<Symbol> unscopables_symbol = isolate->factory()->unscopables_symbol(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 129 | Handle<Object> receiver = it->GetReceiver(); |
| 130 | Handle<Object> unscopables; |
| 131 | MaybeHandle<Object> maybe_unscopables = |
| 132 | Object::GetProperty(receiver, unscopables_symbol); |
| 133 | if (!maybe_unscopables.ToHandle(&unscopables)) { |
| 134 | return Maybe<PropertyAttributes>(); |
| 135 | } |
| 136 | if (!unscopables->IsSpecObject()) return attrs; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 137 | Handle<Object> blacklist; |
| 138 | MaybeHandle<Object> maybe_blacklist = |
| 139 | Object::GetProperty(unscopables, it->name()); |
| 140 | if (!maybe_blacklist.ToHandle(&blacklist)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 141 | DCHECK(isolate->has_pending_exception()); |
| 142 | return Maybe<PropertyAttributes>(); |
| 143 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 144 | if (!blacklist->IsUndefined()) return maybe(ABSENT); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 145 | return attrs; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 148 | static void GetAttributesAndBindingFlags(VariableMode mode, |
| 149 | InitializationFlag init_flag, |
| 150 | PropertyAttributes* attributes, |
| 151 | BindingFlags* binding_flags) { |
| 152 | switch (mode) { |
| 153 | case INTERNAL: // Fall through. |
| 154 | case VAR: |
| 155 | *attributes = NONE; |
| 156 | *binding_flags = MUTABLE_IS_INITIALIZED; |
| 157 | break; |
| 158 | case LET: |
| 159 | *attributes = NONE; |
| 160 | *binding_flags = (init_flag == kNeedsInitialization) |
| 161 | ? MUTABLE_CHECK_INITIALIZED |
| 162 | : MUTABLE_IS_INITIALIZED; |
| 163 | break; |
| 164 | case CONST_LEGACY: |
| 165 | *attributes = READ_ONLY; |
| 166 | *binding_flags = (init_flag == kNeedsInitialization) |
| 167 | ? IMMUTABLE_CHECK_INITIALIZED |
| 168 | : IMMUTABLE_IS_INITIALIZED; |
| 169 | break; |
| 170 | case CONST: |
| 171 | *attributes = READ_ONLY; |
| 172 | *binding_flags = (init_flag == kNeedsInitialization) |
| 173 | ? IMMUTABLE_CHECK_INITIALIZED_HARMONY |
| 174 | : IMMUTABLE_IS_INITIALIZED_HARMONY; |
| 175 | break; |
| 176 | case MODULE: |
| 177 | *attributes = READ_ONLY; |
| 178 | *binding_flags = IMMUTABLE_IS_INITIALIZED_HARMONY; |
| 179 | break; |
| 180 | case DYNAMIC: |
| 181 | case DYNAMIC_GLOBAL: |
| 182 | case DYNAMIC_LOCAL: |
| 183 | case TEMPORARY: |
| 184 | // Note: Fixed context slots are statically allocated by the compiler. |
| 185 | // Statically allocated variables always have a statically known mode, |
| 186 | // which is the mode with which they were declared when added to the |
| 187 | // scope. Thus, the DYNAMIC mode (which corresponds to dynamically |
| 188 | // declared variables that were introduced through declaration nodes) |
| 189 | // must not appear here. |
| 190 | UNREACHABLE(); |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 195 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 196 | Handle<Object> Context::Lookup(Handle<String> name, |
| 197 | ContextLookupFlags flags, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 198 | int* index, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 199 | PropertyAttributes* attributes, |
| 200 | BindingFlags* binding_flags) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 201 | Isolate* isolate = GetIsolate(); |
| 202 | Handle<Context> context(this, isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 203 | |
| 204 | bool follow_context_chain = (flags & FOLLOW_CONTEXT_CHAIN) != 0; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 205 | *index = -1; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 206 | *attributes = ABSENT; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 207 | *binding_flags = MISSING_BINDING; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 208 | |
| 209 | if (FLAG_trace_contexts) { |
| 210 | PrintF("Context::Lookup("); |
| 211 | name->ShortPrint(); |
| 212 | PrintF(")\n"); |
| 213 | } |
| 214 | |
| 215 | do { |
| 216 | if (FLAG_trace_contexts) { |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 217 | PrintF(" - looking in context %p", reinterpret_cast<void*>(*context)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 218 | if (context->IsScriptContext()) PrintF(" (script context)"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 219 | if (context->IsNativeContext()) PrintF(" (native context)"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 220 | PrintF("\n"); |
| 221 | } |
| 222 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 223 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 224 | // 1. Check global objects, subjects of with, and extension objects. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 225 | if (context->IsNativeContext() || |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 226 | context->IsWithContext() || |
| 227 | (context->IsFunctionContext() && context->has_extension())) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 228 | Handle<JSReceiver> object( |
| 229 | JSReceiver::cast(context->extension()), isolate); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 230 | |
| 231 | if (context->IsNativeContext()) { |
| 232 | if (FLAG_trace_contexts) { |
| 233 | PrintF(" - trying other script contexts\n"); |
| 234 | } |
| 235 | // Try other script contexts. |
| 236 | Handle<ScriptContextTable> script_contexts( |
| 237 | context->global_object()->native_context()->script_context_table()); |
| 238 | ScriptContextTable::LookupResult r; |
| 239 | if (ScriptContextTable::Lookup(script_contexts, name, &r)) { |
| 240 | if (FLAG_trace_contexts) { |
| 241 | Handle<Context> c = ScriptContextTable::GetContext(script_contexts, |
| 242 | r.context_index); |
| 243 | PrintF("=> found property in script context %d: %p\n", |
| 244 | r.context_index, reinterpret_cast<void*>(*c)); |
| 245 | } |
| 246 | *index = r.slot_index; |
| 247 | GetAttributesAndBindingFlags(r.mode, r.init_flag, attributes, |
| 248 | binding_flags); |
| 249 | return ScriptContextTable::GetContext(script_contexts, |
| 250 | r.context_index); |
| 251 | } |
| 252 | } |
| 253 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 254 | // Context extension objects needs to behave as if they have no |
| 255 | // prototype. So even if we want to follow prototype chains, we need |
| 256 | // to only do a local lookup for context extension objects. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 257 | Maybe<PropertyAttributes> maybe; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 258 | if ((flags & FOLLOW_PROTOTYPE_CHAIN) == 0 || |
| 259 | object->IsJSContextExtensionObject()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 260 | maybe = JSReceiver::GetOwnPropertyAttributes(object, name); |
| 261 | } else if (context->IsWithContext()) { |
| 262 | LookupIterator it(object, name); |
| 263 | maybe = UnscopableLookup(&it); |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 264 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 265 | maybe = JSReceiver::GetPropertyAttributes(object, name); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 266 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 267 | |
| 268 | if (!maybe.has_value) return Handle<Object>(); |
| 269 | DCHECK(!isolate->has_pending_exception()); |
| 270 | *attributes = maybe.value; |
| 271 | |
| 272 | if (maybe.value != ABSENT) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 273 | if (FLAG_trace_contexts) { |
| 274 | PrintF("=> found property in context object %p\n", |
| 275 | reinterpret_cast<void*>(*object)); |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 276 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 277 | return object; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 281 | // 2. Check the context proper if it has slots. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 282 | if (context->IsFunctionContext() || context->IsBlockContext() || |
| 283 | (FLAG_harmony_scoping && context->IsScriptContext())) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 284 | // Use serialized scope information of functions and blocks to search |
| 285 | // for the context index. |
| 286 | Handle<ScopeInfo> scope_info; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 287 | if (context->IsFunctionContext()) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 288 | scope_info = Handle<ScopeInfo>( |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 289 | context->closure()->shared()->scope_info(), isolate); |
| 290 | } else { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 291 | scope_info = Handle<ScopeInfo>( |
| 292 | ScopeInfo::cast(context->extension()), isolate); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 293 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 294 | VariableMode mode; |
| 295 | InitializationFlag init_flag; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 296 | // TODO(sigurds) Figure out whether maybe_assigned_flag should |
| 297 | // be used to compute binding_flags. |
| 298 | MaybeAssignedFlag maybe_assigned_flag; |
| 299 | int slot_index = ScopeInfo::ContextSlotIndex( |
| 300 | scope_info, name, &mode, &init_flag, &maybe_assigned_flag); |
| 301 | DCHECK(slot_index < 0 || slot_index >= MIN_CONTEXT_SLOTS); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 302 | if (slot_index >= 0) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 303 | if (FLAG_trace_contexts) { |
| 304 | PrintF("=> found local in context slot %d (mode = %d)\n", |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 305 | slot_index, mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 306 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 307 | *index = slot_index; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 308 | GetAttributesAndBindingFlags(mode, init_flag, attributes, |
| 309 | binding_flags); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 310 | return context; |
| 311 | } |
| 312 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 313 | // Check the slot corresponding to the intermediate context holding |
| 314 | // only the function name variable. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 315 | if (follow_context_chain && context->IsFunctionContext()) { |
| 316 | VariableMode mode; |
| 317 | int function_index = scope_info->FunctionContextSlotIndex(*name, &mode); |
| 318 | if (function_index >= 0) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 319 | if (FLAG_trace_contexts) { |
| 320 | PrintF("=> found intermediate function in context slot %d\n", |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 321 | function_index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 322 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 323 | *index = function_index; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 324 | *attributes = READ_ONLY; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 325 | DCHECK(mode == CONST_LEGACY || mode == CONST); |
| 326 | *binding_flags = (mode == CONST_LEGACY) |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 327 | ? IMMUTABLE_IS_INITIALIZED : IMMUTABLE_IS_INITIALIZED_HARMONY; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 328 | return context; |
| 329 | } |
| 330 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 331 | |
| 332 | } else if (context->IsCatchContext()) { |
| 333 | // Catch contexts have the variable name in the extension slot. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 334 | if (String::Equals(name, handle(String::cast(context->extension())))) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 335 | if (FLAG_trace_contexts) { |
| 336 | PrintF("=> found in catch context\n"); |
| 337 | } |
| 338 | *index = Context::THROWN_OBJECT_INDEX; |
| 339 | *attributes = NONE; |
| 340 | *binding_flags = MUTABLE_IS_INITIALIZED; |
| 341 | return context; |
| 342 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 345 | // 3. Prepare to continue with the previous (next outermost) context. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 346 | if (context->IsNativeContext()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 347 | follow_context_chain = false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 348 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 349 | context = Handle<Context>(context->previous(), isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 350 | } |
| 351 | } while (follow_context_chain); |
| 352 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 353 | if (FLAG_trace_contexts) { |
| 354 | PrintF("=> no property/slot found\n"); |
| 355 | } |
| 356 | return Handle<Object>::null(); |
| 357 | } |
| 358 | |
| 359 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 360 | void Context::AddOptimizedFunction(JSFunction* function) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 361 | DCHECK(IsNativeContext()); |
| 362 | #ifdef ENABLE_SLOW_DCHECKS |
| 363 | if (FLAG_enable_slow_asserts) { |
| 364 | Object* element = get(OPTIMIZED_FUNCTIONS_LIST); |
| 365 | while (!element->IsUndefined()) { |
| 366 | CHECK(element != function); |
| 367 | element = JSFunction::cast(element)->next_function_link(); |
| 368 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 369 | } |
| 370 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 371 | // Check that the context belongs to the weak native contexts list. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 372 | bool found = false; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 373 | Object* context = GetHeap()->native_contexts_list(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 374 | while (!context->IsUndefined()) { |
| 375 | if (context == this) { |
| 376 | found = true; |
| 377 | break; |
| 378 | } |
| 379 | context = Context::cast(context)->get(Context::NEXT_CONTEXT_LINK); |
| 380 | } |
| 381 | CHECK(found); |
| 382 | #endif |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 383 | |
| 384 | // If the function link field is already used then the function was |
| 385 | // enqueued as a code flushing candidate and we remove it now. |
| 386 | if (!function->next_function_link()->IsUndefined()) { |
| 387 | CodeFlusher* flusher = GetHeap()->mark_compact_collector()->code_flusher(); |
| 388 | flusher->EvictCandidate(function); |
| 389 | } |
| 390 | |
| 391 | DCHECK(function->next_function_link()->IsUndefined()); |
| 392 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 393 | function->set_next_function_link(get(OPTIMIZED_FUNCTIONS_LIST)); |
| 394 | set(OPTIMIZED_FUNCTIONS_LIST, function); |
| 395 | } |
| 396 | |
| 397 | |
| 398 | void Context::RemoveOptimizedFunction(JSFunction* function) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 399 | DCHECK(IsNativeContext()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 400 | Object* element = get(OPTIMIZED_FUNCTIONS_LIST); |
| 401 | JSFunction* prev = NULL; |
| 402 | while (!element->IsUndefined()) { |
| 403 | JSFunction* element_function = JSFunction::cast(element); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 404 | DCHECK(element_function->next_function_link()->IsUndefined() || |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 405 | element_function->next_function_link()->IsJSFunction()); |
| 406 | if (element_function == function) { |
| 407 | if (prev == NULL) { |
| 408 | set(OPTIMIZED_FUNCTIONS_LIST, element_function->next_function_link()); |
| 409 | } else { |
| 410 | prev->set_next_function_link(element_function->next_function_link()); |
| 411 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 412 | element_function->set_next_function_link(GetHeap()->undefined_value()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 413 | return; |
| 414 | } |
| 415 | prev = element_function; |
| 416 | element = element_function->next_function_link(); |
| 417 | } |
| 418 | UNREACHABLE(); |
| 419 | } |
| 420 | |
| 421 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 422 | void Context::SetOptimizedFunctionsListHead(Object* head) { |
| 423 | DCHECK(IsNativeContext()); |
| 424 | set(OPTIMIZED_FUNCTIONS_LIST, head); |
| 425 | } |
| 426 | |
| 427 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 428 | Object* Context::OptimizedFunctionsListHead() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 429 | DCHECK(IsNativeContext()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 430 | return get(OPTIMIZED_FUNCTIONS_LIST); |
| 431 | } |
| 432 | |
| 433 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 434 | void Context::AddOptimizedCode(Code* code) { |
| 435 | DCHECK(IsNativeContext()); |
| 436 | DCHECK(code->kind() == Code::OPTIMIZED_FUNCTION); |
| 437 | DCHECK(code->next_code_link()->IsUndefined()); |
| 438 | code->set_next_code_link(get(OPTIMIZED_CODE_LIST)); |
| 439 | set(OPTIMIZED_CODE_LIST, code); |
| 440 | } |
| 441 | |
| 442 | |
| 443 | void Context::SetOptimizedCodeListHead(Object* head) { |
| 444 | DCHECK(IsNativeContext()); |
| 445 | set(OPTIMIZED_CODE_LIST, head); |
| 446 | } |
| 447 | |
| 448 | |
| 449 | Object* Context::OptimizedCodeListHead() { |
| 450 | DCHECK(IsNativeContext()); |
| 451 | return get(OPTIMIZED_CODE_LIST); |
| 452 | } |
| 453 | |
| 454 | |
| 455 | void Context::SetDeoptimizedCodeListHead(Object* head) { |
| 456 | DCHECK(IsNativeContext()); |
| 457 | set(DEOPTIMIZED_CODE_LIST, head); |
| 458 | } |
| 459 | |
| 460 | |
| 461 | Object* Context::DeoptimizedCodeListHead() { |
| 462 | DCHECK(IsNativeContext()); |
| 463 | return get(DEOPTIMIZED_CODE_LIST); |
| 464 | } |
| 465 | |
| 466 | |
| 467 | Handle<Object> Context::ErrorMessageForCodeGenerationFromStrings() { |
| 468 | Isolate* isolate = GetIsolate(); |
| 469 | Handle<Object> result(error_message_for_code_gen_from_strings(), isolate); |
| 470 | if (!result->IsUndefined()) return result; |
| 471 | return isolate->factory()->NewStringFromStaticChars( |
| 472 | "Code generation from strings disallowed for this context"); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 476 | #ifdef DEBUG |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 477 | bool Context::IsBootstrappingOrValidParentContext( |
| 478 | Object* object, Context* child) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 479 | // During bootstrapping we allow all objects to pass as |
| 480 | // contexts. This is necessary to fix circular dependencies. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 481 | if (child->GetIsolate()->bootstrapper()->IsActive()) return true; |
| 482 | if (!object->IsContext()) return false; |
| 483 | Context* context = Context::cast(object); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 484 | return context->IsNativeContext() || context->IsScriptContext() || |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 485 | context->IsModuleContext() || !child->IsModuleContext(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 489 | bool Context::IsBootstrappingOrGlobalObject(Isolate* isolate, Object* object) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 490 | // During bootstrapping we allow all objects to pass as global |
| 491 | // objects. This is necessary to fix circular dependencies. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 492 | return isolate->heap()->gc_state() != Heap::NOT_IN_GC || |
| 493 | isolate->bootstrapper()->IsActive() || |
| 494 | object->IsGlobalObject(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 495 | } |
| 496 | #endif |
| 497 | |
| 498 | } } // namespace v8::internal |