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
11namespace v8 {
12namespace internal {
13
14class RegExpMatchInfo;
15
16enum ContextLookupFlags {
17  FOLLOW_CONTEXT_CHAIN = 1 << 0,
18  FOLLOW_PROTOTYPE_CHAIN = 1 << 1,
19  STOP_AT_DECLARATION_SCOPE = 1 << 2,
20  SKIP_WITH_CONTEXT = 1 << 3,
21
22  DONT_FOLLOW_CHAINS = 0,
23  FOLLOW_CHAINS = FOLLOW_CONTEXT_CHAIN | FOLLOW_PROTOTYPE_CHAIN,
24  LEXICAL_TEST =
25      FOLLOW_CONTEXT_CHAIN | STOP_AT_DECLARATION_SCOPE | SKIP_WITH_CONTEXT,
26};
27
28
29// Heap-allocated activation contexts.
30//
31// Contexts are implemented as FixedArray objects; the Context
32// class is a convenience interface casted on a FixedArray object.
33//
34// Note: Context must have no virtual functions and Context objects
35// must always be allocated via Heap::AllocateContext() or
36// Factory::NewContext.
37
38#define NATIVE_CONTEXT_INTRINSIC_FUNCTIONS(V)                           \
39  V(ASYNC_FUNCTION_AWAIT_CAUGHT_INDEX, JSFunction,                      \
40    async_function_await_caught)                                        \
41  V(ASYNC_FUNCTION_AWAIT_UNCAUGHT_INDEX, JSFunction,                    \
42    async_function_await_uncaught)                                      \
43  V(ASYNC_FUNCTION_PROMISE_CREATE_INDEX, JSFunction,                    \
44    async_function_promise_create)                                      \
45  V(ASYNC_FUNCTION_PROMISE_RELEASE_INDEX, JSFunction,                   \
46    async_function_promise_release)                                     \
47  V(IS_ARRAYLIKE, JSFunction, is_arraylike)                             \
48  V(GENERATOR_NEXT_INTERNAL, JSFunction, generator_next_internal)       \
49  V(GET_TEMPLATE_CALL_SITE_INDEX, JSFunction, get_template_call_site)   \
50  V(MAKE_ERROR_INDEX, JSFunction, make_error)                           \
51  V(MAKE_RANGE_ERROR_INDEX, JSFunction, make_range_error)               \
52  V(MAKE_SYNTAX_ERROR_INDEX, JSFunction, make_syntax_error)             \
53  V(MAKE_TYPE_ERROR_INDEX, JSFunction, make_type_error)                 \
54  V(MAKE_URI_ERROR_INDEX, JSFunction, make_uri_error)                   \
55  V(OBJECT_CREATE, JSFunction, object_create)                           \
56  V(OBJECT_DEFINE_PROPERTIES, JSFunction, object_define_properties)     \
57  V(OBJECT_DEFINE_PROPERTY, JSFunction, object_define_property)         \
58  V(OBJECT_FREEZE, JSFunction, object_freeze)                           \
59  V(OBJECT_GET_PROTOTYPE_OF, JSFunction, object_get_prototype_of)       \
60  V(OBJECT_IS_EXTENSIBLE, JSFunction, object_is_extensible)             \
61  V(OBJECT_IS_FROZEN, JSFunction, object_is_frozen)                     \
62  V(OBJECT_IS_SEALED, JSFunction, object_is_sealed)                     \
63  V(OBJECT_KEYS, JSFunction, object_keys)                               \
64  V(REGEXP_INTERNAL_MATCH, JSFunction, regexp_internal_match)           \
65  V(REFLECT_APPLY_INDEX, JSFunction, reflect_apply)                     \
66  V(REFLECT_CONSTRUCT_INDEX, JSFunction, reflect_construct)             \
67  V(REFLECT_DEFINE_PROPERTY_INDEX, JSFunction, reflect_define_property) \
68  V(REFLECT_DELETE_PROPERTY_INDEX, JSFunction, reflect_delete_property) \
69  V(SPREAD_ARGUMENTS_INDEX, JSFunction, spread_arguments)               \
70  V(SPREAD_ITERABLE_INDEX, JSFunction, spread_iterable)                 \
71  V(MATH_FLOOR_INDEX, JSFunction, math_floor)                           \
72  V(MATH_POW_INDEX, JSFunction, math_pow)                               \
73  V(NEW_PROMISE_CAPABILITY_INDEX, JSFunction, new_promise_capability)   \
74  V(PROMISE_INTERNAL_CONSTRUCTOR_INDEX, JSFunction,                     \
75    promise_internal_constructor)                                       \
76  V(PROMISE_INTERNAL_REJECT_INDEX, JSFunction, promise_internal_reject) \
77  V(IS_PROMISE_INDEX, JSFunction, is_promise)                           \
78  V(PROMISE_RESOLVE_INDEX, JSFunction, promise_resolve)                 \
79  V(PROMISE_THEN_INDEX, JSFunction, promise_then)                       \
80  V(PROMISE_HANDLE_INDEX, JSFunction, promise_handle)                   \
81  V(PROMISE_HANDLE_REJECT_INDEX, JSFunction, promise_handle_reject)
82
83#define NATIVE_CONTEXT_IMPORTED_FIELDS(V)                                     \
84  V(ARRAY_CONCAT_INDEX, JSFunction, array_concat)                             \
85  V(ARRAY_POP_INDEX, JSFunction, array_pop)                                   \
86  V(ARRAY_PUSH_INDEX, JSFunction, array_push)                                 \
87  V(ARRAY_SHIFT_INDEX, JSFunction, array_shift)                               \
88  V(ARRAY_SPLICE_INDEX, JSFunction, array_splice)                             \
89  V(ARRAY_SLICE_INDEX, JSFunction, array_slice)                               \
90  V(ARRAY_UNSHIFT_INDEX, JSFunction, array_unshift)                           \
91  V(ARRAY_ENTRIES_ITERATOR_INDEX, JSFunction, array_entries_iterator)         \
92  V(ARRAY_FOR_EACH_ITERATOR_INDEX, JSFunction, array_for_each_iterator)       \
93  V(ARRAY_KEYS_ITERATOR_INDEX, JSFunction, array_keys_iterator)               \
94  V(ARRAY_VALUES_ITERATOR_INDEX, JSFunction, array_values_iterator)           \
95  V(DERIVED_GET_TRAP_INDEX, JSFunction, derived_get_trap)                     \
96  V(ERROR_FUNCTION_INDEX, JSFunction, error_function)                         \
97  V(ERROR_TO_STRING, JSFunction, error_to_string)                             \
98  V(EVAL_ERROR_FUNCTION_INDEX, JSFunction, eval_error_function)               \
99  V(GLOBAL_EVAL_FUN_INDEX, JSFunction, global_eval_fun)                       \
100  V(GLOBAL_PROXY_FUNCTION_INDEX, JSFunction, global_proxy_function)           \
101  V(MAP_DELETE_METHOD_INDEX, JSFunction, map_delete)                          \
102  V(MAP_GET_METHOD_INDEX, JSFunction, map_get)                                \
103  V(MAP_HAS_METHOD_INDEX, JSFunction, map_has)                                \
104  V(MAP_SET_METHOD_INDEX, JSFunction, map_set)                                \
105  V(FUNCTION_HAS_INSTANCE_INDEX, JSFunction, function_has_instance)           \
106  V(OBJECT_VALUE_OF, JSFunction, object_value_of)                             \
107  V(OBJECT_TO_STRING, JSFunction, object_to_string)                           \
108  V(PROMISE_CATCH_INDEX, JSFunction, promise_catch)                           \
109  V(PROMISE_FUNCTION_INDEX, JSFunction, promise_function)                     \
110  V(RANGE_ERROR_FUNCTION_INDEX, JSFunction, range_error_function)             \
111  V(REFERENCE_ERROR_FUNCTION_INDEX, JSFunction, reference_error_function)     \
112  V(SET_ADD_METHOD_INDEX, JSFunction, set_add)                                \
113  V(SET_DELETE_METHOD_INDEX, JSFunction, set_delete)                          \
114  V(SET_HAS_METHOD_INDEX, JSFunction, set_has)                                \
115  V(SYNTAX_ERROR_FUNCTION_INDEX, JSFunction, syntax_error_function)           \
116  V(TYPE_ERROR_FUNCTION_INDEX, JSFunction, type_error_function)               \
117  V(URI_ERROR_FUNCTION_INDEX, JSFunction, uri_error_function)                 \
118  V(WASM_COMPILE_ERROR_FUNCTION_INDEX, JSFunction,                            \
119    wasm_compile_error_function)                                              \
120  V(WASM_LINK_ERROR_FUNCTION_INDEX, JSFunction, wasm_link_error_function)     \
121  V(WASM_RUNTIME_ERROR_FUNCTION_INDEX, JSFunction, wasm_runtime_error_function)
122
123#define NATIVE_CONTEXT_JS_ARRAY_ITERATOR_MAPS(V)                               \
124  V(TYPED_ARRAY_KEY_ITERATOR_MAP_INDEX, Map, typed_array_key_iterator_map)     \
125  V(FAST_ARRAY_KEY_ITERATOR_MAP_INDEX, Map, fast_array_key_iterator_map)       \
126  V(GENERIC_ARRAY_KEY_ITERATOR_MAP_INDEX, Map, array_key_iterator_map)         \
127                                                                               \
128  V(UINT8_ARRAY_KEY_VALUE_ITERATOR_MAP_INDEX, Map,                             \
129    uint8_array_key_value_iterator_map)                                        \
130  V(INT8_ARRAY_KEY_VALUE_ITERATOR_MAP_INDEX, Map,                              \
131    int8_array_key_value_iterator_map)                                         \
132  V(UINT16_ARRAY_KEY_VALUE_ITERATOR_MAP_INDEX, Map,                            \
133    uint16_array_key_value_iterator_map)                                       \
134  V(INT16_ARRAY_KEY_VALUE_ITERATOR_MAP_INDEX, Map,                             \
135    int16_array_key_value_iterator_map)                                        \
136  V(UINT32_ARRAY_KEY_VALUE_ITERATOR_MAP_INDEX, Map,                            \
137    uint32_array_key_value_iterator_map)                                       \
138  V(INT32_ARRAY_KEY_VALUE_ITERATOR_MAP_INDEX, Map,                             \
139    int32_array_key_value_iterator_map)                                        \
140  V(FLOAT32_ARRAY_KEY_VALUE_ITERATOR_MAP_INDEX, Map,                           \
141    float32_array_key_value_iterator_map)                                      \
142  V(FLOAT64_ARRAY_KEY_VALUE_ITERATOR_MAP_INDEX, Map,                           \
143    float64_array_key_value_iterator_map)                                      \
144  V(UINT8_CLAMPED_ARRAY_KEY_VALUE_ITERATOR_MAP_INDEX, Map,                     \
145    uint8_clamped_array_key_value_iterator_map)                                \
146                                                                               \
147  V(FAST_SMI_ARRAY_KEY_VALUE_ITERATOR_MAP_INDEX, Map,                          \
148    fast_smi_array_key_value_iterator_map)                                     \
149  V(FAST_HOLEY_SMI_ARRAY_KEY_VALUE_ITERATOR_MAP_INDEX, Map,                    \
150    fast_holey_smi_array_key_value_iterator_map)                               \
151  V(FAST_ARRAY_KEY_VALUE_ITERATOR_MAP_INDEX, Map,                              \
152    fast_array_key_value_iterator_map)                                         \
153  V(FAST_HOLEY_ARRAY_KEY_VALUE_ITERATOR_MAP_INDEX, Map,                        \
154    fast_holey_array_key_value_iterator_map)                                   \
155  V(FAST_DOUBLE_ARRAY_KEY_VALUE_ITERATOR_MAP_INDEX, Map,                       \
156    fast_double_array_key_value_iterator_map)                                  \
157  V(FAST_HOLEY_DOUBLE_ARRAY_KEY_VALUE_ITERATOR_MAP_INDEX, Map,                 \
158    fast_holey_double_array_key_value_iterator_map)                            \
159  V(GENERIC_ARRAY_KEY_VALUE_ITERATOR_MAP_INDEX, Map,                           \
160    array_key_value_iterator_map)                                              \
161                                                                               \
162  V(UINT8_ARRAY_VALUE_ITERATOR_MAP_INDEX, Map, uint8_array_value_iterator_map) \
163  V(INT8_ARRAY_VALUE_ITERATOR_MAP_INDEX, Map, int8_array_value_iterator_map)   \
164  V(UINT16_ARRAY_VALUE_ITERATOR_MAP_INDEX, Map,                                \
165    uint16_array_value_iterator_map)                                           \
166  V(INT16_ARRAY_VALUE_ITERATOR_MAP_INDEX, Map, int16_array_value_iterator_map) \
167  V(UINT32_ARRAY_VALUE_ITERATOR_MAP_INDEX, Map,                                \
168    uint32_array_value_iterator_map)                                           \
169  V(INT32_ARRAY_VALUE_ITERATOR_MAP_INDEX, Map, int32_array_value_iterator_map) \
170  V(FLOAT32_ARRAY_VALUE_ITERATOR_MAP_INDEX, Map,                               \
171    float32_array_value_iterator_map)                                          \
172  V(FLOAT64_ARRAY_VALUE_ITERATOR_MAP_INDEX, Map,                               \
173    float64_array_value_iterator_map)                                          \
174  V(UINT8_CLAMPED_ARRAY_VALUE_ITERATOR_MAP_INDEX, Map,                         \
175    uint8_clamped_array_value_iterator_map)                                    \
176                                                                               \
177  V(FAST_SMI_ARRAY_VALUE_ITERATOR_MAP_INDEX, Map,                              \
178    fast_smi_array_value_iterator_map)                                         \
179  V(FAST_HOLEY_SMI_ARRAY_VALUE_ITERATOR_MAP_INDEX, Map,                        \
180    fast_holey_smi_array_value_iterator_map)                                   \
181  V(FAST_ARRAY_VALUE_ITERATOR_MAP_INDEX, Map, fast_array_value_iterator_map)   \
182  V(FAST_HOLEY_ARRAY_VALUE_ITERATOR_MAP_INDEX, Map,                            \
183    fast_holey_array_value_iterator_map)                                       \
184  V(FAST_DOUBLE_ARRAY_VALUE_ITERATOR_MAP_INDEX, Map,                           \
185    fast_double_array_value_iterator_map)                                      \
186  V(FAST_HOLEY_DOUBLE_ARRAY_VALUE_ITERATOR_MAP_INDEX, Map,                     \
187    fast_holey_double_array_value_iterator_map)                                \
188  V(GENERIC_ARRAY_VALUE_ITERATOR_MAP_INDEX, Map, array_value_iterator_map)
189
190#define NATIVE_CONTEXT_FIELDS(V)                                               \
191  V(GLOBAL_PROXY_INDEX, JSObject, global_proxy_object)                         \
192  V(EMBEDDER_DATA_INDEX, FixedArray, embedder_data)                            \
193  /* Below is alpha-sorted */                                                  \
194  V(ACCESSOR_PROPERTY_DESCRIPTOR_MAP_INDEX, Map,                               \
195    accessor_property_descriptor_map)                                          \
196  V(ALLOW_CODE_GEN_FROM_STRINGS_INDEX, Object, allow_code_gen_from_strings)    \
197  V(ARRAY_BUFFER_FUN_INDEX, JSFunction, array_buffer_fun)                      \
198  V(ARRAY_BUFFER_MAP_INDEX, Map, array_buffer_map)                             \
199  V(ARRAY_FUNCTION_INDEX, JSFunction, array_function)                          \
200  V(ASYNC_FROM_SYNC_ITERATOR_MAP_INDEX, Map, async_from_sync_iterator_map)     \
201  V(ASYNC_FUNCTION_AWAIT_REJECT_SHARED_FUN, SharedFunctionInfo,                \
202    async_function_await_reject_shared_fun)                                    \
203  V(ASYNC_FUNCTION_AWAIT_RESOLVE_SHARED_FUN, SharedFunctionInfo,               \
204    async_function_await_resolve_shared_fun)                                   \
205  V(ASYNC_FUNCTION_FUNCTION_INDEX, JSFunction, async_function_constructor)     \
206  V(ASYNC_ITERATOR_VALUE_UNWRAP_SHARED_FUN, SharedFunctionInfo,                \
207    async_iterator_value_unwrap_shared_fun)                                    \
208  V(BOOLEAN_FUNCTION_INDEX, JSFunction, boolean_function)                      \
209  V(BOUND_FUNCTION_WITH_CONSTRUCTOR_MAP_INDEX, Map,                            \
210    bound_function_with_constructor_map)                                       \
211  V(BOUND_FUNCTION_WITHOUT_CONSTRUCTOR_MAP_INDEX, Map,                         \
212    bound_function_without_constructor_map)                                    \
213  V(CALL_AS_CONSTRUCTOR_DELEGATE_INDEX, JSFunction,                            \
214    call_as_constructor_delegate)                                              \
215  V(CALL_AS_FUNCTION_DELEGATE_INDEX, JSFunction, call_as_function_delegate)    \
216  V(CALLSITE_FUNCTION_INDEX, JSFunction, callsite_function)                    \
217  V(CONTEXT_EXTENSION_FUNCTION_INDEX, JSFunction, context_extension_function)  \
218  V(CURRENT_MODULE_INDEX, Module, current_module)                              \
219  V(DATA_PROPERTY_DESCRIPTOR_MAP_INDEX, Map, data_property_descriptor_map)     \
220  V(DATA_VIEW_FUN_INDEX, JSFunction, data_view_fun)                            \
221  V(DATE_FUNCTION_INDEX, JSFunction, date_function)                            \
222  V(ERROR_MESSAGE_FOR_CODE_GEN_FROM_STRINGS_INDEX, Object,                     \
223    error_message_for_code_gen_from_strings)                                   \
224  V(ERRORS_THROWN_INDEX, Smi, errors_thrown)                                   \
225  V(EXTRAS_EXPORTS_OBJECT_INDEX, JSObject, extras_binding_object)              \
226  V(EXTRAS_UTILS_OBJECT_INDEX, Object, extras_utils_object)                    \
227  V(FAST_ALIASED_ARGUMENTS_MAP_INDEX, Map, fast_aliased_arguments_map)         \
228  V(FAST_TEMPLATE_INSTANTIATIONS_CACHE_INDEX, FixedArray,                      \
229    fast_template_instantiations_cache)                                        \
230  V(FLOAT32_ARRAY_FUN_INDEX, JSFunction, float32_array_fun)                    \
231  V(FLOAT64_ARRAY_FUN_INDEX, JSFunction, float64_array_fun)                    \
232  V(FUNCTION_FUNCTION_INDEX, JSFunction, function_function)                    \
233  V(GENERATOR_FUNCTION_FUNCTION_INDEX, JSFunction,                             \
234    generator_function_function)                                               \
235  V(GENERATOR_OBJECT_PROTOTYPE_MAP_INDEX, Map, generator_object_prototype_map) \
236  V(INITIAL_ARRAY_ITERATOR_PROTOTYPE_INDEX, JSObject,                          \
237    initial_array_iterator_prototype)                                          \
238  V(INITIAL_ARRAY_ITERATOR_PROTOTYPE_MAP_INDEX, Map,                           \
239    initial_array_iterator_prototype_map)                                      \
240  V(INITIAL_ARRAY_PROTOTYPE_INDEX, JSObject, initial_array_prototype)          \
241  V(INITIAL_GENERATOR_PROTOTYPE_INDEX, JSObject, initial_generator_prototype)  \
242  V(INITIAL_ITERATOR_PROTOTYPE_INDEX, JSObject, initial_iterator_prototype)    \
243  V(INITIAL_OBJECT_PROTOTYPE_INDEX, JSObject, initial_object_prototype)        \
244  V(INT16_ARRAY_FUN_INDEX, JSFunction, int16_array_fun)                        \
245  V(INT32_ARRAY_FUN_INDEX, JSFunction, int32_array_fun)                        \
246  V(INT8_ARRAY_FUN_INDEX, JSFunction, int8_array_fun)                          \
247  V(INTERNAL_ARRAY_FUNCTION_INDEX, JSFunction, internal_array_function)        \
248  V(ITERATOR_RESULT_MAP_INDEX, Map, iterator_result_map)                       \
249  V(INTL_DATE_TIME_FORMAT_FUNCTION_INDEX, JSFunction,                          \
250    intl_date_time_format_function)                                            \
251  V(INTL_NUMBER_FORMAT_FUNCTION_INDEX, JSFunction,                             \
252    intl_number_format_function)                                               \
253  V(INTL_COLLATOR_FUNCTION_INDEX, JSFunction, intl_collator_function)          \
254  V(INTL_V8_BREAK_ITERATOR_FUNCTION_INDEX, JSFunction,                         \
255    intl_v8_break_iterator_function)                                           \
256  V(JS_ARRAY_FAST_SMI_ELEMENTS_MAP_INDEX, Map,                                 \
257    js_array_fast_smi_elements_map_index)                                      \
258  V(JS_ARRAY_FAST_HOLEY_SMI_ELEMENTS_MAP_INDEX, Map,                           \
259    js_array_fast_holey_smi_elements_map_index)                                \
260  V(JS_ARRAY_FAST_ELEMENTS_MAP_INDEX, Map, js_array_fast_elements_map_index)   \
261  V(JS_ARRAY_FAST_HOLEY_ELEMENTS_MAP_INDEX, Map,                               \
262    js_array_fast_holey_elements_map_index)                                    \
263  V(JS_ARRAY_FAST_DOUBLE_ELEMENTS_MAP_INDEX, Map,                              \
264    js_array_fast_double_elements_map_index)                                   \
265  V(JS_ARRAY_FAST_HOLEY_DOUBLE_ELEMENTS_MAP_INDEX, Map,                        \
266    js_array_fast_holey_double_elements_map_index)                             \
267  V(JS_MAP_FUN_INDEX, JSFunction, js_map_fun)                                  \
268  V(JS_MAP_MAP_INDEX, Map, js_map_map)                                         \
269  V(JS_MODULE_NAMESPACE_MAP, Map, js_module_namespace_map)                     \
270  V(JS_SET_FUN_INDEX, JSFunction, js_set_fun)                                  \
271  V(JS_SET_MAP_INDEX, Map, js_set_map)                                         \
272  V(JS_WEAK_MAP_FUN_INDEX, JSFunction, js_weak_map_fun)                        \
273  V(JS_WEAK_SET_FUN_INDEX, JSFunction, js_weak_set_fun)                        \
274  V(MAP_CACHE_INDEX, Object, map_cache)                                        \
275  V(MAP_ITERATOR_MAP_INDEX, Map, map_iterator_map)                             \
276  V(MATH_RANDOM_INDEX_INDEX, Smi, math_random_index)                           \
277  V(MATH_RANDOM_CACHE_INDEX, Object, math_random_cache)                        \
278  V(MESSAGE_LISTENERS_INDEX, TemplateList, message_listeners)                  \
279  V(NATIVES_UTILS_OBJECT_INDEX, Object, natives_utils_object)                  \
280  V(NORMALIZED_MAP_CACHE_INDEX, Object, normalized_map_cache)                  \
281  V(NUMBER_FUNCTION_INDEX, JSFunction, number_function)                        \
282  V(OBJECT_FUNCTION_INDEX, JSFunction, object_function)                        \
283  V(OBJECT_FUNCTION_PROTOTYPE_MAP_INDEX, Map, object_function_prototype_map)   \
284  V(OPAQUE_REFERENCE_FUNCTION_INDEX, JSFunction, opaque_reference_function)    \
285  V(OSR_CODE_TABLE_INDEX, FixedArray, osr_code_table)                          \
286  V(PROXY_CALLABLE_MAP_INDEX, Map, proxy_callable_map)                         \
287  V(PROXY_CONSTRUCTOR_MAP_INDEX, Map, proxy_constructor_map)                   \
288  V(PROXY_FUNCTION_INDEX, JSFunction, proxy_function)                          \
289  V(PROXY_FUNCTION_MAP_INDEX, Map, proxy_function_map)                         \
290  V(PROXY_MAP_INDEX, Map, proxy_map)                                           \
291  V(PROMISE_GET_CAPABILITIES_EXECUTOR_SHARED_FUN, SharedFunctionInfo,          \
292    promise_get_capabilities_executor_shared_fun)                              \
293  V(PROMISE_RESOLVE_SHARED_FUN, SharedFunctionInfo,                            \
294    promise_resolve_shared_fun)                                                \
295  V(PROMISE_REJECT_SHARED_FUN, SharedFunctionInfo, promise_reject_shared_fun)  \
296  V(PROMISE_THEN_FINALLY_SHARED_FUN, SharedFunctionInfo,                       \
297    promise_then_finally_shared_fun)                                           \
298  V(PROMISE_CATCH_FINALLY_SHARED_FUN, SharedFunctionInfo,                      \
299    promise_catch_finally_shared_fun)                                          \
300  V(PROMISE_VALUE_THUNK_FINALLY_SHARED_FUN, SharedFunctionInfo,                \
301    promise_value_thunk_finally_shared_fun)                                    \
302  V(PROMISE_THROWER_FINALLY_SHARED_FUN, SharedFunctionInfo,                    \
303    promise_thrower_finally_shared_fun)                                        \
304  V(PROMISE_PROTOTYPE_MAP_INDEX, Map, promise_prototype_map)                   \
305  V(REGEXP_EXEC_FUNCTION_INDEX, JSFunction, regexp_exec_function)              \
306  V(REGEXP_FUNCTION_INDEX, JSFunction, regexp_function)                        \
307  V(REGEXP_LAST_MATCH_INFO_INDEX, RegExpMatchInfo, regexp_last_match_info)     \
308  V(REGEXP_INTERNAL_MATCH_INFO_INDEX, RegExpMatchInfo,                         \
309    regexp_internal_match_info)                                                \
310  V(REGEXP_PROTOTYPE_MAP_INDEX, Map, regexp_prototype_map)                     \
311  V(REGEXP_RESULT_MAP_INDEX, Map, regexp_result_map)                           \
312  V(SCRIPT_CONTEXT_TABLE_INDEX, ScriptContextTable, script_context_table)      \
313  V(SCRIPT_FUNCTION_INDEX, JSFunction, script_function)                        \
314  V(SECURITY_TOKEN_INDEX, Object, security_token)                              \
315  V(SELF_WEAK_CELL_INDEX, WeakCell, self_weak_cell)                            \
316  V(SET_ITERATOR_MAP_INDEX, Map, set_iterator_map)                             \
317  V(SHARED_ARRAY_BUFFER_FUN_INDEX, JSFunction, shared_array_buffer_fun)        \
318  V(SLOPPY_ARGUMENTS_MAP_INDEX, Map, sloppy_arguments_map)                     \
319  V(SLOPPY_FUNCTION_MAP_INDEX, Map, sloppy_function_map)                       \
320  V(SLOPPY_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map,                          \
321    sloppy_function_without_prototype_map)                                     \
322  V(SLOPPY_FUNCTION_WITH_READONLY_PROTOTYPE_MAP_INDEX, Map,                    \
323    sloppy_function_with_readonly_prototype_map)                               \
324  V(SLOW_ALIASED_ARGUMENTS_MAP_INDEX, Map, slow_aliased_arguments_map)         \
325  V(SLOW_OBJECT_WITH_NULL_PROTOTYPE_MAP, Map,                                  \
326    slow_object_with_null_prototype_map)                                       \
327  V(SLOW_TEMPLATE_INSTANTIATIONS_CACHE_INDEX, UnseededNumberDictionary,        \
328    slow_template_instantiations_cache)                                        \
329  V(STRICT_ARGUMENTS_MAP_INDEX, Map, strict_arguments_map)                     \
330  V(ASYNC_FUNCTION_MAP_INDEX, Map, async_function_map)                         \
331  V(STRICT_FUNCTION_MAP_INDEX, Map, strict_function_map)                       \
332  V(STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map,                          \
333    strict_function_without_prototype_map)                                     \
334  V(GENERATOR_FUNCTION_MAP_INDEX, Map, generator_function_map)                 \
335  V(CLASS_FUNCTION_MAP_INDEX, Map, class_function_map)                         \
336  V(STRING_FUNCTION_INDEX, JSFunction, string_function)                        \
337  V(STRING_FUNCTION_PROTOTYPE_MAP_INDEX, Map, string_function_prototype_map)   \
338  V(STRING_ITERATOR_MAP_INDEX, Map, string_iterator_map)                       \
339  V(SYMBOL_FUNCTION_INDEX, JSFunction, symbol_function)                        \
340  V(NATIVE_FUNCTION_MAP_INDEX, Map, native_function_map)                       \
341  V(WASM_FUNCTION_MAP_INDEX, Map, wasm_function_map)                           \
342  V(WASM_INSTANCE_CONSTRUCTOR_INDEX, JSFunction, wasm_instance_constructor)    \
343  V(WASM_INSTANCE_SYM_INDEX, Symbol, wasm_instance_sym)                        \
344  V(WASM_MEMORY_CONSTRUCTOR_INDEX, JSFunction, wasm_memory_constructor)        \
345  V(WASM_MEMORY_SYM_INDEX, Symbol, wasm_memory_sym)                            \
346  V(WASM_MODULE_CONSTRUCTOR_INDEX, JSFunction, wasm_module_constructor)        \
347  V(WASM_MODULE_SYM_INDEX, Symbol, wasm_module_sym)                            \
348  V(WASM_TABLE_CONSTRUCTOR_INDEX, JSFunction, wasm_table_constructor)          \
349  V(WASM_TABLE_SYM_INDEX, Symbol, wasm_table_sym)                              \
350  V(TYPED_ARRAY_FUN_INDEX, JSFunction, typed_array_function)                   \
351  V(TYPED_ARRAY_PROTOTYPE_INDEX, JSObject, typed_array_prototype)              \
352  V(UINT16_ARRAY_FUN_INDEX, JSFunction, uint16_array_fun)                      \
353  V(UINT32_ARRAY_FUN_INDEX, JSFunction, uint32_array_fun)                      \
354  V(UINT8_ARRAY_FUN_INDEX, JSFunction, uint8_array_fun)                        \
355  V(UINT8_CLAMPED_ARRAY_FUN_INDEX, JSFunction, uint8_clamped_array_fun)        \
356  V(EXPORTS_CONTAINER, Object, exports_container)                              \
357  NATIVE_CONTEXT_INTRINSIC_FUNCTIONS(V)                                        \
358  NATIVE_CONTEXT_IMPORTED_FIELDS(V)                                            \
359  NATIVE_CONTEXT_JS_ARRAY_ITERATOR_MAPS(V)
360
361// A table of all script contexts. Every loaded top-level script with top-level
362// lexical declarations contributes its ScriptContext into this table.
363//
364// The table is a fixed array, its first slot is the current used count and
365// the subsequent slots 1..used contain ScriptContexts.
366class ScriptContextTable : public FixedArray {
367 public:
368  // Conversions.
369  static inline ScriptContextTable* cast(Object* context);
370
371  struct LookupResult {
372    int context_index;
373    int slot_index;
374    VariableMode mode;
375    InitializationFlag init_flag;
376    MaybeAssignedFlag maybe_assigned_flag;
377  };
378
379  inline int used() const;
380  inline void set_used(int used);
381
382  static inline Handle<Context> GetContext(Handle<ScriptContextTable> table,
383                                           int i);
384
385  // Lookup a variable `name` in a ScriptContextTable.
386  // If it returns true, the variable is found and `result` contains
387  // valid information about its location.
388  // If it returns false, `result` is untouched.
389  MUST_USE_RESULT
390  static bool Lookup(Handle<ScriptContextTable> table, Handle<String> name,
391                     LookupResult* result);
392
393  MUST_USE_RESULT
394  static Handle<ScriptContextTable> Extend(Handle<ScriptContextTable> table,
395                                           Handle<Context> script_context);
396
397  static int GetContextOffset(int context_index) {
398    return kFirstContextOffset + context_index * kPointerSize;
399  }
400
401 private:
402  static const int kUsedSlot = 0;
403  static const int kFirstContextSlot = kUsedSlot + 1;
404  static const int kFirstContextOffset =
405      FixedArray::kHeaderSize + kFirstContextSlot * kPointerSize;
406
407  DISALLOW_IMPLICIT_CONSTRUCTORS(ScriptContextTable);
408};
409
410// JSFunctions are pairs (context, function code), sometimes also called
411// closures. A Context object is used to represent function contexts and
412// dynamically pushed 'with' contexts (or 'scopes' in ECMA-262 speak).
413//
414// At runtime, the contexts build a stack in parallel to the execution
415// stack, with the top-most context being the current context. All contexts
416// have the following slots:
417//
418// [ closure   ]  This is the current function. It is the same for all
419//                contexts inside a function. It provides access to the
420//                incoming context (i.e., the outer context, which may
421//                or may not become the current function's context), and
422//                it provides access to the functions code and thus it's
423//                scope information, which in turn contains the names of
424//                statically allocated context slots. The names are needed
425//                for dynamic lookups in the presence of 'with' or 'eval'.
426//
427// [ previous  ]  A pointer to the previous context.
428//
429// [ extension ]  Additional data.
430//
431//                For script contexts, it contains the respective ScopeInfo.
432//
433//                For catch contexts, it contains a ContextExtension object
434//                consisting of the ScopeInfo and the name of the catch
435//                variable.
436//
437//                For module contexts, it contains the module object.
438//
439//                For block contexts, it contains either the respective
440//                ScopeInfo or a ContextExtension object consisting of the
441//                ScopeInfo and an "extension object" (see below).
442//
443//                For with contexts, it contains a ContextExtension object
444//                consisting of the ScopeInfo and an "extension object".
445//
446//                An "extension object" is used to dynamically extend a context
447//                with additional variables, namely in the implementation of the
448//                'with' construct and the 'eval' construct.  For instance,
449//                Context::Lookup also searches the extension object for
450//                properties.  (Storing the extension object is the original
451//                purpose of this context slot, hence the name.)
452//
453// [ native_context ]  A pointer to the native context.
454//
455// In addition, function contexts may have statically allocated context slots
456// to store local variables/functions that are accessed from inner functions
457// (via static context addresses) or through 'eval' (dynamic context lookups).
458// The native context contains additional slots for fast access to native
459// properties.
460//
461// Finally, with Harmony scoping, the JSFunction representing a top level
462// script will have the ScriptContext rather than a FunctionContext.
463// Script contexts from all top-level scripts are gathered in
464// ScriptContextTable.
465
466class Context: public FixedArray {
467 public:
468  // Conversions.
469  static inline Context* cast(Object* context);
470
471  // The default context slot layout; indices are FixedArray slot indices.
472  enum Field {
473    // These slots are in all contexts.
474    CLOSURE_INDEX,
475    PREVIOUS_INDEX,
476    // The extension slot is used for either the global object (in native
477    // contexts), eval extension object (function contexts), subject of with
478    // (with contexts), or the variable name (catch contexts), the serialized
479    // scope info (block contexts), or the module instance (module contexts).
480    EXTENSION_INDEX,
481    NATIVE_CONTEXT_INDEX,
482
483    // These slots are only in native contexts.
484#define NATIVE_CONTEXT_SLOT(index, type, name) index,
485    NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_SLOT)
486#undef NATIVE_CONTEXT_SLOT
487
488    // Properties from here are treated as weak references by the full GC.
489    // Scavenge treats them as strong references.
490    OPTIMIZED_FUNCTIONS_LIST,  // Weak.
491    OPTIMIZED_CODE_LIST,       // Weak.
492    DEOPTIMIZED_CODE_LIST,     // Weak.
493    NEXT_CONTEXT_LINK,         // Weak.
494
495    // Total number of slots.
496    NATIVE_CONTEXT_SLOTS,
497    FIRST_WEAK_SLOT = OPTIMIZED_FUNCTIONS_LIST,
498    FIRST_JS_ARRAY_MAP_SLOT = JS_ARRAY_FAST_SMI_ELEMENTS_MAP_INDEX,
499
500    MIN_CONTEXT_SLOTS = GLOBAL_PROXY_INDEX,
501    // This slot holds the thrown value in catch contexts.
502    THROWN_OBJECT_INDEX = MIN_CONTEXT_SLOTS,
503
504    // These slots hold values in debug evaluate contexts.
505    WRAPPED_CONTEXT_INDEX = MIN_CONTEXT_SLOTS,
506    WHITE_LIST_INDEX = MIN_CONTEXT_SLOTS + 1
507  };
508
509  void ResetErrorsThrown();
510  void IncrementErrorsThrown();
511  int GetErrorsThrown();
512
513  // Direct slot access.
514  inline JSFunction* closure();
515  inline void set_closure(JSFunction* closure);
516
517  inline Context* previous();
518  inline void set_previous(Context* context);
519
520  inline Object* next_context_link();
521
522  inline bool has_extension();
523  inline HeapObject* extension();
524  inline void set_extension(HeapObject* object);
525  JSObject* extension_object();
526  JSReceiver* extension_receiver();
527  ScopeInfo* scope_info();
528  String* catch_name();
529
530  // Find the module context (assuming there is one) and return the associated
531  // module object.
532  Module* module();
533
534  // Get the context where var declarations will be hoisted to, which
535  // may be the context itself.
536  Context* declaration_context();
537  bool is_declaration_context();
538
539  // Get the next closure's context on the context chain.
540  Context* closure_context();
541
542  // Returns a JSGlobalProxy object or null.
543  JSObject* global_proxy();
544  void set_global_proxy(JSObject* global);
545
546  // Get the JSGlobalObject object.
547  V8_EXPORT_PRIVATE JSGlobalObject* global_object();
548
549  // Get the script context by traversing the context chain.
550  Context* script_context();
551
552  // Compute the native context.
553  inline Context* native_context();
554  inline void set_native_context(Context* context);
555
556  // Predicates for context types.  IsNativeContext is also defined on Object
557  // because we frequently have to know if arbitrary objects are natives
558  // contexts.
559  inline bool IsNativeContext();
560  inline bool IsFunctionContext();
561  inline bool IsCatchContext();
562  inline bool IsWithContext();
563  inline bool IsDebugEvaluateContext();
564  inline bool IsBlockContext();
565  inline bool IsModuleContext();
566  inline bool IsEvalContext();
567  inline bool IsScriptContext();
568
569  inline bool HasSameSecurityTokenAs(Context* that);
570
571  // Removes a specific optimized code object from the optimized code map.
572  // In case of non-OSR the code reference is cleared from the cache entry but
573  // the entry itself is left in the map in order to proceed sharing literals.
574  void EvictFromOptimizedCodeMap(Code* optimized_code, const char* reason);
575
576  // Clear optimized code map.
577  void ClearOptimizedCodeMap();
578
579  // A native context keeps track of all osrd optimized functions.
580  inline bool OptimizedCodeMapIsCleared();
581  Code* SearchOptimizedCodeMap(SharedFunctionInfo* shared,
582                               BailoutId osr_ast_id);
583  int SearchOptimizedCodeMapEntry(SharedFunctionInfo* shared,
584                                  BailoutId osr_ast_id);
585
586  static void AddToOptimizedCodeMap(Handle<Context> native_context,
587                                    Handle<SharedFunctionInfo> shared,
588                                    Handle<Code> code,
589                                    BailoutId osr_ast_id);
590
591  // A native context holds a list of all functions with optimized code.
592  void AddOptimizedFunction(JSFunction* function);
593  void RemoveOptimizedFunction(JSFunction* function);
594  void SetOptimizedFunctionsListHead(Object* head);
595  Object* OptimizedFunctionsListHead();
596
597  // The native context also stores a list of all optimized code and a
598  // list of all deoptimized code, which are needed by the deoptimizer.
599  void AddOptimizedCode(Code* code);
600  void SetOptimizedCodeListHead(Object* head);
601  Object* OptimizedCodeListHead();
602  void SetDeoptimizedCodeListHead(Object* head);
603  Object* DeoptimizedCodeListHead();
604
605  Handle<Object> ErrorMessageForCodeGenerationFromStrings();
606
607  static int ImportedFieldIndexForName(Handle<String> name);
608  static int IntrinsicIndexForName(Handle<String> name);
609  static int IntrinsicIndexForName(const unsigned char* name, int length);
610
611#define NATIVE_CONTEXT_FIELD_ACCESSORS(index, type, name) \
612  inline void set_##name(type* value);                    \
613  inline bool is_##name(type* value);                     \
614  inline type* name();
615  NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_FIELD_ACCESSORS)
616#undef NATIVE_CONTEXT_FIELD_ACCESSORS
617
618  // Lookup the slot called name, starting with the current context.
619  // There are three possibilities:
620  //
621  // 1) result->IsContext():
622  //    The binding was found in a context.  *index is always the
623  //    non-negative slot index.  *attributes is NONE for var and let
624  //    declarations, READ_ONLY for const declarations (never ABSENT).
625  //
626  // 2) result->IsJSObject():
627  //    The binding was found as a named property in a context extension
628  //    object (i.e., was introduced via eval), as a property on the subject
629  //    of with, or as a property of the global object.  *index is -1 and
630  //    *attributes is not ABSENT.
631  //
632  // 3) result.is_null():
633  //    There was no binding found, *index is always -1 and *attributes is
634  //    always ABSENT.
635  Handle<Object> Lookup(Handle<String> name, ContextLookupFlags flags,
636                        int* index, PropertyAttributes* attributes,
637                        InitializationFlag* init_flag,
638                        VariableMode* variable_mode);
639
640  // Code generation support.
641  static int SlotOffset(int index) {
642    return kHeaderSize + index * kPointerSize - kHeapObjectTag;
643  }
644
645  static int FunctionMapIndex(LanguageMode language_mode, FunctionKind kind) {
646    // Note: Must be kept in sync with the FastNewClosure builtin.
647    if (IsGeneratorFunction(kind)) {
648      return GENERATOR_FUNCTION_MAP_INDEX;
649    }
650
651    if (IsAsyncFunction(kind)) {
652      return ASYNC_FUNCTION_MAP_INDEX;
653    }
654
655    if (IsClassConstructor(kind)) {
656      // Like the strict function map, but with no 'name' accessor. 'name'
657      // needs to be the last property and it is added during instantiation,
658      // in case a static property with the same name exists"
659      return CLASS_FUNCTION_MAP_INDEX;
660    }
661
662    if (IsArrowFunction(kind) || IsConciseMethod(kind) ||
663        IsAccessorFunction(kind)) {
664      return STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX;
665    }
666
667    return is_strict(language_mode) ? STRICT_FUNCTION_MAP_INDEX
668                                    : SLOPPY_FUNCTION_MAP_INDEX;
669  }
670
671  static int ArrayMapIndex(ElementsKind elements_kind) {
672    DCHECK(IsFastElementsKind(elements_kind));
673    return elements_kind + FIRST_JS_ARRAY_MAP_SLOT;
674  }
675
676  static const int kSize = kHeaderSize + NATIVE_CONTEXT_SLOTS * kPointerSize;
677  static const int kNotFound = -1;
678
679  // GC support.
680  typedef FixedBodyDescriptor<
681      kHeaderSize, kSize, kSize> ScavengeBodyDescriptor;
682
683  typedef FixedBodyDescriptor<
684      kHeaderSize,
685      kHeaderSize + FIRST_WEAK_SLOT * kPointerSize,
686      kSize> MarkCompactBodyDescriptor;
687
688 private:
689#ifdef DEBUG
690  // Bootstrapping-aware type checks.
691  V8_EXPORT_PRIVATE static bool IsBootstrappingOrNativeContext(Isolate* isolate,
692                                                               Object* object);
693  static bool IsBootstrappingOrValidParentContext(Object* object, Context* kid);
694#endif
695
696  STATIC_ASSERT(kHeaderSize == Internals::kContextHeaderSize);
697  STATIC_ASSERT(EMBEDDER_DATA_INDEX == Internals::kContextEmbedderDataIndex);
698};
699
700typedef Context::Field ContextField;
701
702}  // namespace internal
703}  // namespace v8
704
705#endif  // V8_CONTEXTS_H_
706