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