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