blob: 78ec4319eb6c615586ffb455eec6c8613ae8fe0c [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
5#ifndef V8_CONTEXTS_H_
6#define V8_CONTEXTS_H_
7
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008#include "src/heap/heap.h"
9#include "src/objects.h"
Kristian Monsen80d68ea2010-09-08 11:05:35 +010010
Steve Blocka7e24c12009-10-30 11:49:00 +000011namespace v8 {
12namespace internal {
13
14
15enum ContextLookupFlags {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016 FOLLOW_CONTEXT_CHAIN = 1 << 0,
17 FOLLOW_PROTOTYPE_CHAIN = 1 << 1,
18 STOP_AT_DECLARATION_SCOPE = 1 << 2,
19 SKIP_WITH_CONTEXT = 1 << 3,
Steve Blocka7e24c12009-10-30 11:49:00 +000020
21 DONT_FOLLOW_CHAINS = 0,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000022 FOLLOW_CHAINS = FOLLOW_CONTEXT_CHAIN | FOLLOW_PROTOTYPE_CHAIN,
23 LEXICAL_TEST =
24 FOLLOW_CONTEXT_CHAIN | STOP_AT_DECLARATION_SCOPE | SKIP_WITH_CONTEXT,
Steve Blocka7e24c12009-10-30 11:49:00 +000025};
26
27
Ben Murdoch69a99ed2011-11-30 16:03:39 +000028// ES5 10.2 defines lexical environments with mutable and immutable bindings.
29// Immutable bindings have two states, initialized and uninitialized, and
Ben Murdoch3ef787d2012-04-12 10:51:47 +010030// their state is changed by the InitializeImmutableBinding method. The
31// BindingFlags enum represents information if a binding has definitely been
32// initialized. A mutable binding does not need to be checked and thus has
Ben Murdochc5610432016-08-08 18:44:38 +010033// the BindingFlag BINDING_IS_INITIALIZED.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010034//
Ben Murdochc5610432016-08-08 18:44:38 +010035// There is one possibility for legacy immutable bindings:
Ben Murdoch3ef787d2012-04-12 10:51:47 +010036// * The function name of a named function literal. The binding is immediately
37// initialized when entering the function and thus does not need to be
Ben Murdochc5610432016-08-08 18:44:38 +010038// checked. it gets the BindingFlag BINDING_IS_INITIALIZED.
Ben Murdoch69a99ed2011-11-30 16:03:39 +000039//
40// The harmony proposal for block scoped bindings also introduces the
Ben Murdoch3ef787d2012-04-12 10:51:47 +010041// uninitialized state for mutable bindings.
42// * A 'let' declared variable. They are initialized when evaluating the
43// corresponding declaration statement. They need to be checked for being
Ben Murdochc5610432016-08-08 18:44:38 +010044// initialized and thus get the flag BINDING_CHECK_INITIALIZED.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010045// * A 'var' declared variable. It is initialized immediately upon creation
46// and thus doesn't need to be checked. It gets the flag
Ben Murdochc5610432016-08-08 18:44:38 +010047// BINDING_IS_INITIALIZED.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010048// * Catch bound variables, function parameters and variables introduced by
49// function declarations are initialized immediately and do not need to be
Ben Murdochc5610432016-08-08 18:44:38 +010050// checked. Thus they get the flag BINDING_IS_INITIALIZED.
51// Accessing an uninitialized binding produces a reference error.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010052//
53// In V8 uninitialized bindings are set to the hole value upon creation and set
54// to a different value upon initialization.
Ben Murdoch69a99ed2011-11-30 16:03:39 +000055enum BindingFlags {
Ben Murdochc5610432016-08-08 18:44:38 +010056 BINDING_IS_INITIALIZED,
57 BINDING_CHECK_INITIALIZED,
Ben Murdoch69a99ed2011-11-30 16:03:39 +000058 MISSING_BINDING
59};
60
Steve Blocka7e24c12009-10-30 11:49:00 +000061// Heap-allocated activation contexts.
62//
63// Contexts are implemented as FixedArray objects; the Context
64// class is a convenience interface casted on a FixedArray object.
65//
66// Note: Context must have no virtual functions and Context objects
67// must always be allocated via Heap::AllocateContext() or
68// Factory::NewContext.
69
Ben Murdochc5610432016-08-08 18:44:38 +010070#define NATIVE_CONTEXT_INTRINSIC_FUNCTIONS(V) \
71 V(IS_ARRAYLIKE, JSFunction, is_arraylike) \
72 V(GET_TEMPLATE_CALL_SITE_INDEX, JSFunction, get_template_call_site) \
73 V(MAKE_RANGE_ERROR_INDEX, JSFunction, make_range_error) \
74 V(MAKE_TYPE_ERROR_INDEX, JSFunction, make_type_error) \
75 V(OBJECT_DEFINE_PROPERTIES, JSFunction, object_define_properties) \
76 V(OBJECT_DEFINE_PROPERTY, JSFunction, object_define_property) \
77 V(OBJECT_FREEZE, JSFunction, object_freeze) \
78 V(OBJECT_GET_PROTOTYPE_OF, JSFunction, object_get_prototype_of) \
79 V(OBJECT_IS_EXTENSIBLE, JSFunction, object_is_extensible) \
80 V(OBJECT_IS_FROZEN, JSFunction, object_is_frozen) \
81 V(OBJECT_IS_SEALED, JSFunction, object_is_sealed) \
82 V(OBJECT_KEYS, JSFunction, object_keys) \
83 V(REFLECT_APPLY_INDEX, JSFunction, reflect_apply) \
84 V(REFLECT_CONSTRUCT_INDEX, JSFunction, reflect_construct) \
85 V(REFLECT_DEFINE_PROPERTY_INDEX, JSFunction, reflect_define_property) \
86 V(REFLECT_DELETE_PROPERTY_INDEX, JSFunction, reflect_delete_property) \
87 V(SPREAD_ARGUMENTS_INDEX, JSFunction, spread_arguments) \
88 V(SPREAD_ITERABLE_INDEX, JSFunction, spread_iterable) \
Ben Murdoch61f157c2016-09-16 13:49:30 +010089 V(MATH_EXP_INDEX, JSFunction, math_exp) \
90 V(MATH_FLOOR_INDEX, JSFunction, math_floor) \
91 V(MATH_LOG_INDEX, JSFunction, math_log) \
92 V(MATH_SQRT_INDEX, JSFunction, math_sqrt)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000093
Ben Murdochc5610432016-08-08 18:44:38 +010094#define NATIVE_CONTEXT_IMPORTED_FIELDS(V) \
95 V(ARRAY_CONCAT_INDEX, JSFunction, array_concat) \
96 V(ARRAY_POP_INDEX, JSFunction, array_pop) \
97 V(ARRAY_PUSH_INDEX, JSFunction, array_push) \
98 V(ARRAY_SHIFT_INDEX, JSFunction, array_shift) \
99 V(ARRAY_SPLICE_INDEX, JSFunction, array_splice) \
100 V(ARRAY_SLICE_INDEX, JSFunction, array_slice) \
101 V(ARRAY_UNSHIFT_INDEX, JSFunction, array_unshift) \
102 V(ARRAY_VALUES_ITERATOR_INDEX, JSFunction, array_values_iterator) \
103 V(ASYNC_FUNCTION_AWAIT_INDEX, JSFunction, async_function_await) \
104 V(DERIVED_GET_TRAP_INDEX, JSFunction, derived_get_trap) \
105 V(ERROR_FUNCTION_INDEX, JSFunction, error_function) \
106 V(EVAL_ERROR_FUNCTION_INDEX, JSFunction, eval_error_function) \
107 V(GET_STACK_TRACE_LINE_INDEX, JSFunction, get_stack_trace_line_fun) \
108 V(GLOBAL_EVAL_FUN_INDEX, JSFunction, global_eval_fun) \
Ben Murdochc5610432016-08-08 18:44:38 +0100109 V(MAKE_ERROR_FUNCTION_INDEX, JSFunction, make_error_function) \
110 V(MAP_DELETE_METHOD_INDEX, JSFunction, map_delete) \
111 V(MAP_GET_METHOD_INDEX, JSFunction, map_get) \
112 V(MAP_HAS_METHOD_INDEX, JSFunction, map_has) \
113 V(MAP_SET_METHOD_INDEX, JSFunction, map_set) \
114 V(MATH_POW_METHOD_INDEX, JSFunction, math_pow) \
115 V(MESSAGE_GET_COLUMN_NUMBER_INDEX, JSFunction, message_get_column_number) \
116 V(MESSAGE_GET_LINE_NUMBER_INDEX, JSFunction, message_get_line_number) \
117 V(MESSAGE_GET_SOURCE_LINE_INDEX, JSFunction, message_get_source_line) \
118 V(NO_SIDE_EFFECTS_TO_STRING_FUN_INDEX, JSFunction, \
119 no_side_effects_to_string_fun) \
120 V(OBJECT_VALUE_OF, JSFunction, object_value_of) \
121 V(OBJECT_TO_STRING, JSFunction, object_to_string) \
122 V(PROMISE_CATCH_INDEX, JSFunction, promise_catch) \
123 V(PROMISE_CHAIN_INDEX, JSFunction, promise_chain) \
124 V(PROMISE_CREATE_INDEX, JSFunction, promise_create) \
125 V(PROMISE_FUNCTION_INDEX, JSFunction, promise_function) \
126 V(PROMISE_HAS_USER_DEFINED_REJECT_HANDLER_INDEX, JSFunction, \
127 promise_has_user_defined_reject_handler) \
128 V(PROMISE_REJECT_INDEX, JSFunction, promise_reject) \
129 V(PROMISE_RESOLVE_INDEX, JSFunction, promise_resolve) \
130 V(PROMISE_CREATE_RESOLVED_INDEX, JSFunction, promise_create_resolved) \
131 V(PROMISE_CREATE_REJECTED_INDEX, JSFunction, promise_create_rejected) \
132 V(PROMISE_THEN_INDEX, JSFunction, promise_then) \
133 V(RANGE_ERROR_FUNCTION_INDEX, JSFunction, range_error_function) \
134 V(REFERENCE_ERROR_FUNCTION_INDEX, JSFunction, reference_error_function) \
135 V(SET_ADD_METHOD_INDEX, JSFunction, set_add) \
136 V(SET_DELETE_METHOD_INDEX, JSFunction, set_delete) \
137 V(SET_HAS_METHOD_INDEX, JSFunction, set_has) \
138 V(STACK_OVERFLOW_BOILERPLATE_INDEX, JSObject, stack_overflow_boilerplate) \
139 V(SYNTAX_ERROR_FUNCTION_INDEX, JSFunction, syntax_error_function) \
140 V(TYPE_ERROR_FUNCTION_INDEX, JSFunction, type_error_function) \
Ben Murdoch097c5b22016-05-18 11:27:45 +0100141 V(URI_ERROR_FUNCTION_INDEX, JSFunction, uri_error_function)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000142
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000143#define NATIVE_CONTEXT_FIELDS(V) \
144 V(GLOBAL_PROXY_INDEX, JSObject, global_proxy_object) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000145 V(EMBEDDER_DATA_INDEX, FixedArray, embedder_data) \
146 /* Below is alpha-sorted */ \
Ben Murdoch097c5b22016-05-18 11:27:45 +0100147 V(ACCESSOR_PROPERTY_DESCRIPTOR_MAP_INDEX, Map, \
148 accessor_property_descriptor_map) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000149 V(ALLOW_CODE_GEN_FROM_STRINGS_INDEX, Object, allow_code_gen_from_strings) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000150 V(ARRAY_BUFFER_FUN_INDEX, JSFunction, array_buffer_fun) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000151 V(ARRAY_BUFFER_MAP_INDEX, Map, array_buffer_map) \
152 V(ARRAY_FUNCTION_INDEX, JSFunction, array_function) \
Ben Murdochc5610432016-08-08 18:44:38 +0100153 V(ASYNC_FUNCTION_FUNCTION_INDEX, JSFunction, async_function_constructor) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000154 V(BOOL16X8_FUNCTION_INDEX, JSFunction, bool16x8_function) \
155 V(BOOL32X4_FUNCTION_INDEX, JSFunction, bool32x4_function) \
156 V(BOOL8X16_FUNCTION_INDEX, JSFunction, bool8x16_function) \
157 V(BOOLEAN_FUNCTION_INDEX, JSFunction, boolean_function) \
158 V(BOUND_FUNCTION_WITH_CONSTRUCTOR_MAP_INDEX, Map, \
159 bound_function_with_constructor_map) \
160 V(BOUND_FUNCTION_WITHOUT_CONSTRUCTOR_MAP_INDEX, Map, \
161 bound_function_without_constructor_map) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000162 V(CALL_AS_CONSTRUCTOR_DELEGATE_INDEX, JSFunction, \
163 call_as_constructor_delegate) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000164 V(CALL_AS_FUNCTION_DELEGATE_INDEX, JSFunction, call_as_function_delegate) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000165 V(CONTEXT_EXTENSION_FUNCTION_INDEX, JSFunction, context_extension_function) \
Ben Murdoch097c5b22016-05-18 11:27:45 +0100166 V(DATA_PROPERTY_DESCRIPTOR_MAP_INDEX, Map, data_property_descriptor_map) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000167 V(DATA_VIEW_FUN_INDEX, JSFunction, data_view_fun) \
168 V(DATE_FUNCTION_INDEX, JSFunction, date_function) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000169 V(ERROR_MESSAGE_FOR_CODE_GEN_FROM_STRINGS_INDEX, Object, \
170 error_message_for_code_gen_from_strings) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000171 V(ERRORS_THROWN_INDEX, Smi, errors_thrown) \
172 V(EXTRAS_EXPORTS_OBJECT_INDEX, JSObject, extras_binding_object) \
173 V(EXTRAS_UTILS_OBJECT_INDEX, JSObject, extras_utils_object) \
174 V(FAST_ALIASED_ARGUMENTS_MAP_INDEX, Map, fast_aliased_arguments_map) \
175 V(FLOAT32_ARRAY_FUN_INDEX, JSFunction, float32_array_fun) \
176 V(FLOAT32X4_FUNCTION_INDEX, JSFunction, float32x4_function) \
177 V(FLOAT64_ARRAY_FUN_INDEX, JSFunction, float64_array_fun) \
Ben Murdochda12d292016-06-02 14:46:10 +0100178 V(TEMPLATE_INSTANTIATIONS_CACHE_INDEX, UnseededNumberDictionary, \
Ben Murdoch097c5b22016-05-18 11:27:45 +0100179 template_instantiations_cache) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000180 V(FUNCTION_FUNCTION_INDEX, JSFunction, function_function) \
181 V(GENERATOR_FUNCTION_FUNCTION_INDEX, JSFunction, \
182 generator_function_function) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000183 V(GENERATOR_OBJECT_PROTOTYPE_MAP_INDEX, Map, generator_object_prototype_map) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000184 V(INITIAL_ARRAY_PROTOTYPE_INDEX, JSObject, initial_array_prototype) \
Ben Murdochc5610432016-08-08 18:44:38 +0100185 V(INITIAL_GENERATOR_PROTOTYPE_INDEX, JSObject, initial_generator_prototype) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000186 V(INITIAL_OBJECT_PROTOTYPE_INDEX, JSObject, initial_object_prototype) \
187 V(INT16_ARRAY_FUN_INDEX, JSFunction, int16_array_fun) \
188 V(INT16X8_FUNCTION_INDEX, JSFunction, int16x8_function) \
189 V(INT32_ARRAY_FUN_INDEX, JSFunction, int32_array_fun) \
190 V(INT32X4_FUNCTION_INDEX, JSFunction, int32x4_function) \
191 V(INT8_ARRAY_FUN_INDEX, JSFunction, int8_array_fun) \
192 V(INT8X16_FUNCTION_INDEX, JSFunction, int8x16_function) \
193 V(INTERNAL_ARRAY_FUNCTION_INDEX, JSFunction, internal_array_function) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000194 V(ITERATOR_RESULT_MAP_INDEX, Map, iterator_result_map) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000195 V(JS_ARRAY_FAST_SMI_ELEMENTS_MAP_INDEX, Map, \
196 js_array_fast_smi_elements_map_index) \
197 V(JS_ARRAY_FAST_HOLEY_SMI_ELEMENTS_MAP_INDEX, Map, \
198 js_array_fast_holey_smi_elements_map_index) \
199 V(JS_ARRAY_FAST_ELEMENTS_MAP_INDEX, Map, js_array_fast_elements_map_index) \
200 V(JS_ARRAY_FAST_HOLEY_ELEMENTS_MAP_INDEX, Map, \
201 js_array_fast_holey_elements_map_index) \
202 V(JS_ARRAY_FAST_DOUBLE_ELEMENTS_MAP_INDEX, Map, \
203 js_array_fast_double_elements_map_index) \
204 V(JS_ARRAY_FAST_HOLEY_DOUBLE_ELEMENTS_MAP_INDEX, Map, \
205 js_array_fast_holey_double_elements_map_index) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000206 V(JS_MAP_FUN_INDEX, JSFunction, js_map_fun) \
207 V(JS_MAP_MAP_INDEX, Map, js_map_map) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000208 V(JS_SET_FUN_INDEX, JSFunction, js_set_fun) \
209 V(JS_SET_MAP_INDEX, Map, js_set_map) \
210 V(JS_WEAK_MAP_FUN_INDEX, JSFunction, js_weak_map_fun) \
211 V(JS_WEAK_SET_FUN_INDEX, JSFunction, js_weak_set_fun) \
212 V(MAP_CACHE_INDEX, Object, map_cache) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000213 V(MAP_ITERATOR_MAP_INDEX, Map, map_iterator_map) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000214 V(STRING_ITERATOR_MAP_INDEX, Map, string_iterator_map) \
215 V(MESSAGE_LISTENERS_INDEX, JSObject, message_listeners) \
216 V(NATIVES_UTILS_OBJECT_INDEX, Object, natives_utils_object) \
217 V(NORMALIZED_MAP_CACHE_INDEX, Object, normalized_map_cache) \
218 V(NUMBER_FUNCTION_INDEX, JSFunction, number_function) \
219 V(OBJECT_FUNCTION_INDEX, JSFunction, object_function) \
Ben Murdoch61f157c2016-09-16 13:49:30 +0100220 V(OBJECT_WITH_NULL_PROTOTYPE_MAP, Map, object_with_null_prototype_map) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000221 V(OBJECT_FUNCTION_PROTOTYPE_MAP_INDEX, Map, object_function_prototype_map) \
222 V(OPAQUE_REFERENCE_FUNCTION_INDEX, JSFunction, opaque_reference_function) \
223 V(PROXY_CALLABLE_MAP_INDEX, Map, proxy_callable_map) \
224 V(PROXY_CONSTRUCTOR_MAP_INDEX, Map, proxy_constructor_map) \
225 V(PROXY_FUNCTION_INDEX, JSFunction, proxy_function) \
226 V(PROXY_FUNCTION_MAP_INDEX, Map, proxy_function_map) \
227 V(PROXY_MAP_INDEX, Map, proxy_map) \
228 V(REGEXP_FUNCTION_INDEX, JSFunction, regexp_function) \
229 V(REGEXP_RESULT_MAP_INDEX, Map, regexp_result_map) \
230 V(SCRIPT_CONTEXT_TABLE_INDEX, ScriptContextTable, script_context_table) \
231 V(SCRIPT_FUNCTION_INDEX, JSFunction, script_function) \
232 V(SECURITY_TOKEN_INDEX, Object, security_token) \
233 V(SELF_WEAK_CELL_INDEX, WeakCell, self_weak_cell) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000234 V(SET_ITERATOR_MAP_INDEX, Map, set_iterator_map) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000235 V(SHARED_ARRAY_BUFFER_FUN_INDEX, JSFunction, shared_array_buffer_fun) \
236 V(SLOPPY_ARGUMENTS_MAP_INDEX, Map, sloppy_arguments_map) \
237 V(SLOPPY_FUNCTION_MAP_INDEX, Map, sloppy_function_map) \
238 V(SLOPPY_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map, \
239 sloppy_function_without_prototype_map) \
240 V(SLOPPY_FUNCTION_WITH_READONLY_PROTOTYPE_MAP_INDEX, Map, \
241 sloppy_function_with_readonly_prototype_map) \
242 V(WASM_FUNCTION_MAP_INDEX, Map, wasm_function_map) \
Ben Murdoch61f157c2016-09-16 13:49:30 +0100243 V(WASM_MODULE_CONSTRUCTOR_INDEX, JSFunction, wasm_module_constructor) \
244 V(WASM_INSTANCE_CONSTRUCTOR_INDEX, JSFunction, wasm_instance_constructor) \
245 V(WASM_MODULE_SYM_INDEX, Symbol, wasm_module_sym) \
246 V(WASM_INSTANCE_SYM_INDEX, Symbol, wasm_instance_sym) \
Ben Murdochc5610432016-08-08 18:44:38 +0100247 V(SLOPPY_ASYNC_FUNCTION_MAP_INDEX, Map, sloppy_async_function_map) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000248 V(SLOPPY_GENERATOR_FUNCTION_MAP_INDEX, Map, sloppy_generator_function_map) \
249 V(SLOW_ALIASED_ARGUMENTS_MAP_INDEX, Map, slow_aliased_arguments_map) \
Ben Murdochc5610432016-08-08 18:44:38 +0100250 V(STRICT_ASYNC_FUNCTION_MAP_INDEX, Map, strict_async_function_map) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000251 V(STRICT_ARGUMENTS_MAP_INDEX, Map, strict_arguments_map) \
252 V(STRICT_FUNCTION_MAP_INDEX, Map, strict_function_map) \
253 V(STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map, \
254 strict_function_without_prototype_map) \
255 V(STRICT_GENERATOR_FUNCTION_MAP_INDEX, Map, strict_generator_function_map) \
256 V(STRING_FUNCTION_INDEX, JSFunction, string_function) \
257 V(STRING_FUNCTION_PROTOTYPE_MAP_INDEX, Map, string_function_prototype_map) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000258 V(SYMBOL_FUNCTION_INDEX, JSFunction, symbol_function) \
Ben Murdoch61f157c2016-09-16 13:49:30 +0100259 V(TYPED_ARRAY_FUN_INDEX, JSFunction, typed_array_function) \
260 V(TYPED_ARRAY_PROTOTYPE_INDEX, JSObject, typed_array_prototype) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000261 V(UINT16_ARRAY_FUN_INDEX, JSFunction, uint16_array_fun) \
262 V(UINT16X8_FUNCTION_INDEX, JSFunction, uint16x8_function) \
263 V(UINT32_ARRAY_FUN_INDEX, JSFunction, uint32_array_fun) \
264 V(UINT32X4_FUNCTION_INDEX, JSFunction, uint32x4_function) \
265 V(UINT8_ARRAY_FUN_INDEX, JSFunction, uint8_array_fun) \
266 V(UINT8_CLAMPED_ARRAY_FUN_INDEX, JSFunction, uint8_clamped_array_fun) \
267 V(UINT8X16_FUNCTION_INDEX, JSFunction, uint8x16_function) \
268 NATIVE_CONTEXT_INTRINSIC_FUNCTIONS(V) \
269 NATIVE_CONTEXT_IMPORTED_FIELDS(V)
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400270
271// A table of all script contexts. Every loaded top-level script with top-level
272// lexical declarations contributes its ScriptContext into this table.
273//
274// The table is a fixed array, its first slot is the current used count and
275// the subsequent slots 1..used contain ScriptContexts.
276class ScriptContextTable : public FixedArray {
277 public:
278 // Conversions.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000279 static inline ScriptContextTable* cast(Object* context);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400280
281 struct LookupResult {
282 int context_index;
283 int slot_index;
284 VariableMode mode;
285 InitializationFlag init_flag;
286 MaybeAssignedFlag maybe_assigned_flag;
287 };
288
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000289 inline int used() const;
290 inline void set_used(int used);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400291
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000292 static inline Handle<Context> GetContext(Handle<ScriptContextTable> table,
293 int i);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400294
295 // Lookup a variable `name` in a ScriptContextTable.
296 // If it returns true, the variable is found and `result` contains
297 // valid information about its location.
298 // If it returns false, `result` is untouched.
299 MUST_USE_RESULT
300 static bool Lookup(Handle<ScriptContextTable> table, Handle<String> name,
301 LookupResult* result);
302
303 MUST_USE_RESULT
304 static Handle<ScriptContextTable> Extend(Handle<ScriptContextTable> table,
305 Handle<Context> script_context);
306
307 static int GetContextOffset(int context_index) {
308 return kFirstContextOffset + context_index * kPointerSize;
309 }
310
311 private:
312 static const int kUsedSlot = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000313 static const int kFirstContextSlot = kUsedSlot + 1;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400314 static const int kFirstContextOffset =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000315 FixedArray::kHeaderSize + kFirstContextSlot * kPointerSize;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400316
317 DISALLOW_IMPLICIT_CONSTRUCTORS(ScriptContextTable);
318};
Steve Blocka7e24c12009-10-30 11:49:00 +0000319
320// JSFunctions are pairs (context, function code), sometimes also called
321// closures. A Context object is used to represent function contexts and
322// dynamically pushed 'with' contexts (or 'scopes' in ECMA-262 speak).
323//
324// At runtime, the contexts build a stack in parallel to the execution
325// stack, with the top-most context being the current context. All contexts
326// have the following slots:
327//
328// [ closure ] This is the current function. It is the same for all
329// contexts inside a function. It provides access to the
330// incoming context (i.e., the outer context, which may
331// or may not become the current function's context), and
332// it provides access to the functions code and thus it's
333// scope information, which in turn contains the names of
334// statically allocated context slots. The names are needed
335// for dynamic lookups in the presence of 'with' or 'eval'.
336//
Steve Blocka7e24c12009-10-30 11:49:00 +0000337// [ previous ] A pointer to the previous context. It is NULL for
338// function contexts, and non-NULL for 'with' contexts.
339// Used to implement the 'with' statement.
340//
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000341// [ extension ] A pointer to an extension JSObject, or "the hole". Used to
Steve Blocka7e24c12009-10-30 11:49:00 +0000342// implement 'with' statements and dynamic declarations
343// (through 'eval'). The object in a 'with' statement is
344// stored in the extension slot of a 'with' context.
345// Dynamically declared variables/functions are also added
346// to lazily allocated extension object. Context::Lookup
347// searches the extension object for properties.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000348// For script and block contexts, contains the respective
349// ScopeInfo. For block contexts representing sloppy declaration
350// block scopes, it may also be a struct being a
351// SloppyBlockWithEvalContextExtension, pairing the ScopeInfo
352// with an extension object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000353// For module contexts, points back to the respective JSModule.
Steve Blocka7e24c12009-10-30 11:49:00 +0000354//
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000355// [ global_object ] A pointer to the global object. Provided for quick
Steve Blocka7e24c12009-10-30 11:49:00 +0000356// access to the global object from inside the code (since
357// we always have a context pointer).
358//
359// In addition, function contexts may have statically allocated context slots
360// to store local variables/functions that are accessed from inner functions
361// (via static context addresses) or through 'eval' (dynamic context lookups).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000362// The native context contains additional slots for fast access to native
363// properties.
364//
365// Finally, with Harmony scoping, the JSFunction representing a top level
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400366// script will have the ScriptContext rather than a FunctionContext.
367// Script contexts from all top-level scripts are gathered in
368// ScriptContextTable.
Steve Blocka7e24c12009-10-30 11:49:00 +0000369
370class Context: public FixedArray {
371 public:
372 // Conversions.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000373 static inline Context* cast(Object* context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000374
375 // The default context slot layout; indices are FixedArray slot indices.
376 enum {
377 // These slots are in all contexts.
378 CLOSURE_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000379 PREVIOUS_INDEX,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000380 // The extension slot is used for either the global object (in native
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000381 // contexts), eval extension object (function contexts), subject of with
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100382 // (with contexts), or the variable name (catch contexts), the serialized
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000383 // scope info (block contexts), or the module instance (module contexts).
Steve Blocka7e24c12009-10-30 11:49:00 +0000384 EXTENSION_INDEX,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000385 NATIVE_CONTEXT_INDEX,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000386
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000387 // These slots are only in native contexts.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000388#define NATIVE_CONTEXT_SLOT(index, type, name) index,
389 NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_SLOT)
390#undef NATIVE_CONTEXT_SLOT
Ben Murdochf87a2032010-10-22 12:50:53 +0100391
392 // Properties from here are treated as weak references by the full GC.
393 // Scavenge treats them as strong references.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100394 OPTIMIZED_FUNCTIONS_LIST, // Weak.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000395 OPTIMIZED_CODE_LIST, // Weak.
396 DEOPTIMIZED_CODE_LIST, // Weak.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000397 NEXT_CONTEXT_LINK, // Weak.
Ben Murdochf87a2032010-10-22 12:50:53 +0100398
399 // Total number of slots.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000400 NATIVE_CONTEXT_SLOTS,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000401 FIRST_WEAK_SLOT = OPTIMIZED_FUNCTIONS_LIST,
402 FIRST_JS_ARRAY_MAP_SLOT = JS_ARRAY_FAST_SMI_ELEMENTS_MAP_INDEX,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000403
404 MIN_CONTEXT_SLOTS = GLOBAL_PROXY_INDEX,
405 // This slot holds the thrown value in catch contexts.
406 THROWN_OBJECT_INDEX = MIN_CONTEXT_SLOTS,
Ben Murdochda12d292016-06-02 14:46:10 +0100407
408 // These slots hold values in debug evaluate contexts.
409 WRAPPED_CONTEXT_INDEX = MIN_CONTEXT_SLOTS,
410 WHITE_LIST_INDEX = MIN_CONTEXT_SLOTS + 1
Steve Blocka7e24c12009-10-30 11:49:00 +0000411 };
412
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000413 void IncrementErrorsThrown();
414 int GetErrorsThrown();
415
Steve Blocka7e24c12009-10-30 11:49:00 +0000416 // Direct slot access.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000417 inline JSFunction* closure();
418 inline void set_closure(JSFunction* closure);
Steve Blocka7e24c12009-10-30 11:49:00 +0000419
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000420 inline Context* previous();
421 inline void set_previous(Context* context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000422
Ben Murdochc5610432016-08-08 18:44:38 +0100423 inline Object* next_context_link();
424
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000425 inline bool has_extension();
426 inline HeapObject* extension();
427 inline void set_extension(HeapObject* object);
428 JSObject* extension_object();
429 JSReceiver* extension_receiver();
430 ScopeInfo* scope_info();
431 String* catch_name();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000432
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000433 inline JSModule* module();
434 inline void set_module(JSModule* module);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000435
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000436 // Get the context where var declarations will be hoisted to, which
437 // may be the context itself.
438 Context* declaration_context();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000439 bool is_declaration_context();
Steve Blocka7e24c12009-10-30 11:49:00 +0000440
Ben Murdoch097c5b22016-05-18 11:27:45 +0100441 // Get the next closure's context on the context chain.
442 Context* closure_context();
443
Steve Blocka7e24c12009-10-30 11:49:00 +0000444 // Returns a JSGlobalProxy object or null.
445 JSObject* global_proxy();
446 void set_global_proxy(JSObject* global);
447
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000448 // Get the JSGlobalObject object.
449 JSGlobalObject* global_object();
Steve Blocka7e24c12009-10-30 11:49:00 +0000450
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400451 // Get the script context by traversing the context chain.
452 Context* script_context();
Steve Blocka7e24c12009-10-30 11:49:00 +0000453
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000454 // Compute the native context.
455 inline Context* native_context();
456 inline void set_native_context(Context* context);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000457
458 // Predicates for context types. IsNativeContext is also defined on Object
459 // because we frequently have to know if arbitrary objects are natives
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000460 // contexts.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000461 inline bool IsNativeContext();
462 inline bool IsFunctionContext();
463 inline bool IsCatchContext();
464 inline bool IsWithContext();
Ben Murdochda12d292016-06-02 14:46:10 +0100465 inline bool IsDebugEvaluateContext();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000466 inline bool IsBlockContext();
467 inline bool IsModuleContext();
468 inline bool IsScriptContext();
Steve Blocka7e24c12009-10-30 11:49:00 +0000469
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000470 inline bool HasSameSecurityTokenAs(Context* that);
471
472 // Initializes global variable bindings in given script context.
473 void InitializeGlobalSlots();
Steve Blocka7e24c12009-10-30 11:49:00 +0000474
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000475 // A native context holds a list of all functions with optimized code.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100476 void AddOptimizedFunction(JSFunction* function);
477 void RemoveOptimizedFunction(JSFunction* function);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000478 void SetOptimizedFunctionsListHead(Object* head);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100479 Object* OptimizedFunctionsListHead();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100480
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000481 // The native context also stores a list of all optimized code and a
482 // list of all deoptimized code, which are needed by the deoptimizer.
483 void AddOptimizedCode(Code* code);
484 void SetOptimizedCodeListHead(Object* head);
485 Object* OptimizedCodeListHead();
486 void SetDeoptimizedCodeListHead(Object* head);
487 Object* DeoptimizedCodeListHead();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100488
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000489 Handle<Object> ErrorMessageForCodeGenerationFromStrings();
490
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000491 static int ImportedFieldIndexForName(Handle<String> name);
492 static int IntrinsicIndexForName(Handle<String> name);
493
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000494#define NATIVE_CONTEXT_FIELD_ACCESSORS(index, type, name) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000495 inline void set_##name(type* value); \
496 inline bool is_##name(type* value); \
497 inline type* name();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000498 NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_FIELD_ACCESSORS)
499#undef NATIVE_CONTEXT_FIELD_ACCESSORS
Steve Blocka7e24c12009-10-30 11:49:00 +0000500
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000501 // Lookup the slot called name, starting with the current context.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100502 // There are three possibilities:
Steve Blocka7e24c12009-10-30 11:49:00 +0000503 //
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100504 // 1) result->IsContext():
505 // The binding was found in a context. *index is always the
506 // non-negative slot index. *attributes is NONE for var and let
507 // declarations, READ_ONLY for const declarations (never ABSENT).
Steve Blocka7e24c12009-10-30 11:49:00 +0000508 //
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100509 // 2) result->IsJSObject():
510 // The binding was found as a named property in a context extension
511 // object (i.e., was introduced via eval), as a property on the subject
512 // of with, or as a property of the global object. *index is -1 and
513 // *attributes is not ABSENT.
Steve Blocka7e24c12009-10-30 11:49:00 +0000514 //
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100515 // 3) result.is_null():
516 // There was no binding found, *index is always -1 and *attributes is
517 // always ABSENT.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000518 Handle<Object> Lookup(Handle<String> name,
519 ContextLookupFlags flags,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100520 int* index,
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000521 PropertyAttributes* attributes,
522 BindingFlags* binding_flags);
Steve Blocka7e24c12009-10-30 11:49:00 +0000523
Steve Blocka7e24c12009-10-30 11:49:00 +0000524 // Code generation support.
525 static int SlotOffset(int index) {
526 return kHeaderSize + index * kPointerSize - kHeapObjectTag;
527 }
528
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000529 static int FunctionMapIndex(LanguageMode language_mode, FunctionKind kind) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000530 if (IsGeneratorFunction(kind)) {
Ben Murdochda12d292016-06-02 14:46:10 +0100531 return is_strict(language_mode) ? STRICT_GENERATOR_FUNCTION_MAP_INDEX
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000532 : SLOPPY_GENERATOR_FUNCTION_MAP_INDEX;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000533 }
534
Ben Murdochc5610432016-08-08 18:44:38 +0100535 if (IsAsyncFunction(kind)) {
536 return is_strict(language_mode) ? STRICT_ASYNC_FUNCTION_MAP_INDEX
537 : SLOPPY_ASYNC_FUNCTION_MAP_INDEX;
538 }
539
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000540 if (IsClassConstructor(kind)) {
541 // Use strict function map (no own "caller" / "arguments")
Ben Murdochda12d292016-06-02 14:46:10 +0100542 return STRICT_FUNCTION_MAP_INDEX;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000543 }
544
545 if (IsArrowFunction(kind) || IsConciseMethod(kind) ||
546 IsAccessorFunction(kind)) {
Ben Murdochda12d292016-06-02 14:46:10 +0100547 return STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000548 }
549
Ben Murdochda12d292016-06-02 14:46:10 +0100550 return is_strict(language_mode) ? STRICT_FUNCTION_MAP_INDEX
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000551 : SLOPPY_FUNCTION_MAP_INDEX;
552 }
553
Ben Murdochda12d292016-06-02 14:46:10 +0100554 static int ArrayMapIndex(ElementsKind elements_kind) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000555 DCHECK(IsFastElementsKind(elements_kind));
Ben Murdochda12d292016-06-02 14:46:10 +0100556 return elements_kind + FIRST_JS_ARRAY_MAP_SLOT;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000557 }
558
559 static const int kSize = kHeaderSize + NATIVE_CONTEXT_SLOTS * kPointerSize;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000560 static const int kNotFound = -1;
Ben Murdochf87a2032010-10-22 12:50:53 +0100561
562 // GC support.
563 typedef FixedBodyDescriptor<
564 kHeaderSize, kSize, kSize> ScavengeBodyDescriptor;
565
566 typedef FixedBodyDescriptor<
567 kHeaderSize,
568 kHeaderSize + FIRST_WEAK_SLOT * kPointerSize,
569 kSize> MarkCompactBodyDescriptor;
570
Steve Blocka7e24c12009-10-30 11:49:00 +0000571 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000572#ifdef DEBUG
573 // Bootstrapping-aware type checks.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000574 static bool IsBootstrappingOrNativeContext(Isolate* isolate, Object* object);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000575 static bool IsBootstrappingOrValidParentContext(Object* object, Context* kid);
Steve Blocka7e24c12009-10-30 11:49:00 +0000576#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000577
578 STATIC_ASSERT(kHeaderSize == Internals::kContextHeaderSize);
579 STATIC_ASSERT(EMBEDDER_DATA_INDEX == Internals::kContextEmbedderDataIndex);
Steve Blocka7e24c12009-10-30 11:49:00 +0000580};
581
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000582} // namespace internal
583} // namespace v8
Steve Blocka7e24c12009-10-30 11:49:00 +0000584
585#endif // V8_CONTEXTS_H_