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/objects/fixed-array.h"
9 
10 namespace v8 {
11 namespace internal {
12 
13 class RegExpMatchInfo;
14 
15 enum ContextLookupFlags {
16   FOLLOW_CONTEXT_CHAIN = 1 << 0,
17   FOLLOW_PROTOTYPE_CHAIN = 1 << 1,
18   STOP_AT_DECLARATION_SCOPE = 1 << 2,
19   SKIP_WITH_CONTEXT = 1 << 3,
20 
21   DONT_FOLLOW_CHAINS = 0,
22   FOLLOW_CHAINS = FOLLOW_CONTEXT_CHAIN | FOLLOW_PROTOTYPE_CHAIN,
23 };
24 
25 // Heap-allocated activation contexts.
26 //
27 // Contexts are implemented as FixedArray objects; the Context
28 // class is a convenience interface casted on a FixedArray object.
29 //
30 // Note: Context must have no virtual functions and Context objects
31 // must always be allocated via Heap::AllocateContext() or
32 // Factory::NewContext.
33 
34 #define NATIVE_CONTEXT_INTRINSIC_FUNCTIONS(V)                               \
35   V(ASYNC_FUNCTION_AWAIT_CAUGHT_INDEX, JSFunction,                          \
36     async_function_await_caught)                                            \
37   V(ASYNC_FUNCTION_AWAIT_UNCAUGHT_INDEX, JSFunction,                        \
38     async_function_await_uncaught)                                          \
39   V(ASYNC_FUNCTION_PROMISE_CREATE_INDEX, JSFunction,                        \
40     async_function_promise_create)                                          \
41   V(ASYNC_FUNCTION_PROMISE_RELEASE_INDEX, JSFunction,                       \
42     async_function_promise_release)                                         \
43   V(IS_ARRAYLIKE, JSFunction, is_arraylike)                                 \
44   V(GENERATOR_NEXT_INTERNAL, JSFunction, generator_next_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(OBJECT_DEFINE_PROPERTIES, JSFunction, object_define_properties)         \
52   V(OBJECT_DEFINE_PROPERTY, JSFunction, object_define_property)             \
53   V(OBJECT_GET_PROTOTYPE_OF, JSFunction, object_get_prototype_of)           \
54   V(OBJECT_IS_EXTENSIBLE, JSFunction, object_is_extensible)                 \
55   V(OBJECT_IS_FROZEN, JSFunction, object_is_frozen)                         \
56   V(OBJECT_IS_SEALED, JSFunction, object_is_sealed)                         \
57   V(OBJECT_KEYS, JSFunction, object_keys)                                   \
58   V(REGEXP_INTERNAL_MATCH, JSFunction, regexp_internal_match)               \
59   V(REFLECT_APPLY_INDEX, JSFunction, reflect_apply)                         \
60   V(REFLECT_CONSTRUCT_INDEX, JSFunction, reflect_construct)                 \
61   V(REFLECT_DEFINE_PROPERTY_INDEX, JSFunction, reflect_define_property)     \
62   V(REFLECT_DELETE_PROPERTY_INDEX, JSFunction, reflect_delete_property)     \
63   V(MATH_FLOOR_INDEX, JSFunction, math_floor)                               \
64   V(MATH_POW_INDEX, JSFunction, math_pow)                                   \
65   V(NEW_PROMISE_CAPABILITY_INDEX, JSFunction, new_promise_capability)       \
66   V(PROMISE_INTERNAL_CONSTRUCTOR_INDEX, JSFunction,                         \
67     promise_internal_constructor)                                           \
68   V(IS_PROMISE_INDEX, JSFunction, is_promise)                               \
69   V(PROMISE_THEN_INDEX, JSFunction, promise_then)                           \
70   V(ASYNC_GENERATOR_AWAIT_CAUGHT, JSFunction, async_generator_await_caught) \
71   V(ASYNC_GENERATOR_AWAIT_UNCAUGHT, JSFunction, async_generator_await_uncaught)
72 
73 #define NATIVE_CONTEXT_IMPORTED_FIELDS(V)                                 \
74   V(ARRAY_POP_INDEX, JSFunction, array_pop)                               \
75   V(ARRAY_PUSH_INDEX, JSFunction, array_push)                             \
76   V(ARRAY_SHIFT_INDEX, JSFunction, array_shift)                           \
77   V(ARRAY_SPLICE_INDEX, JSFunction, array_splice)                         \
78   V(ARRAY_UNSHIFT_INDEX, JSFunction, array_unshift)                       \
79   V(ARRAY_ENTRIES_ITERATOR_INDEX, JSFunction, array_entries_iterator)     \
80   V(ARRAY_FOR_EACH_ITERATOR_INDEX, JSFunction, array_for_each_iterator)   \
81   V(ARRAY_KEYS_ITERATOR_INDEX, JSFunction, array_keys_iterator)           \
82   V(ARRAY_VALUES_ITERATOR_INDEX, JSFunction, array_values_iterator)       \
83   V(ERROR_FUNCTION_INDEX, JSFunction, error_function)                     \
84   V(ERROR_TO_STRING, JSFunction, error_to_string)                         \
85   V(EVAL_ERROR_FUNCTION_INDEX, JSFunction, eval_error_function)           \
86   V(GLOBAL_EVAL_FUN_INDEX, JSFunction, global_eval_fun)                   \
87   V(GLOBAL_PROXY_FUNCTION_INDEX, JSFunction, global_proxy_function)       \
88   V(MAP_DELETE_INDEX, JSFunction, map_delete)                             \
89   V(MAP_GET_INDEX, JSFunction, map_get)                                   \
90   V(MAP_HAS_INDEX, JSFunction, map_has)                                   \
91   V(MAP_SET_INDEX, JSFunction, map_set)                                   \
92   V(FUNCTION_HAS_INSTANCE_INDEX, JSFunction, function_has_instance)       \
93   V(OBJECT_VALUE_OF, JSFunction, object_value_of)                         \
94   V(OBJECT_TO_STRING, JSFunction, object_to_string)                       \
95   V(PROMISE_CATCH_INDEX, JSFunction, promise_catch)                       \
96   V(PROMISE_FUNCTION_INDEX, JSFunction, promise_function)                 \
97   V(RANGE_ERROR_FUNCTION_INDEX, JSFunction, range_error_function)         \
98   V(REFERENCE_ERROR_FUNCTION_INDEX, JSFunction, reference_error_function) \
99   V(SET_ADD_INDEX, JSFunction, set_add)                                   \
100   V(SET_DELETE_INDEX, JSFunction, set_delete)                             \
101   V(SET_HAS_INDEX, JSFunction, set_has)                                   \
102   V(SYNTAX_ERROR_FUNCTION_INDEX, JSFunction, syntax_error_function)       \
103   V(TYPE_ERROR_FUNCTION_INDEX, JSFunction, type_error_function)           \
104   V(URI_ERROR_FUNCTION_INDEX, JSFunction, uri_error_function)             \
105   V(WASM_COMPILE_ERROR_FUNCTION_INDEX, JSFunction,                        \
106     wasm_compile_error_function)                                          \
107   V(WASM_LINK_ERROR_FUNCTION_INDEX, JSFunction, wasm_link_error_function) \
108   V(WASM_RUNTIME_ERROR_FUNCTION_INDEX, JSFunction,                        \
109     wasm_runtime_error_function)                                          \
110   V(WEAKMAP_SET_INDEX, JSFunction, weakmap_set)                           \
111   V(WEAKMAP_GET_INDEX, JSFunction, weakmap_get)                           \
112   V(WEAKSET_ADD_INDEX, JSFunction, weakset_add)
113 
114 #define NATIVE_CONTEXT_FIELDS(V)                                               \
115   V(GLOBAL_PROXY_INDEX, JSObject, global_proxy_object)                         \
116   V(EMBEDDER_DATA_INDEX, FixedArray, embedder_data)                            \
117   /* Below is alpha-sorted */                                                  \
118   V(ACCESSOR_PROPERTY_DESCRIPTOR_MAP_INDEX, Map,                               \
119     accessor_property_descriptor_map)                                          \
120   V(ALLOW_CODE_GEN_FROM_STRINGS_INDEX, Object, allow_code_gen_from_strings)    \
121   V(ARRAY_BUFFER_FUN_INDEX, JSFunction, array_buffer_fun)                      \
122   V(ARRAY_BUFFER_MAP_INDEX, Map, array_buffer_map)                             \
123   V(ARRAY_BUFFER_NOINIT_FUN_INDEX, JSFunction, array_buffer_noinit_fun)        \
124   V(ARRAY_FUNCTION_INDEX, JSFunction, array_function)                          \
125   V(ASYNC_FROM_SYNC_ITERATOR_MAP_INDEX, Map, async_from_sync_iterator_map)     \
126   V(ASYNC_FUNCTION_AWAIT_REJECT_SHARED_FUN, SharedFunctionInfo,                \
127     async_function_await_reject_shared_fun)                                    \
128   V(ASYNC_FUNCTION_AWAIT_RESOLVE_SHARED_FUN, SharedFunctionInfo,               \
129     async_function_await_resolve_shared_fun)                                   \
130   V(ASYNC_FUNCTION_FUNCTION_INDEX, JSFunction, async_function_constructor)     \
131   V(ASYNC_GENERATOR_FUNCTION_FUNCTION_INDEX, JSFunction,                       \
132     async_generator_function_function)                                         \
133   V(ASYNC_ITERATOR_VALUE_UNWRAP_SHARED_FUN, SharedFunctionInfo,                \
134     async_iterator_value_unwrap_shared_fun)                                    \
135   V(ASYNC_GENERATOR_AWAIT_REJECT_SHARED_FUN, SharedFunctionInfo,               \
136     async_generator_await_reject_shared_fun)                                   \
137   V(ASYNC_GENERATOR_AWAIT_RESOLVE_SHARED_FUN, SharedFunctionInfo,              \
138     async_generator_await_resolve_shared_fun)                                  \
139   V(ASYNC_GENERATOR_YIELD_RESOLVE_SHARED_FUN, SharedFunctionInfo,              \
140     async_generator_yield_resolve_shared_fun)                                  \
141   V(ASYNC_GENERATOR_RETURN_RESOLVE_SHARED_FUN, SharedFunctionInfo,             \
142     async_generator_return_resolve_shared_fun)                                 \
143   V(ASYNC_GENERATOR_RETURN_CLOSED_RESOLVE_SHARED_FUN, SharedFunctionInfo,      \
144     async_generator_return_closed_resolve_shared_fun)                          \
145   V(ASYNC_GENERATOR_RETURN_CLOSED_REJECT_SHARED_FUN, SharedFunctionInfo,       \
146     async_generator_return_closed_reject_shared_fun)                           \
147   V(ATOMICS_OBJECT, JSObject, atomics_object)                                  \
148   V(BIGINT_FUNCTION_INDEX, JSFunction, bigint_function)                        \
149   V(BIGINT64_ARRAY_FUN_INDEX, JSFunction, bigint64_array_fun)                  \
150   V(BIGUINT64_ARRAY_FUN_INDEX, JSFunction, biguint64_array_fun)                \
151   V(BOOLEAN_FUNCTION_INDEX, JSFunction, boolean_function)                      \
152   V(BOUND_FUNCTION_WITH_CONSTRUCTOR_MAP_INDEX, Map,                            \
153     bound_function_with_constructor_map)                                       \
154   V(BOUND_FUNCTION_WITHOUT_CONSTRUCTOR_MAP_INDEX, Map,                         \
155     bound_function_without_constructor_map)                                    \
156   V(CALL_AS_CONSTRUCTOR_DELEGATE_INDEX, JSFunction,                            \
157     call_as_constructor_delegate)                                              \
158   V(CALL_AS_FUNCTION_DELEGATE_INDEX, JSFunction, call_as_function_delegate)    \
159   V(CALLSITE_FUNCTION_INDEX, JSFunction, callsite_function)                    \
160   V(CONTEXT_EXTENSION_FUNCTION_INDEX, JSFunction, context_extension_function)  \
161   V(DATA_PROPERTY_DESCRIPTOR_MAP_INDEX, Map, data_property_descriptor_map)     \
162   V(DATA_VIEW_FUN_INDEX, JSFunction, data_view_fun)                            \
163   V(DATE_FUNCTION_INDEX, JSFunction, date_function)                            \
164   V(DEBUG_CONTEXT_ID_INDEX, Object, debug_context_id)                          \
165   V(EMPTY_FUNCTION_INDEX, JSFunction, empty_function)                          \
166   V(ERROR_MESSAGE_FOR_CODE_GEN_FROM_STRINGS_INDEX, Object,                     \
167     error_message_for_code_gen_from_strings)                                   \
168   V(ERRORS_THROWN_INDEX, Smi, errors_thrown)                                   \
169   V(EXTRAS_EXPORTS_OBJECT_INDEX, JSObject, extras_binding_object)              \
170   V(EXTRAS_UTILS_OBJECT_INDEX, Object, extras_utils_object)                    \
171   V(FAST_ALIASED_ARGUMENTS_MAP_INDEX, Map, fast_aliased_arguments_map)         \
172   V(FAST_TEMPLATE_INSTANTIATIONS_CACHE_INDEX, FixedArray,                      \
173     fast_template_instantiations_cache)                                        \
174   V(FLOAT32_ARRAY_FUN_INDEX, JSFunction, float32_array_fun)                    \
175   V(FLOAT64_ARRAY_FUN_INDEX, JSFunction, float64_array_fun)                    \
176   V(FUNCTION_FUNCTION_INDEX, JSFunction, function_function)                    \
177   V(GENERATOR_FUNCTION_FUNCTION_INDEX, JSFunction,                             \
178     generator_function_function)                                               \
179   V(GENERATOR_OBJECT_PROTOTYPE_MAP_INDEX, Map, generator_object_prototype_map) \
180   V(ASYNC_GENERATOR_OBJECT_PROTOTYPE_MAP_INDEX, Map,                           \
181     async_generator_object_prototype_map)                                      \
182   V(INITIAL_ARRAY_ITERATOR_MAP_INDEX, Map, initial_array_iterator_map)         \
183   V(INITIAL_ARRAY_ITERATOR_PROTOTYPE_INDEX, JSObject,                          \
184     initial_array_iterator_prototype)                                          \
185   V(INITIAL_ARRAY_PROTOTYPE_INDEX, JSObject, initial_array_prototype)          \
186   V(INITIAL_ERROR_PROTOTYPE_INDEX, JSObject, initial_error_prototype)          \
187   V(INITIAL_GENERATOR_PROTOTYPE_INDEX, JSObject, initial_generator_prototype)  \
188   V(INITIAL_ASYNC_GENERATOR_PROTOTYPE_INDEX, JSObject,                         \
189     initial_async_generator_prototype)                                         \
190   V(INITIAL_ITERATOR_PROTOTYPE_INDEX, JSObject, initial_iterator_prototype)    \
191   V(INITIAL_MAP_PROTOTYPE_MAP_INDEX, Map, initial_map_prototype_map)           \
192   V(INITIAL_OBJECT_PROTOTYPE_INDEX, JSObject, initial_object_prototype)        \
193   V(INITIAL_SET_PROTOTYPE_MAP_INDEX, Map, initial_set_prototype_map)           \
194   V(INITIAL_STRING_PROTOTYPE_INDEX, JSObject, initial_string_prototype)        \
195   V(INITIAL_WEAKMAP_PROTOTYPE_MAP_INDEX, Map, initial_weakmap_prototype_map)   \
196   V(INITIAL_WEAKSET_PROTOTYPE_MAP_INDEX, Map, initial_weakset_prototype_map)   \
197   V(INT16_ARRAY_FUN_INDEX, JSFunction, int16_array_fun)                        \
198   V(INT32_ARRAY_FUN_INDEX, JSFunction, int32_array_fun)                        \
199   V(INT8_ARRAY_FUN_INDEX, JSFunction, int8_array_fun)                          \
200   V(INTERNAL_ARRAY_FUNCTION_INDEX, JSFunction, internal_array_function)        \
201   V(ITERATOR_RESULT_MAP_INDEX, Map, iterator_result_map)                       \
202   V(INTL_DATE_TIME_FORMAT_FUNCTION_INDEX, JSFunction,                          \
203     intl_date_time_format_function)                                            \
204   V(INTL_NUMBER_FORMAT_FUNCTION_INDEX, JSFunction,                             \
205     intl_number_format_function)                                               \
206   V(INTL_LOCALE_FUNCTION_INDEX, JSFunction, intl_locale_function)              \
207   V(INTL_COLLATOR_FUNCTION_INDEX, JSFunction, intl_collator_function)          \
208   V(INTL_PLURAL_RULES_FUNCTION_INDEX, JSFunction, intl_plural_rules_function)  \
209   V(INTL_V8_BREAK_ITERATOR_FUNCTION_INDEX, JSFunction,                         \
210     intl_v8_break_iterator_function)                                           \
211   V(JS_ARRAY_PACKED_SMI_ELEMENTS_MAP_INDEX, Map,                               \
212     js_array_fast_smi_elements_map_index)                                      \
213   V(JS_ARRAY_HOLEY_SMI_ELEMENTS_MAP_INDEX, Map,                                \
214     js_array_fast_holey_smi_elements_map_index)                                \
215   V(JS_ARRAY_PACKED_ELEMENTS_MAP_INDEX, Map, js_array_fast_elements_map_index) \
216   V(JS_ARRAY_HOLEY_ELEMENTS_MAP_INDEX, Map,                                    \
217     js_array_fast_holey_elements_map_index)                                    \
218   V(JS_ARRAY_PACKED_DOUBLE_ELEMENTS_MAP_INDEX, Map,                            \
219     js_array_fast_double_elements_map_index)                                   \
220   V(JS_ARRAY_HOLEY_DOUBLE_ELEMENTS_MAP_INDEX, Map,                             \
221     js_array_fast_holey_double_elements_map_index)                             \
222   V(JS_MAP_FUN_INDEX, JSFunction, js_map_fun)                                  \
223   V(JS_MAP_MAP_INDEX, Map, js_map_map)                                         \
224   V(JS_MODULE_NAMESPACE_MAP, Map, js_module_namespace_map)                     \
225   V(JS_SET_FUN_INDEX, JSFunction, js_set_fun)                                  \
226   V(JS_SET_MAP_INDEX, Map, js_set_map)                                         \
227   V(JS_WEAK_MAP_FUN_INDEX, JSFunction, js_weak_map_fun)                        \
228   V(JS_WEAK_SET_FUN_INDEX, JSFunction, js_weak_set_fun)                        \
229   V(MAP_CACHE_INDEX, Object, map_cache)                                        \
230   V(MAP_KEY_ITERATOR_MAP_INDEX, Map, map_key_iterator_map)                     \
231   V(MAP_KEY_VALUE_ITERATOR_MAP_INDEX, Map, map_key_value_iterator_map)         \
232   V(MAP_VALUE_ITERATOR_MAP_INDEX, Map, map_value_iterator_map)                 \
233   V(MATH_RANDOM_INDEX_INDEX, Smi, math_random_index)                           \
234   V(MATH_RANDOM_CACHE_INDEX, Object, math_random_cache)                        \
235   V(MESSAGE_LISTENERS_INDEX, TemplateList, message_listeners)                  \
236   V(NATIVES_UTILS_OBJECT_INDEX, Object, natives_utils_object)                  \
237   V(NORMALIZED_MAP_CACHE_INDEX, Object, normalized_map_cache)                  \
238   V(NUMBER_FUNCTION_INDEX, JSFunction, number_function)                        \
239   V(OBJECT_FUNCTION_INDEX, JSFunction, object_function)                        \
240   V(OBJECT_FUNCTION_PROTOTYPE_MAP_INDEX, Map, object_function_prototype_map)   \
241   V(OPAQUE_REFERENCE_FUNCTION_INDEX, JSFunction, opaque_reference_function)    \
242   V(PROXY_CALLABLE_MAP_INDEX, Map, proxy_callable_map)                         \
243   V(PROXY_CONSTRUCTOR_MAP_INDEX, Map, proxy_constructor_map)                   \
244   V(PROXY_FUNCTION_INDEX, JSFunction, proxy_function)                          \
245   V(PROXY_MAP_INDEX, Map, proxy_map)                                           \
246   V(PROXY_REVOCABLE_RESULT_MAP_INDEX, Map, proxy_revocable_result_map)         \
247   V(PROXY_REVOKE_SHARED_FUN, SharedFunctionInfo, proxy_revoke_shared_fun)      \
248   V(PROMISE_GET_CAPABILITIES_EXECUTOR_SHARED_FUN, SharedFunctionInfo,          \
249     promise_get_capabilities_executor_shared_fun)                              \
250   V(PROMISE_CAPABILITY_DEFAULT_REJECT_SHARED_FUN_INDEX, SharedFunctionInfo,    \
251     promise_capability_default_reject_shared_fun)                              \
252   V(PROMISE_CAPABILITY_DEFAULT_RESOLVE_SHARED_FUN_INDEX, SharedFunctionInfo,   \
253     promise_capability_default_resolve_shared_fun)                             \
254   V(PROMISE_THEN_FINALLY_SHARED_FUN, SharedFunctionInfo,                       \
255     promise_then_finally_shared_fun)                                           \
256   V(PROMISE_CATCH_FINALLY_SHARED_FUN, SharedFunctionInfo,                      \
257     promise_catch_finally_shared_fun)                                          \
258   V(PROMISE_VALUE_THUNK_FINALLY_SHARED_FUN, SharedFunctionInfo,                \
259     promise_value_thunk_finally_shared_fun)                                    \
260   V(PROMISE_THROWER_FINALLY_SHARED_FUN, SharedFunctionInfo,                    \
261     promise_thrower_finally_shared_fun)                                        \
262   V(PROMISE_ALL_RESOLVE_ELEMENT_SHARED_FUN, SharedFunctionInfo,                \
263     promise_all_resolve_element_shared_fun)                                    \
264   V(PROMISE_PROTOTYPE_INDEX, JSObject, promise_prototype)                      \
265   V(REGEXP_EXEC_FUNCTION_INDEX, JSFunction, regexp_exec_function)              \
266   V(REGEXP_FUNCTION_INDEX, JSFunction, regexp_function)                        \
267   V(REGEXP_LAST_MATCH_INFO_INDEX, RegExpMatchInfo, regexp_last_match_info)     \
268   V(REGEXP_INTERNAL_MATCH_INFO_INDEX, RegExpMatchInfo,                         \
269     regexp_internal_match_info)                                                \
270   V(REGEXP_PROTOTYPE_MAP_INDEX, Map, regexp_prototype_map)                     \
271   V(INITIAL_REGEXP_STRING_ITERATOR_PROTOTYPE_MAP_INDEX, Map,                   \
272     initial_regexp_string_iterator_prototype_map_index)                        \
273   V(REGEXP_RESULT_MAP_INDEX, Map, regexp_result_map)                           \
274   V(SCRIPT_CONTEXT_TABLE_INDEX, ScriptContextTable, script_context_table)      \
275   V(SCRIPT_FUNCTION_INDEX, JSFunction, script_function)                        \
276   V(SECURITY_TOKEN_INDEX, Object, security_token)                              \
277   V(SELF_WEAK_CELL_INDEX, WeakCell, self_weak_cell)                            \
278   V(SERIALIZED_OBJECTS, FixedArray, serialized_objects)                        \
279   V(SET_VALUE_ITERATOR_MAP_INDEX, Map, set_value_iterator_map)                 \
280   V(SET_KEY_VALUE_ITERATOR_MAP_INDEX, Map, set_key_value_iterator_map)         \
281   V(SHARED_ARRAY_BUFFER_FUN_INDEX, JSFunction, shared_array_buffer_fun)        \
282   V(SLOPPY_ARGUMENTS_MAP_INDEX, Map, sloppy_arguments_map)                     \
283   V(SLOW_ALIASED_ARGUMENTS_MAP_INDEX, Map, slow_aliased_arguments_map)         \
284   V(STRICT_ARGUMENTS_MAP_INDEX, Map, strict_arguments_map)                     \
285   V(SLOW_OBJECT_WITH_NULL_PROTOTYPE_MAP, Map,                                  \
286     slow_object_with_null_prototype_map)                                       \
287   V(SLOW_OBJECT_WITH_OBJECT_PROTOTYPE_MAP, Map,                                \
288     slow_object_with_object_prototype_map)                                     \
289   V(SLOW_TEMPLATE_INSTANTIATIONS_CACHE_INDEX, SimpleNumberDictionary,          \
290     slow_template_instantiations_cache)                                        \
291   /* All *_FUNCTION_MAP_INDEX definitions used by Context::FunctionMapIndex */ \
292   /* must remain together. */                                                  \
293   V(SLOPPY_FUNCTION_MAP_INDEX, Map, sloppy_function_map)                       \
294   V(SLOPPY_FUNCTION_WITH_NAME_MAP_INDEX, Map, sloppy_function_with_name_map)   \
295   V(SLOPPY_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map,                          \
296     sloppy_function_without_prototype_map)                                     \
297   V(SLOPPY_FUNCTION_WITH_READONLY_PROTOTYPE_MAP_INDEX, Map,                    \
298     sloppy_function_with_readonly_prototype_map)                               \
299   V(STRICT_FUNCTION_MAP_INDEX, Map, strict_function_map)                       \
300   V(STRICT_FUNCTION_WITH_NAME_MAP_INDEX, Map, strict_function_with_name_map)   \
301   V(STRICT_FUNCTION_WITH_READONLY_PROTOTYPE_MAP_INDEX, Map,                    \
302     strict_function_with_readonly_prototype_map)                               \
303   V(STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map,                          \
304     strict_function_without_prototype_map)                                     \
305   V(METHOD_WITH_NAME_MAP_INDEX, Map, method_with_name_map)                     \
306   V(METHOD_WITH_HOME_OBJECT_MAP_INDEX, Map, method_with_home_object_map)       \
307   V(METHOD_WITH_NAME_AND_HOME_OBJECT_MAP_INDEX, Map,                           \
308     method_with_name_and_home_object_map)                                      \
309   V(ASYNC_FUNCTION_MAP_INDEX, Map, async_function_map)                         \
310   V(ASYNC_FUNCTION_WITH_NAME_MAP_INDEX, Map, async_function_with_name_map)     \
311   V(ASYNC_FUNCTION_WITH_HOME_OBJECT_MAP_INDEX, Map,                            \
312     async_function_with_home_object_map)                                       \
313   V(ASYNC_FUNCTION_WITH_NAME_AND_HOME_OBJECT_MAP_INDEX, Map,                   \
314     async_function_with_name_and_home_object_map)                              \
315   V(GENERATOR_FUNCTION_MAP_INDEX, Map, generator_function_map)                 \
316   V(GENERATOR_FUNCTION_WITH_NAME_MAP_INDEX, Map,                               \
317     generator_function_with_name_map)                                          \
318   V(GENERATOR_FUNCTION_WITH_HOME_OBJECT_MAP_INDEX, Map,                        \
319     generator_function_with_home_object_map)                                   \
320   V(GENERATOR_FUNCTION_WITH_NAME_AND_HOME_OBJECT_MAP_INDEX, Map,               \
321     generator_function_with_name_and_home_object_map)                          \
322   V(ASYNC_GENERATOR_FUNCTION_MAP_INDEX, Map, async_generator_function_map)     \
323   V(ASYNC_GENERATOR_FUNCTION_WITH_NAME_MAP_INDEX, Map,                         \
324     async_generator_function_with_name_map)                                    \
325   V(ASYNC_GENERATOR_FUNCTION_WITH_HOME_OBJECT_MAP_INDEX, Map,                  \
326     async_generator_function_with_home_object_map)                             \
327   V(ASYNC_GENERATOR_FUNCTION_WITH_NAME_AND_HOME_OBJECT_MAP_INDEX, Map,         \
328     async_generator_function_with_name_and_home_object_map)                    \
329   V(CLASS_FUNCTION_MAP_INDEX, Map, class_function_map)                         \
330   V(STRING_FUNCTION_INDEX, JSFunction, string_function)                        \
331   V(STRING_FUNCTION_PROTOTYPE_MAP_INDEX, Map, string_function_prototype_map)   \
332   V(STRING_ITERATOR_MAP_INDEX, Map, string_iterator_map)                       \
333   V(SYMBOL_FUNCTION_INDEX, JSFunction, symbol_function)                        \
334   V(NATIVE_FUNCTION_MAP_INDEX, Map, native_function_map)                       \
335   V(WASM_GLOBAL_CONSTRUCTOR_INDEX, JSFunction, wasm_global_constructor)        \
336   V(WASM_INSTANCE_CONSTRUCTOR_INDEX, JSFunction, wasm_instance_constructor)    \
337   V(WASM_MEMORY_CONSTRUCTOR_INDEX, JSFunction, wasm_memory_constructor)        \
338   V(WASM_MODULE_CONSTRUCTOR_INDEX, JSFunction, wasm_module_constructor)        \
339   V(WASM_TABLE_CONSTRUCTOR_INDEX, JSFunction, wasm_table_constructor)          \
340   V(TYPED_ARRAY_FUN_INDEX, JSFunction, typed_array_function)                   \
341   V(TYPED_ARRAY_PROTOTYPE_INDEX, JSObject, typed_array_prototype)              \
342   V(UINT16_ARRAY_FUN_INDEX, JSFunction, uint16_array_fun)                      \
343   V(UINT32_ARRAY_FUN_INDEX, JSFunction, uint32_array_fun)                      \
344   V(UINT8_ARRAY_FUN_INDEX, JSFunction, uint8_array_fun)                        \
345   V(UINT8_CLAMPED_ARRAY_FUN_INDEX, JSFunction, uint8_clamped_array_fun)        \
346   NATIVE_CONTEXT_INTRINSIC_FUNCTIONS(V)                                        \
347   NATIVE_CONTEXT_IMPORTED_FIELDS(V)
348 
349 // A table of all script contexts. Every loaded top-level script with top-level
350 // lexical declarations contributes its ScriptContext into this table.
351 //
352 // The table is a fixed array, its first slot is the current used count and
353 // the subsequent slots 1..used contain ScriptContexts.
354 class ScriptContextTable : public FixedArray {
355  public:
356   // Conversions.
357   static inline ScriptContextTable* cast(Object* context);
358 
359   struct LookupResult {
360     int context_index;
361     int slot_index;
362     VariableMode mode;
363     InitializationFlag init_flag;
364     MaybeAssignedFlag maybe_assigned_flag;
365   };
366 
367   inline int used() const;
368   inline void set_used(int used);
369 
370   static inline Handle<Context> GetContext(Handle<ScriptContextTable> table,
371                                            int i);
372 
373   // Lookup a variable `name` in a ScriptContextTable.
374   // If it returns true, the variable is found and `result` contains
375   // valid information about its location.
376   // If it returns false, `result` is untouched.
377   V8_WARN_UNUSED_RESULT
378   static bool Lookup(Handle<ScriptContextTable> table, Handle<String> name,
379                      LookupResult* result);
380 
381   V8_WARN_UNUSED_RESULT
382   static Handle<ScriptContextTable> Extend(Handle<ScriptContextTable> table,
383                                            Handle<Context> script_context);
384 
385   static const int kUsedSlotIndex = 0;
386   static const int kFirstContextSlotIndex = 1;
387   static const int kMinLength = kFirstContextSlotIndex;
388 
389   DISALLOW_IMPLICIT_CONSTRUCTORS(ScriptContextTable);
390 };
391 
392 // JSFunctions are pairs (context, function code), sometimes also called
393 // closures. A Context object is used to represent function contexts and
394 // dynamically pushed 'with' contexts (or 'scopes' in ECMA-262 speak).
395 //
396 // At runtime, the contexts build a stack in parallel to the execution
397 // stack, with the top-most context being the current context. All contexts
398 // have the following slots:
399 //
400 // [ scope_info     ]  This is the scope info describing the current context. It
401 //                     contains the names of statically allocated context slots,
402 //                     and stack-allocated locals.  The names are needed for
403 //                     dynamic lookups in the presence of 'with' or 'eval', and
404 //                     for the debugger.
405 //
406 // [ previous       ]  A pointer to the previous context.
407 //
408 // [ extension      ]  Additional data.
409 //
410 //                     For module contexts, it contains the module object.
411 //
412 //                     For block contexts, it may contain an "extension object"
413 //                     (see below).
414 //
415 //                     For with contexts, it contains an "extension object".
416 //
417 //                     An "extension object" is used to dynamically extend a
418 //                     context with additional variables, namely in the
419 //                     implementation of the 'with' construct and the 'eval'
420 //                     construct.  For instance, Context::Lookup also searches
421 //                     the extension object for properties.  (Storing the
422 //                     extension object is the original purpose of this context
423 //                     slot, hence the name.)
424 //
425 // [ native_context ]  A pointer to the native context.
426 //
427 // In addition, function contexts may have statically allocated context slots
428 // to store local variables/functions that are accessed from inner functions
429 // (via static context addresses) or through 'eval' (dynamic context lookups).
430 // The native context contains additional slots for fast access to native
431 // properties.
432 //
433 // Finally, with Harmony scoping, the JSFunction representing a top level
434 // script will have the ScriptContext rather than a FunctionContext.
435 // Script contexts from all top-level scripts are gathered in
436 // ScriptContextTable.
437 
438 class Context : public FixedArray {
439  public:
440   // Conversions.
441   static inline Context* cast(Object* context);
442 
443   // The default context slot layout; indices are FixedArray slot indices.
444   enum Field {
445     // These slots are in all contexts.
446     SCOPE_INFO_INDEX,
447     PREVIOUS_INDEX,
448     // The extension slot is used for either the global object (in native
449     // contexts), eval extension object (function contexts), subject of with
450     // (with contexts), or the variable name (catch contexts), the serialized
451     // scope info (block contexts), or the module instance (module contexts).
452     EXTENSION_INDEX,
453     NATIVE_CONTEXT_INDEX,
454 
455 // These slots are only in native contexts.
456 #define NATIVE_CONTEXT_SLOT(index, type, name) index,
457     NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_SLOT)
458 #undef NATIVE_CONTEXT_SLOT
459 
460     // Properties from here are treated as weak references by the full GC.
461     // Scavenge treats them as strong references.
462     OPTIMIZED_CODE_LIST,    // Weak.
463     DEOPTIMIZED_CODE_LIST,  // Weak.
464     NEXT_CONTEXT_LINK,      // Weak.
465 
466     // Total number of slots.
467     NATIVE_CONTEXT_SLOTS,
468     FIRST_WEAK_SLOT = OPTIMIZED_CODE_LIST,
469     FIRST_JS_ARRAY_MAP_SLOT = JS_ARRAY_PACKED_SMI_ELEMENTS_MAP_INDEX,
470 
471     MIN_CONTEXT_SLOTS = GLOBAL_PROXY_INDEX,
472     // This slot holds the thrown value in catch contexts.
473     THROWN_OBJECT_INDEX = MIN_CONTEXT_SLOTS,
474 
475     // These slots hold values in debug evaluate contexts.
476     WRAPPED_CONTEXT_INDEX = MIN_CONTEXT_SLOTS,
477     WHITE_LIST_INDEX = MIN_CONTEXT_SLOTS + 1
478   };
479 
480   // A region of native context entries containing maps for functions created
481   // by Builtins::kFastNewClosure.
482   static const int FIRST_FUNCTION_MAP_INDEX = SLOPPY_FUNCTION_MAP_INDEX;
483   static const int LAST_FUNCTION_MAP_INDEX = CLASS_FUNCTION_MAP_INDEX;
484 
485   static const int kNoContext = 0;
486   static const int kInvalidContext = 1;
487 
488   void ResetErrorsThrown();
489   void IncrementErrorsThrown();
490   int GetErrorsThrown();
491 
492   // Direct slot access.
493   inline void set_scope_info(ScopeInfo* scope_info);
494   inline Context* previous();
495   inline void set_previous(Context* context);
496 
497   inline Object* next_context_link();
498 
499   inline bool has_extension();
500   inline HeapObject* extension();
501   inline void set_extension(HeapObject* object);
502   JSObject* extension_object();
503   JSReceiver* extension_receiver();
504   ScopeInfo* scope_info();
505 
506   // Find the module context (assuming there is one) and return the associated
507   // module object.
508   Module* module();
509 
510   // Get the context where var declarations will be hoisted to, which
511   // may be the context itself.
512   Context* declaration_context();
513   bool is_declaration_context();
514 
515   // Get the next closure's context on the context chain.
516   Context* closure_context();
517 
518   // Returns a JSGlobalProxy object or null.
519   JSObject* global_proxy();
520   void set_global_proxy(JSObject* global);
521 
522   // Get the JSGlobalObject object.
523   V8_EXPORT_PRIVATE JSGlobalObject* global_object();
524 
525   // Get the script context by traversing the context chain.
526   Context* script_context();
527 
528   // Compute the native context.
529   inline Context* native_context() const;
530   inline void set_native_context(Context* context);
531 
532   // Predicates for context types.  IsNativeContext is also defined on Object
533   // because we frequently have to know if arbitrary objects are natives
534   // contexts.
535   inline bool IsNativeContext() const;
536   inline bool IsFunctionContext() const;
537   inline bool IsCatchContext() const;
538   inline bool IsWithContext() const;
539   inline bool IsDebugEvaluateContext() const;
540   inline bool IsBlockContext() const;
541   inline bool IsModuleContext() const;
542   inline bool IsEvalContext() const;
543   inline bool IsScriptContext() const;
544 
545   inline bool HasSameSecurityTokenAs(Context* that) const;
546 
547   // The native context also stores a list of all optimized code and a
548   // list of all deoptimized code, which are needed by the deoptimizer.
549   void AddOptimizedCode(Code* code);
550   void SetOptimizedCodeListHead(Object* head);
551   Object* OptimizedCodeListHead();
552   void SetDeoptimizedCodeListHead(Object* head);
553   Object* DeoptimizedCodeListHead();
554 
555   Handle<Object> ErrorMessageForCodeGenerationFromStrings();
556 
557   static int ImportedFieldIndexForName(Handle<String> name);
558   static int IntrinsicIndexForName(Handle<String> name);
559   static int IntrinsicIndexForName(const unsigned char* name, int length);
560 
561 #define NATIVE_CONTEXT_FIELD_ACCESSORS(index, type, name) \
562   inline void set_##name(type* value);                    \
563   inline bool is_##name(type* value) const;               \
564   inline type* name() const;
565   NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_FIELD_ACCESSORS)
566 #undef NATIVE_CONTEXT_FIELD_ACCESSORS
567 
568   // Lookup the slot called name, starting with the current context.
569   // There are three possibilities:
570   //
571   // 1) result->IsContext():
572   //    The binding was found in a context.  *index is always the
573   //    non-negative slot index.  *attributes is NONE for var and let
574   //    declarations, READ_ONLY for const declarations (never ABSENT).
575   //
576   // 2) result->IsJSObject():
577   //    The binding was found as a named property in a context extension
578   //    object (i.e., was introduced via eval), as a property on the subject
579   //    of with, or as a property of the global object.  *index is -1 and
580   //    *attributes is not ABSENT.
581   //
582   // 3) result->IsModule():
583   //    The binding was found in module imports or exports.
584   //     *attributes is never ABSENT. imports are READ_ONLY.
585   //
586   // 4) result.is_null():
587   //    There was no binding found, *index is always -1 and *attributes is
588   //    always ABSENT.
589   Handle<Object> Lookup(Handle<String> name, ContextLookupFlags flags,
590                         int* index, PropertyAttributes* attributes,
591                         InitializationFlag* init_flag,
592                         VariableMode* variable_mode,
593                         bool* is_sloppy_function_name = nullptr);
594 
595   // Code generation support.
SlotOffset(int index)596   static int SlotOffset(int index) {
597     return kHeaderSize + index * kPointerSize - kHeapObjectTag;
598   }
599 
600   static inline int FunctionMapIndex(LanguageMode language_mode,
601                                      FunctionKind kind, bool has_prototype_slot,
602                                      bool has_shared_name,
603                                      bool needs_home_object);
604 
ArrayMapIndex(ElementsKind elements_kind)605   static int ArrayMapIndex(ElementsKind elements_kind) {
606     DCHECK(IsFastElementsKind(elements_kind));
607     return elements_kind + FIRST_JS_ARRAY_MAP_SLOT;
608   }
609 
610   inline Map* GetInitialJSArrayMap(ElementsKind kind) const;
611 
612   static const int kSize = kHeaderSize + NATIVE_CONTEXT_SLOTS * kPointerSize;
613   static const int kNotFound = -1;
614 
615   // GC support.
616   typedef FixedBodyDescriptor<kHeaderSize, kSize, kSize> BodyDescriptor;
617 
618   typedef FixedBodyDescriptor<
619       kHeaderSize, kHeaderSize + FIRST_WEAK_SLOT * kPointerSize, kSize>
620       BodyDescriptorWeak;
621 
622  private:
623 #ifdef DEBUG
624   // Bootstrapping-aware type checks.
625   V8_EXPORT_PRIVATE static bool IsBootstrappingOrNativeContext(Isolate* isolate,
626                                                                Object* object);
627   static bool IsBootstrappingOrValidParentContext(Object* object, Context* kid);
628 #endif
629 
630   STATIC_ASSERT(kHeaderSize == Internals::kContextHeaderSize);
631   STATIC_ASSERT(EMBEDDER_DATA_INDEX == Internals::kContextEmbedderDataIndex);
632 };
633 
634 typedef Context::Field ContextField;
635 
636 }  // namespace internal
637 }  // namespace v8
638 
639 #endif  // V8_CONTEXTS_H_
640