deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / src / contexts.h
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
3 // found in the LICENSE file.
4
5 #ifndef V8_CONTEXTS_H_
6 #define V8_CONTEXTS_H_
7
8 #include "src/heap/heap.h"
9 #include "src/objects.h"
10
11 namespace v8 {
12 namespace internal {
13
14
15 enum ContextLookupFlags {
16   FOLLOW_CONTEXT_CHAIN = 1,
17   FOLLOW_PROTOTYPE_CHAIN = 2,
18
19   DONT_FOLLOW_CHAINS = 0,
20   FOLLOW_CHAINS = FOLLOW_CONTEXT_CHAIN | FOLLOW_PROTOTYPE_CHAIN
21 };
22
23
24 // ES5 10.2 defines lexical environments with mutable and immutable bindings.
25 // Immutable bindings have two states, initialized and uninitialized, and
26 // their state is changed by the InitializeImmutableBinding method. The
27 // BindingFlags enum represents information if a binding has definitely been
28 // initialized. A mutable binding does not need to be checked and thus has
29 // the BindingFlag MUTABLE_IS_INITIALIZED.
30 //
31 // There are two possibilities for immutable bindings
32 //  * 'const' declared variables. They are initialized when evaluating the
33 //    corresponding declaration statement. They need to be checked for being
34 //    initialized and thus get the flag IMMUTABLE_CHECK_INITIALIZED.
35 //  * The function name of a named function literal. The binding is immediately
36 //    initialized when entering the function and thus does not need to be
37 //    checked. it gets the BindingFlag IMMUTABLE_IS_INITIALIZED.
38 // Accessing an uninitialized binding produces the undefined value.
39 //
40 // The harmony proposal for block scoped bindings also introduces the
41 // uninitialized state for mutable bindings.
42 //  * A 'let' declared variable. They are initialized when evaluating the
43 //    corresponding declaration statement. They need to be checked for being
44 //    initialized and thus get the flag MUTABLE_CHECK_INITIALIZED.
45 //  * A 'var' declared variable. It is initialized immediately upon creation
46 //    and thus doesn't need to be checked. It gets the flag
47 //    MUTABLE_IS_INITIALIZED.
48 //  * Catch bound variables, function parameters and variables introduced by
49 //    function declarations are initialized immediately and do not need to be
50 //    checked. Thus they get the flag MUTABLE_IS_INITIALIZED.
51 // Immutable bindings in harmony mode get the _HARMONY flag variants. Accessing
52 // an uninitialized binding produces a reference error.
53 //
54 // In V8 uninitialized bindings are set to the hole value upon creation and set
55 // to a different value upon initialization.
56 enum BindingFlags {
57   MUTABLE_IS_INITIALIZED,
58   MUTABLE_CHECK_INITIALIZED,
59   IMMUTABLE_IS_INITIALIZED,
60   IMMUTABLE_CHECK_INITIALIZED,
61   IMMUTABLE_IS_INITIALIZED_HARMONY,
62   IMMUTABLE_CHECK_INITIALIZED_HARMONY,
63   MISSING_BINDING
64 };
65
66
67 // Heap-allocated activation contexts.
68 //
69 // Contexts are implemented as FixedArray objects; the Context
70 // class is a convenience interface casted on a FixedArray object.
71 //
72 // Note: Context must have no virtual functions and Context objects
73 // must always be allocated via Heap::AllocateContext() or
74 // Factory::NewContext.
75
76 #define NATIVE_CONTEXT_FIELDS(V)                                               \
77   V(GLOBAL_PROXY_INDEX, JSObject, global_proxy_object)                         \
78   V(SECURITY_TOKEN_INDEX, Object, security_token)                              \
79   V(BOOLEAN_FUNCTION_INDEX, JSFunction, boolean_function)                      \
80   V(NUMBER_FUNCTION_INDEX, JSFunction, number_function)                        \
81   V(STRING_FUNCTION_INDEX, JSFunction, string_function)                        \
82   V(STRING_FUNCTION_PROTOTYPE_MAP_INDEX, Map, string_function_prototype_map)   \
83   V(SYMBOL_FUNCTION_INDEX, JSFunction, symbol_function)                        \
84   V(OBJECT_FUNCTION_INDEX, JSFunction, object_function)                        \
85   V(INTERNAL_ARRAY_FUNCTION_INDEX, JSFunction, internal_array_function)        \
86   V(ARRAY_FUNCTION_INDEX, JSFunction, array_function)                          \
87   V(JS_ARRAY_MAPS_INDEX, Object, js_array_maps)                                \
88   V(DATE_FUNCTION_INDEX, JSFunction, date_function)                            \
89   V(JSON_OBJECT_INDEX, JSObject, json_object)                                  \
90   V(REGEXP_FUNCTION_INDEX, JSFunction, regexp_function)                        \
91   V(INITIAL_OBJECT_PROTOTYPE_INDEX, JSObject, initial_object_prototype)        \
92   V(INITIAL_ARRAY_PROTOTYPE_INDEX, JSObject, initial_array_prototype)          \
93   V(CREATE_DATE_FUN_INDEX, JSFunction, create_date_fun)                        \
94   V(TO_NUMBER_FUN_INDEX, JSFunction, to_number_fun)                            \
95   V(TO_STRING_FUN_INDEX, JSFunction, to_string_fun)                            \
96   V(TO_DETAIL_STRING_FUN_INDEX, JSFunction, to_detail_string_fun)              \
97   V(TO_OBJECT_FUN_INDEX, JSFunction, to_object_fun)                            \
98   V(TO_INTEGER_FUN_INDEX, JSFunction, to_integer_fun)                          \
99   V(TO_UINT32_FUN_INDEX, JSFunction, to_uint32_fun)                            \
100   V(TO_INT32_FUN_INDEX, JSFunction, to_int32_fun)                              \
101   V(TO_LENGTH_FUN_INDEX, JSFunction, to_length_fun)                            \
102   V(GLOBAL_EVAL_FUN_INDEX, JSFunction, global_eval_fun)                        \
103   V(ARRAY_BUFFER_FUN_INDEX, JSFunction, array_buffer_fun)                      \
104   V(UINT8_ARRAY_FUN_INDEX, JSFunction, uint8_array_fun)                        \
105   V(INT8_ARRAY_FUN_INDEX, JSFunction, int8_array_fun)                          \
106   V(UINT16_ARRAY_FUN_INDEX, JSFunction, uint16_array_fun)                      \
107   V(INT16_ARRAY_FUN_INDEX, JSFunction, int16_array_fun)                        \
108   V(UINT32_ARRAY_FUN_INDEX, JSFunction, uint32_array_fun)                      \
109   V(INT32_ARRAY_FUN_INDEX, JSFunction, int32_array_fun)                        \
110   V(FLOAT32_ARRAY_FUN_INDEX, JSFunction, float32_array_fun)                    \
111   V(FLOAT64_ARRAY_FUN_INDEX, JSFunction, float64_array_fun)                    \
112   V(UINT8_CLAMPED_ARRAY_FUN_INDEX, JSFunction, uint8_clamped_array_fun)        \
113   V(INT8_ARRAY_EXTERNAL_MAP_INDEX, Map, int8_array_external_map)               \
114   V(UINT8_ARRAY_EXTERNAL_MAP_INDEX, Map, uint8_array_external_map)             \
115   V(INT16_ARRAY_EXTERNAL_MAP_INDEX, Map, int16_array_external_map)             \
116   V(UINT16_ARRAY_EXTERNAL_MAP_INDEX, Map, uint16_array_external_map)           \
117   V(INT32_ARRAY_EXTERNAL_MAP_INDEX, Map, int32_array_external_map)             \
118   V(UINT32_ARRAY_EXTERNAL_MAP_INDEX, Map, uint32_array_external_map)           \
119   V(FLOAT32_ARRAY_EXTERNAL_MAP_INDEX, Map, float32_array_external_map)         \
120   V(FLOAT64_ARRAY_EXTERNAL_MAP_INDEX, Map, float64_array_external_map)         \
121   V(UINT8_CLAMPED_ARRAY_EXTERNAL_MAP_INDEX, Map,                               \
122     uint8_clamped_array_external_map)                                          \
123   V(DATA_VIEW_FUN_INDEX, JSFunction, data_view_fun)                            \
124   V(SLOPPY_FUNCTION_MAP_INDEX, Map, sloppy_function_map)                       \
125   V(SLOPPY_FUNCTION_WITH_READONLY_PROTOTYPE_MAP_INDEX, Map,                    \
126     sloppy_function_with_readonly_prototype_map)                               \
127   V(STRICT_FUNCTION_MAP_INDEX, Map, strict_function_map)                       \
128   V(STRONG_FUNCTION_MAP_INDEX, Map, strong_function_map)                       \
129   V(SLOPPY_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map,                          \
130     sloppy_function_without_prototype_map)                                     \
131   V(STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map,                          \
132     strict_function_without_prototype_map)                                     \
133   V(STRONG_CONSTRUCTOR_MAP_INDEX, Map, strong_constructor_map)                 \
134   V(BOUND_FUNCTION_MAP_INDEX, Map, bound_function_map)                         \
135   V(REGEXP_RESULT_MAP_INDEX, Map, regexp_result_map)                           \
136   V(SLOPPY_ARGUMENTS_MAP_INDEX, Map, sloppy_arguments_map)                     \
137   V(ALIASED_ARGUMENTS_MAP_INDEX, Map, aliased_arguments_map)                   \
138   V(STRICT_ARGUMENTS_MAP_INDEX, Map, strict_arguments_map)                     \
139   V(MESSAGE_LISTENERS_INDEX, JSObject, message_listeners)                      \
140   V(MAKE_MESSAGE_FUN_INDEX, JSFunction, make_message_fun)                      \
141   V(GET_STACK_TRACE_LINE_INDEX, JSFunction, get_stack_trace_line_fun)          \
142   V(CONFIGURE_GLOBAL_INDEX, JSFunction, configure_global_fun)                  \
143   V(FUNCTION_CACHE_INDEX, ObjectHashTable, function_cache)                     \
144   V(JSFUNCTION_RESULT_CACHES_INDEX, FixedArray, jsfunction_result_caches)      \
145   V(NORMALIZED_MAP_CACHE_INDEX, Object, normalized_map_cache)                  \
146   V(RUNTIME_CONTEXT_INDEX, Context, runtime_context)                           \
147   V(CALL_AS_FUNCTION_DELEGATE_INDEX, JSFunction, call_as_function_delegate)    \
148   V(CALL_AS_CONSTRUCTOR_DELEGATE_INDEX, JSFunction,                            \
149     call_as_constructor_delegate)                                              \
150   V(SCRIPT_FUNCTION_INDEX, JSFunction, script_function)                        \
151   V(OPAQUE_REFERENCE_FUNCTION_INDEX, JSFunction, opaque_reference_function)    \
152   V(CONTEXT_EXTENSION_FUNCTION_INDEX, JSFunction, context_extension_function)  \
153   V(MAP_CACHE_INDEX, Object, map_cache)                                        \
154   V(EMBEDDER_DATA_INDEX, FixedArray, embedder_data)                            \
155   V(ALLOW_CODE_GEN_FROM_STRINGS_INDEX, Object, allow_code_gen_from_strings)    \
156   V(ERROR_MESSAGE_FOR_CODE_GEN_FROM_STRINGS_INDEX, Object,                     \
157     error_message_for_code_gen_from_strings)                                   \
158   V(PROMISE_STATUS_INDEX, Symbol, promise_status)                              \
159   V(PROMISE_CREATE_INDEX, JSFunction, promise_create)                          \
160   V(PROMISE_RESOLVE_INDEX, JSFunction, promise_resolve)                        \
161   V(PROMISE_REJECT_INDEX, JSFunction, promise_reject)                          \
162   V(PROMISE_CHAIN_INDEX, JSFunction, promise_chain)                            \
163   V(PROMISE_CATCH_INDEX, JSFunction, promise_catch)                            \
164   V(PROMISE_THEN_INDEX, JSFunction, promise_then)                              \
165   V(TO_COMPLETE_PROPERTY_DESCRIPTOR_INDEX, JSFunction,                         \
166     to_complete_property_descriptor)                                           \
167   V(DERIVED_HAS_TRAP_INDEX, JSFunction, derived_has_trap)                      \
168   V(DERIVED_GET_TRAP_INDEX, JSFunction, derived_get_trap)                      \
169   V(DERIVED_SET_TRAP_INDEX, JSFunction, derived_set_trap)                      \
170   V(PROXY_ENUMERATE_INDEX, JSFunction, proxy_enumerate)                        \
171   V(OBSERVERS_NOTIFY_CHANGE_INDEX, JSFunction, observers_notify_change)        \
172   V(OBSERVERS_ENQUEUE_SPLICE_INDEX, JSFunction, observers_enqueue_splice)      \
173   V(OBSERVERS_BEGIN_SPLICE_INDEX, JSFunction, observers_begin_perform_splice)  \
174   V(OBSERVERS_END_SPLICE_INDEX, JSFunction, observers_end_perform_splice)      \
175   V(NATIVE_OBJECT_OBSERVE_INDEX, JSFunction, native_object_observe)            \
176   V(NATIVE_OBJECT_GET_NOTIFIER_INDEX, JSFunction, native_object_get_notifier)  \
177   V(NATIVE_OBJECT_NOTIFIER_PERFORM_CHANGE, JSFunction,                         \
178     native_object_notifier_perform_change)                                     \
179   V(SLOPPY_GENERATOR_FUNCTION_MAP_INDEX, Map, sloppy_generator_function_map)   \
180   V(STRICT_GENERATOR_FUNCTION_MAP_INDEX, Map, strict_generator_function_map)   \
181   V(STRONG_GENERATOR_FUNCTION_MAP_INDEX, Map, strong_generator_function_map)   \
182   V(GENERATOR_OBJECT_PROTOTYPE_MAP_INDEX, Map, generator_object_prototype_map) \
183   V(ITERATOR_RESULT_MAP_INDEX, Map, iterator_result_map)                       \
184   V(MAP_ITERATOR_MAP_INDEX, Map, map_iterator_map)                             \
185   V(SET_ITERATOR_MAP_INDEX, Map, set_iterator_map)                             \
186   V(ARRAY_VALUES_ITERATOR_INDEX, JSFunction, array_values_iterator)            \
187   V(SCRIPT_CONTEXT_TABLE_INDEX, ScriptContextTable, script_context_table)
188
189
190 // A table of all script contexts. Every loaded top-level script with top-level
191 // lexical declarations contributes its ScriptContext 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 ScriptContexts.
195 class ScriptContextTable : public FixedArray {
196  public:
197   // Conversions.
198   static ScriptContextTable* cast(Object* context) {
199     DCHECK(context->IsScriptContextTable());
200     return reinterpret_cast<ScriptContextTable*>(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<ScriptContextTable> 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 ScriptContextTable.
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<ScriptContextTable> table, Handle<String> name,
226                      LookupResult* result);
227
228   MUST_USE_RESULT
229   static Handle<ScriptContextTable> Extend(Handle<ScriptContextTable> table,
230                                            Handle<Context> script_context);
231
232   static int GetContextOffset(int context_index) {
233     return kFirstContextOffset + context_index * kPointerSize;
234   }
235
236  private:
237   static const int kUsedSlot = 0;
238   static const int kFirstContextOffset =
239       FixedArray::kHeaderSize + (kUsedSlot + 1) * kPointerSize;
240
241   DISALLOW_IMPLICIT_CONSTRUCTORS(ScriptContextTable);
242 };
243
244 // JSFunctions are pairs (context, function code), sometimes also called
245 // closures. A Context object is used to represent function contexts and
246 // dynamically pushed 'with' contexts (or 'scopes' in ECMA-262 speak).
247 //
248 // At runtime, the contexts build a stack in parallel to the execution
249 // stack, with the top-most context being the current context. All contexts
250 // have the following slots:
251 //
252 // [ closure   ]  This is the current function. It is the same for all
253 //                contexts inside a function. It provides access to the
254 //                incoming context (i.e., the outer context, which may
255 //                or may not become the current function's context), and
256 //                it provides access to the functions code and thus it's
257 //                scope information, which in turn contains the names of
258 //                statically allocated context slots. The names are needed
259 //                for dynamic lookups in the presence of 'with' or 'eval'.
260 //
261 // [ previous  ]  A pointer to the previous context. It is NULL for
262 //                function contexts, and non-NULL for 'with' contexts.
263 //                Used to implement the 'with' statement.
264 //
265 // [ extension ]  A pointer to an extension JSObject, or NULL. Used to
266 //                implement 'with' statements and dynamic declarations
267 //                (through 'eval'). The object in a 'with' statement is
268 //                stored in the extension slot of a 'with' context.
269 //                Dynamically declared variables/functions are also added
270 //                to lazily allocated extension object. Context::Lookup
271 //                searches the extension object for properties.
272 //                For global and block contexts, contains the respective
273 //                ScopeInfo.
274 //                For module contexts, points back to the respective JSModule.
275 //
276 // [ global_object ]  A pointer to the global object. Provided for quick
277 //                access to the global object from inside the code (since
278 //                we always have a context pointer).
279 //
280 // In addition, function contexts may have statically allocated context slots
281 // to store local variables/functions that are accessed from inner functions
282 // (via static context addresses) or through 'eval' (dynamic context lookups).
283 // The native context contains additional slots for fast access to native
284 // properties.
285 //
286 // Finally, with Harmony scoping, the JSFunction representing a top level
287 // script will have the ScriptContext rather than a FunctionContext.
288 // Script contexts from all top-level scripts are gathered in
289 // ScriptContextTable.
290
291 class Context: public FixedArray {
292  public:
293   // Conversions.
294   static Context* cast(Object* context) {
295     DCHECK(context->IsContext());
296     return reinterpret_cast<Context*>(context);
297   }
298
299   // The default context slot layout; indices are FixedArray slot indices.
300   enum {
301     // These slots are in all contexts.
302     CLOSURE_INDEX,
303     PREVIOUS_INDEX,
304     // The extension slot is used for either the global object (in global
305     // contexts), eval extension object (function contexts), subject of with
306     // (with contexts), or the variable name (catch contexts), the serialized
307     // scope info (block contexts), or the module instance (module contexts).
308     EXTENSION_INDEX,
309     GLOBAL_OBJECT_INDEX,
310     MIN_CONTEXT_SLOTS,
311
312     // This slot holds the thrown value in catch contexts.
313     THROWN_OBJECT_INDEX = MIN_CONTEXT_SLOTS,
314
315     // These slots are only in native contexts.
316     GLOBAL_PROXY_INDEX = MIN_CONTEXT_SLOTS,
317     SECURITY_TOKEN_INDEX,
318     SLOPPY_ARGUMENTS_MAP_INDEX,
319     ALIASED_ARGUMENTS_MAP_INDEX,
320     STRICT_ARGUMENTS_MAP_INDEX,
321     REGEXP_RESULT_MAP_INDEX,
322     SLOPPY_FUNCTION_MAP_INDEX,
323     SLOPPY_FUNCTION_WITH_READONLY_PROTOTYPE_MAP_INDEX,
324     STRICT_FUNCTION_MAP_INDEX,
325     STRONG_FUNCTION_MAP_INDEX,
326     SLOPPY_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX,
327     STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX,
328     STRONG_CONSTRUCTOR_MAP_INDEX,
329     BOUND_FUNCTION_MAP_INDEX,
330     INITIAL_OBJECT_PROTOTYPE_INDEX,
331     INITIAL_ARRAY_PROTOTYPE_INDEX,
332     BOOLEAN_FUNCTION_INDEX,
333     NUMBER_FUNCTION_INDEX,
334     STRING_FUNCTION_INDEX,
335     STRING_FUNCTION_PROTOTYPE_MAP_INDEX,
336     SYMBOL_FUNCTION_INDEX,
337     OBJECT_FUNCTION_INDEX,
338     INTERNAL_ARRAY_FUNCTION_INDEX,
339     ARRAY_FUNCTION_INDEX,
340     JS_ARRAY_MAPS_INDEX,
341     DATE_FUNCTION_INDEX,
342     JSON_OBJECT_INDEX,
343     REGEXP_FUNCTION_INDEX,
344     CREATE_DATE_FUN_INDEX,
345     TO_NUMBER_FUN_INDEX,
346     TO_STRING_FUN_INDEX,
347     TO_DETAIL_STRING_FUN_INDEX,
348     TO_OBJECT_FUN_INDEX,
349     TO_INTEGER_FUN_INDEX,
350     TO_UINT32_FUN_INDEX,
351     TO_INT32_FUN_INDEX,
352     TO_BOOLEAN_FUN_INDEX,
353     GLOBAL_EVAL_FUN_INDEX,
354     ARRAY_BUFFER_FUN_INDEX,
355     UINT8_ARRAY_FUN_INDEX,
356     INT8_ARRAY_FUN_INDEX,
357     UINT16_ARRAY_FUN_INDEX,
358     INT16_ARRAY_FUN_INDEX,
359     UINT32_ARRAY_FUN_INDEX,
360     INT32_ARRAY_FUN_INDEX,
361     FLOAT32_ARRAY_FUN_INDEX,
362     FLOAT64_ARRAY_FUN_INDEX,
363     UINT8_CLAMPED_ARRAY_FUN_INDEX,
364     INT8_ARRAY_EXTERNAL_MAP_INDEX,
365     UINT8_ARRAY_EXTERNAL_MAP_INDEX,
366     INT16_ARRAY_EXTERNAL_MAP_INDEX,
367     UINT16_ARRAY_EXTERNAL_MAP_INDEX,
368     INT32_ARRAY_EXTERNAL_MAP_INDEX,
369     UINT32_ARRAY_EXTERNAL_MAP_INDEX,
370     FLOAT32_ARRAY_EXTERNAL_MAP_INDEX,
371     FLOAT64_ARRAY_EXTERNAL_MAP_INDEX,
372     UINT8_CLAMPED_ARRAY_EXTERNAL_MAP_INDEX,
373     DATA_VIEW_FUN_INDEX,
374     MESSAGE_LISTENERS_INDEX,
375     MAKE_MESSAGE_FUN_INDEX,
376     GET_STACK_TRACE_LINE_INDEX,
377     CONFIGURE_GLOBAL_INDEX,
378     FUNCTION_CACHE_INDEX,
379     JSFUNCTION_RESULT_CACHES_INDEX,
380     NORMALIZED_MAP_CACHE_INDEX,
381     RUNTIME_CONTEXT_INDEX,
382     CALL_AS_FUNCTION_DELEGATE_INDEX,
383     CALL_AS_CONSTRUCTOR_DELEGATE_INDEX,
384     SCRIPT_FUNCTION_INDEX,
385     OPAQUE_REFERENCE_FUNCTION_INDEX,
386     CONTEXT_EXTENSION_FUNCTION_INDEX,
387     OUT_OF_MEMORY_INDEX,
388     EMBEDDER_DATA_INDEX,
389     ALLOW_CODE_GEN_FROM_STRINGS_INDEX,
390     ERROR_MESSAGE_FOR_CODE_GEN_FROM_STRINGS_INDEX,
391     RUN_MICROTASKS_INDEX,
392     ENQUEUE_MICROTASK_INDEX,
393     PROMISE_STATUS_INDEX,
394     PROMISE_CREATE_INDEX,
395     PROMISE_RESOLVE_INDEX,
396     PROMISE_REJECT_INDEX,
397     PROMISE_CHAIN_INDEX,
398     PROMISE_CATCH_INDEX,
399     PROMISE_THEN_INDEX,
400     TO_COMPLETE_PROPERTY_DESCRIPTOR_INDEX,
401     DERIVED_HAS_TRAP_INDEX,
402     DERIVED_GET_TRAP_INDEX,
403     DERIVED_SET_TRAP_INDEX,
404     PROXY_ENUMERATE_INDEX,
405     OBSERVERS_NOTIFY_CHANGE_INDEX,
406     OBSERVERS_ENQUEUE_SPLICE_INDEX,
407     OBSERVERS_BEGIN_SPLICE_INDEX,
408     OBSERVERS_END_SPLICE_INDEX,
409     NATIVE_OBJECT_OBSERVE_INDEX,
410     NATIVE_OBJECT_GET_NOTIFIER_INDEX,
411     NATIVE_OBJECT_NOTIFIER_PERFORM_CHANGE,
412     SLOPPY_GENERATOR_FUNCTION_MAP_INDEX,
413     STRICT_GENERATOR_FUNCTION_MAP_INDEX,
414     STRONG_GENERATOR_FUNCTION_MAP_INDEX,
415     GENERATOR_OBJECT_PROTOTYPE_MAP_INDEX,
416     ITERATOR_RESULT_MAP_INDEX,
417     MAP_ITERATOR_MAP_INDEX,
418     SET_ITERATOR_MAP_INDEX,
419     ARRAY_VALUES_ITERATOR_INDEX,
420     SCRIPT_CONTEXT_TABLE_INDEX,
421     MAP_CACHE_INDEX,
422     TO_LENGTH_FUN_INDEX,
423
424     // Properties from here are treated as weak references by the full GC.
425     // Scavenge treats them as strong references.
426     OPTIMIZED_FUNCTIONS_LIST,  // Weak.
427     OPTIMIZED_CODE_LIST,       // Weak.
428     DEOPTIMIZED_CODE_LIST,     // Weak.
429     NEXT_CONTEXT_LINK,         // Weak.
430
431     // Total number of slots.
432     NATIVE_CONTEXT_SLOTS,
433     FIRST_WEAK_SLOT = OPTIMIZED_FUNCTIONS_LIST
434   };
435
436   // Direct slot access.
437   JSFunction* closure() { return JSFunction::cast(get(CLOSURE_INDEX)); }
438   void set_closure(JSFunction* closure) { set(CLOSURE_INDEX, closure); }
439
440   Context* previous() {
441     Object* result = unchecked_previous();
442     DCHECK(IsBootstrappingOrValidParentContext(result, this));
443     return reinterpret_cast<Context*>(result);
444   }
445   void set_previous(Context* context) { set(PREVIOUS_INDEX, context); }
446
447   bool has_extension() { return extension() != NULL; }
448   Object* extension() { return get(EXTENSION_INDEX); }
449   void set_extension(Object* object) { set(EXTENSION_INDEX, object); }
450
451   JSModule* module() { return JSModule::cast(get(EXTENSION_INDEX)); }
452   void set_module(JSModule* module) { set(EXTENSION_INDEX, module); }
453
454   // Get the context where var declarations will be hoisted to, which
455   // may be the context itself.
456   Context* declaration_context();
457
458   GlobalObject* global_object() {
459     Object* result = get(GLOBAL_OBJECT_INDEX);
460     DCHECK(IsBootstrappingOrGlobalObject(this->GetIsolate(), result));
461     return reinterpret_cast<GlobalObject*>(result);
462   }
463   void set_global_object(GlobalObject* object) {
464     set(GLOBAL_OBJECT_INDEX, object);
465   }
466
467   // Returns a JSGlobalProxy object or null.
468   JSObject* global_proxy();
469   void set_global_proxy(JSObject* global);
470
471   // The builtins object.
472   JSBuiltinsObject* builtins();
473
474   // Get the script context by traversing the context chain.
475   Context* script_context();
476
477   // Compute the native context by traversing the context chain.
478   Context* native_context();
479
480   // Predicates for context types.  IsNativeContext is also defined on Object
481   // because we frequently have to know if arbitrary objects are natives
482   // contexts.
483   bool IsNativeContext() {
484     Map* map = this->map();
485     return map == map->GetHeap()->native_context_map();
486   }
487   bool IsFunctionContext() {
488     Map* map = this->map();
489     return map == map->GetHeap()->function_context_map();
490   }
491   bool IsCatchContext() {
492     Map* map = this->map();
493     return map == map->GetHeap()->catch_context_map();
494   }
495   bool IsWithContext() {
496     Map* map = this->map();
497     return map == map->GetHeap()->with_context_map();
498   }
499   bool IsBlockContext() {
500     Map* map = this->map();
501     return map == map->GetHeap()->block_context_map();
502   }
503   bool IsModuleContext() {
504     Map* map = this->map();
505     return map == map->GetHeap()->module_context_map();
506   }
507   bool IsScriptContext() {
508     Map* map = this->map();
509     return map == map->GetHeap()->script_context_map();
510   }
511
512   bool HasSameSecurityTokenAs(Context* that) {
513     return this->global_object()->native_context()->security_token() ==
514         that->global_object()->native_context()->security_token();
515   }
516
517   // A native context holds a list of all functions with optimized code.
518   void AddOptimizedFunction(JSFunction* function);
519   void RemoveOptimizedFunction(JSFunction* function);
520   void SetOptimizedFunctionsListHead(Object* head);
521   Object* OptimizedFunctionsListHead();
522
523   // The native context also stores a list of all optimized code and a
524   // list of all deoptimized code, which are needed by the deoptimizer.
525   void AddOptimizedCode(Code* code);
526   void SetOptimizedCodeListHead(Object* head);
527   Object* OptimizedCodeListHead();
528   void SetDeoptimizedCodeListHead(Object* head);
529   Object* DeoptimizedCodeListHead();
530
531   Handle<Object> ErrorMessageForCodeGenerationFromStrings();
532
533 #define NATIVE_CONTEXT_FIELD_ACCESSORS(index, type, name) \
534   void  set_##name(type* value) {                         \
535     DCHECK(IsNativeContext());                            \
536     set(index, value);                                    \
537   }                                                       \
538   bool is_##name(type* value) {                           \
539     DCHECK(IsNativeContext());                            \
540     return type::cast(get(index)) == value;               \
541   }                                                       \
542   type* name() {                                          \
543     DCHECK(IsNativeContext());                            \
544     return type::cast(get(index));                        \
545   }
546   NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_FIELD_ACCESSORS)
547 #undef NATIVE_CONTEXT_FIELD_ACCESSORS
548
549   // Lookup the slot called name, starting with the current context.
550   // There are three possibilities:
551   //
552   // 1) result->IsContext():
553   //    The binding was found in a context.  *index is always the
554   //    non-negative slot index.  *attributes is NONE for var and let
555   //    declarations, READ_ONLY for const declarations (never ABSENT).
556   //
557   // 2) result->IsJSObject():
558   //    The binding was found as a named property in a context extension
559   //    object (i.e., was introduced via eval), as a property on the subject
560   //    of with, or as a property of the global object.  *index is -1 and
561   //    *attributes is not ABSENT.
562   //
563   // 3) result.is_null():
564   //    There was no binding found, *index is always -1 and *attributes is
565   //    always ABSENT.
566   Handle<Object> Lookup(Handle<String> name,
567                         ContextLookupFlags flags,
568                         int* index,
569                         PropertyAttributes* attributes,
570                         BindingFlags* binding_flags);
571
572   // Code generation support.
573   static int SlotOffset(int index) {
574     return kHeaderSize + index * kPointerSize - kHeapObjectTag;
575   }
576
577   static int FunctionMapIndex(LanguageMode language_mode, FunctionKind kind) {
578     if (IsGeneratorFunction(kind)) {
579       return is_strong(language_mode) ? STRONG_GENERATOR_FUNCTION_MAP_INDEX :
580              is_strict(language_mode) ? STRICT_GENERATOR_FUNCTION_MAP_INDEX
581                                       : SLOPPY_GENERATOR_FUNCTION_MAP_INDEX;
582     }
583
584     if (IsConstructor(kind)) {
585       return is_strong(language_mode) ? STRONG_CONSTRUCTOR_MAP_INDEX :
586              is_strict(language_mode) ? STRICT_FUNCTION_MAP_INDEX
587                                       : SLOPPY_FUNCTION_MAP_INDEX;
588     }
589
590     if (IsArrowFunction(kind) || IsConciseMethod(kind) ||
591         IsAccessorFunction(kind)) {
592       return is_strong(language_mode) ? STRONG_FUNCTION_MAP_INDEX :
593              is_strict(language_mode) ?
594                  STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX :
595                  SLOPPY_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX;
596     }
597
598     return is_strong(language_mode) ? STRONG_FUNCTION_MAP_INDEX :
599            is_strict(language_mode) ? STRICT_FUNCTION_MAP_INDEX
600                                     : SLOPPY_FUNCTION_MAP_INDEX;
601   }
602
603   static const int kSize = kHeaderSize + NATIVE_CONTEXT_SLOTS * kPointerSize;
604
605   // GC support.
606   typedef FixedBodyDescriptor<
607       kHeaderSize, kSize, kSize> ScavengeBodyDescriptor;
608
609   typedef FixedBodyDescriptor<
610       kHeaderSize,
611       kHeaderSize + FIRST_WEAK_SLOT * kPointerSize,
612       kSize> MarkCompactBodyDescriptor;
613
614  private:
615   // Unchecked access to the slots.
616   Object* unchecked_previous() { return get(PREVIOUS_INDEX); }
617
618 #ifdef DEBUG
619   // Bootstrapping-aware type checks.
620   static bool IsBootstrappingOrValidParentContext(Object* object, Context* kid);
621   static bool IsBootstrappingOrGlobalObject(Isolate* isolate, Object* object);
622 #endif
623
624   STATIC_ASSERT(kHeaderSize == Internals::kContextHeaderSize);
625   STATIC_ASSERT(EMBEDDER_DATA_INDEX == Internals::kContextEmbedderDataIndex);
626 };
627
628 } }  // namespace v8::internal
629
630 #endif  // V8_CONTEXTS_H_