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