fb83923b6f2c1d3480cb57873925ddc740ff4596
[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_INTRINSIC_FUNCTIONS(V)                             \
77   V(CONCAT_ITERABLE_TO_ARRAY_INDEX, JSFunction, concat_iterable_to_array) \
78   V(GET_TEMPLATE_CALL_SITE_INDEX, JSFunction, get_template_call_site)     \
79   V(MAKE_RANGE_ERROR_INDEX, JSFunction, make_range_error)                 \
80   V(MAKE_TYPE_ERROR_INDEX, JSFunction, make_type_error)                   \
81   V(NON_NUMBER_TO_NUMBER_INDEX, JSFunction, non_number_to_number)         \
82   V(NON_STRING_TO_STRING_INDEX, JSFunction, non_string_to_string)         \
83   V(REFLECT_APPLY_INDEX, JSFunction, reflect_apply)                       \
84   V(REFLECT_CONSTRUCT_INDEX, JSFunction, reflect_construct)               \
85   V(SPREAD_ARGUMENTS_INDEX, JSFunction, spread_arguments)                 \
86   V(SPREAD_ITERABLE_INDEX, JSFunction, spread_iterable)                   \
87   V(TO_LENGTH_FUN_INDEX, JSFunction, to_length_fun)                       \
88   V(TO_NUMBER_FUN_INDEX, JSFunction, to_number_fun)                       \
89   V(TO_STRING_FUN_INDEX, JSFunction, to_string_fun)
90
91
92 #define NATIVE_CONTEXT_JS_BUILTINS(V)                                 \
93   V(APPLY_PREPARE_BUILTIN_INDEX, JSFunction, apply_prepare_builtin)   \
94   V(CONCAT_ITERABLE_TO_ARRAY_BUILTIN_INDEX, JSFunction,               \
95     concat_iterable_to_array_builtin)                                 \
96   V(REFLECT_APPLY_PREPARE_BUILTIN_INDEX, JSFunction,                  \
97     reflect_apply_prepare_builtin)                                    \
98   V(REFLECT_CONSTRUCT_PREPARE_BUILTIN_INDEX, JSFunction,              \
99     reflect_construct_prepare_builtin)
100
101
102 #define NATIVE_CONTEXT_IMPORTED_FIELDS(V)                                     \
103   V(ARRAY_CONCAT_INDEX, JSFunction, array_concat)                             \
104   V(ARRAY_POP_INDEX, JSFunction, array_pop)                                   \
105   V(ARRAY_PUSH_INDEX, JSFunction, array_push)                                 \
106   V(ARRAY_SHIFT_INDEX, JSFunction, array_shift)                               \
107   V(ARRAY_SPLICE_INDEX, JSFunction, array_splice)                             \
108   V(ARRAY_SLICE_INDEX, JSFunction, array_slice)                               \
109   V(ARRAY_UNSHIFT_INDEX, JSFunction, array_unshift)                           \
110   V(ARRAY_VALUES_ITERATOR_INDEX, JSFunction, array_values_iterator)           \
111   V(CREATE_DATE_FUN_INDEX, JSFunction, create_date_fun)                       \
112   V(DERIVED_GET_TRAP_INDEX, JSFunction, derived_get_trap)                     \
113   V(DERIVED_HAS_TRAP_INDEX, JSFunction, derived_has_trap)                     \
114   V(DERIVED_SET_TRAP_INDEX, JSFunction, derived_set_trap)                     \
115   V(ERROR_FUNCTION_INDEX, JSFunction, error_function)                         \
116   V(EVAL_ERROR_FUNCTION_INDEX, JSFunction, eval_error_function)               \
117   V(GET_STACK_TRACE_LINE_INDEX, JSFunction, get_stack_trace_line_fun)         \
118   V(GLOBAL_EVAL_FUN_INDEX, JSFunction, global_eval_fun)                       \
119   V(JSON_SERIALIZE_ADAPTER_INDEX, JSFunction, json_serialize_adapter)         \
120   V(MAKE_ERROR_FUNCTION_INDEX, JSFunction, make_error_function)               \
121   V(MAP_DELETE_METHOD_INDEX, JSFunction, map_delete)                          \
122   V(MAP_FROM_ARRAY_INDEX, JSFunction, map_from_array)                         \
123   V(MAP_GET_METHOD_INDEX, JSFunction, map_get)                                \
124   V(MAP_HAS_METHOD_INDEX, JSFunction, map_has)                                \
125   V(MAP_SET_METHOD_INDEX, JSFunction, map_set)                                \
126   V(MESSAGE_GET_COLUMN_NUMBER_INDEX, JSFunction, message_get_column_number)   \
127   V(MESSAGE_GET_LINE_NUMBER_INDEX, JSFunction, message_get_line_number)       \
128   V(MESSAGE_GET_SOURCE_LINE_INDEX, JSFunction, message_get_source_line)       \
129   V(NATIVE_OBJECT_GET_NOTIFIER_INDEX, JSFunction, native_object_get_notifier) \
130   V(NATIVE_OBJECT_NOTIFIER_PERFORM_CHANGE, JSFunction,                        \
131     native_object_notifier_perform_change)                                    \
132   V(NATIVE_OBJECT_OBSERVE_INDEX, JSFunction, native_object_observe)           \
133   V(NO_SIDE_EFFECT_TO_STRING_FUN_INDEX, JSFunction,                           \
134     no_side_effect_to_string_fun)                                             \
135   V(OBJECT_DEFINE_OWN_PROPERTY_INDEX, JSFunction, object_define_own_property) \
136   V(OBJECT_GET_OWN_PROPERTY_DESCROPTOR_INDEX, JSFunction,                     \
137     object_get_own_property_descriptor)                                       \
138   V(OBSERVERS_BEGIN_SPLICE_INDEX, JSFunction, observers_begin_perform_splice) \
139   V(OBSERVERS_END_SPLICE_INDEX, JSFunction, observers_end_perform_splice)     \
140   V(OBSERVERS_ENQUEUE_SPLICE_INDEX, JSFunction, observers_enqueue_splice)     \
141   V(OBSERVERS_NOTIFY_CHANGE_INDEX, JSFunction, observers_notify_change)       \
142   V(PROMISE_CATCH_INDEX, JSFunction, promise_catch)                           \
143   V(PROMISE_CHAIN_INDEX, JSFunction, promise_chain)                           \
144   V(PROMISE_CREATE_INDEX, JSFunction, promise_create)                         \
145   V(PROMISE_HAS_USER_DEFINED_REJECT_HANDLER_INDEX, JSFunction,                \
146     promise_has_user_defined_reject_handler)                                  \
147   V(PROMISE_REJECT_INDEX, JSFunction, promise_reject)                         \
148   V(PROMISE_RESOLVE_INDEX, JSFunction, promise_resolve)                       \
149   V(PROMISE_THEN_INDEX, JSFunction, promise_then)                             \
150   V(PROXY_ENUMERATE_INDEX, JSFunction, proxy_enumerate)                       \
151   V(RANGE_ERROR_FUNCTION_INDEX, JSFunction, range_error_function)             \
152   V(REFERENCE_ERROR_FUNCTION_INDEX, JSFunction, reference_error_function)     \
153   V(SET_ADD_METHOD_INDEX, JSFunction, set_add)                                \
154   V(SET_DELETE_METHOD_INDEX, JSFunction, set_delete)                          \
155   V(SET_FROM_ARRAY_INDEX, JSFunction, set_from_array)                         \
156   V(SET_HAS_METHOD_INDEX, JSFunction, set_has)                                \
157   V(STACK_OVERFLOW_BOILERPLATE_INDEX, JSObject, stack_overflow_boilerplate)   \
158   V(SYNTAX_ERROR_FUNCTION_INDEX, JSFunction, syntax_error_function)           \
159   V(TO_COMPLETE_PROPERTY_DESCRIPTOR_INDEX, JSFunction,                        \
160     to_complete_property_descriptor)                                          \
161   V(TO_DETAIL_STRING_FUN_INDEX, JSFunction, to_detail_string_fun)             \
162   V(TO_INTEGER_FUN_INDEX, JSFunction, to_integer_fun)                         \
163   V(TYPE_ERROR_FUNCTION_INDEX, JSFunction, type_error_function)               \
164   V(URI_ERROR_FUNCTION_INDEX, JSFunction, uri_error_function)                 \
165   NATIVE_CONTEXT_JS_BUILTINS(V)
166
167 #define NATIVE_CONTEXT_FIELDS(V)                                               \
168   V(GLOBAL_PROXY_INDEX, JSObject, global_proxy_object)                         \
169   V(EMBEDDER_DATA_INDEX, FixedArray, embedder_data)                            \
170   /* Below is alpha-sorted */                                                  \
171   V(ALLOW_CODE_GEN_FROM_STRINGS_INDEX, Object, allow_code_gen_from_strings)    \
172   V(ARRAY_BUFFER_FUN_INDEX, JSFunction, array_buffer_fun)                      \
173   V(ARRAY_BUFFER_MAP_INDEX, Map, array_buffer_map)                             \
174   V(ARRAY_FUNCTION_INDEX, JSFunction, array_function)                          \
175   V(BOOL16X8_FUNCTION_INDEX, JSFunction, bool16x8_function)                    \
176   V(BOOL32X4_FUNCTION_INDEX, JSFunction, bool32x4_function)                    \
177   V(BOOL8X16_FUNCTION_INDEX, JSFunction, bool8x16_function)                    \
178   V(BOOLEAN_FUNCTION_INDEX, JSFunction, boolean_function)                      \
179   V(BOUND_FUNCTION_MAP_INDEX, Map, bound_function_map)                         \
180   V(CALL_AS_CONSTRUCTOR_DELEGATE_INDEX, JSFunction,                            \
181     call_as_constructor_delegate)                                              \
182   V(CALL_AS_FUNCTION_DELEGATE_INDEX, JSFunction, call_as_function_delegate)    \
183   V(CONTEXT_EXTENSION_FUNCTION_INDEX, JSFunction, context_extension_function)  \
184   V(DATA_VIEW_FUN_INDEX, JSFunction, data_view_fun)                            \
185   V(ERROR_MESSAGE_FOR_CODE_GEN_FROM_STRINGS_INDEX, Object,                     \
186     error_message_for_code_gen_from_strings)                                   \
187   V(EXTRAS_EXPORTS_OBJECT_INDEX, JSObject, extras_binding_object)              \
188   V(EXTRAS_UTILS_OBJECT_INDEX, JSObject, extras_utils_object)                  \
189   V(FAST_ALIASED_ARGUMENTS_MAP_INDEX, Map, fast_aliased_arguments_map)         \
190   V(FLOAT32_ARRAY_FUN_INDEX, JSFunction, float32_array_fun)                    \
191   V(FLOAT32X4_FUNCTION_INDEX, JSFunction, float32x4_function)                  \
192   V(FLOAT64_ARRAY_FUN_INDEX, JSFunction, float64_array_fun)                    \
193   V(FUNCTION_CACHE_INDEX, ObjectHashTable, function_cache)                     \
194   V(GENERATOR_OBJECT_PROTOTYPE_MAP_INDEX, Map, generator_object_prototype_map) \
195   V(INITIAL_ARRAY_PROTOTYPE_INDEX, JSObject, initial_array_prototype)          \
196   V(INITIAL_OBJECT_PROTOTYPE_INDEX, JSObject, initial_object_prototype)        \
197   V(INT16_ARRAY_FUN_INDEX, JSFunction, int16_array_fun)                        \
198   V(INT16X8_FUNCTION_INDEX, JSFunction, int16x8_function)                      \
199   V(INT32_ARRAY_FUN_INDEX, JSFunction, int32_array_fun)                        \
200   V(INT32X4_FUNCTION_INDEX, JSFunction, int32x4_function)                      \
201   V(INT8_ARRAY_FUN_INDEX, JSFunction, int8_array_fun)                          \
202   V(INT8X16_FUNCTION_INDEX, JSFunction, int8x16_function)                      \
203   V(INTERNAL_ARRAY_FUNCTION_INDEX, JSFunction, internal_array_function)        \
204   V(ITERATOR_RESULT_MAP_INDEX, Map, iterator_result_map)                       \
205   V(JS_ARRAY_MAPS_INDEX, Object, js_array_maps)                                \
206   V(JS_ARRAY_STRONG_MAPS_INDEX, Object, js_array_strong_maps)                  \
207   V(JS_MAP_FUN_INDEX, JSFunction, js_map_fun)                                  \
208   V(JS_MAP_MAP_INDEX, Map, js_map_map)                                         \
209   V(JS_OBJECT_STRONG_MAP_INDEX, Map, js_object_strong_map)                     \
210   V(JS_SET_FUN_INDEX, JSFunction, js_set_fun)                                  \
211   V(JS_SET_MAP_INDEX, Map, js_set_map)                                         \
212   V(MAP_CACHE_INDEX, Object, map_cache)                                        \
213   V(MAP_ITERATOR_MAP_INDEX, Map, map_iterator_map)                             \
214   V(STRING_ITERATOR_MAP_INDEX, Map, string_iterator_map)                       \
215   V(MESSAGE_LISTENERS_INDEX, JSObject, message_listeners)                      \
216   V(NATIVES_UTILS_OBJECT_INDEX, Object, natives_utils_object)                  \
217   V(NORMALIZED_MAP_CACHE_INDEX, Object, normalized_map_cache)                  \
218   V(NUMBER_FUNCTION_INDEX, JSFunction, number_function)                        \
219   V(OBJECT_FUNCTION_INDEX, JSFunction, object_function)                        \
220   V(OPAQUE_REFERENCE_FUNCTION_INDEX, JSFunction, opaque_reference_function)    \
221   V(REGEXP_FUNCTION_INDEX, JSFunction, regexp_function)                        \
222   V(REGEXP_RESULT_MAP_INDEX, Map, regexp_result_map)                           \
223   V(RUNTIME_CONTEXT_INDEX, Context, runtime_context)                           \
224   V(SCRIPT_CONTEXT_TABLE_INDEX, ScriptContextTable, script_context_table)      \
225   V(SCRIPT_FUNCTION_INDEX, JSFunction, script_function)                        \
226   V(SECURITY_TOKEN_INDEX, Object, security_token)                              \
227   V(SET_ITERATOR_MAP_INDEX, Map, set_iterator_map)                             \
228   V(SHARED_ARRAY_BUFFER_FUN_INDEX, JSFunction, shared_array_buffer_fun)        \
229   V(SLOPPY_ARGUMENTS_MAP_INDEX, Map, sloppy_arguments_map)                     \
230   V(SLOPPY_FUNCTION_MAP_INDEX, Map, sloppy_function_map)                       \
231   V(SLOPPY_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map,                          \
232     sloppy_function_without_prototype_map)                                     \
233   V(SLOPPY_FUNCTION_WITH_READONLY_PROTOTYPE_MAP_INDEX, Map,                    \
234     sloppy_function_with_readonly_prototype_map)                               \
235   V(SLOPPY_GENERATOR_FUNCTION_MAP_INDEX, Map, sloppy_generator_function_map)   \
236   V(SLOW_ALIASED_ARGUMENTS_MAP_INDEX, Map, slow_aliased_arguments_map)         \
237   V(STRICT_ARGUMENTS_MAP_INDEX, Map, strict_arguments_map)                     \
238   V(STRICT_FUNCTION_MAP_INDEX, Map, strict_function_map)                       \
239   V(STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map,                          \
240     strict_function_without_prototype_map)                                     \
241   V(STRICT_GENERATOR_FUNCTION_MAP_INDEX, Map, strict_generator_function_map)   \
242   V(STRING_FUNCTION_INDEX, JSFunction, string_function)                        \
243   V(STRING_FUNCTION_PROTOTYPE_MAP_INDEX, Map, string_function_prototype_map)   \
244   V(STRONG_CONSTRUCTOR_MAP_INDEX, Map, strong_constructor_map)                 \
245   V(STRONG_FUNCTION_MAP_INDEX, Map, strong_function_map)                       \
246   V(STRONG_GENERATOR_FUNCTION_MAP_INDEX, Map, strong_generator_function_map)   \
247   V(STRONG_MAP_CACHE_INDEX, Object, strong_map_cache)                          \
248   V(SYMBOL_FUNCTION_INDEX, JSFunction, symbol_function)                        \
249   V(UINT16_ARRAY_FUN_INDEX, JSFunction, uint16_array_fun)                      \
250   V(UINT16X8_FUNCTION_INDEX, JSFunction, uint16x8_function)                    \
251   V(UINT32_ARRAY_FUN_INDEX, JSFunction, uint32_array_fun)                      \
252   V(UINT32X4_FUNCTION_INDEX, JSFunction, uint32x4_function)                    \
253   V(UINT8_ARRAY_FUN_INDEX, JSFunction, uint8_array_fun)                        \
254   V(UINT8_CLAMPED_ARRAY_FUN_INDEX, JSFunction, uint8_clamped_array_fun)        \
255   V(UINT8X16_FUNCTION_INDEX, JSFunction, uint8x16_function)                    \
256   NATIVE_CONTEXT_INTRINSIC_FUNCTIONS(V)                                        \
257   NATIVE_CONTEXT_IMPORTED_FIELDS(V)
258
259 // A table of all script contexts. Every loaded top-level script with top-level
260 // lexical declarations contributes its ScriptContext into this table.
261 //
262 // The table is a fixed array, its first slot is the current used count and
263 // the subsequent slots 1..used contain ScriptContexts.
264 class ScriptContextTable : public FixedArray {
265  public:
266   // Conversions.
267   static inline ScriptContextTable* cast(Object* context);
268
269   struct LookupResult {
270     int context_index;
271     int slot_index;
272     VariableMode mode;
273     InitializationFlag init_flag;
274     MaybeAssignedFlag maybe_assigned_flag;
275   };
276
277   inline int used() const;
278   inline void set_used(int used);
279
280   static inline Handle<Context> GetContext(Handle<ScriptContextTable> table,
281                                            int i);
282
283   // Lookup a variable `name` in a ScriptContextTable.
284   // If it returns true, the variable is found and `result` contains
285   // valid information about its location.
286   // If it returns false, `result` is untouched.
287   MUST_USE_RESULT
288   static bool Lookup(Handle<ScriptContextTable> table, Handle<String> name,
289                      LookupResult* result);
290
291   MUST_USE_RESULT
292   static Handle<ScriptContextTable> Extend(Handle<ScriptContextTable> table,
293                                            Handle<Context> script_context);
294
295   static int GetContextOffset(int context_index) {
296     return kFirstContextOffset + context_index * kPointerSize;
297   }
298
299  private:
300   static const int kUsedSlot = 0;
301   static const int kFirstContextSlot = kUsedSlot + 1;
302   static const int kFirstContextOffset =
303       FixedArray::kHeaderSize + kFirstContextSlot * kPointerSize;
304
305   DISALLOW_IMPLICIT_CONSTRUCTORS(ScriptContextTable);
306 };
307
308 // JSFunctions are pairs (context, function code), sometimes also called
309 // closures. A Context object is used to represent function contexts and
310 // dynamically pushed 'with' contexts (or 'scopes' in ECMA-262 speak).
311 //
312 // At runtime, the contexts build a stack in parallel to the execution
313 // stack, with the top-most context being the current context. All contexts
314 // have the following slots:
315 //
316 // [ closure   ]  This is the current function. It is the same for all
317 //                contexts inside a function. It provides access to the
318 //                incoming context (i.e., the outer context, which may
319 //                or may not become the current function's context), and
320 //                it provides access to the functions code and thus it's
321 //                scope information, which in turn contains the names of
322 //                statically allocated context slots. The names are needed
323 //                for dynamic lookups in the presence of 'with' or 'eval'.
324 //
325 // [ previous  ]  A pointer to the previous context. It is NULL for
326 //                function contexts, and non-NULL for 'with' contexts.
327 //                Used to implement the 'with' statement.
328 //
329 // [ extension ]  A pointer to an extension JSObject, or NULL. Used to
330 //                implement 'with' statements and dynamic declarations
331 //                (through 'eval'). The object in a 'with' statement is
332 //                stored in the extension slot of a 'with' context.
333 //                Dynamically declared variables/functions are also added
334 //                to lazily allocated extension object. Context::Lookup
335 //                searches the extension object for properties.
336 //                For script and block contexts, contains the respective
337 //                ScopeInfo. For block contexts representing sloppy declaration
338 //                block scopes, it may also be a struct being a
339 //                SloppyBlockWithEvalContextExtension, pairing the ScopeInfo
340 //                with an extension object.
341 //                For module contexts, points back to the respective JSModule.
342 //
343 // [ global_object ]  A pointer to the global object. Provided for quick
344 //                access to the global object from inside the code (since
345 //                we always have a context pointer).
346 //
347 // In addition, function contexts may have statically allocated context slots
348 // to store local variables/functions that are accessed from inner functions
349 // (via static context addresses) or through 'eval' (dynamic context lookups).
350 // The native context contains additional slots for fast access to native
351 // properties.
352 //
353 // Finally, with Harmony scoping, the JSFunction representing a top level
354 // script will have the ScriptContext rather than a FunctionContext.
355 // Script contexts from all top-level scripts are gathered in
356 // ScriptContextTable.
357
358 class Context: public FixedArray {
359  public:
360   // Conversions.
361   static inline Context* cast(Object* context);
362
363   // The default context slot layout; indices are FixedArray slot indices.
364   enum {
365     // These slots are in all contexts.
366     CLOSURE_INDEX,
367     PREVIOUS_INDEX,
368     // The extension slot is used for either the global object (in global
369     // contexts), eval extension object (function contexts), subject of with
370     // (with contexts), or the variable name (catch contexts), the serialized
371     // scope info (block contexts), or the module instance (module contexts).
372     EXTENSION_INDEX,
373     GLOBAL_OBJECT_INDEX,
374
375     // These slots are only in native contexts.
376 #define NATIVE_CONTEXT_SLOT(index, type, name) index,
377     NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_SLOT)
378 #undef NATIVE_CONTEXT_SLOT
379
380     // Properties from here are treated as weak references by the full GC.
381     // Scavenge treats them as strong references.
382     OPTIMIZED_FUNCTIONS_LIST,  // Weak.
383     OPTIMIZED_CODE_LIST,       // Weak.
384     DEOPTIMIZED_CODE_LIST,     // Weak.
385     NEXT_CONTEXT_LINK,         // Weak.
386
387     // Total number of slots.
388     NATIVE_CONTEXT_SLOTS,
389     FIRST_WEAK_SLOT = OPTIMIZED_FUNCTIONS_LIST,
390
391     MIN_CONTEXT_SLOTS = GLOBAL_PROXY_INDEX,
392     // This slot holds the thrown value in catch contexts.
393     THROWN_OBJECT_INDEX = MIN_CONTEXT_SLOTS,
394   };
395
396   // Direct slot access.
397   inline JSFunction* closure();
398   inline void set_closure(JSFunction* closure);
399
400   inline Context* previous();
401   inline void set_previous(Context* context);
402
403   inline bool has_extension();
404   inline Object* extension();
405   inline void set_extension(Object* object);
406   JSObject* extension_object();
407   JSReceiver* extension_receiver();
408   ScopeInfo* scope_info();
409   String* catch_name();
410
411   inline JSModule* module();
412   inline void set_module(JSModule* module);
413
414   // Get the context where var declarations will be hoisted to, which
415   // may be the context itself.
416   Context* declaration_context();
417   bool is_declaration_context();
418
419   inline GlobalObject* global_object();
420   inline void set_global_object(GlobalObject* object);
421
422   // Returns a JSGlobalProxy object or null.
423   JSObject* global_proxy();
424   void set_global_proxy(JSObject* global);
425
426   // The builtins object.
427   JSBuiltinsObject* builtins();
428
429   // Get the script context by traversing the context chain.
430   Context* script_context();
431
432   // Compute the native context by traversing the context chain.
433   Context* native_context();
434
435   // Predicates for context types.  IsNativeContext is also defined on Object
436   // because we frequently have to know if arbitrary objects are natives
437   // contexts.
438   inline bool IsNativeContext();
439   inline bool IsFunctionContext();
440   inline bool IsCatchContext();
441   inline bool IsWithContext();
442   inline bool IsBlockContext();
443   inline bool IsModuleContext();
444   inline bool IsScriptContext();
445
446   inline bool HasSameSecurityTokenAs(Context* that);
447
448   // Initializes global variable bindings in given script context.
449   void InitializeGlobalSlots();
450
451   // A native context holds a list of all functions with optimized code.
452   void AddOptimizedFunction(JSFunction* function);
453   void RemoveOptimizedFunction(JSFunction* function);
454   void SetOptimizedFunctionsListHead(Object* head);
455   Object* OptimizedFunctionsListHead();
456
457   // The native context also stores a list of all optimized code and a
458   // list of all deoptimized code, which are needed by the deoptimizer.
459   void AddOptimizedCode(Code* code);
460   void SetOptimizedCodeListHead(Object* head);
461   Object* OptimizedCodeListHead();
462   void SetDeoptimizedCodeListHead(Object* head);
463   Object* DeoptimizedCodeListHead();
464
465   Handle<Object> ErrorMessageForCodeGenerationFromStrings();
466
467   static int ImportedFieldIndexForName(Handle<String> name);
468   static int IntrinsicIndexForName(Handle<String> name);
469
470   static bool IsJSBuiltin(Handle<Context> native_context,
471                           Handle<JSFunction> function);
472
473 #define NATIVE_CONTEXT_FIELD_ACCESSORS(index, type, name) \
474   inline void set_##name(type* value);                    \
475   inline bool is_##name(type* value);                     \
476   inline type* name();
477   NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_FIELD_ACCESSORS)
478 #undef NATIVE_CONTEXT_FIELD_ACCESSORS
479
480   // Lookup the slot called name, starting with the current context.
481   // There are three possibilities:
482   //
483   // 1) result->IsContext():
484   //    The binding was found in a context.  *index is always the
485   //    non-negative slot index.  *attributes is NONE for var and let
486   //    declarations, READ_ONLY for const declarations (never ABSENT).
487   //
488   // 2) result->IsJSObject():
489   //    The binding was found as a named property in a context extension
490   //    object (i.e., was introduced via eval), as a property on the subject
491   //    of with, or as a property of the global object.  *index is -1 and
492   //    *attributes is not ABSENT.
493   //
494   // 3) result.is_null():
495   //    There was no binding found, *index is always -1 and *attributes is
496   //    always ABSENT.
497   Handle<Object> Lookup(Handle<String> name,
498                         ContextLookupFlags flags,
499                         int* index,
500                         PropertyAttributes* attributes,
501                         BindingFlags* binding_flags);
502
503   // Code generation support.
504   static int SlotOffset(int index) {
505     return kHeaderSize + index * kPointerSize - kHeapObjectTag;
506   }
507
508   static int FunctionMapIndex(LanguageMode language_mode, FunctionKind kind) {
509     if (IsGeneratorFunction(kind)) {
510       return is_strong(language_mode) ? STRONG_GENERATOR_FUNCTION_MAP_INDEX :
511              is_strict(language_mode) ? STRICT_GENERATOR_FUNCTION_MAP_INDEX
512                                       : SLOPPY_GENERATOR_FUNCTION_MAP_INDEX;
513     }
514
515     if (IsConstructor(kind)) {
516       // Use strict function map (no own "caller" / "arguments")
517       return is_strong(language_mode) ? STRONG_CONSTRUCTOR_MAP_INDEX
518                                       : STRICT_FUNCTION_MAP_INDEX;
519     }
520
521     if (IsArrowFunction(kind) || IsConciseMethod(kind) ||
522         IsAccessorFunction(kind)) {
523       return is_strong(language_mode)
524                  ? STRONG_FUNCTION_MAP_INDEX
525                  : STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX;
526     }
527
528     return is_strong(language_mode) ? STRONG_FUNCTION_MAP_INDEX :
529            is_strict(language_mode) ? STRICT_FUNCTION_MAP_INDEX
530                                     : SLOPPY_FUNCTION_MAP_INDEX;
531   }
532
533   static const int kSize = kHeaderSize + NATIVE_CONTEXT_SLOTS * kPointerSize;
534   static const int kNotFound = -1;
535
536   // GC support.
537   typedef FixedBodyDescriptor<
538       kHeaderSize, kSize, kSize> ScavengeBodyDescriptor;
539
540   typedef FixedBodyDescriptor<
541       kHeaderSize,
542       kHeaderSize + FIRST_WEAK_SLOT * kPointerSize,
543       kSize> MarkCompactBodyDescriptor;
544
545  private:
546 #ifdef DEBUG
547   // Bootstrapping-aware type checks.
548   static bool IsBootstrappingOrValidParentContext(Object* object, Context* kid);
549   static bool IsBootstrappingOrGlobalObject(Isolate* isolate, Object* object);
550 #endif
551
552   STATIC_ASSERT(kHeaderSize == Internals::kContextHeaderSize);
553   STATIC_ASSERT(EMBEDDER_DATA_INDEX == Internals::kContextEmbedderDataIndex);
554 };
555
556 } }  // namespace v8::internal
557
558 #endif  // V8_CONTEXTS_H_