OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_CONTEXTS_H_ | 5 #ifndef V8_CONTEXTS_H_ |
6 #define V8_CONTEXTS_H_ | 6 #define V8_CONTEXTS_H_ |
7 | 7 |
8 #include "src/heap/heap.h" | 8 #include "src/heap/heap.h" |
9 #include "src/objects.h" | 9 #include "src/objects.h" |
10 | 10 |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 V(NATIVE_OBJECT_NOTIFIER_PERFORM_CHANGE, JSFunction, \ | 176 V(NATIVE_OBJECT_NOTIFIER_PERFORM_CHANGE, JSFunction, \ |
177 native_object_notifier_perform_change) \ | 177 native_object_notifier_perform_change) \ |
178 V(SLOPPY_GENERATOR_FUNCTION_MAP_INDEX, Map, sloppy_generator_function_map) \ | 178 V(SLOPPY_GENERATOR_FUNCTION_MAP_INDEX, Map, sloppy_generator_function_map) \ |
179 V(STRICT_GENERATOR_FUNCTION_MAP_INDEX, Map, strict_generator_function_map) \ | 179 V(STRICT_GENERATOR_FUNCTION_MAP_INDEX, Map, strict_generator_function_map) \ |
180 V(GENERATOR_OBJECT_PROTOTYPE_MAP_INDEX, Map, generator_object_prototype_map) \ | 180 V(GENERATOR_OBJECT_PROTOTYPE_MAP_INDEX, Map, generator_object_prototype_map) \ |
181 V(ITERATOR_RESULT_MAP_INDEX, Map, iterator_result_map) \ | 181 V(ITERATOR_RESULT_MAP_INDEX, Map, iterator_result_map) \ |
182 V(MAP_ITERATOR_MAP_INDEX, Map, map_iterator_map) \ | 182 V(MAP_ITERATOR_MAP_INDEX, Map, map_iterator_map) \ |
183 V(SET_ITERATOR_MAP_INDEX, Map, set_iterator_map) \ | 183 V(SET_ITERATOR_MAP_INDEX, Map, set_iterator_map) \ |
184 V(ITERATOR_SYMBOL_INDEX, Symbol, iterator_symbol) \ | 184 V(ITERATOR_SYMBOL_INDEX, Symbol, iterator_symbol) \ |
185 V(UNSCOPABLES_SYMBOL_INDEX, Symbol, unscopables_symbol) \ | 185 V(UNSCOPABLES_SYMBOL_INDEX, Symbol, unscopables_symbol) \ |
186 V(ARRAY_VALUES_ITERATOR_INDEX, JSFunction, array_values_iterator) | 186 V(ARRAY_VALUES_ITERATOR_INDEX, JSFunction, array_values_iterator) \ |
| 187 V(GLOBAL_CONTEXT_TABLE_INDEX, GlobalContextTable, global_context_table) |
| 188 |
| 189 |
| 190 // A table of all global contexts. Every loaded top-level script with top-level |
| 191 // lexical declarations contributes its GlobalContext into this table. |
| 192 // |
| 193 // The table is a fixed array, its first slot is the current used count and |
| 194 // the subsequent slots 1..used contain GlobalContexts. |
| 195 class GlobalContextTable : public FixedArray { |
| 196 public: |
| 197 // Conversions. |
| 198 static GlobalContextTable* cast(Object* context) { |
| 199 DCHECK(context->IsGlobalContextTable()); |
| 200 return reinterpret_cast<GlobalContextTable*>(context); |
| 201 } |
| 202 |
| 203 struct LookupResult { |
| 204 int context_index; |
| 205 int slot_index; |
| 206 VariableMode mode; |
| 207 InitializationFlag init_flag; |
| 208 MaybeAssignedFlag maybe_assigned_flag; |
| 209 }; |
| 210 |
| 211 int used() const { return Smi::cast(get(kUsedSlot))->value(); } |
| 212 |
| 213 void set_used(int used) { set(kUsedSlot, Smi::FromInt(used)); } |
| 214 |
| 215 static Handle<Context> GetContext(Handle<GlobalContextTable> table, int i) { |
| 216 DCHECK(i < table->used()); |
| 217 return Handle<Context>::cast(FixedArray::get(table, i + 1)); |
| 218 } |
| 219 |
| 220 // Lookup a variable `name` in a GlobalContextTable. |
| 221 // If it returns true, the variable is found and `result` contains |
| 222 // valid information about its location. |
| 223 // If it returns false, `result` is untouched. |
| 224 MUST_USE_RESULT |
| 225 static bool Lookup(Handle<GlobalContextTable> table, Handle<String> name, |
| 226 LookupResult* result); |
| 227 |
| 228 MUST_USE_RESULT |
| 229 static Handle<GlobalContextTable> Extend(Handle<GlobalContextTable> table, |
| 230 Handle<Context> global_context); |
| 231 |
| 232 private: |
| 233 static const int kUsedSlot = 0; |
| 234 |
| 235 DISALLOW_IMPLICIT_CONSTRUCTORS(GlobalContextTable); |
| 236 }; |
187 | 237 |
188 // JSFunctions are pairs (context, function code), sometimes also called | 238 // JSFunctions are pairs (context, function code), sometimes also called |
189 // closures. A Context object is used to represent function contexts and | 239 // closures. A Context object is used to represent function contexts and |
190 // dynamically pushed 'with' contexts (or 'scopes' in ECMA-262 speak). | 240 // dynamically pushed 'with' contexts (or 'scopes' in ECMA-262 speak). |
191 // | 241 // |
192 // At runtime, the contexts build a stack in parallel to the execution | 242 // At runtime, the contexts build a stack in parallel to the execution |
193 // stack, with the top-most context being the current context. All contexts | 243 // stack, with the top-most context being the current context. All contexts |
194 // have the following slots: | 244 // have the following slots: |
195 // | 245 // |
196 // [ closure ] This is the current function. It is the same for all | 246 // [ closure ] This is the current function. It is the same for all |
(...skipping 25 matching lines...) Expand all Loading... |
222 // we always have a context pointer). | 272 // we always have a context pointer). |
223 // | 273 // |
224 // In addition, function contexts may have statically allocated context slots | 274 // In addition, function contexts may have statically allocated context slots |
225 // to store local variables/functions that are accessed from inner functions | 275 // to store local variables/functions that are accessed from inner functions |
226 // (via static context addresses) or through 'eval' (dynamic context lookups). | 276 // (via static context addresses) or through 'eval' (dynamic context lookups). |
227 // The native context contains additional slots for fast access to native | 277 // The native context contains additional slots for fast access to native |
228 // properties. | 278 // properties. |
229 // | 279 // |
230 // Finally, with Harmony scoping, the JSFunction representing a top level | 280 // Finally, with Harmony scoping, the JSFunction representing a top level |
231 // script will have the GlobalContext rather than a FunctionContext. | 281 // script will have the GlobalContext rather than a FunctionContext. |
| 282 // Global contexts from all top-level scripts are gathered in |
| 283 // GlobalContextTable. |
232 | 284 |
233 class Context: public FixedArray { | 285 class Context: public FixedArray { |
234 public: | 286 public: |
235 // Conversions. | 287 // Conversions. |
236 static Context* cast(Object* context) { | 288 static Context* cast(Object* context) { |
237 DCHECK(context->IsContext()); | 289 DCHECK(context->IsContext()); |
238 return reinterpret_cast<Context*>(context); | 290 return reinterpret_cast<Context*>(context); |
239 } | 291 } |
240 | 292 |
241 // The default context slot layout; indices are FixedArray slot indices. | 293 // The default context slot layout; indices are FixedArray slot indices. |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
353 NATIVE_OBJECT_NOTIFIER_PERFORM_CHANGE, | 405 NATIVE_OBJECT_NOTIFIER_PERFORM_CHANGE, |
354 SLOPPY_GENERATOR_FUNCTION_MAP_INDEX, | 406 SLOPPY_GENERATOR_FUNCTION_MAP_INDEX, |
355 STRICT_GENERATOR_FUNCTION_MAP_INDEX, | 407 STRICT_GENERATOR_FUNCTION_MAP_INDEX, |
356 GENERATOR_OBJECT_PROTOTYPE_MAP_INDEX, | 408 GENERATOR_OBJECT_PROTOTYPE_MAP_INDEX, |
357 ITERATOR_RESULT_MAP_INDEX, | 409 ITERATOR_RESULT_MAP_INDEX, |
358 MAP_ITERATOR_MAP_INDEX, | 410 MAP_ITERATOR_MAP_INDEX, |
359 SET_ITERATOR_MAP_INDEX, | 411 SET_ITERATOR_MAP_INDEX, |
360 ITERATOR_SYMBOL_INDEX, | 412 ITERATOR_SYMBOL_INDEX, |
361 UNSCOPABLES_SYMBOL_INDEX, | 413 UNSCOPABLES_SYMBOL_INDEX, |
362 ARRAY_VALUES_ITERATOR_INDEX, | 414 ARRAY_VALUES_ITERATOR_INDEX, |
| 415 GLOBAL_CONTEXT_TABLE_INDEX, |
363 | 416 |
364 // Properties from here are treated as weak references by the full GC. | 417 // Properties from here are treated as weak references by the full GC. |
365 // Scavenge treats them as strong references. | 418 // Scavenge treats them as strong references. |
366 OPTIMIZED_FUNCTIONS_LIST, // Weak. | 419 OPTIMIZED_FUNCTIONS_LIST, // Weak. |
367 OPTIMIZED_CODE_LIST, // Weak. | 420 OPTIMIZED_CODE_LIST, // Weak. |
368 DEOPTIMIZED_CODE_LIST, // Weak. | 421 DEOPTIMIZED_CODE_LIST, // Weak. |
369 MAP_CACHE_INDEX, // Weak. | 422 MAP_CACHE_INDEX, // Weak. |
370 NEXT_CONTEXT_LINK, // Weak. | 423 NEXT_CONTEXT_LINK, // Weak. |
371 | 424 |
372 // Total number of slots. | 425 // Total number of slots. |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
552 static bool IsBootstrappingOrGlobalObject(Isolate* isolate, Object* object); | 605 static bool IsBootstrappingOrGlobalObject(Isolate* isolate, Object* object); |
553 #endif | 606 #endif |
554 | 607 |
555 STATIC_ASSERT(kHeaderSize == Internals::kContextHeaderSize); | 608 STATIC_ASSERT(kHeaderSize == Internals::kContextHeaderSize); |
556 STATIC_ASSERT(EMBEDDER_DATA_INDEX == Internals::kContextEmbedderDataIndex); | 609 STATIC_ASSERT(EMBEDDER_DATA_INDEX == Internals::kContextEmbedderDataIndex); |
557 }; | 610 }; |
558 | 611 |
559 } } // namespace v8::internal | 612 } } // namespace v8::internal |
560 | 613 |
561 #endif // V8_CONTEXTS_H_ | 614 #endif // V8_CONTEXTS_H_ |
OLD | NEW |