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_OBJECTS_CONTEXTS_H_
6 #define V8_OBJECTS_CONTEXTS_H_
7 
8 #include "src/objects/fixed-array.h"
9 #include "src/objects/function-kind.h"
10 #include "src/objects/osr-optimized-code-cache.h"
11 #include "torque-generated/field-offsets-tq.h"
12 // Has to be the last include (doesn't have include guards):
13 #include "src/objects/object-macros.h"
14 
15 namespace v8 {
16 namespace internal {
17 
18 class JSGlobalObject;
19 class JSGlobalProxy;
20 class MicrotaskQueue;
21 class NativeContext;
22 class RegExpMatchInfo;
23 
24 enum ContextLookupFlags {
25   FOLLOW_CONTEXT_CHAIN = 1 << 0,
26   FOLLOW_PROTOTYPE_CHAIN = 1 << 1,
27 
28   DONT_FOLLOW_CHAINS = 0,
29   FOLLOW_CHAINS = FOLLOW_CONTEXT_CHAIN | FOLLOW_PROTOTYPE_CHAIN,
30 };
31 
32 // Heap-allocated activation contexts.
33 //
34 // Contexts are implemented as FixedArray-like objects having a fixed
35 // header with a set of common fields.
36 //
37 // Note: Context must have no virtual functions and Context objects
38 // must always be allocated via Heap::AllocateContext() or
39 // Factory::NewContext.
40 
41 #define NATIVE_CONTEXT_INTRINSIC_FUNCTIONS(V)                     \
42   V(GENERATOR_NEXT_INTERNAL, JSFunction, generator_next_internal) \
43   V(ASYNC_MODULE_EVALUATE_INTERNAL, JSFunction,                   \
44     async_module_evaluate_internal)                               \
45   V(MAKE_ERROR_INDEX, JSFunction, make_error)                     \
46   V(MAKE_RANGE_ERROR_INDEX, JSFunction, make_range_error)         \
47   V(MAKE_SYNTAX_ERROR_INDEX, JSFunction, make_syntax_error)       \
48   V(MAKE_TYPE_ERROR_INDEX, JSFunction, make_type_error)           \
49   V(MAKE_URI_ERROR_INDEX, JSFunction, make_uri_error)             \
50   V(OBJECT_CREATE, JSFunction, object_create)                     \
51   V(REFLECT_APPLY_INDEX, JSFunction, reflect_apply)               \
52   V(REFLECT_CONSTRUCT_INDEX, JSFunction, reflect_construct)       \
53   V(MATH_FLOOR_INDEX, JSFunction, math_floor)                     \
54   V(MATH_POW_INDEX, JSFunction, math_pow)                         \
55   V(PROMISE_INTERNAL_CONSTRUCTOR_INDEX, JSFunction,               \
56     promise_internal_constructor)                                 \
57   V(IS_PROMISE_INDEX, JSFunction, is_promise)                     \
58   V(PROMISE_THEN_INDEX, JSFunction, promise_then)
59 
60 #define NATIVE_CONTEXT_FIELDS(V)                                               \
61   V(GLOBAL_PROXY_INDEX, JSGlobalProxy, global_proxy_object)                    \
62   /* TODO(ishell): Actually we store exactly EmbedderDataArray here but */     \
63   /* it's already UBSan-fiendly and doesn't require a star... So declare */    \
64   /* it as a HeapObject for now. */                                            \
65   V(EMBEDDER_DATA_INDEX, HeapObject, embedder_data)                            \
66   V(CONTINUATION_PRESERVED_EMBEDDER_DATA_INDEX, HeapObject,                    \
67     continuation_preserved_embedder_data)                                      \
68   NATIVE_CONTEXT_INTRINSIC_FUNCTIONS(V)                                        \
69   /* Below is alpha-sorted */                                                  \
70   V(ACCESSOR_PROPERTY_DESCRIPTOR_MAP_INDEX, Map,                               \
71     accessor_property_descriptor_map)                                          \
72   V(ALLOW_CODE_GEN_FROM_STRINGS_INDEX, Object, allow_code_gen_from_strings)    \
73   V(ARRAY_BUFFER_FUN_INDEX, JSFunction, array_buffer_fun)                      \
74   V(ARRAY_BUFFER_MAP_INDEX, Map, array_buffer_map)                             \
75   V(ARRAY_BUFFER_NOINIT_FUN_INDEX, JSFunction, array_buffer_noinit_fun)        \
76   V(ARRAY_FUNCTION_INDEX, JSFunction, array_function)                          \
77   V(ARRAY_JOIN_STACK_INDEX, HeapObject, array_join_stack)                      \
78   V(ASYNC_FROM_SYNC_ITERATOR_MAP_INDEX, Map, async_from_sync_iterator_map)     \
79   V(ASYNC_FUNCTION_AWAIT_REJECT_SHARED_FUN, SharedFunctionInfo,                \
80     async_function_await_reject_shared_fun)                                    \
81   V(ASYNC_FUNCTION_AWAIT_RESOLVE_SHARED_FUN, SharedFunctionInfo,               \
82     async_function_await_resolve_shared_fun)                                   \
83   V(ASYNC_FUNCTION_FUNCTION_INDEX, JSFunction, async_function_constructor)     \
84   V(ASYNC_FUNCTION_OBJECT_MAP_INDEX, Map, async_function_object_map)           \
85   V(ASYNC_GENERATOR_FUNCTION_FUNCTION_INDEX, JSFunction,                       \
86     async_generator_function_function)                                         \
87   V(ASYNC_ITERATOR_VALUE_UNWRAP_SHARED_FUN, SharedFunctionInfo,                \
88     async_iterator_value_unwrap_shared_fun)                                    \
89   V(ASYNC_GENERATOR_AWAIT_REJECT_SHARED_FUN, SharedFunctionInfo,               \
90     async_generator_await_reject_shared_fun)                                   \
91   V(ASYNC_GENERATOR_AWAIT_RESOLVE_SHARED_FUN, SharedFunctionInfo,              \
92     async_generator_await_resolve_shared_fun)                                  \
93   V(ASYNC_GENERATOR_YIELD_RESOLVE_SHARED_FUN, SharedFunctionInfo,              \
94     async_generator_yield_resolve_shared_fun)                                  \
95   V(ASYNC_GENERATOR_RETURN_RESOLVE_SHARED_FUN, SharedFunctionInfo,             \
96     async_generator_return_resolve_shared_fun)                                 \
97   V(ASYNC_GENERATOR_RETURN_CLOSED_RESOLVE_SHARED_FUN, SharedFunctionInfo,      \
98     async_generator_return_closed_resolve_shared_fun)                          \
99   V(ASYNC_GENERATOR_RETURN_CLOSED_REJECT_SHARED_FUN, SharedFunctionInfo,       \
100     async_generator_return_closed_reject_shared_fun)                           \
101   V(ATOMICS_OBJECT, JSObject, atomics_object)                                  \
102   V(BIGINT_FUNCTION_INDEX, JSFunction, bigint_function)                        \
103   V(BIGINT64_ARRAY_FUN_INDEX, JSFunction, bigint64_array_fun)                  \
104   V(BIGUINT64_ARRAY_FUN_INDEX, JSFunction, biguint64_array_fun)                \
105   V(BOOLEAN_FUNCTION_INDEX, JSFunction, boolean_function)                      \
106   V(BOUND_FUNCTION_WITH_CONSTRUCTOR_MAP_INDEX, Map,                            \
107     bound_function_with_constructor_map)                                       \
108   V(BOUND_FUNCTION_WITHOUT_CONSTRUCTOR_MAP_INDEX, Map,                         \
109     bound_function_without_constructor_map)                                    \
110   V(CALL_AS_CONSTRUCTOR_DELEGATE_INDEX, JSFunction,                            \
111     call_as_constructor_delegate)                                              \
112   V(CALL_AS_FUNCTION_DELEGATE_INDEX, JSFunction, call_as_function_delegate)    \
113   V(CALL_ASYNC_MODULE_FULFILLED, JSFunction, call_async_module_fulfilled)      \
114   V(CALL_ASYNC_MODULE_REJECTED, JSFunction, call_async_module_rejected)        \
115   V(CALLSITE_FUNCTION_INDEX, JSFunction, callsite_function)                    \
116   V(CONTEXT_EXTENSION_FUNCTION_INDEX, JSFunction, context_extension_function)  \
117   V(DATA_PROPERTY_DESCRIPTOR_MAP_INDEX, Map, data_property_descriptor_map)     \
118   V(DATA_VIEW_FUN_INDEX, JSFunction, data_view_fun)                            \
119   V(DATE_FUNCTION_INDEX, JSFunction, date_function)                            \
120   V(DEBUG_CONTEXT_ID_INDEX, Object, debug_context_id)                          \
121   V(EMPTY_FUNCTION_INDEX, JSFunction, empty_function)                          \
122   V(ERROR_MESSAGE_FOR_CODE_GEN_FROM_STRINGS_INDEX, Object,                     \
123     error_message_for_code_gen_from_strings)                                   \
124   V(ERRORS_THROWN_INDEX, Smi, errors_thrown)                                   \
125   V(EXTRAS_BINDING_OBJECT_INDEX, JSObject, extras_binding_object)              \
126   V(FAST_ALIASED_ARGUMENTS_MAP_INDEX, Map, fast_aliased_arguments_map)         \
127   V(FAST_TEMPLATE_INSTANTIATIONS_CACHE_INDEX, FixedArray,                      \
128     fast_template_instantiations_cache)                                        \
129   V(FLOAT32_ARRAY_FUN_INDEX, JSFunction, float32_array_fun)                    \
130   V(FLOAT64_ARRAY_FUN_INDEX, JSFunction, float64_array_fun)                    \
131   V(FUNCTION_FUNCTION_INDEX, JSFunction, function_function)                    \
132   V(GENERATOR_FUNCTION_FUNCTION_INDEX, JSFunction,                             \
133     generator_function_function)                                               \
134   V(GENERATOR_OBJECT_PROTOTYPE_MAP_INDEX, Map, generator_object_prototype_map) \
135   V(ASYNC_GENERATOR_OBJECT_PROTOTYPE_MAP_INDEX, Map,                           \
136     async_generator_object_prototype_map)                                      \
137   V(INITIAL_ARRAY_ITERATOR_MAP_INDEX, Map, initial_array_iterator_map)         \
138   V(INITIAL_ARRAY_ITERATOR_PROTOTYPE_INDEX, JSObject,                          \
139     initial_array_iterator_prototype)                                          \
140   V(INITIAL_ARRAY_PROTOTYPE_INDEX, JSObject, initial_array_prototype)          \
141   V(INITIAL_ERROR_PROTOTYPE_INDEX, JSObject, initial_error_prototype)          \
142   V(INITIAL_GENERATOR_PROTOTYPE_INDEX, JSObject, initial_generator_prototype)  \
143   V(INITIAL_ASYNC_GENERATOR_PROTOTYPE_INDEX, JSObject,                         \
144     initial_async_generator_prototype)                                         \
145   V(INITIAL_ITERATOR_PROTOTYPE_INDEX, JSObject, initial_iterator_prototype)    \
146   V(INITIAL_MAP_ITERATOR_PROTOTYPE_INDEX, JSObject,                            \
147     initial_map_iterator_prototype)                                            \
148   V(INITIAL_MAP_PROTOTYPE_MAP_INDEX, Map, initial_map_prototype_map)           \
149   V(INITIAL_OBJECT_PROTOTYPE_INDEX, JSObject, initial_object_prototype)        \
150   V(INITIAL_SET_ITERATOR_PROTOTYPE_INDEX, JSObject,                            \
151     initial_set_iterator_prototype)                                            \
152   V(INITIAL_SET_PROTOTYPE_INDEX, JSObject, initial_set_prototype)              \
153   V(INITIAL_SET_PROTOTYPE_MAP_INDEX, Map, initial_set_prototype_map)           \
154   V(INITIAL_STRING_ITERATOR_MAP_INDEX, Map, initial_string_iterator_map)       \
155   V(INITIAL_STRING_ITERATOR_PROTOTYPE_INDEX, JSObject,                         \
156     initial_string_iterator_prototype)                                         \
157   V(INITIAL_STRING_PROTOTYPE_INDEX, JSObject, initial_string_prototype)        \
158   V(INITIAL_WEAKMAP_PROTOTYPE_MAP_INDEX, Map, initial_weakmap_prototype_map)   \
159   V(INITIAL_WEAKSET_PROTOTYPE_MAP_INDEX, Map, initial_weakset_prototype_map)   \
160   V(INT16_ARRAY_FUN_INDEX, JSFunction, int16_array_fun)                        \
161   V(INT32_ARRAY_FUN_INDEX, JSFunction, int32_array_fun)                        \
162   V(INT8_ARRAY_FUN_INDEX, JSFunction, int8_array_fun)                          \
163   V(INTL_COLLATOR_FUNCTION_INDEX, JSFunction, intl_collator_function)          \
164   V(INTL_DATE_TIME_FORMAT_FUNCTION_INDEX, JSFunction,                          \
165     intl_date_time_format_function)                                            \
166   V(INTL_DISPLAY_NAMES_FUNCTION_INDEX, JSFunction,                             \
167     intl_display_names_function)                                               \
168   V(INTL_NUMBER_FORMAT_FUNCTION_INDEX, JSFunction,                             \
169     intl_number_format_function)                                               \
170   V(INTL_LOCALE_FUNCTION_INDEX, JSFunction, intl_locale_function)              \
171   V(INTL_LIST_FORMAT_FUNCTION_INDEX, JSFunction, intl_list_format_function)    \
172   V(INTL_PLURAL_RULES_FUNCTION_INDEX, JSFunction, intl_plural_rules_function)  \
173   V(INTL_RELATIVE_TIME_FORMAT_FUNCTION_INDEX, JSFunction,                      \
174     intl_relative_time_format_function)                                        \
175   V(INTL_SEGMENTER_FUNCTION_INDEX, JSFunction, intl_segmenter_function)        \
176   V(INTL_SEGMENT_ITERATOR_MAP_INDEX, Map, intl_segment_iterator_map)           \
177   V(ITERATOR_RESULT_MAP_INDEX, Map, iterator_result_map)                       \
178   V(JS_ARRAY_PACKED_SMI_ELEMENTS_MAP_INDEX, Map,                               \
179     js_array_packed_smi_elements_map)                                          \
180   V(JS_ARRAY_HOLEY_SMI_ELEMENTS_MAP_INDEX, Map,                                \
181     js_array_holey_smi_elements_map)                                           \
182   V(JS_ARRAY_PACKED_ELEMENTS_MAP_INDEX, Map, js_array_packed_elements_map)     \
183   V(JS_ARRAY_HOLEY_ELEMENTS_MAP_INDEX, Map, js_array_holey_elements_map)       \
184   V(JS_ARRAY_PACKED_DOUBLE_ELEMENTS_MAP_INDEX, Map,                            \
185     js_array_packed_double_elements_map)                                       \
186   V(JS_ARRAY_HOLEY_DOUBLE_ELEMENTS_MAP_INDEX, Map,                             \
187     js_array_holey_double_elements_map)                                        \
188   V(JS_MAP_FUN_INDEX, JSFunction, js_map_fun)                                  \
189   V(JS_MAP_MAP_INDEX, Map, js_map_map)                                         \
190   V(JS_MODULE_NAMESPACE_MAP, Map, js_module_namespace_map)                     \
191   V(JS_SET_FUN_INDEX, JSFunction, js_set_fun)                                  \
192   V(JS_SET_MAP_INDEX, Map, js_set_map)                                         \
193   V(WEAK_CELL_MAP_INDEX, Map, weak_cell_map)                                   \
194   V(JS_FINALIZATION_REGISTRY_CLEANUP_ITERATOR_MAP_INDEX, Map,                  \
195     js_finalization_registry_cleanup_iterator_map)                             \
196   V(JS_WEAK_MAP_FUN_INDEX, JSFunction, js_weak_map_fun)                        \
197   V(JS_WEAK_SET_FUN_INDEX, JSFunction, js_weak_set_fun)                        \
198   V(JS_WEAK_REF_FUNCTION_INDEX, JSFunction, js_weak_ref_fun)                   \
199   V(JS_FINALIZATION_REGISTRY_FUNCTION_INDEX, JSFunction,                       \
200     js_finalization_registry_fun)                                              \
201   /* Context maps */                                                           \
202   V(NATIVE_CONTEXT_MAP_INDEX, Map, native_context_map)                         \
203   V(FUNCTION_CONTEXT_MAP_INDEX, Map, function_context_map)                     \
204   V(MODULE_CONTEXT_MAP_INDEX, Map, module_context_map)                         \
205   V(EVAL_CONTEXT_MAP_INDEX, Map, eval_context_map)                             \
206   V(SCRIPT_CONTEXT_MAP_INDEX, Map, script_context_map)                         \
207   V(AWAIT_CONTEXT_MAP_INDEX, Map, await_context_map)                           \
208   V(BLOCK_CONTEXT_MAP_INDEX, Map, block_context_map)                           \
209   V(CATCH_CONTEXT_MAP_INDEX, Map, catch_context_map)                           \
210   V(WITH_CONTEXT_MAP_INDEX, Map, with_context_map)                             \
211   V(DEBUG_EVALUATE_CONTEXT_MAP_INDEX, Map, debug_evaluate_context_map)         \
212   V(MAP_CACHE_INDEX, Object, map_cache)                                        \
213   V(MAP_KEY_ITERATOR_MAP_INDEX, Map, map_key_iterator_map)                     \
214   V(MAP_KEY_VALUE_ITERATOR_MAP_INDEX, Map, map_key_value_iterator_map)         \
215   V(MAP_VALUE_ITERATOR_MAP_INDEX, Map, map_value_iterator_map)                 \
216   V(MATH_RANDOM_INDEX_INDEX, Smi, math_random_index)                           \
217   V(MATH_RANDOM_STATE_INDEX, ByteArray, math_random_state)                     \
218   V(MATH_RANDOM_CACHE_INDEX, FixedDoubleArray, math_random_cache)              \
219   V(MESSAGE_LISTENERS_INDEX, TemplateList, message_listeners)                  \
220   V(NORMALIZED_MAP_CACHE_INDEX, Object, normalized_map_cache)                  \
221   V(NUMBER_FUNCTION_INDEX, JSFunction, number_function)                        \
222   V(OBJECT_FUNCTION_INDEX, JSFunction, object_function)                        \
223   V(OBJECT_FUNCTION_PROTOTYPE_MAP_INDEX, Map, object_function_prototype_map)   \
224   V(PROXY_CALLABLE_MAP_INDEX, Map, proxy_callable_map)                         \
225   V(PROXY_CONSTRUCTOR_MAP_INDEX, Map, proxy_constructor_map)                   \
226   V(PROXY_FUNCTION_INDEX, JSFunction, proxy_function)                          \
227   V(PROXY_MAP_INDEX, Map, proxy_map)                                           \
228   V(PROXY_REVOCABLE_RESULT_MAP_INDEX, Map, proxy_revocable_result_map)         \
229   V(PROXY_REVOKE_SHARED_FUN, SharedFunctionInfo, proxy_revoke_shared_fun)      \
230   V(PROMISE_GET_CAPABILITIES_EXECUTOR_SHARED_FUN, SharedFunctionInfo,          \
231     promise_get_capabilities_executor_shared_fun)                              \
232   V(PROMISE_CAPABILITY_DEFAULT_REJECT_SHARED_FUN_INDEX, SharedFunctionInfo,    \
233     promise_capability_default_reject_shared_fun)                              \
234   V(PROMISE_CAPABILITY_DEFAULT_RESOLVE_SHARED_FUN_INDEX, SharedFunctionInfo,   \
235     promise_capability_default_resolve_shared_fun)                             \
236   V(PROMISE_THEN_FINALLY_SHARED_FUN, SharedFunctionInfo,                       \
237     promise_then_finally_shared_fun)                                           \
238   V(PROMISE_CATCH_FINALLY_SHARED_FUN, SharedFunctionInfo,                      \
239     promise_catch_finally_shared_fun)                                          \
240   V(PROMISE_VALUE_THUNK_FINALLY_SHARED_FUN, SharedFunctionInfo,                \
241     promise_value_thunk_finally_shared_fun)                                    \
242   V(PROMISE_THROWER_FINALLY_SHARED_FUN, SharedFunctionInfo,                    \
243     promise_thrower_finally_shared_fun)                                        \
244   V(PROMISE_ALL_RESOLVE_ELEMENT_SHARED_FUN, SharedFunctionInfo,                \
245     promise_all_resolve_element_shared_fun)                                    \
246   V(PROMISE_ALL_SETTLED_RESOLVE_ELEMENT_SHARED_FUN, SharedFunctionInfo,        \
247     promise_all_settled_resolve_element_shared_fun)                            \
248   V(PROMISE_ALL_SETTLED_REJECT_ELEMENT_SHARED_FUN, SharedFunctionInfo,         \
249     promise_all_settled_reject_element_shared_fun)                             \
250   V(PROMISE_PROTOTYPE_INDEX, JSObject, promise_prototype)                      \
251   V(REGEXP_EXEC_FUNCTION_INDEX, JSFunction, regexp_exec_function)              \
252   V(REGEXP_FUNCTION_INDEX, JSFunction, regexp_function)                        \
253   V(REGEXP_LAST_MATCH_INFO_INDEX, RegExpMatchInfo, regexp_last_match_info)     \
254   V(REGEXP_MATCH_ALL_FUNCTION_INDEX, JSFunction, regexp_match_all_function)    \
255   V(REGEXP_MATCH_FUNCTION_INDEX, JSFunction, regexp_match_function)            \
256   V(REGEXP_PROTOTYPE_INDEX, JSObject, regexp_prototype)                        \
257   V(REGEXP_PROTOTYPE_MAP_INDEX, Map, regexp_prototype_map)                     \
258   V(REGEXP_REPLACE_FUNCTION_INDEX, JSFunction, regexp_replace_function)        \
259   V(REGEXP_RESULT_MAP_INDEX, Map, regexp_result_map)                           \
260   V(REGEXP_RESULT_INDICES_MAP_INDEX, Map, regexp_result_indices_map)           \
261   V(REGEXP_SEARCH_FUNCTION_INDEX, JSFunction, regexp_search_function)          \
262   V(REGEXP_SPLIT_FUNCTION_INDEX, JSFunction, regexp_split_function)            \
263   V(INITIAL_REGEXP_STRING_ITERATOR_PROTOTYPE_MAP_INDEX, Map,                   \
264     initial_regexp_string_iterator_prototype_map)                              \
265   V(SCRIPT_CONTEXT_TABLE_INDEX, ScriptContextTable, script_context_table)      \
266   V(SCRIPT_EXECUTION_CALLBACK_INDEX, Object, script_execution_callback)        \
267   V(SECURITY_TOKEN_INDEX, Object, security_token)                              \
268   V(SERIALIZED_OBJECTS, FixedArray, serialized_objects)                        \
269   V(SET_VALUE_ITERATOR_MAP_INDEX, Map, set_value_iterator_map)                 \
270   V(SET_KEY_VALUE_ITERATOR_MAP_INDEX, Map, set_key_value_iterator_map)         \
271   V(SHARED_ARRAY_BUFFER_FUN_INDEX, JSFunction, shared_array_buffer_fun)        \
272   V(SLOPPY_ARGUMENTS_MAP_INDEX, Map, sloppy_arguments_map)                     \
273   V(SLOW_ALIASED_ARGUMENTS_MAP_INDEX, Map, slow_aliased_arguments_map)         \
274   V(STRICT_ARGUMENTS_MAP_INDEX, Map, strict_arguments_map)                     \
275   V(SLOW_OBJECT_WITH_NULL_PROTOTYPE_MAP, Map,                                  \
276     slow_object_with_null_prototype_map)                                       \
277   V(SLOW_OBJECT_WITH_OBJECT_PROTOTYPE_MAP, Map,                                \
278     slow_object_with_object_prototype_map)                                     \
279   V(SLOW_TEMPLATE_INSTANTIATIONS_CACHE_INDEX, SimpleNumberDictionary,          \
280     slow_template_instantiations_cache)                                        \
281   /* Fast Path Protectors */                                                   \
282   V(REGEXP_SPECIES_PROTECTOR_INDEX, PropertyCell, regexp_species_protector)    \
283   /* All *_FUNCTION_MAP_INDEX definitions used by Context::FunctionMapIndex */ \
284   /* must remain together. */                                                  \
285   V(SLOPPY_FUNCTION_MAP_INDEX, Map, sloppy_function_map)                       \
286   V(SLOPPY_FUNCTION_WITH_NAME_MAP_INDEX, Map, sloppy_function_with_name_map)   \
287   V(SLOPPY_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map,                          \
288     sloppy_function_without_prototype_map)                                     \
289   V(SLOPPY_FUNCTION_WITH_READONLY_PROTOTYPE_MAP_INDEX, Map,                    \
290     sloppy_function_with_readonly_prototype_map)                               \
291   V(STRICT_FUNCTION_MAP_INDEX, Map, strict_function_map)                       \
292   V(STRICT_FUNCTION_WITH_NAME_MAP_INDEX, Map, strict_function_with_name_map)   \
293   V(STRICT_FUNCTION_WITH_READONLY_PROTOTYPE_MAP_INDEX, Map,                    \
294     strict_function_with_readonly_prototype_map)                               \
295   V(STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map,                          \
296     strict_function_without_prototype_map)                                     \
297   V(METHOD_WITH_NAME_MAP_INDEX, Map, method_with_name_map)                     \
298   V(METHOD_WITH_HOME_OBJECT_MAP_INDEX, Map, method_with_home_object_map)       \
299   V(METHOD_WITH_NAME_AND_HOME_OBJECT_MAP_INDEX, Map,                           \
300     method_with_name_and_home_object_map)                                      \
301   V(ASYNC_FUNCTION_MAP_INDEX, Map, async_function_map)                         \
302   V(ASYNC_FUNCTION_WITH_NAME_MAP_INDEX, Map, async_function_with_name_map)     \
303   V(ASYNC_FUNCTION_WITH_HOME_OBJECT_MAP_INDEX, Map,                            \
304     async_function_with_home_object_map)                                       \
305   V(ASYNC_FUNCTION_WITH_NAME_AND_HOME_OBJECT_MAP_INDEX, Map,                   \
306     async_function_with_name_and_home_object_map)                              \
307   V(GENERATOR_FUNCTION_MAP_INDEX, Map, generator_function_map)                 \
308   V(GENERATOR_FUNCTION_WITH_NAME_MAP_INDEX, Map,                               \
309     generator_function_with_name_map)                                          \
310   V(GENERATOR_FUNCTION_WITH_HOME_OBJECT_MAP_INDEX, Map,                        \
311     generator_function_with_home_object_map)                                   \
312   V(GENERATOR_FUNCTION_WITH_NAME_AND_HOME_OBJECT_MAP_INDEX, Map,               \
313     generator_function_with_name_and_home_object_map)                          \
314   V(ASYNC_GENERATOR_FUNCTION_MAP_INDEX, Map, async_generator_function_map)     \
315   V(ASYNC_GENERATOR_FUNCTION_WITH_NAME_MAP_INDEX, Map,                         \
316     async_generator_function_with_name_map)                                    \
317   V(ASYNC_GENERATOR_FUNCTION_WITH_HOME_OBJECT_MAP_INDEX, Map,                  \
318     async_generator_function_with_home_object_map)                             \
319   V(ASYNC_GENERATOR_FUNCTION_WITH_NAME_AND_HOME_OBJECT_MAP_INDEX, Map,         \
320     async_generator_function_with_name_and_home_object_map)                    \
321   V(CLASS_FUNCTION_MAP_INDEX, Map, class_function_map)                         \
322   V(STRING_FUNCTION_INDEX, JSFunction, string_function)                        \
323   V(STRING_FUNCTION_PROTOTYPE_MAP_INDEX, Map, string_function_prototype_map)   \
324   V(SYMBOL_FUNCTION_INDEX, JSFunction, symbol_function)                        \
325   V(WASM_EXPORTED_FUNCTION_MAP_INDEX, Map, wasm_exported_function_map)         \
326   V(WASM_EXCEPTION_CONSTRUCTOR_INDEX, JSFunction, wasm_exception_constructor)  \
327   V(WASM_GLOBAL_CONSTRUCTOR_INDEX, JSFunction, wasm_global_constructor)        \
328   V(WASM_INSTANCE_CONSTRUCTOR_INDEX, JSFunction, wasm_instance_constructor)    \
329   V(WASM_MEMORY_CONSTRUCTOR_INDEX, JSFunction, wasm_memory_constructor)        \
330   V(WASM_MODULE_CONSTRUCTOR_INDEX, JSFunction, wasm_module_constructor)        \
331   V(WASM_TABLE_CONSTRUCTOR_INDEX, JSFunction, wasm_table_constructor)          \
332   V(TEMPLATE_WEAKMAP_INDEX, HeapObject, template_weakmap)                      \
333   V(TYPED_ARRAY_FUN_INDEX, JSFunction, typed_array_function)                   \
334   V(TYPED_ARRAY_PROTOTYPE_INDEX, JSObject, typed_array_prototype)              \
335   V(UINT16_ARRAY_FUN_INDEX, JSFunction, uint16_array_fun)                      \
336   V(UINT32_ARRAY_FUN_INDEX, JSFunction, uint32_array_fun)                      \
337   V(UINT8_ARRAY_FUN_INDEX, JSFunction, uint8_array_fun)                        \
338   V(UINT8_CLAMPED_ARRAY_FUN_INDEX, JSFunction, uint8_clamped_array_fun)        \
339   V(ARRAY_ENTRIES_ITERATOR_INDEX, JSFunction, array_entries_iterator)          \
340   V(ARRAY_FOR_EACH_ITERATOR_INDEX, JSFunction, array_for_each_iterator)        \
341   V(ARRAY_KEYS_ITERATOR_INDEX, JSFunction, array_keys_iterator)                \
342   V(ARRAY_VALUES_ITERATOR_INDEX, JSFunction, array_values_iterator)            \
343   V(ERROR_FUNCTION_INDEX, JSFunction, error_function)                          \
344   V(ERROR_TO_STRING, JSFunction, error_to_string)                              \
345   V(EVAL_ERROR_FUNCTION_INDEX, JSFunction, eval_error_function)                \
346   V(GLOBAL_EVAL_FUN_INDEX, JSFunction, global_eval_fun)                        \
347   V(GLOBAL_PROXY_FUNCTION_INDEX, JSFunction, global_proxy_function)            \
348   V(MAP_DELETE_INDEX, JSFunction, map_delete)                                  \
349   V(MAP_GET_INDEX, JSFunction, map_get)                                        \
350   V(MAP_HAS_INDEX, JSFunction, map_has)                                        \
351   V(MAP_SET_INDEX, JSFunction, map_set)                                        \
352   V(FUNCTION_HAS_INSTANCE_INDEX, JSFunction, function_has_instance)            \
353   V(OBJECT_TO_STRING, JSFunction, object_to_string)                            \
354   V(OBJECT_VALUE_OF_FUNCTION_INDEX, JSFunction, object_value_of_function)      \
355   V(PROMISE_ALL_INDEX, JSFunction, promise_all)                                \
356   V(PROMISE_CATCH_INDEX, JSFunction, promise_catch)                            \
357   V(PROMISE_FUNCTION_INDEX, JSFunction, promise_function)                      \
358   V(RANGE_ERROR_FUNCTION_INDEX, JSFunction, range_error_function)              \
359   V(REFERENCE_ERROR_FUNCTION_INDEX, JSFunction, reference_error_function)      \
360   V(SET_ADD_INDEX, JSFunction, set_add)                                        \
361   V(SET_DELETE_INDEX, JSFunction, set_delete)                                  \
362   V(SET_HAS_INDEX, JSFunction, set_has)                                        \
363   V(SYNTAX_ERROR_FUNCTION_INDEX, JSFunction, syntax_error_function)            \
364   V(TYPE_ERROR_FUNCTION_INDEX, JSFunction, type_error_function)                \
365   V(URI_ERROR_FUNCTION_INDEX, JSFunction, uri_error_function)                  \
366   V(WASM_COMPILE_ERROR_FUNCTION_INDEX, JSFunction,                             \
367     wasm_compile_error_function)                                               \
368   V(WASM_LINK_ERROR_FUNCTION_INDEX, JSFunction, wasm_link_error_function)      \
369   V(WASM_RUNTIME_ERROR_FUNCTION_INDEX, JSFunction,                             \
370     wasm_runtime_error_function)                                               \
371   V(WEAKMAP_SET_INDEX, JSFunction, weakmap_set)                                \
372   V(WEAKMAP_GET_INDEX, JSFunction, weakmap_get)                                \
373   V(WEAKSET_ADD_INDEX, JSFunction, weakset_add)                                \
374   V(OSR_CODE_CACHE_INDEX, WeakFixedArray, osr_code_cache)
375 
376 // A table of all script contexts. Every loaded top-level script with top-level
377 // lexical declarations contributes its ScriptContext into this table.
378 //
379 // The table is a fixed array, its first slot is the current used count and
380 // the subsequent slots 1..used contain ScriptContexts.
381 class ScriptContextTable : public FixedArray {
382  public:
383   DECL_CAST(ScriptContextTable)
384 
385   struct LookupResult {
386     int context_index;
387     int slot_index;
388     VariableMode mode;
389     InitializationFlag init_flag;
390     MaybeAssignedFlag maybe_assigned_flag;
391   };
392 
393   inline int used() const;
394   inline void set_used(int used);
395 
396   static inline Handle<Context> GetContext(Isolate* isolate,
397                                            Handle<ScriptContextTable> table,
398                                            int i);
399   inline Context get_context(int i) const;
400 
401   // Lookup a variable `name` in a ScriptContextTable.
402   // If it returns true, the variable is found and `result` contains
403   // valid information about its location.
404   // If it returns false, `result` is untouched.
405   V8_WARN_UNUSED_RESULT
406   V8_EXPORT_PRIVATE static bool Lookup(Isolate* isolate,
407                                        ScriptContextTable table, String name,
408                                        LookupResult* result);
409 
410   V8_WARN_UNUSED_RESULT
411   static Handle<ScriptContextTable> Extend(Handle<ScriptContextTable> table,
412                                            Handle<Context> script_context);
413 
414   static const int kUsedSlotIndex = 0;
415   static const int kFirstContextSlotIndex = 1;
416   static const int kMinLength = kFirstContextSlotIndex;
417 
418   OBJECT_CONSTRUCTORS(ScriptContextTable, FixedArray);
419 };
420 
421 // JSFunctions are pairs (context, function code), sometimes also called
422 // closures. A Context object is used to represent function contexts and
423 // dynamically pushed 'with' contexts (or 'scopes' in ECMA-262 speak).
424 //
425 // At runtime, the contexts build a stack in parallel to the execution
426 // stack, with the top-most context being the current context. All contexts
427 // have the following slots:
428 //
429 // [ scope_info     ]  This is the scope info describing the current context. It
430 //                     contains the names of statically allocated context slots,
431 //                     and stack-allocated locals.  The names are needed for
432 //                     dynamic lookups in the presence of 'with' or 'eval', and
433 //                     for the debugger.
434 //
435 // [ previous       ]  A pointer to the previous context.
436 //
437 // [ extension      ]  Additional data. This slot is only available when
438 //                     extension_bit is set. Check using has_extension.
439 //
440 //                     For native contexts, it contains the global object.
441 //                     For module contexts, it contains the module object.
442 //                     For await contexts, it contains the generator object.
443 //                     For var block contexts, it may contain an "extension
444 //                     object".
445 //                     For with contexts, it contains an "extension object".
446 //
447 //                     An "extension object" is used to dynamically extend a
448 //                     context with additional variables, namely in the
449 //                     implementation of the 'with' construct and the 'eval'
450 //                     construct.  For instance, Context::Lookup also searches
451 //                     the extension object for properties.  (Storing the
452 //                     extension object is the original purpose of this context
453 //                     slot, hence the name.)
454 //
455 // In addition, function contexts with sloppy eval may have statically
456 // allocated context slots to store local variables/functions that are accessed
457 // from inner functions (via static context addresses) or through 'eval'
458 // (dynamic context lookups).
459 // The native context contains additional slots for fast access to native
460 // properties.
461 //
462 // Finally, with Harmony scoping, the JSFunction representing a top level
463 // script will have the ScriptContext rather than a FunctionContext.
464 // Script contexts from all top-level scripts are gathered in
465 // ScriptContextTable.
466 
467 class Context : public HeapObject {
468  public:
469   NEVER_READ_ONLY_SPACE
470 
471   DECL_CAST(Context)
472   // [length]: length of the context.
473   V8_INLINE int length() const;
474   V8_INLINE void set_length(int value);
475 
476   // Setter and getter for elements.
477   V8_INLINE Object get(int index) const;
478   V8_INLINE Object get(const Isolate* isolate, int index) const;
479   V8_INLINE void set(int index, Object value);
480   // Setter with explicit barrier mode.
481   V8_INLINE void set(int index, Object value, WriteBarrierMode mode);
482 
483   DEFINE_FIELD_OFFSET_CONSTANTS(HeapObject::kHeaderSize,
484                                 TORQUE_GENERATED_CONTEXT_FIELDS)
485 
486   // TODO(v8:8989): [torque] Support marker constants
487   /* TODO(ishell): remove this fixedArray-like header size. */
488   static const int kFixedArrayLikeHeaderSize = kScopeInfoOffset;
489   static const int kStartOfTaggedFieldsOffset = kScopeInfoOffset;
490   /* Header size. */                                                  \
491   /* TODO(ishell): use this as header size once MIN_CONTEXT_SLOTS */  \
492   /* is removed in favour of offset-based access to common fields. */ \
493   static const int kTodoHeaderSize = kHeaderSize;
494 
495   // If the extension slot exists, it is the first slot after the header.
496   static const int kExtensionOffset = kHeaderSize;
497 
498   // Garbage collection support.
SizeFor(int length)499   V8_INLINE static constexpr int SizeFor(int length) {
500     return kFixedArrayLikeHeaderSize + length * kTaggedSize;
501   }
502 
503   // Code Generation support.
504   // Offset of the element from the beginning of object.
OffsetOfElementAt(int index)505   V8_INLINE static constexpr int OffsetOfElementAt(int index) {
506     return SizeFor(index);
507   }
508   // Offset of the element from the heap object pointer.
SlotOffset(int index)509   V8_INLINE static constexpr int SlotOffset(int index) {
510     return SizeFor(index) - kHeapObjectTag;
511   }
512 
513   // Initializes the variable slots of the context. Lexical variables that need
514   // initialization are filled with the hole.
515   void Initialize(Isolate* isolate);
516 
517   // TODO(ishell): eventually migrate to the offset based access instead of
518   // index-based.
519   // The default context slot layout; indices are FixedArray slot indices.
520   enum Field {
521     // TODO(shell): use offset-based approach for accessing common values.
522     // These slots are in all contexts.
523     SCOPE_INFO_INDEX,
524     PREVIOUS_INDEX,
525 
526     // This slot only exists if the extension_flag bit is set.
527     EXTENSION_INDEX,
528 
529 // These slots are only in native contexts.
530 #define NATIVE_CONTEXT_SLOT(index, type, name) index,
531     NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_SLOT)
532 #undef NATIVE_CONTEXT_SLOT
533 
534     // Properties from here are treated as weak references by the full GC.
535     // Scavenge treats them as strong references.
536     OPTIMIZED_CODE_LIST,    // Weak.
537     DEOPTIMIZED_CODE_LIST,  // Weak.
538     NEXT_CONTEXT_LINK,      // Weak.
539 
540     // Total number of slots.
541     NATIVE_CONTEXT_SLOTS,
542     FIRST_WEAK_SLOT = OPTIMIZED_CODE_LIST,
543     FIRST_JS_ARRAY_MAP_SLOT = JS_ARRAY_PACKED_SMI_ELEMENTS_MAP_INDEX,
544 
545     // TODO(shell): Remove, once it becomes zero
546     MIN_CONTEXT_SLOTS = EXTENSION_INDEX,
547     MIN_CONTEXT_EXTENDED_SLOTS = EXTENSION_INDEX + 1,
548 
549     // This slot holds the thrown value in catch contexts.
550     THROWN_OBJECT_INDEX = MIN_CONTEXT_SLOTS,
551 
552     // These slots hold values in debug evaluate contexts.
553     WRAPPED_CONTEXT_INDEX = MIN_CONTEXT_EXTENDED_SLOTS,
554     BLACK_LIST_INDEX = MIN_CONTEXT_EXTENDED_SLOTS + 1
555   };
556 
557   static const int kExtensionSize =
558       (MIN_CONTEXT_EXTENDED_SLOTS - MIN_CONTEXT_SLOTS) * kTaggedSize;
559   static const int kExtendedHeaderSize = kHeaderSize + kExtensionSize;
560 
561   // A region of native context entries containing maps for functions created
562   // by Builtins::kFastNewClosure.
563   static const int FIRST_FUNCTION_MAP_INDEX = SLOPPY_FUNCTION_MAP_INDEX;
564   static const int LAST_FUNCTION_MAP_INDEX = CLASS_FUNCTION_MAP_INDEX;
565 
566   static const int kNoContext = 0;
567   static const int kInvalidContext = 1;
568 
569   // Direct slot access.
570   inline void set_scope_info(ScopeInfo scope_info);
571 
572   inline Object unchecked_previous();
573   inline Context previous();
574   inline void set_previous(Context context);
575 
576   inline Object next_context_link();
577 
578   inline bool has_extension();
579   inline HeapObject extension();
580   inline void set_extension(HeapObject object);
581   JSObject extension_object();
582   JSReceiver extension_receiver();
583   V8_EXPORT_PRIVATE ScopeInfo scope_info();
584 
585   // Find the module context (assuming there is one) and return the associated
586   // module object.
587   SourceTextModule module();
588 
589   // Get the context where var declarations will be hoisted to, which
590   // may be the context itself.
591   Context declaration_context();
592   bool is_declaration_context();
593 
594   // Get the next closure's context on the context chain.
595   Context closure_context();
596 
597   // Returns a JSGlobalProxy object or null.
598   V8_EXPORT_PRIVATE JSGlobalProxy global_proxy();
599 
600   // Get the JSGlobalObject object.
601   V8_EXPORT_PRIVATE JSGlobalObject global_object();
602 
603   // Get the script context by traversing the context chain.
604   Context script_context();
605 
606   // Compute the native context.
607   inline NativeContext native_context() const;
608 
609   // Predicates for context types.  IsNativeContext is already defined on
610   // Object.
611   inline bool IsFunctionContext() const;
612   inline bool IsCatchContext() const;
613   inline bool IsWithContext() const;
614   inline bool IsDebugEvaluateContext() const;
615   inline bool IsAwaitContext() const;
616   inline bool IsBlockContext() const;
617   inline bool IsModuleContext() const;
618   inline bool IsEvalContext() const;
619   inline bool IsScriptContext() const;
620 
621   inline bool HasSameSecurityTokenAs(Context that) const;
622 
623   Handle<Object> ErrorMessageForCodeGenerationFromStrings();
624 
625   static int IntrinsicIndexForName(Handle<String> name);
626   static int IntrinsicIndexForName(const unsigned char* name, int length);
627 
628 #define NATIVE_CONTEXT_FIELD_ACCESSORS(index, type, name) \
629   inline void set_##name(type value);                     \
630   inline bool is_##name(type value) const;                \
631   inline type name() const;
632   NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_FIELD_ACCESSORS)
633 #undef NATIVE_CONTEXT_FIELD_ACCESSORS
634 
635   // Lookup the slot called name, starting with the current context.
636   // There are three possibilities:
637   //
638   // 1) result->IsContext():
639   //    The binding was found in a context.  *index is always the
640   //    non-negative slot index.  *attributes is NONE for var and let
641   //    declarations, READ_ONLY for const declarations (never ABSENT).
642   //
643   // 2) result->IsJSObject():
644   //    The binding was found as a named property in a context extension
645   //    object (i.e., was introduced via eval), as a property on the subject
646   //    of with, or as a property of the global object.  *index is -1 and
647   //    *attributes is not ABSENT.
648   //
649   // 3) result->IsModule():
650   //    The binding was found in module imports or exports.
651   //     *attributes is never ABSENT. imports are READ_ONLY.
652   //
653   // 4) result.is_null():
654   //    There was no binding found, *index is always -1 and *attributes is
655   //    always ABSENT.
656   static Handle<Object> Lookup(Handle<Context> context, Handle<String> name,
657                                ContextLookupFlags flags, int* index,
658                                PropertyAttributes* attributes,
659                                InitializationFlag* init_flag,
660                                VariableMode* variable_mode,
661                                bool* is_sloppy_function_name = nullptr);
662 
663   static inline int FunctionMapIndex(LanguageMode language_mode,
664                                      FunctionKind kind, bool has_shared_name,
665                                      bool needs_home_object);
666 
ArrayMapIndex(ElementsKind elements_kind)667   static int ArrayMapIndex(ElementsKind elements_kind) {
668     DCHECK(IsFastElementsKind(elements_kind));
669     return elements_kind + FIRST_JS_ARRAY_MAP_SLOT;
670   }
671 
672   inline Map GetInitialJSArrayMap(ElementsKind kind) const;
673 
674   static const int kNotFound = -1;
675 
676   // Dispatched behavior.
677   DECL_PRINTER(Context)
678   DECL_VERIFIER(Context)
679 
680   using BodyDescriptor = FlexibleBodyDescriptor<kStartOfTaggedFieldsOffset>;
681 
682  private:
683 #ifdef DEBUG
684   // Bootstrapping-aware type checks.
685   static bool IsBootstrappingOrValidParentContext(Object object, Context kid);
686 #endif
687 
688   OBJECT_CONSTRUCTORS(Context, HeapObject);
689 };
690 
691 class NativeContext : public Context {
692  public:
693   DECL_CAST(NativeContext)
694   // TODO(neis): Move some stuff from Context here.
695 
696   // [microtask_queue]: pointer to the MicrotaskQueue object.
697   DECL_PRIMITIVE_ACCESSORS(microtask_queue, MicrotaskQueue*)
698 
699   // Dispatched behavior.
700   DECL_PRINTER(NativeContext)
701   DECL_VERIFIER(NativeContext)
702 
703   // Layout description.
704 #define NATIVE_CONTEXT_FIELDS_DEF(V)                                        \
705   /* TODO(ishell): move definition of common context offsets to Context. */ \
706   V(kStartOfNativeContextFieldsOffset,                                      \
707     (FIRST_WEAK_SLOT - MIN_CONTEXT_EXTENDED_SLOTS) * kTaggedSize)           \
708   V(kEndOfStrongFieldsOffset, 0)                                            \
709   V(kStartOfWeakFieldsOffset,                                               \
710     (NATIVE_CONTEXT_SLOTS - FIRST_WEAK_SLOT) * kTaggedSize)                 \
711   V(kEndOfWeakFieldsOffset, 0)                                              \
712   V(kEndOfNativeContextFieldsOffset, 0)                                     \
713   V(kEndOfTaggedFieldsOffset, 0)                                            \
714   /* Raw data. */                                                           \
715   V(kMicrotaskQueueOffset, kSystemPointerSize)                              \
716   /* Total size. */                                                         \
717   V(kSize, 0)
718 
719   DEFINE_FIELD_OFFSET_CONSTANTS(Context::kExtendedHeaderSize,
720                                 NATIVE_CONTEXT_FIELDS_DEF)
721 #undef NATIVE_CONTEXT_FIELDS_DEF
722 
723   class BodyDescriptor;
724 
725   // The native context stores a list of all optimized code and a list of all
726   // deoptimized code, which are needed by the deoptimizer.
727   V8_EXPORT_PRIVATE void AddOptimizedCode(Code code);
728   void SetOptimizedCodeListHead(Object head);
729   Object OptimizedCodeListHead();
730   void SetDeoptimizedCodeListHead(Object head);
731   Object DeoptimizedCodeListHead();
732 
733   inline OSROptimizedCodeCache GetOSROptimizedCodeCache();
734 
735   void ResetErrorsThrown();
736   void IncrementErrorsThrown();
737   int GetErrorsThrown();
738 
739  private:
740   STATIC_ASSERT(OffsetOfElementAt(EMBEDDER_DATA_INDEX) ==
741                 Internals::kNativeContextEmbedderDataOffset);
742 
743   OBJECT_CONSTRUCTORS(NativeContext, Context);
744 };
745 
746 using ContextField = Context::Field;
747 
748 }  // namespace internal
749 }  // namespace v8
750 
751 #include "src/objects/object-macros-undef.h"
752 
753 #endif  // V8_OBJECTS_CONTEXTS_H_
754