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