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