blob: 38ebf64ae17ec2ac33ddb3802b155f62d801a7c6 [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
33// the BindingFlag MUTABLE_IS_INITIALIZED.
34//
35// There are two possibilities for immutable bindings
36// * 'const' declared variables. They are initialized when evaluating the
37// corresponding declaration statement. They need to be checked for being
38// initialized and thus get the flag IMMUTABLE_CHECK_INITIALIZED.
39// * The function name of a named function literal. The binding is immediately
40// initialized when entering the function and thus does not need to be
41// checked. it gets the BindingFlag IMMUTABLE_IS_INITIALIZED.
42// Accessing an uninitialized binding produces the undefined value.
Ben Murdoch69a99ed2011-11-30 16:03:39 +000043//
44// The harmony proposal for block scoped bindings also introduces the
Ben Murdoch3ef787d2012-04-12 10:51:47 +010045// uninitialized state for mutable bindings.
46// * A 'let' declared variable. They are initialized when evaluating the
47// corresponding declaration statement. They need to be checked for being
48// initialized and thus get the flag MUTABLE_CHECK_INITIALIZED.
49// * A 'var' declared variable. It is initialized immediately upon creation
50// and thus doesn't need to be checked. It gets the flag
51// MUTABLE_IS_INITIALIZED.
52// * Catch bound variables, function parameters and variables introduced by
53// function declarations are initialized immediately and do not need to be
54// checked. Thus they get the flag MUTABLE_IS_INITIALIZED.
55// Immutable bindings in harmony mode get the _HARMONY flag variants. Accessing
56// an uninitialized binding produces a reference error.
57//
58// In V8 uninitialized bindings are set to the hole value upon creation and set
59// to a different value upon initialization.
Ben Murdoch69a99ed2011-11-30 16:03:39 +000060enum BindingFlags {
61 MUTABLE_IS_INITIALIZED,
62 MUTABLE_CHECK_INITIALIZED,
63 IMMUTABLE_IS_INITIALIZED,
64 IMMUTABLE_CHECK_INITIALIZED,
Ben Murdoch3ef787d2012-04-12 10:51:47 +010065 IMMUTABLE_IS_INITIALIZED_HARMONY,
66 IMMUTABLE_CHECK_INITIALIZED_HARMONY,
Ben Murdoch69a99ed2011-11-30 16:03:39 +000067 MISSING_BINDING
68};
69
70
Steve Blocka7e24c12009-10-30 11:49:00 +000071// Heap-allocated activation contexts.
72//
73// Contexts are implemented as FixedArray objects; the Context
74// class is a convenience interface casted on a FixedArray object.
75//
76// Note: Context must have no virtual functions and Context objects
77// must always be allocated via Heap::AllocateContext() or
78// Factory::NewContext.
79
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000080#define NATIVE_CONTEXT_INTRINSIC_FUNCTIONS(V) \
81 V(IS_ARRAYLIKE, JSFunction, is_arraylike) \
82 V(CONCAT_ITERABLE_TO_ARRAY_INDEX, JSFunction, concat_iterable_to_array) \
83 V(GET_TEMPLATE_CALL_SITE_INDEX, JSFunction, get_template_call_site) \
84 V(MAKE_RANGE_ERROR_INDEX, JSFunction, make_range_error) \
85 V(MAKE_TYPE_ERROR_INDEX, JSFunction, make_type_error) \
86 V(OBJECT_FREEZE, JSFunction, object_freeze) \
87 V(OBJECT_IS_EXTENSIBLE, JSFunction, object_is_extensible) \
88 V(OBJECT_IS_FROZEN, JSFunction, object_is_frozen) \
89 V(OBJECT_IS_SEALED, JSFunction, object_is_sealed) \
90 V(OBJECT_KEYS, JSFunction, object_keys) \
91 V(REFLECT_APPLY_INDEX, JSFunction, reflect_apply) \
92 V(REFLECT_CONSTRUCT_INDEX, JSFunction, reflect_construct) \
93 V(REFLECT_DEFINE_PROPERTY_INDEX, JSFunction, reflect_define_property) \
94 V(REFLECT_DELETE_PROPERTY_INDEX, JSFunction, reflect_delete_property) \
95 V(SPREAD_ARGUMENTS_INDEX, JSFunction, spread_arguments) \
Ben Murdoch097c5b22016-05-18 11:27:45 +010096 V(SPREAD_ITERABLE_INDEX, JSFunction, spread_iterable) \
97 V(ORDINARY_HAS_INSTANCE_INDEX, JSFunction, ordinary_has_instance)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000098
99#define NATIVE_CONTEXT_IMPORTED_FIELDS(V) \
100 V(ARRAY_CONCAT_INDEX, JSFunction, array_concat) \
101 V(ARRAY_POP_INDEX, JSFunction, array_pop) \
102 V(ARRAY_PUSH_INDEX, JSFunction, array_push) \
103 V(ARRAY_SHIFT_INDEX, JSFunction, array_shift) \
104 V(ARRAY_SPLICE_INDEX, JSFunction, array_splice) \
105 V(ARRAY_SLICE_INDEX, JSFunction, array_slice) \
106 V(ARRAY_UNSHIFT_INDEX, JSFunction, array_unshift) \
107 V(ARRAY_VALUES_ITERATOR_INDEX, JSFunction, array_values_iterator) \
108 V(DERIVED_GET_TRAP_INDEX, JSFunction, derived_get_trap) \
109 V(ERROR_FUNCTION_INDEX, JSFunction, error_function) \
110 V(EVAL_ERROR_FUNCTION_INDEX, JSFunction, eval_error_function) \
111 V(GET_STACK_TRACE_LINE_INDEX, JSFunction, get_stack_trace_line_fun) \
112 V(GLOBAL_EVAL_FUN_INDEX, JSFunction, global_eval_fun) \
113 V(JSON_SERIALIZE_ADAPTER_INDEX, JSFunction, json_serialize_adapter) \
114 V(MAKE_ERROR_FUNCTION_INDEX, JSFunction, make_error_function) \
115 V(MAP_DELETE_METHOD_INDEX, JSFunction, map_delete) \
116 V(MAP_GET_METHOD_INDEX, JSFunction, map_get) \
117 V(MAP_HAS_METHOD_INDEX, JSFunction, map_has) \
118 V(MAP_SET_METHOD_INDEX, JSFunction, map_set) \
119 V(MESSAGE_GET_COLUMN_NUMBER_INDEX, JSFunction, message_get_column_number) \
120 V(MESSAGE_GET_LINE_NUMBER_INDEX, JSFunction, message_get_line_number) \
121 V(MESSAGE_GET_SOURCE_LINE_INDEX, JSFunction, message_get_source_line) \
122 V(NATIVE_OBJECT_GET_NOTIFIER_INDEX, JSFunction, native_object_get_notifier) \
123 V(NATIVE_OBJECT_NOTIFIER_PERFORM_CHANGE, JSFunction, \
124 native_object_notifier_perform_change) \
125 V(NATIVE_OBJECT_OBSERVE_INDEX, JSFunction, native_object_observe) \
126 V(NO_SIDE_EFFECTS_TO_STRING_FUN_INDEX, JSFunction, \
127 no_side_effects_to_string_fun) \
128 V(OBJECT_VALUE_OF, JSFunction, object_value_of) \
129 V(OBJECT_TO_STRING, JSFunction, object_to_string) \
130 V(OBSERVERS_BEGIN_SPLICE_INDEX, JSFunction, observers_begin_perform_splice) \
131 V(OBSERVERS_END_SPLICE_INDEX, JSFunction, observers_end_perform_splice) \
132 V(OBSERVERS_ENQUEUE_SPLICE_INDEX, JSFunction, observers_enqueue_splice) \
133 V(OBSERVERS_NOTIFY_CHANGE_INDEX, JSFunction, observers_notify_change) \
134 V(PROMISE_CATCH_INDEX, JSFunction, promise_catch) \
135 V(PROMISE_CHAIN_INDEX, JSFunction, promise_chain) \
136 V(PROMISE_CREATE_INDEX, JSFunction, promise_create) \
137 V(PROMISE_FUNCTION_INDEX, JSFunction, promise_function) \
138 V(PROMISE_HAS_USER_DEFINED_REJECT_HANDLER_INDEX, JSFunction, \
139 promise_has_user_defined_reject_handler) \
140 V(PROMISE_REJECT_INDEX, JSFunction, promise_reject) \
141 V(PROMISE_RESOLVE_INDEX, JSFunction, promise_resolve) \
142 V(PROMISE_THEN_INDEX, JSFunction, promise_then) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000143 V(RANGE_ERROR_FUNCTION_INDEX, JSFunction, range_error_function) \
144 V(REFERENCE_ERROR_FUNCTION_INDEX, JSFunction, reference_error_function) \
145 V(SET_ADD_METHOD_INDEX, JSFunction, set_add) \
146 V(SET_DELETE_METHOD_INDEX, JSFunction, set_delete) \
147 V(SET_HAS_METHOD_INDEX, JSFunction, set_has) \
148 V(STACK_OVERFLOW_BOILERPLATE_INDEX, JSObject, stack_overflow_boilerplate) \
149 V(SYNTAX_ERROR_FUNCTION_INDEX, JSFunction, syntax_error_function) \
150 V(TYPE_ERROR_FUNCTION_INDEX, JSFunction, type_error_function) \
Ben Murdoch097c5b22016-05-18 11:27:45 +0100151 V(URI_ERROR_FUNCTION_INDEX, JSFunction, uri_error_function)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000152
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000153#define NATIVE_CONTEXT_FIELDS(V) \
154 V(GLOBAL_PROXY_INDEX, JSObject, global_proxy_object) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000155 V(EMBEDDER_DATA_INDEX, FixedArray, embedder_data) \
156 /* Below is alpha-sorted */ \
Ben Murdoch097c5b22016-05-18 11:27:45 +0100157 V(ACCESSOR_PROPERTY_DESCRIPTOR_MAP_INDEX, Map, \
158 accessor_property_descriptor_map) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000159 V(ALLOW_CODE_GEN_FROM_STRINGS_INDEX, Object, allow_code_gen_from_strings) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000160 V(ARRAY_BUFFER_FUN_INDEX, JSFunction, array_buffer_fun) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000161 V(ARRAY_BUFFER_MAP_INDEX, Map, array_buffer_map) \
162 V(ARRAY_FUNCTION_INDEX, JSFunction, array_function) \
163 V(BOOL16X8_FUNCTION_INDEX, JSFunction, bool16x8_function) \
164 V(BOOL32X4_FUNCTION_INDEX, JSFunction, bool32x4_function) \
165 V(BOOL8X16_FUNCTION_INDEX, JSFunction, bool8x16_function) \
166 V(BOOLEAN_FUNCTION_INDEX, JSFunction, boolean_function) \
167 V(BOUND_FUNCTION_WITH_CONSTRUCTOR_MAP_INDEX, Map, \
168 bound_function_with_constructor_map) \
169 V(BOUND_FUNCTION_WITHOUT_CONSTRUCTOR_MAP_INDEX, Map, \
170 bound_function_without_constructor_map) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000171 V(CALL_AS_CONSTRUCTOR_DELEGATE_INDEX, JSFunction, \
172 call_as_constructor_delegate) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000173 V(CALL_AS_FUNCTION_DELEGATE_INDEX, JSFunction, call_as_function_delegate) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000174 V(CONTEXT_EXTENSION_FUNCTION_INDEX, JSFunction, context_extension_function) \
Ben Murdoch097c5b22016-05-18 11:27:45 +0100175 V(DATA_PROPERTY_DESCRIPTOR_MAP_INDEX, Map, data_property_descriptor_map) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000176 V(DATA_VIEW_FUN_INDEX, JSFunction, data_view_fun) \
177 V(DATE_FUNCTION_INDEX, JSFunction, date_function) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000178 V(ERROR_MESSAGE_FOR_CODE_GEN_FROM_STRINGS_INDEX, Object, \
179 error_message_for_code_gen_from_strings) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000180 V(ERRORS_THROWN_INDEX, Smi, errors_thrown) \
181 V(EXTRAS_EXPORTS_OBJECT_INDEX, JSObject, extras_binding_object) \
182 V(EXTRAS_UTILS_OBJECT_INDEX, JSObject, extras_utils_object) \
183 V(FAST_ALIASED_ARGUMENTS_MAP_INDEX, Map, fast_aliased_arguments_map) \
184 V(FLOAT32_ARRAY_FUN_INDEX, JSFunction, float32_array_fun) \
185 V(FLOAT32X4_FUNCTION_INDEX, JSFunction, float32x4_function) \
186 V(FLOAT64_ARRAY_FUN_INDEX, JSFunction, float64_array_fun) \
Ben Murdoch097c5b22016-05-18 11:27:45 +0100187 V(TEMPLATE_INSTANTIATIONS_CACHE_INDEX, ObjectHashTable, \
188 template_instantiations_cache) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000189 V(FUNCTION_FUNCTION_INDEX, JSFunction, function_function) \
190 V(GENERATOR_FUNCTION_FUNCTION_INDEX, JSFunction, \
191 generator_function_function) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000192 V(GENERATOR_OBJECT_PROTOTYPE_MAP_INDEX, Map, generator_object_prototype_map) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000193 V(INITIAL_ARRAY_PROTOTYPE_INDEX, JSObject, initial_array_prototype) \
194 V(INITIAL_OBJECT_PROTOTYPE_INDEX, JSObject, initial_object_prototype) \
195 V(INT16_ARRAY_FUN_INDEX, JSFunction, int16_array_fun) \
196 V(INT16X8_FUNCTION_INDEX, JSFunction, int16x8_function) \
197 V(INT32_ARRAY_FUN_INDEX, JSFunction, int32_array_fun) \
198 V(INT32X4_FUNCTION_INDEX, JSFunction, int32x4_function) \
199 V(INT8_ARRAY_FUN_INDEX, JSFunction, int8_array_fun) \
200 V(INT8X16_FUNCTION_INDEX, JSFunction, int8x16_function) \
201 V(INTERNAL_ARRAY_FUNCTION_INDEX, JSFunction, internal_array_function) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000202 V(ITERATOR_RESULT_MAP_INDEX, Map, iterator_result_map) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000203 V(JS_ARRAY_FAST_SMI_ELEMENTS_MAP_INDEX, Map, \
204 js_array_fast_smi_elements_map_index) \
205 V(JS_ARRAY_FAST_HOLEY_SMI_ELEMENTS_MAP_INDEX, Map, \
206 js_array_fast_holey_smi_elements_map_index) \
207 V(JS_ARRAY_FAST_ELEMENTS_MAP_INDEX, Map, js_array_fast_elements_map_index) \
208 V(JS_ARRAY_FAST_HOLEY_ELEMENTS_MAP_INDEX, Map, \
209 js_array_fast_holey_elements_map_index) \
210 V(JS_ARRAY_FAST_DOUBLE_ELEMENTS_MAP_INDEX, Map, \
211 js_array_fast_double_elements_map_index) \
212 V(JS_ARRAY_FAST_HOLEY_DOUBLE_ELEMENTS_MAP_INDEX, Map, \
213 js_array_fast_holey_double_elements_map_index) \
214 V(JS_ARRAY_FAST_SMI_ELEMENTS_STRONG_MAP_INDEX, Map, \
215 js_array_fast_smi_elements_strong_map_index) \
216 V(JS_ARRAY_FAST_HOLEY_SMI_ELEMENTS_STRONG_MAP_INDEX, Map, \
217 js_array_fast_holey_smi_elements_strong_map_index) \
218 V(JS_ARRAY_FAST_ELEMENTS_STRONG_MAP_INDEX, Map, \
219 js_array_fast_elements_strong_map_index) \
220 V(JS_ARRAY_FAST_HOLEY_ELEMENTS_STRONG_MAP_INDEX, Map, \
221 js_array_fast_holey_elements_strong_map_index) \
222 V(JS_ARRAY_FAST_DOUBLE_ELEMENTS_STRONG_MAP_INDEX, Map, \
223 js_array_fast_double_elements_strong_map_index) \
224 V(JS_ARRAY_FAST_HOLEY_DOUBLE_ELEMENTS_STRONG_MAP_INDEX, Map, \
225 js_array_fast_holey_double_elements_strong_map_index) \
226 V(JS_MAP_FUN_INDEX, JSFunction, js_map_fun) \
227 V(JS_MAP_MAP_INDEX, Map, js_map_map) \
228 V(JS_OBJECT_STRONG_MAP_INDEX, Map, js_object_strong_map) \
229 V(JS_SET_FUN_INDEX, JSFunction, js_set_fun) \
230 V(JS_SET_MAP_INDEX, Map, js_set_map) \
231 V(JS_WEAK_MAP_FUN_INDEX, JSFunction, js_weak_map_fun) \
232 V(JS_WEAK_SET_FUN_INDEX, JSFunction, js_weak_set_fun) \
233 V(MAP_CACHE_INDEX, Object, map_cache) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000234 V(MAP_ITERATOR_MAP_INDEX, Map, map_iterator_map) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000235 V(STRING_ITERATOR_MAP_INDEX, Map, string_iterator_map) \
236 V(MESSAGE_LISTENERS_INDEX, JSObject, message_listeners) \
237 V(NATIVES_UTILS_OBJECT_INDEX, Object, natives_utils_object) \
238 V(NORMALIZED_MAP_CACHE_INDEX, Object, normalized_map_cache) \
239 V(NUMBER_FUNCTION_INDEX, JSFunction, number_function) \
240 V(OBJECT_FUNCTION_INDEX, JSFunction, object_function) \
241 V(OBJECT_FUNCTION_PROTOTYPE_MAP_INDEX, Map, object_function_prototype_map) \
242 V(OPAQUE_REFERENCE_FUNCTION_INDEX, JSFunction, opaque_reference_function) \
243 V(PROXY_CALLABLE_MAP_INDEX, Map, proxy_callable_map) \
244 V(PROXY_CONSTRUCTOR_MAP_INDEX, Map, proxy_constructor_map) \
245 V(PROXY_FUNCTION_INDEX, JSFunction, proxy_function) \
246 V(PROXY_FUNCTION_MAP_INDEX, Map, proxy_function_map) \
247 V(PROXY_MAP_INDEX, Map, proxy_map) \
248 V(REGEXP_FUNCTION_INDEX, JSFunction, regexp_function) \
249 V(REGEXP_RESULT_MAP_INDEX, Map, regexp_result_map) \
250 V(SCRIPT_CONTEXT_TABLE_INDEX, ScriptContextTable, script_context_table) \
251 V(SCRIPT_FUNCTION_INDEX, JSFunction, script_function) \
252 V(SECURITY_TOKEN_INDEX, Object, security_token) \
253 V(SELF_WEAK_CELL_INDEX, WeakCell, self_weak_cell) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000254 V(SET_ITERATOR_MAP_INDEX, Map, set_iterator_map) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000255 V(SHARED_ARRAY_BUFFER_FUN_INDEX, JSFunction, shared_array_buffer_fun) \
256 V(SLOPPY_ARGUMENTS_MAP_INDEX, Map, sloppy_arguments_map) \
257 V(SLOPPY_FUNCTION_MAP_INDEX, Map, sloppy_function_map) \
258 V(SLOPPY_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map, \
259 sloppy_function_without_prototype_map) \
260 V(SLOPPY_FUNCTION_WITH_READONLY_PROTOTYPE_MAP_INDEX, Map, \
261 sloppy_function_with_readonly_prototype_map) \
262 V(WASM_FUNCTION_MAP_INDEX, Map, wasm_function_map) \
263 V(SLOPPY_GENERATOR_FUNCTION_MAP_INDEX, Map, sloppy_generator_function_map) \
264 V(SLOW_ALIASED_ARGUMENTS_MAP_INDEX, Map, slow_aliased_arguments_map) \
265 V(STRICT_ARGUMENTS_MAP_INDEX, Map, strict_arguments_map) \
266 V(STRICT_FUNCTION_MAP_INDEX, Map, strict_function_map) \
267 V(STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map, \
268 strict_function_without_prototype_map) \
269 V(STRICT_GENERATOR_FUNCTION_MAP_INDEX, Map, strict_generator_function_map) \
270 V(STRING_FUNCTION_INDEX, JSFunction, string_function) \
271 V(STRING_FUNCTION_PROTOTYPE_MAP_INDEX, Map, string_function_prototype_map) \
272 V(STRONG_CONSTRUCTOR_MAP_INDEX, Map, strong_constructor_map) \
273 V(STRONG_FUNCTION_MAP_INDEX, Map, strong_function_map) \
274 V(STRONG_GENERATOR_FUNCTION_MAP_INDEX, Map, strong_generator_function_map) \
275 V(STRONG_MAP_CACHE_INDEX, Object, strong_map_cache) \
276 V(SYMBOL_FUNCTION_INDEX, JSFunction, symbol_function) \
277 V(UINT16_ARRAY_FUN_INDEX, JSFunction, uint16_array_fun) \
278 V(UINT16X8_FUNCTION_INDEX, JSFunction, uint16x8_function) \
279 V(UINT32_ARRAY_FUN_INDEX, JSFunction, uint32_array_fun) \
280 V(UINT32X4_FUNCTION_INDEX, JSFunction, uint32x4_function) \
281 V(UINT8_ARRAY_FUN_INDEX, JSFunction, uint8_array_fun) \
282 V(UINT8_CLAMPED_ARRAY_FUN_INDEX, JSFunction, uint8_clamped_array_fun) \
283 V(UINT8X16_FUNCTION_INDEX, JSFunction, uint8x16_function) \
284 NATIVE_CONTEXT_INTRINSIC_FUNCTIONS(V) \
285 NATIVE_CONTEXT_IMPORTED_FIELDS(V)
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400286
287// A table of all script contexts. Every loaded top-level script with top-level
288// lexical declarations contributes its ScriptContext into this table.
289//
290// The table is a fixed array, its first slot is the current used count and
291// the subsequent slots 1..used contain ScriptContexts.
292class ScriptContextTable : public FixedArray {
293 public:
294 // Conversions.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000295 static inline ScriptContextTable* cast(Object* context);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400296
297 struct LookupResult {
298 int context_index;
299 int slot_index;
300 VariableMode mode;
301 InitializationFlag init_flag;
302 MaybeAssignedFlag maybe_assigned_flag;
303 };
304
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000305 inline int used() const;
306 inline void set_used(int used);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400307
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000308 static inline Handle<Context> GetContext(Handle<ScriptContextTable> table,
309 int i);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400310
311 // Lookup a variable `name` in a ScriptContextTable.
312 // If it returns true, the variable is found and `result` contains
313 // valid information about its location.
314 // If it returns false, `result` is untouched.
315 MUST_USE_RESULT
316 static bool Lookup(Handle<ScriptContextTable> table, Handle<String> name,
317 LookupResult* result);
318
319 MUST_USE_RESULT
320 static Handle<ScriptContextTable> Extend(Handle<ScriptContextTable> table,
321 Handle<Context> script_context);
322
323 static int GetContextOffset(int context_index) {
324 return kFirstContextOffset + context_index * kPointerSize;
325 }
326
327 private:
328 static const int kUsedSlot = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000329 static const int kFirstContextSlot = kUsedSlot + 1;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400330 static const int kFirstContextOffset =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000331 FixedArray::kHeaderSize + kFirstContextSlot * kPointerSize;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400332
333 DISALLOW_IMPLICIT_CONSTRUCTORS(ScriptContextTable);
334};
Steve Blocka7e24c12009-10-30 11:49:00 +0000335
336// JSFunctions are pairs (context, function code), sometimes also called
337// closures. A Context object is used to represent function contexts and
338// dynamically pushed 'with' contexts (or 'scopes' in ECMA-262 speak).
339//
340// At runtime, the contexts build a stack in parallel to the execution
341// stack, with the top-most context being the current context. All contexts
342// have the following slots:
343//
344// [ closure ] This is the current function. It is the same for all
345// contexts inside a function. It provides access to the
346// incoming context (i.e., the outer context, which may
347// or may not become the current function's context), and
348// it provides access to the functions code and thus it's
349// scope information, which in turn contains the names of
350// statically allocated context slots. The names are needed
351// for dynamic lookups in the presence of 'with' or 'eval'.
352//
Steve Blocka7e24c12009-10-30 11:49:00 +0000353// [ previous ] A pointer to the previous context. It is NULL for
354// function contexts, and non-NULL for 'with' contexts.
355// Used to implement the 'with' statement.
356//
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000357// [ extension ] A pointer to an extension JSObject, or "the hole". Used to
Steve Blocka7e24c12009-10-30 11:49:00 +0000358// implement 'with' statements and dynamic declarations
359// (through 'eval'). The object in a 'with' statement is
360// stored in the extension slot of a 'with' context.
361// Dynamically declared variables/functions are also added
362// to lazily allocated extension object. Context::Lookup
363// searches the extension object for properties.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000364// For script and block contexts, contains the respective
365// ScopeInfo. For block contexts representing sloppy declaration
366// block scopes, it may also be a struct being a
367// SloppyBlockWithEvalContextExtension, pairing the ScopeInfo
368// with an extension object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000369// For module contexts, points back to the respective JSModule.
Steve Blocka7e24c12009-10-30 11:49:00 +0000370//
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000371// [ global_object ] A pointer to the global object. Provided for quick
Steve Blocka7e24c12009-10-30 11:49:00 +0000372// access to the global object from inside the code (since
373// we always have a context pointer).
374//
375// In addition, function contexts may have statically allocated context slots
376// to store local variables/functions that are accessed from inner functions
377// (via static context addresses) or through 'eval' (dynamic context lookups).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000378// The native context contains additional slots for fast access to native
379// properties.
380//
381// Finally, with Harmony scoping, the JSFunction representing a top level
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400382// script will have the ScriptContext rather than a FunctionContext.
383// Script contexts from all top-level scripts are gathered in
384// ScriptContextTable.
Steve Blocka7e24c12009-10-30 11:49:00 +0000385
386class Context: public FixedArray {
387 public:
388 // Conversions.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000389 static inline Context* cast(Object* context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000390
391 // The default context slot layout; indices are FixedArray slot indices.
392 enum {
393 // These slots are in all contexts.
394 CLOSURE_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000395 PREVIOUS_INDEX,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000396 // The extension slot is used for either the global object (in native
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000397 // contexts), eval extension object (function contexts), subject of with
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100398 // (with contexts), or the variable name (catch contexts), the serialized
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000399 // scope info (block contexts), or the module instance (module contexts).
Steve Blocka7e24c12009-10-30 11:49:00 +0000400 EXTENSION_INDEX,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000401 NATIVE_CONTEXT_INDEX,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000402
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000403 // These slots are only in native contexts.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000404#define NATIVE_CONTEXT_SLOT(index, type, name) index,
405 NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_SLOT)
406#undef NATIVE_CONTEXT_SLOT
Ben Murdochf87a2032010-10-22 12:50:53 +0100407
408 // Properties from here are treated as weak references by the full GC.
409 // Scavenge treats them as strong references.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100410 OPTIMIZED_FUNCTIONS_LIST, // Weak.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000411 OPTIMIZED_CODE_LIST, // Weak.
412 DEOPTIMIZED_CODE_LIST, // Weak.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000413 NEXT_CONTEXT_LINK, // Weak.
Ben Murdochf87a2032010-10-22 12:50:53 +0100414
415 // Total number of slots.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000416 NATIVE_CONTEXT_SLOTS,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000417 FIRST_WEAK_SLOT = OPTIMIZED_FUNCTIONS_LIST,
418 FIRST_JS_ARRAY_MAP_SLOT = JS_ARRAY_FAST_SMI_ELEMENTS_MAP_INDEX,
419 FIRST_JS_ARRAY_STRONG_MAP_SLOT =
420 JS_ARRAY_FAST_SMI_ELEMENTS_STRONG_MAP_INDEX,
421
422 MIN_CONTEXT_SLOTS = GLOBAL_PROXY_INDEX,
423 // This slot holds the thrown value in catch contexts.
424 THROWN_OBJECT_INDEX = MIN_CONTEXT_SLOTS,
Steve Blocka7e24c12009-10-30 11:49:00 +0000425 };
426
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000427 void IncrementErrorsThrown();
428 int GetErrorsThrown();
429
Steve Blocka7e24c12009-10-30 11:49:00 +0000430 // Direct slot access.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000431 inline JSFunction* closure();
432 inline void set_closure(JSFunction* closure);
Steve Blocka7e24c12009-10-30 11:49:00 +0000433
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000434 inline Context* previous();
435 inline void set_previous(Context* context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000436
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000437 inline bool has_extension();
438 inline HeapObject* extension();
439 inline void set_extension(HeapObject* object);
440 JSObject* extension_object();
441 JSReceiver* extension_receiver();
442 ScopeInfo* scope_info();
443 String* catch_name();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000444
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000445 inline JSModule* module();
446 inline void set_module(JSModule* module);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000447
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000448 // Get the context where var declarations will be hoisted to, which
449 // may be the context itself.
450 Context* declaration_context();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000451 bool is_declaration_context();
Steve Blocka7e24c12009-10-30 11:49:00 +0000452
Ben Murdoch097c5b22016-05-18 11:27:45 +0100453 // Get the next closure's context on the context chain.
454 Context* closure_context();
455
Steve Blocka7e24c12009-10-30 11:49:00 +0000456 // Returns a JSGlobalProxy object or null.
457 JSObject* global_proxy();
458 void set_global_proxy(JSObject* global);
459
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000460 // Get the JSGlobalObject object.
461 JSGlobalObject* global_object();
Steve Blocka7e24c12009-10-30 11:49:00 +0000462
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400463 // Get the script context by traversing the context chain.
464 Context* script_context();
Steve Blocka7e24c12009-10-30 11:49:00 +0000465
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000466 // Compute the native context.
467 inline Context* native_context();
468 inline void set_native_context(Context* context);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000469
470 // Predicates for context types. IsNativeContext is also defined on Object
471 // because we frequently have to know if arbitrary objects are natives
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000472 // contexts.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000473 inline bool IsNativeContext();
474 inline bool IsFunctionContext();
475 inline bool IsCatchContext();
476 inline bool IsWithContext();
477 inline bool IsBlockContext();
478 inline bool IsModuleContext();
479 inline bool IsScriptContext();
Steve Blocka7e24c12009-10-30 11:49:00 +0000480
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000481 inline bool HasSameSecurityTokenAs(Context* that);
482
483 // Initializes global variable bindings in given script context.
484 void InitializeGlobalSlots();
Steve Blocka7e24c12009-10-30 11:49:00 +0000485
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000486 // A native context holds a list of all functions with optimized code.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100487 void AddOptimizedFunction(JSFunction* function);
488 void RemoveOptimizedFunction(JSFunction* function);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000489 void SetOptimizedFunctionsListHead(Object* head);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100490 Object* OptimizedFunctionsListHead();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100491
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000492 // The native context also stores a list of all optimized code and a
493 // list of all deoptimized code, which are needed by the deoptimizer.
494 void AddOptimizedCode(Code* code);
495 void SetOptimizedCodeListHead(Object* head);
496 Object* OptimizedCodeListHead();
497 void SetDeoptimizedCodeListHead(Object* head);
498 Object* DeoptimizedCodeListHead();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100499
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000500 Handle<Object> ErrorMessageForCodeGenerationFromStrings();
501
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000502 static int ImportedFieldIndexForName(Handle<String> name);
503 static int IntrinsicIndexForName(Handle<String> name);
504
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000505#define NATIVE_CONTEXT_FIELD_ACCESSORS(index, type, name) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000506 inline void set_##name(type* value); \
507 inline bool is_##name(type* value); \
508 inline type* name();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000509 NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_FIELD_ACCESSORS)
510#undef NATIVE_CONTEXT_FIELD_ACCESSORS
Steve Blocka7e24c12009-10-30 11:49:00 +0000511
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000512 // Lookup the slot called name, starting with the current context.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100513 // There are three possibilities:
Steve Blocka7e24c12009-10-30 11:49:00 +0000514 //
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100515 // 1) result->IsContext():
516 // The binding was found in a context. *index is always the
517 // non-negative slot index. *attributes is NONE for var and let
518 // declarations, READ_ONLY for const declarations (never ABSENT).
Steve Blocka7e24c12009-10-30 11:49:00 +0000519 //
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100520 // 2) result->IsJSObject():
521 // The binding was found as a named property in a context extension
522 // object (i.e., was introduced via eval), as a property on the subject
523 // of with, or as a property of the global object. *index is -1 and
524 // *attributes is not ABSENT.
Steve Blocka7e24c12009-10-30 11:49:00 +0000525 //
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100526 // 3) result.is_null():
527 // There was no binding found, *index is always -1 and *attributes is
528 // always ABSENT.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000529 Handle<Object> Lookup(Handle<String> name,
530 ContextLookupFlags flags,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100531 int* index,
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000532 PropertyAttributes* attributes,
533 BindingFlags* binding_flags);
Steve Blocka7e24c12009-10-30 11:49:00 +0000534
Steve Blocka7e24c12009-10-30 11:49:00 +0000535 // Code generation support.
536 static int SlotOffset(int index) {
537 return kHeaderSize + index * kPointerSize - kHeapObjectTag;
538 }
539
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000540 static int FunctionMapIndex(LanguageMode language_mode, FunctionKind kind) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000541 if (IsGeneratorFunction(kind)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000542 return is_strong(language_mode) ? STRONG_GENERATOR_FUNCTION_MAP_INDEX :
543 is_strict(language_mode) ? STRICT_GENERATOR_FUNCTION_MAP_INDEX
544 : SLOPPY_GENERATOR_FUNCTION_MAP_INDEX;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000545 }
546
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000547 if (IsClassConstructor(kind)) {
548 // Use strict function map (no own "caller" / "arguments")
549 return is_strong(language_mode) ? STRONG_CONSTRUCTOR_MAP_INDEX
550 : STRICT_FUNCTION_MAP_INDEX;
551 }
552
553 if (IsArrowFunction(kind) || IsConciseMethod(kind) ||
554 IsAccessorFunction(kind)) {
555 return is_strong(language_mode)
556 ? STRONG_FUNCTION_MAP_INDEX
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000557 : STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX;
558 }
559
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000560 return is_strong(language_mode) ? STRONG_FUNCTION_MAP_INDEX :
561 is_strict(language_mode) ? STRICT_FUNCTION_MAP_INDEX
562 : SLOPPY_FUNCTION_MAP_INDEX;
563 }
564
565 static int ArrayMapIndex(ElementsKind elements_kind,
566 Strength strength = Strength::WEAK) {
567 DCHECK(IsFastElementsKind(elements_kind));
568 return elements_kind + (is_strong(strength) ? FIRST_JS_ARRAY_STRONG_MAP_SLOT
569 : FIRST_JS_ARRAY_MAP_SLOT);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000570 }
571
572 static const int kSize = kHeaderSize + NATIVE_CONTEXT_SLOTS * kPointerSize;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000573 static const int kNotFound = -1;
Ben Murdochf87a2032010-10-22 12:50:53 +0100574
575 // GC support.
576 typedef FixedBodyDescriptor<
577 kHeaderSize, kSize, kSize> ScavengeBodyDescriptor;
578
579 typedef FixedBodyDescriptor<
580 kHeaderSize,
581 kHeaderSize + FIRST_WEAK_SLOT * kPointerSize,
582 kSize> MarkCompactBodyDescriptor;
583
Steve Blocka7e24c12009-10-30 11:49:00 +0000584 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000585#ifdef DEBUG
586 // Bootstrapping-aware type checks.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000587 static bool IsBootstrappingOrNativeContext(Isolate* isolate, Object* object);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000588 static bool IsBootstrappingOrValidParentContext(Object* object, Context* kid);
Steve Blocka7e24c12009-10-30 11:49:00 +0000589#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000590
591 STATIC_ASSERT(kHeaderSize == Internals::kContextHeaderSize);
592 STATIC_ASSERT(EMBEDDER_DATA_INDEX == Internals::kContextEmbedderDataIndex);
Steve Blocka7e24c12009-10-30 11:49:00 +0000593};
594
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000595} // namespace internal
596} // namespace v8
Steve Blocka7e24c12009-10-30 11:49:00 +0000597
598#endif // V8_CONTEXTS_H_