fc29831942645a8cce99de3472e4908b178df8d2
[platform/upstream/v8.git] / 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(FLOAT32X4_FUNCTION_INDEX, JSFunction, float32x4_function)                  \
85   V(OBJECT_FUNCTION_INDEX, JSFunction, object_function)                        \
86   V(JS_OBJECT_STRONG_MAP_INDEX, Map, js_object_strong_map)                     \
87   V(INTERNAL_ARRAY_FUNCTION_INDEX, JSFunction, internal_array_function)        \
88   V(ARRAY_FUNCTION_INDEX, JSFunction, array_function)                          \
89   V(JS_ARRAY_MAPS_INDEX, Object, js_array_maps)                                \
90   V(JS_ARRAY_STRONG_MAPS_INDEX, Object, js_array_strong_maps)                  \
91   V(DATE_FUNCTION_INDEX, JSFunction, date_function)                            \
92   V(JSON_OBJECT_INDEX, JSObject, json_object)                                  \
93   V(REGEXP_FUNCTION_INDEX, JSFunction, regexp_function)                        \
94   V(INITIAL_OBJECT_PROTOTYPE_INDEX, JSObject, initial_object_prototype)        \
95   V(INITIAL_ARRAY_PROTOTYPE_INDEX, JSObject, initial_array_prototype)          \
96   V(CREATE_DATE_FUN_INDEX, JSFunction, create_date_fun)                        \
97   V(TO_NUMBER_FUN_INDEX, JSFunction, to_number_fun)                            \
98   V(TO_STRING_FUN_INDEX, JSFunction, to_string_fun)                            \
99   V(TO_DETAIL_STRING_FUN_INDEX, JSFunction, to_detail_string_fun)              \
100   V(TO_OBJECT_FUN_INDEX, JSFunction, to_object_fun)                            \
101   V(TO_INTEGER_FUN_INDEX, JSFunction, to_integer_fun)                          \
102   V(TO_UINT32_FUN_INDEX, JSFunction, to_uint32_fun)                            \
103   V(TO_INT32_FUN_INDEX, JSFunction, to_int32_fun)                              \
104   V(TO_LENGTH_FUN_INDEX, JSFunction, to_length_fun)                            \
105   V(GLOBAL_EVAL_FUN_INDEX, JSFunction, global_eval_fun)                        \
106   V(ARRAY_BUFFER_FUN_INDEX, JSFunction, array_buffer_fun)                      \
107   V(SHARED_ARRAY_BUFFER_FUN_INDEX, JSFunction, shared_array_buffer_fun)        \
108   V(ARRAY_BUFFER_MAP_INDEX, Map, array_buffer_map)                             \
109   V(UINT8_ARRAY_FUN_INDEX, JSFunction, uint8_array_fun)                        \
110   V(INT8_ARRAY_FUN_INDEX, JSFunction, int8_array_fun)                          \
111   V(UINT16_ARRAY_FUN_INDEX, JSFunction, uint16_array_fun)                      \
112   V(INT16_ARRAY_FUN_INDEX, JSFunction, int16_array_fun)                        \
113   V(UINT32_ARRAY_FUN_INDEX, JSFunction, uint32_array_fun)                      \
114   V(INT32_ARRAY_FUN_INDEX, JSFunction, int32_array_fun)                        \
115   V(FLOAT32_ARRAY_FUN_INDEX, JSFunction, float32_array_fun)                    \
116   V(FLOAT64_ARRAY_FUN_INDEX, JSFunction, float64_array_fun)                    \
117   V(UINT8_CLAMPED_ARRAY_FUN_INDEX, JSFunction, uint8_clamped_array_fun)        \
118   V(INT8_ARRAY_EXTERNAL_MAP_INDEX, Map, int8_array_external_map)               \
119   V(UINT8_ARRAY_EXTERNAL_MAP_INDEX, Map, uint8_array_external_map)             \
120   V(INT16_ARRAY_EXTERNAL_MAP_INDEX, Map, int16_array_external_map)             \
121   V(UINT16_ARRAY_EXTERNAL_MAP_INDEX, Map, uint16_array_external_map)           \
122   V(INT32_ARRAY_EXTERNAL_MAP_INDEX, Map, int32_array_external_map)             \
123   V(UINT32_ARRAY_EXTERNAL_MAP_INDEX, Map, uint32_array_external_map)           \
124   V(FLOAT32_ARRAY_EXTERNAL_MAP_INDEX, Map, float32_array_external_map)         \
125   V(FLOAT64_ARRAY_EXTERNAL_MAP_INDEX, Map, float64_array_external_map)         \
126   V(UINT8_CLAMPED_ARRAY_EXTERNAL_MAP_INDEX, Map,                               \
127     uint8_clamped_array_external_map)                                          \
128   V(DATA_VIEW_FUN_INDEX, JSFunction, data_view_fun)                            \
129   V(SLOPPY_FUNCTION_MAP_INDEX, Map, sloppy_function_map)                       \
130   V(SLOPPY_FUNCTION_WITH_READONLY_PROTOTYPE_MAP_INDEX, Map,                    \
131     sloppy_function_with_readonly_prototype_map)                               \
132   V(STRICT_FUNCTION_MAP_INDEX, Map, strict_function_map)                       \
133   V(STRONG_FUNCTION_MAP_INDEX, Map, strong_function_map)                       \
134   V(SLOPPY_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map,                          \
135     sloppy_function_without_prototype_map)                                     \
136   V(STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map,                          \
137     strict_function_without_prototype_map)                                     \
138   V(STRONG_CONSTRUCTOR_MAP_INDEX, Map, strong_constructor_map)                 \
139   V(BOUND_FUNCTION_MAP_INDEX, Map, bound_function_map)                         \
140   V(REGEXP_RESULT_MAP_INDEX, Map, regexp_result_map)                           \
141   V(SLOPPY_ARGUMENTS_MAP_INDEX, Map, sloppy_arguments_map)                     \
142   V(FAST_ALIASED_ARGUMENTS_MAP_INDEX, Map, fast_aliased_arguments_map)         \
143   V(SLOW_ALIASED_ARGUMENTS_MAP_INDEX, Map, slow_aliased_arguments_map)         \
144   V(STRICT_ARGUMENTS_MAP_INDEX, Map, strict_arguments_map)                     \
145   V(MESSAGE_LISTENERS_INDEX, JSObject, message_listeners)                      \
146   V(MAKE_MESSAGE_FUN_INDEX, JSFunction, make_message_fun)                      \
147   V(GET_STACK_TRACE_LINE_INDEX, JSFunction, get_stack_trace_line_fun)          \
148   V(CONFIGURE_GLOBAL_INDEX, JSFunction, configure_global_fun)                  \
149   V(FUNCTION_CACHE_INDEX, ObjectHashTable, function_cache)                     \
150   V(JSFUNCTION_RESULT_CACHES_INDEX, FixedArray, jsfunction_result_caches)      \
151   V(NORMALIZED_MAP_CACHE_INDEX, Object, normalized_map_cache)                  \
152   V(RUNTIME_CONTEXT_INDEX, Context, runtime_context)                           \
153   V(CALL_AS_FUNCTION_DELEGATE_INDEX, JSFunction, call_as_function_delegate)    \
154   V(CALL_AS_CONSTRUCTOR_DELEGATE_INDEX, JSFunction,                            \
155     call_as_constructor_delegate)                                              \
156   V(SCRIPT_FUNCTION_INDEX, JSFunction, script_function)                        \
157   V(OPAQUE_REFERENCE_FUNCTION_INDEX, JSFunction, opaque_reference_function)    \
158   V(CONTEXT_EXTENSION_FUNCTION_INDEX, JSFunction, context_extension_function)  \
159   V(MAP_CACHE_INDEX, Object, map_cache)                                        \
160   V(STRONG_MAP_CACHE_INDEX, Object, strong_map_cache)                          \
161   V(EMBEDDER_DATA_INDEX, FixedArray, embedder_data)                            \
162   V(ALLOW_CODE_GEN_FROM_STRINGS_INDEX, Object, allow_code_gen_from_strings)    \
163   V(ERROR_MESSAGE_FOR_CODE_GEN_FROM_STRINGS_INDEX, Object,                     \
164     error_message_for_code_gen_from_strings)                                   \
165   V(PROMISE_STATUS_INDEX, Symbol, promise_status)                              \
166   V(PROMISE_VALUE_INDEX, Symbol, promise_value)                                \
167   V(PROMISE_CREATE_INDEX, JSFunction, promise_create)                          \
168   V(PROMISE_RESOLVE_INDEX, JSFunction, promise_resolve)                        \
169   V(PROMISE_REJECT_INDEX, JSFunction, promise_reject)                          \
170   V(PROMISE_CHAIN_INDEX, JSFunction, promise_chain)                            \
171   V(PROMISE_CATCH_INDEX, JSFunction, promise_catch)                            \
172   V(PROMISE_THEN_INDEX, JSFunction, promise_then)                              \
173   V(TO_COMPLETE_PROPERTY_DESCRIPTOR_INDEX, JSFunction,                         \
174     to_complete_property_descriptor)                                           \
175   V(DERIVED_HAS_TRAP_INDEX, JSFunction, derived_has_trap)                      \
176   V(DERIVED_GET_TRAP_INDEX, JSFunction, derived_get_trap)                      \
177   V(DERIVED_SET_TRAP_INDEX, JSFunction, derived_set_trap)                      \
178   V(PROXY_ENUMERATE_INDEX, JSFunction, proxy_enumerate)                        \
179   V(OBSERVERS_NOTIFY_CHANGE_INDEX, JSFunction, observers_notify_change)        \
180   V(OBSERVERS_ENQUEUE_SPLICE_INDEX, JSFunction, observers_enqueue_splice)      \
181   V(OBSERVERS_BEGIN_SPLICE_INDEX, JSFunction, observers_begin_perform_splice)  \
182   V(OBSERVERS_END_SPLICE_INDEX, JSFunction, observers_end_perform_splice)      \
183   V(NATIVE_OBJECT_OBSERVE_INDEX, JSFunction, native_object_observe)            \
184   V(NATIVE_OBJECT_GET_NOTIFIER_INDEX, JSFunction, native_object_get_notifier)  \
185   V(NATIVE_OBJECT_NOTIFIER_PERFORM_CHANGE, JSFunction,                         \
186     native_object_notifier_perform_change)                                     \
187   V(SLOPPY_GENERATOR_FUNCTION_MAP_INDEX, Map, sloppy_generator_function_map)   \
188   V(STRICT_GENERATOR_FUNCTION_MAP_INDEX, Map, strict_generator_function_map)   \
189   V(STRONG_GENERATOR_FUNCTION_MAP_INDEX, Map, strong_generator_function_map)   \
190   V(GENERATOR_OBJECT_PROTOTYPE_MAP_INDEX, Map, generator_object_prototype_map) \
191   V(ITERATOR_RESULT_MAP_INDEX, Map, iterator_result_map)                       \
192   V(JS_MAP_FUN_INDEX, JSFunction, js_map_fun)                                  \
193   V(JS_MAP_MAP_INDEX, Map, js_map_map)                                         \
194   V(JS_SET_FUN_INDEX, JSFunction, js_set_fun)                                  \
195   V(JS_SET_MAP_INDEX, Map, js_set_map)                                         \
196   V(MAP_GET_METHOD_INDEX, JSFunction, map_get)                                 \
197   V(MAP_SET_METHOD_INDEX, JSFunction, map_set)                                 \
198   V(MAP_HAS_METHOD_INDEX, JSFunction, map_has)                                 \
199   V(MAP_DELETE_METHOD_INDEX, JSFunction, map_delete)                           \
200   V(SET_ADD_METHOD_INDEX, JSFunction, set_add)                                 \
201   V(SET_HAS_METHOD_INDEX, JSFunction, set_has)                                 \
202   V(SET_DELETE_METHOD_INDEX, JSFunction, set_delete)                           \
203   V(MAP_FROM_ARRAY_INDEX, JSFunction, map_from_array)                          \
204   V(SET_FROM_ARRAY_INDEX, JSFunction, set_from_array)                          \
205   V(MAP_ITERATOR_MAP_INDEX, Map, map_iterator_map)                             \
206   V(SET_ITERATOR_MAP_INDEX, Map, set_iterator_map)                             \
207   V(ARRAY_VALUES_ITERATOR_INDEX, JSFunction, array_values_iterator)            \
208   V(SCRIPT_CONTEXT_TABLE_INDEX, ScriptContextTable, script_context_table)      \
209   V(NATIVES_UTILS_OBJECT_INDEX, Object, natives_utils_object)                  \
210   V(EXTRAS_EXPORTS_OBJECT_INDEX, JSObject, extras_exports_object)
211
212
213 // A table of all script contexts. Every loaded top-level script with top-level
214 // lexical declarations contributes its ScriptContext into this table.
215 //
216 // The table is a fixed array, its first slot is the current used count and
217 // the subsequent slots 1..used contain ScriptContexts.
218 class ScriptContextTable : public FixedArray {
219  public:
220   // Conversions.
221   static ScriptContextTable* cast(Object* context) {
222     DCHECK(context->IsScriptContextTable());
223     return reinterpret_cast<ScriptContextTable*>(context);
224   }
225
226   struct LookupResult {
227     int context_index;
228     int slot_index;
229     VariableMode mode;
230     VariableLocation location;
231     InitializationFlag init_flag;
232     MaybeAssignedFlag maybe_assigned_flag;
233   };
234
235   int used() const { return Smi::cast(get(kUsedSlot))->value(); }
236
237   void set_used(int used) { set(kUsedSlot, Smi::FromInt(used)); }
238
239   static Handle<Context> GetContext(Handle<ScriptContextTable> table, int i) {
240     DCHECK(i < table->used());
241     return Handle<Context>::cast(FixedArray::get(table, i + 1));
242   }
243
244   // Lookup a variable `name` in a ScriptContextTable.
245   // If it returns true, the variable is found and `result` contains
246   // valid information about its location.
247   // If it returns false, `result` is untouched.
248   MUST_USE_RESULT
249   static bool Lookup(Handle<ScriptContextTable> table, Handle<String> name,
250                      LookupResult* result);
251
252   MUST_USE_RESULT
253   static Handle<ScriptContextTable> Extend(Handle<ScriptContextTable> table,
254                                            Handle<Context> script_context);
255
256   static int GetContextOffset(int context_index) {
257     return kFirstContextOffset + context_index * kPointerSize;
258   }
259
260  private:
261   static const int kUsedSlot = 0;
262   static const int kFirstContextOffset =
263       FixedArray::kHeaderSize + (kUsedSlot + 1) * kPointerSize;
264
265   DISALLOW_IMPLICIT_CONSTRUCTORS(ScriptContextTable);
266 };
267
268 // JSFunctions are pairs (context, function code), sometimes also called
269 // closures. A Context object is used to represent function contexts and
270 // dynamically pushed 'with' contexts (or 'scopes' in ECMA-262 speak).
271 //
272 // At runtime, the contexts build a stack in parallel to the execution
273 // stack, with the top-most context being the current context. All contexts
274 // have the following slots:
275 //
276 // [ closure   ]  This is the current function. It is the same for all
277 //                contexts inside a function. It provides access to the
278 //                incoming context (i.e., the outer context, which may
279 //                or may not become the current function's context), and
280 //                it provides access to the functions code and thus it's
281 //                scope information, which in turn contains the names of
282 //                statically allocated context slots. The names are needed
283 //                for dynamic lookups in the presence of 'with' or 'eval'.
284 //
285 // [ previous  ]  A pointer to the previous context. It is NULL for
286 //                function contexts, and non-NULL for 'with' contexts.
287 //                Used to implement the 'with' statement.
288 //
289 // [ extension ]  A pointer to an extension JSObject, or NULL. Used to
290 //                implement 'with' statements and dynamic declarations
291 //                (through 'eval'). The object in a 'with' statement is
292 //                stored in the extension slot of a 'with' context.
293 //                Dynamically declared variables/functions are also added
294 //                to lazily allocated extension object. Context::Lookup
295 //                searches the extension object for properties.
296 //                For global and block contexts, contains the respective
297 //                ScopeInfo.
298 //                For module contexts, points back to the respective JSModule.
299 //
300 // [ global_object ]  A pointer to the global object. Provided for quick
301 //                access to the global object from inside the code (since
302 //                we always have a context pointer).
303 //
304 // In addition, function contexts may have statically allocated context slots
305 // to store local variables/functions that are accessed from inner functions
306 // (via static context addresses) or through 'eval' (dynamic context lookups).
307 // The native context contains additional slots for fast access to native
308 // properties.
309 //
310 // Finally, with Harmony scoping, the JSFunction representing a top level
311 // script will have the ScriptContext rather than a FunctionContext.
312 // Script contexts from all top-level scripts are gathered in
313 // ScriptContextTable.
314
315 class Context: public FixedArray {
316  public:
317   // Conversions.
318   static Context* cast(Object* context) {
319     DCHECK(context->IsContext());
320     return reinterpret_cast<Context*>(context);
321   }
322
323   // The default context slot layout; indices are FixedArray slot indices.
324   enum {
325     // These slots are in all contexts.
326     CLOSURE_INDEX,
327     PREVIOUS_INDEX,
328     // The extension slot is used for either the global object (in global
329     // contexts), eval extension object (function contexts), subject of with
330     // (with contexts), or the variable name (catch contexts), the serialized
331     // scope info (block contexts), or the module instance (module contexts).
332     EXTENSION_INDEX,
333     GLOBAL_OBJECT_INDEX,
334     MIN_CONTEXT_SLOTS,
335
336     // This slot holds the thrown value in catch contexts.
337     THROWN_OBJECT_INDEX = MIN_CONTEXT_SLOTS,
338
339     // These slots are only in native contexts.
340     GLOBAL_PROXY_INDEX = MIN_CONTEXT_SLOTS,
341     SECURITY_TOKEN_INDEX,
342     SLOPPY_ARGUMENTS_MAP_INDEX,
343     FAST_ALIASED_ARGUMENTS_MAP_INDEX,
344     SLOW_ALIASED_ARGUMENTS_MAP_INDEX,
345     STRICT_ARGUMENTS_MAP_INDEX,
346     REGEXP_RESULT_MAP_INDEX,
347     SLOPPY_FUNCTION_MAP_INDEX,
348     SLOPPY_FUNCTION_WITH_READONLY_PROTOTYPE_MAP_INDEX,
349     STRICT_FUNCTION_MAP_INDEX,
350     STRONG_FUNCTION_MAP_INDEX,
351     SLOPPY_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX,
352     STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX,
353     STRONG_CONSTRUCTOR_MAP_INDEX,
354     BOUND_FUNCTION_MAP_INDEX,
355     INITIAL_OBJECT_PROTOTYPE_INDEX,
356     INITIAL_ARRAY_PROTOTYPE_INDEX,
357     BOOLEAN_FUNCTION_INDEX,
358     NUMBER_FUNCTION_INDEX,
359     STRING_FUNCTION_INDEX,
360     STRING_FUNCTION_PROTOTYPE_MAP_INDEX,
361     SYMBOL_FUNCTION_INDEX,
362     FLOAT32X4_FUNCTION_INDEX,
363     OBJECT_FUNCTION_INDEX,
364     JS_OBJECT_STRONG_MAP_INDEX,
365     INTERNAL_ARRAY_FUNCTION_INDEX,
366     ARRAY_FUNCTION_INDEX,
367     JS_ARRAY_MAPS_INDEX,
368     JS_ARRAY_STRONG_MAPS_INDEX,
369     DATE_FUNCTION_INDEX,
370     JSON_OBJECT_INDEX,
371     REGEXP_FUNCTION_INDEX,
372     CREATE_DATE_FUN_INDEX,
373     TO_NUMBER_FUN_INDEX,
374     TO_STRING_FUN_INDEX,
375     TO_DETAIL_STRING_FUN_INDEX,
376     TO_OBJECT_FUN_INDEX,
377     TO_INTEGER_FUN_INDEX,
378     TO_UINT32_FUN_INDEX,
379     TO_INT32_FUN_INDEX,
380     TO_BOOLEAN_FUN_INDEX,
381     GLOBAL_EVAL_FUN_INDEX,
382     ARRAY_BUFFER_FUN_INDEX,
383     ARRAY_BUFFER_MAP_INDEX,
384     UINT8_ARRAY_FUN_INDEX,
385     INT8_ARRAY_FUN_INDEX,
386     UINT16_ARRAY_FUN_INDEX,
387     INT16_ARRAY_FUN_INDEX,
388     UINT32_ARRAY_FUN_INDEX,
389     INT32_ARRAY_FUN_INDEX,
390     FLOAT32_ARRAY_FUN_INDEX,
391     FLOAT64_ARRAY_FUN_INDEX,
392     UINT8_CLAMPED_ARRAY_FUN_INDEX,
393     INT8_ARRAY_EXTERNAL_MAP_INDEX,
394     UINT8_ARRAY_EXTERNAL_MAP_INDEX,
395     INT16_ARRAY_EXTERNAL_MAP_INDEX,
396     UINT16_ARRAY_EXTERNAL_MAP_INDEX,
397     INT32_ARRAY_EXTERNAL_MAP_INDEX,
398     UINT32_ARRAY_EXTERNAL_MAP_INDEX,
399     FLOAT32_ARRAY_EXTERNAL_MAP_INDEX,
400     FLOAT64_ARRAY_EXTERNAL_MAP_INDEX,
401     UINT8_CLAMPED_ARRAY_EXTERNAL_MAP_INDEX,
402     DATA_VIEW_FUN_INDEX,
403     SHARED_ARRAY_BUFFER_FUN_INDEX,
404     MESSAGE_LISTENERS_INDEX,
405     MAKE_MESSAGE_FUN_INDEX,
406     GET_STACK_TRACE_LINE_INDEX,
407     CONFIGURE_GLOBAL_INDEX,
408     FUNCTION_CACHE_INDEX,
409     JSFUNCTION_RESULT_CACHES_INDEX,
410     NORMALIZED_MAP_CACHE_INDEX,
411     RUNTIME_CONTEXT_INDEX,
412     CALL_AS_FUNCTION_DELEGATE_INDEX,
413     CALL_AS_CONSTRUCTOR_DELEGATE_INDEX,
414     SCRIPT_FUNCTION_INDEX,
415     OPAQUE_REFERENCE_FUNCTION_INDEX,
416     CONTEXT_EXTENSION_FUNCTION_INDEX,
417     OUT_OF_MEMORY_INDEX,
418     EMBEDDER_DATA_INDEX,
419     ALLOW_CODE_GEN_FROM_STRINGS_INDEX,
420     ERROR_MESSAGE_FOR_CODE_GEN_FROM_STRINGS_INDEX,
421     RUN_MICROTASKS_INDEX,
422     ENQUEUE_MICROTASK_INDEX,
423     PROMISE_STATUS_INDEX,
424     PROMISE_VALUE_INDEX,
425     PROMISE_CREATE_INDEX,
426     PROMISE_RESOLVE_INDEX,
427     PROMISE_REJECT_INDEX,
428     PROMISE_CHAIN_INDEX,
429     PROMISE_CATCH_INDEX,
430     PROMISE_THEN_INDEX,
431     TO_COMPLETE_PROPERTY_DESCRIPTOR_INDEX,
432     DERIVED_HAS_TRAP_INDEX,
433     DERIVED_GET_TRAP_INDEX,
434     DERIVED_SET_TRAP_INDEX,
435     PROXY_ENUMERATE_INDEX,
436     OBSERVERS_NOTIFY_CHANGE_INDEX,
437     OBSERVERS_ENQUEUE_SPLICE_INDEX,
438     OBSERVERS_BEGIN_SPLICE_INDEX,
439     OBSERVERS_END_SPLICE_INDEX,
440     NATIVE_OBJECT_OBSERVE_INDEX,
441     NATIVE_OBJECT_GET_NOTIFIER_INDEX,
442     NATIVE_OBJECT_NOTIFIER_PERFORM_CHANGE,
443     SLOPPY_GENERATOR_FUNCTION_MAP_INDEX,
444     STRICT_GENERATOR_FUNCTION_MAP_INDEX,
445     STRONG_GENERATOR_FUNCTION_MAP_INDEX,
446     GENERATOR_OBJECT_PROTOTYPE_MAP_INDEX,
447     ITERATOR_RESULT_MAP_INDEX,
448     JS_MAP_FUN_INDEX,
449     JS_MAP_MAP_INDEX,
450     JS_SET_FUN_INDEX,
451     JS_SET_MAP_INDEX,
452     MAP_GET_METHOD_INDEX,
453     MAP_SET_METHOD_INDEX,
454     MAP_HAS_METHOD_INDEX,
455     MAP_DELETE_METHOD_INDEX,
456     SET_ADD_METHOD_INDEX,
457     SET_HAS_METHOD_INDEX,
458     SET_DELETE_METHOD_INDEX,
459     MAP_FROM_ARRAY_INDEX,
460     SET_FROM_ARRAY_INDEX,
461     MAP_ITERATOR_MAP_INDEX,
462     SET_ITERATOR_MAP_INDEX,
463     ARRAY_VALUES_ITERATOR_INDEX,
464     SCRIPT_CONTEXT_TABLE_INDEX,
465     MAP_CACHE_INDEX,
466     STRONG_MAP_CACHE_INDEX,
467     TO_LENGTH_FUN_INDEX,
468     NATIVES_UTILS_OBJECT_INDEX,
469     EXTRAS_EXPORTS_OBJECT_INDEX,
470     CODE_STUB_EXPORTS_OBJECT_INDEX,
471
472     // Properties from here are treated as weak references by the full GC.
473     // Scavenge treats them as strong references.
474     OPTIMIZED_FUNCTIONS_LIST,  // Weak.
475     OPTIMIZED_CODE_LIST,       // Weak.
476     DEOPTIMIZED_CODE_LIST,     // Weak.
477     NEXT_CONTEXT_LINK,         // Weak.
478
479     // Total number of slots.
480     NATIVE_CONTEXT_SLOTS,
481     FIRST_WEAK_SLOT = OPTIMIZED_FUNCTIONS_LIST
482   };
483
484   // Direct slot access.
485   JSFunction* closure() { return JSFunction::cast(get(CLOSURE_INDEX)); }
486   void set_closure(JSFunction* closure) { set(CLOSURE_INDEX, closure); }
487
488   Context* previous() {
489     Object* result = unchecked_previous();
490     DCHECK(IsBootstrappingOrValidParentContext(result, this));
491     return reinterpret_cast<Context*>(result);
492   }
493   void set_previous(Context* context) { set(PREVIOUS_INDEX, context); }
494
495   bool has_extension() { return extension() != NULL; }
496   Object* extension() { return get(EXTENSION_INDEX); }
497   void set_extension(Object* object) { set(EXTENSION_INDEX, object); }
498
499   JSModule* module() { return JSModule::cast(get(EXTENSION_INDEX)); }
500   void set_module(JSModule* module) { set(EXTENSION_INDEX, module); }
501
502   // Get the context where var declarations will be hoisted to, which
503   // may be the context itself.
504   Context* declaration_context();
505
506   GlobalObject* global_object() {
507     Object* result = get(GLOBAL_OBJECT_INDEX);
508     DCHECK(IsBootstrappingOrGlobalObject(this->GetIsolate(), result));
509     return reinterpret_cast<GlobalObject*>(result);
510   }
511   void set_global_object(GlobalObject* object) {
512     set(GLOBAL_OBJECT_INDEX, object);
513   }
514
515   // Returns a JSGlobalProxy object or null.
516   JSObject* global_proxy();
517   void set_global_proxy(JSObject* global);
518
519   // The builtins object.
520   JSBuiltinsObject* builtins();
521
522   // Get the script context by traversing the context chain.
523   Context* script_context();
524
525   // Compute the native context by traversing the context chain.
526   Context* native_context();
527
528   // Predicates for context types.  IsNativeContext is also defined on Object
529   // because we frequently have to know if arbitrary objects are natives
530   // contexts.
531   bool IsNativeContext() {
532     Map* map = this->map();
533     return map == map->GetHeap()->native_context_map();
534   }
535   bool IsFunctionContext() {
536     Map* map = this->map();
537     return map == map->GetHeap()->function_context_map();
538   }
539   bool IsCatchContext() {
540     Map* map = this->map();
541     return map == map->GetHeap()->catch_context_map();
542   }
543   bool IsWithContext() {
544     Map* map = this->map();
545     return map == map->GetHeap()->with_context_map();
546   }
547   bool IsBlockContext() {
548     Map* map = this->map();
549     return map == map->GetHeap()->block_context_map();
550   }
551   bool IsModuleContext() {
552     Map* map = this->map();
553     return map == map->GetHeap()->module_context_map();
554   }
555   bool IsScriptContext() {
556     Map* map = this->map();
557     return map == map->GetHeap()->script_context_map();
558   }
559
560   bool HasSameSecurityTokenAs(Context* that) {
561     return this->global_object()->native_context()->security_token() ==
562         that->global_object()->native_context()->security_token();
563   }
564
565   // Initializes global variable bindings in given script context.
566   void InitializeGlobalSlots();
567
568   // A native context holds a list of all functions with optimized code.
569   void AddOptimizedFunction(JSFunction* function);
570   void RemoveOptimizedFunction(JSFunction* function);
571   void SetOptimizedFunctionsListHead(Object* head);
572   Object* OptimizedFunctionsListHead();
573
574   // The native context also stores a list of all optimized code and a
575   // list of all deoptimized code, which are needed by the deoptimizer.
576   void AddOptimizedCode(Code* code);
577   void SetOptimizedCodeListHead(Object* head);
578   Object* OptimizedCodeListHead();
579   void SetDeoptimizedCodeListHead(Object* head);
580   Object* DeoptimizedCodeListHead();
581
582   Handle<Object> ErrorMessageForCodeGenerationFromStrings();
583
584 #define NATIVE_CONTEXT_FIELD_ACCESSORS(index, type, name) \
585   void  set_##name(type* value) {                         \
586     DCHECK(IsNativeContext());                            \
587     set(index, value);                                    \
588   }                                                       \
589   bool is_##name(type* value) {                           \
590     DCHECK(IsNativeContext());                            \
591     return type::cast(get(index)) == value;               \
592   }                                                       \
593   type* name() {                                          \
594     DCHECK(IsNativeContext());                            \
595     return type::cast(get(index));                        \
596   }
597   NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_FIELD_ACCESSORS)
598 #undef NATIVE_CONTEXT_FIELD_ACCESSORS
599
600   // Lookup the slot called name, starting with the current context.
601   // There are three possibilities:
602   //
603   // 1) result->IsContext():
604   //    The binding was found in a context.  *index is always the
605   //    non-negative slot index.  *attributes is NONE for var and let
606   //    declarations, READ_ONLY for const declarations (never ABSENT).
607   //
608   // 2) result->IsJSObject():
609   //    The binding was found as a named property in a context extension
610   //    object (i.e., was introduced via eval), as a property on the subject
611   //    of with, or as a property of the global object.  *index is -1 and
612   //    *attributes is not ABSENT.
613   //
614   // 3) result.is_null():
615   //    There was no binding found, *index is always -1 and *attributes is
616   //    always ABSENT.
617   Handle<Object> Lookup(Handle<String> name,
618                         ContextLookupFlags flags,
619                         int* index,
620                         PropertyAttributes* attributes,
621                         BindingFlags* binding_flags);
622
623   // Code generation support.
624   static int SlotOffset(int index) {
625     return kHeaderSize + index * kPointerSize - kHeapObjectTag;
626   }
627
628   static int FunctionMapIndex(LanguageMode language_mode, FunctionKind kind) {
629     if (IsGeneratorFunction(kind)) {
630       return is_strong(language_mode) ? STRONG_GENERATOR_FUNCTION_MAP_INDEX :
631              is_strict(language_mode) ? STRICT_GENERATOR_FUNCTION_MAP_INDEX
632                                       : SLOPPY_GENERATOR_FUNCTION_MAP_INDEX;
633     }
634
635     if (IsConstructor(kind)) {
636       // Use strict function map (no own "caller" / "arguments")
637       return is_strong(language_mode) ? STRONG_CONSTRUCTOR_MAP_INDEX
638                                       : STRICT_FUNCTION_MAP_INDEX;
639     }
640
641     if (IsArrowFunction(kind) || IsConciseMethod(kind) ||
642         IsAccessorFunction(kind)) {
643       return is_strong(language_mode)
644                  ? STRONG_FUNCTION_MAP_INDEX
645                  : STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX;
646     }
647
648     return is_strong(language_mode) ? STRONG_FUNCTION_MAP_INDEX :
649            is_strict(language_mode) ? STRICT_FUNCTION_MAP_INDEX
650                                     : SLOPPY_FUNCTION_MAP_INDEX;
651   }
652
653   static const int kSize = kHeaderSize + NATIVE_CONTEXT_SLOTS * kPointerSize;
654
655   // GC support.
656   typedef FixedBodyDescriptor<
657       kHeaderSize, kSize, kSize> ScavengeBodyDescriptor;
658
659   typedef FixedBodyDescriptor<
660       kHeaderSize,
661       kHeaderSize + FIRST_WEAK_SLOT * kPointerSize,
662       kSize> MarkCompactBodyDescriptor;
663
664  private:
665   // Unchecked access to the slots.
666   Object* unchecked_previous() { return get(PREVIOUS_INDEX); }
667
668 #ifdef DEBUG
669   // Bootstrapping-aware type checks.
670   static bool IsBootstrappingOrValidParentContext(Object* object, Context* kid);
671   static bool IsBootstrappingOrGlobalObject(Isolate* isolate, Object* object);
672 #endif
673
674   STATIC_ASSERT(kHeaderSize == Internals::kContextHeaderSize);
675   STATIC_ASSERT(EMBEDDER_DATA_INDEX == Internals::kContextEmbedderDataIndex);
676 };
677
678 } }  // namespace v8::internal
679
680 #endif  // V8_CONTEXTS_H_