blob: 90fb9a42785219088aa228a1a4690f45153ce308 [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) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000082 V(GET_TEMPLATE_CALL_SITE_INDEX, JSFunction, get_template_call_site) \
83 V(MAKE_RANGE_ERROR_INDEX, JSFunction, make_range_error) \
84 V(MAKE_TYPE_ERROR_INDEX, JSFunction, make_type_error) \
85 V(OBJECT_FREEZE, JSFunction, object_freeze) \
86 V(OBJECT_IS_EXTENSIBLE, JSFunction, object_is_extensible) \
87 V(OBJECT_IS_FROZEN, JSFunction, object_is_frozen) \
88 V(OBJECT_IS_SEALED, JSFunction, object_is_sealed) \
89 V(OBJECT_KEYS, JSFunction, object_keys) \
90 V(REFLECT_APPLY_INDEX, JSFunction, reflect_apply) \
91 V(REFLECT_CONSTRUCT_INDEX, JSFunction, reflect_construct) \
92 V(REFLECT_DEFINE_PROPERTY_INDEX, JSFunction, reflect_define_property) \
93 V(REFLECT_DELETE_PROPERTY_INDEX, JSFunction, reflect_delete_property) \
94 V(SPREAD_ARGUMENTS_INDEX, JSFunction, spread_arguments) \
Ben Murdoch097c5b22016-05-18 11:27:45 +010095 V(SPREAD_ITERABLE_INDEX, JSFunction, spread_iterable) \
Ben Murdochda12d292016-06-02 14:46:10 +010096 V(ORDINARY_HAS_INSTANCE_INDEX, JSFunction, ordinary_has_instance) \
97 V(MATH_FLOOR, JSFunction, math_floor) \
98 V(MATH_SQRT, JSFunction, math_sqrt)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000099
100#define NATIVE_CONTEXT_IMPORTED_FIELDS(V) \
101 V(ARRAY_CONCAT_INDEX, JSFunction, array_concat) \
102 V(ARRAY_POP_INDEX, JSFunction, array_pop) \
103 V(ARRAY_PUSH_INDEX, JSFunction, array_push) \
104 V(ARRAY_SHIFT_INDEX, JSFunction, array_shift) \
105 V(ARRAY_SPLICE_INDEX, JSFunction, array_splice) \
106 V(ARRAY_SLICE_INDEX, JSFunction, array_slice) \
107 V(ARRAY_UNSHIFT_INDEX, JSFunction, array_unshift) \
108 V(ARRAY_VALUES_ITERATOR_INDEX, JSFunction, array_values_iterator) \
109 V(DERIVED_GET_TRAP_INDEX, JSFunction, derived_get_trap) \
110 V(ERROR_FUNCTION_INDEX, JSFunction, error_function) \
111 V(EVAL_ERROR_FUNCTION_INDEX, JSFunction, eval_error_function) \
112 V(GET_STACK_TRACE_LINE_INDEX, JSFunction, get_stack_trace_line_fun) \
113 V(GLOBAL_EVAL_FUN_INDEX, JSFunction, global_eval_fun) \
114 V(JSON_SERIALIZE_ADAPTER_INDEX, JSFunction, json_serialize_adapter) \
115 V(MAKE_ERROR_FUNCTION_INDEX, JSFunction, make_error_function) \
116 V(MAP_DELETE_METHOD_INDEX, JSFunction, map_delete) \
117 V(MAP_GET_METHOD_INDEX, JSFunction, map_get) \
118 V(MAP_HAS_METHOD_INDEX, JSFunction, map_has) \
119 V(MAP_SET_METHOD_INDEX, JSFunction, map_set) \
Ben Murdochda12d292016-06-02 14:46:10 +0100120 V(MATH_POW_METHOD_INDEX, JSFunction, math_pow) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000121 V(MESSAGE_GET_COLUMN_NUMBER_INDEX, JSFunction, message_get_column_number) \
122 V(MESSAGE_GET_LINE_NUMBER_INDEX, JSFunction, message_get_line_number) \
123 V(MESSAGE_GET_SOURCE_LINE_INDEX, JSFunction, message_get_source_line) \
124 V(NATIVE_OBJECT_GET_NOTIFIER_INDEX, JSFunction, native_object_get_notifier) \
125 V(NATIVE_OBJECT_NOTIFIER_PERFORM_CHANGE, JSFunction, \
126 native_object_notifier_perform_change) \
127 V(NATIVE_OBJECT_OBSERVE_INDEX, JSFunction, native_object_observe) \
128 V(NO_SIDE_EFFECTS_TO_STRING_FUN_INDEX, JSFunction, \
129 no_side_effects_to_string_fun) \
130 V(OBJECT_VALUE_OF, JSFunction, object_value_of) \
131 V(OBJECT_TO_STRING, JSFunction, object_to_string) \
132 V(OBSERVERS_BEGIN_SPLICE_INDEX, JSFunction, observers_begin_perform_splice) \
133 V(OBSERVERS_END_SPLICE_INDEX, JSFunction, observers_end_perform_splice) \
134 V(OBSERVERS_ENQUEUE_SPLICE_INDEX, JSFunction, observers_enqueue_splice) \
135 V(OBSERVERS_NOTIFY_CHANGE_INDEX, JSFunction, observers_notify_change) \
136 V(PROMISE_CATCH_INDEX, JSFunction, promise_catch) \
137 V(PROMISE_CHAIN_INDEX, JSFunction, promise_chain) \
138 V(PROMISE_CREATE_INDEX, JSFunction, promise_create) \
139 V(PROMISE_FUNCTION_INDEX, JSFunction, promise_function) \
140 V(PROMISE_HAS_USER_DEFINED_REJECT_HANDLER_INDEX, JSFunction, \
141 promise_has_user_defined_reject_handler) \
142 V(PROMISE_REJECT_INDEX, JSFunction, promise_reject) \
143 V(PROMISE_RESOLVE_INDEX, JSFunction, promise_resolve) \
144 V(PROMISE_THEN_INDEX, JSFunction, promise_then) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000145 V(RANGE_ERROR_FUNCTION_INDEX, JSFunction, range_error_function) \
146 V(REFERENCE_ERROR_FUNCTION_INDEX, JSFunction, reference_error_function) \
147 V(SET_ADD_METHOD_INDEX, JSFunction, set_add) \
148 V(SET_DELETE_METHOD_INDEX, JSFunction, set_delete) \
149 V(SET_HAS_METHOD_INDEX, JSFunction, set_has) \
150 V(STACK_OVERFLOW_BOILERPLATE_INDEX, JSObject, stack_overflow_boilerplate) \
151 V(SYNTAX_ERROR_FUNCTION_INDEX, JSFunction, syntax_error_function) \
152 V(TYPE_ERROR_FUNCTION_INDEX, JSFunction, type_error_function) \
Ben Murdoch097c5b22016-05-18 11:27:45 +0100153 V(URI_ERROR_FUNCTION_INDEX, JSFunction, uri_error_function)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000154
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000155#define NATIVE_CONTEXT_FIELDS(V) \
156 V(GLOBAL_PROXY_INDEX, JSObject, global_proxy_object) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000157 V(EMBEDDER_DATA_INDEX, FixedArray, embedder_data) \
158 /* Below is alpha-sorted */ \
Ben Murdoch097c5b22016-05-18 11:27:45 +0100159 V(ACCESSOR_PROPERTY_DESCRIPTOR_MAP_INDEX, Map, \
160 accessor_property_descriptor_map) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000161 V(ALLOW_CODE_GEN_FROM_STRINGS_INDEX, Object, allow_code_gen_from_strings) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000162 V(ARRAY_BUFFER_FUN_INDEX, JSFunction, array_buffer_fun) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000163 V(ARRAY_BUFFER_MAP_INDEX, Map, array_buffer_map) \
164 V(ARRAY_FUNCTION_INDEX, JSFunction, array_function) \
165 V(BOOL16X8_FUNCTION_INDEX, JSFunction, bool16x8_function) \
166 V(BOOL32X4_FUNCTION_INDEX, JSFunction, bool32x4_function) \
167 V(BOOL8X16_FUNCTION_INDEX, JSFunction, bool8x16_function) \
168 V(BOOLEAN_FUNCTION_INDEX, JSFunction, boolean_function) \
169 V(BOUND_FUNCTION_WITH_CONSTRUCTOR_MAP_INDEX, Map, \
170 bound_function_with_constructor_map) \
171 V(BOUND_FUNCTION_WITHOUT_CONSTRUCTOR_MAP_INDEX, Map, \
172 bound_function_without_constructor_map) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000173 V(CALL_AS_CONSTRUCTOR_DELEGATE_INDEX, JSFunction, \
174 call_as_constructor_delegate) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000175 V(CALL_AS_FUNCTION_DELEGATE_INDEX, JSFunction, call_as_function_delegate) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000176 V(CONTEXT_EXTENSION_FUNCTION_INDEX, JSFunction, context_extension_function) \
Ben Murdoch097c5b22016-05-18 11:27:45 +0100177 V(DATA_PROPERTY_DESCRIPTOR_MAP_INDEX, Map, data_property_descriptor_map) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000178 V(DATA_VIEW_FUN_INDEX, JSFunction, data_view_fun) \
179 V(DATE_FUNCTION_INDEX, JSFunction, date_function) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000180 V(ERROR_MESSAGE_FOR_CODE_GEN_FROM_STRINGS_INDEX, Object, \
181 error_message_for_code_gen_from_strings) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000182 V(ERRORS_THROWN_INDEX, Smi, errors_thrown) \
183 V(EXTRAS_EXPORTS_OBJECT_INDEX, JSObject, extras_binding_object) \
184 V(EXTRAS_UTILS_OBJECT_INDEX, JSObject, extras_utils_object) \
185 V(FAST_ALIASED_ARGUMENTS_MAP_INDEX, Map, fast_aliased_arguments_map) \
186 V(FLOAT32_ARRAY_FUN_INDEX, JSFunction, float32_array_fun) \
187 V(FLOAT32X4_FUNCTION_INDEX, JSFunction, float32x4_function) \
188 V(FLOAT64_ARRAY_FUN_INDEX, JSFunction, float64_array_fun) \
Ben Murdochda12d292016-06-02 14:46:10 +0100189 V(TEMPLATE_INSTANTIATIONS_CACHE_INDEX, UnseededNumberDictionary, \
Ben Murdoch097c5b22016-05-18 11:27:45 +0100190 template_instantiations_cache) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000191 V(FUNCTION_FUNCTION_INDEX, JSFunction, function_function) \
192 V(GENERATOR_FUNCTION_FUNCTION_INDEX, JSFunction, \
193 generator_function_function) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000194 V(GENERATOR_OBJECT_PROTOTYPE_MAP_INDEX, Map, generator_object_prototype_map) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000195 V(INITIAL_ARRAY_PROTOTYPE_INDEX, JSObject, initial_array_prototype) \
196 V(INITIAL_OBJECT_PROTOTYPE_INDEX, JSObject, initial_object_prototype) \
197 V(INT16_ARRAY_FUN_INDEX, JSFunction, int16_array_fun) \
198 V(INT16X8_FUNCTION_INDEX, JSFunction, int16x8_function) \
199 V(INT32_ARRAY_FUN_INDEX, JSFunction, int32_array_fun) \
200 V(INT32X4_FUNCTION_INDEX, JSFunction, int32x4_function) \
201 V(INT8_ARRAY_FUN_INDEX, JSFunction, int8_array_fun) \
202 V(INT8X16_FUNCTION_INDEX, JSFunction, int8x16_function) \
203 V(INTERNAL_ARRAY_FUNCTION_INDEX, JSFunction, internal_array_function) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000204 V(ITERATOR_RESULT_MAP_INDEX, Map, iterator_result_map) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000205 V(JS_ARRAY_FAST_SMI_ELEMENTS_MAP_INDEX, Map, \
206 js_array_fast_smi_elements_map_index) \
207 V(JS_ARRAY_FAST_HOLEY_SMI_ELEMENTS_MAP_INDEX, Map, \
208 js_array_fast_holey_smi_elements_map_index) \
209 V(JS_ARRAY_FAST_ELEMENTS_MAP_INDEX, Map, js_array_fast_elements_map_index) \
210 V(JS_ARRAY_FAST_HOLEY_ELEMENTS_MAP_INDEX, Map, \
211 js_array_fast_holey_elements_map_index) \
212 V(JS_ARRAY_FAST_DOUBLE_ELEMENTS_MAP_INDEX, Map, \
213 js_array_fast_double_elements_map_index) \
214 V(JS_ARRAY_FAST_HOLEY_DOUBLE_ELEMENTS_MAP_INDEX, Map, \
215 js_array_fast_holey_double_elements_map_index) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000216 V(JS_MAP_FUN_INDEX, JSFunction, js_map_fun) \
217 V(JS_MAP_MAP_INDEX, Map, js_map_map) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000218 V(JS_SET_FUN_INDEX, JSFunction, js_set_fun) \
219 V(JS_SET_MAP_INDEX, Map, js_set_map) \
220 V(JS_WEAK_MAP_FUN_INDEX, JSFunction, js_weak_map_fun) \
221 V(JS_WEAK_SET_FUN_INDEX, JSFunction, js_weak_set_fun) \
222 V(MAP_CACHE_INDEX, Object, map_cache) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000223 V(MAP_ITERATOR_MAP_INDEX, Map, map_iterator_map) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000224 V(STRING_ITERATOR_MAP_INDEX, Map, string_iterator_map) \
225 V(MESSAGE_LISTENERS_INDEX, JSObject, message_listeners) \
226 V(NATIVES_UTILS_OBJECT_INDEX, Object, natives_utils_object) \
227 V(NORMALIZED_MAP_CACHE_INDEX, Object, normalized_map_cache) \
228 V(NUMBER_FUNCTION_INDEX, JSFunction, number_function) \
229 V(OBJECT_FUNCTION_INDEX, JSFunction, object_function) \
230 V(OBJECT_FUNCTION_PROTOTYPE_MAP_INDEX, Map, object_function_prototype_map) \
231 V(OPAQUE_REFERENCE_FUNCTION_INDEX, JSFunction, opaque_reference_function) \
232 V(PROXY_CALLABLE_MAP_INDEX, Map, proxy_callable_map) \
233 V(PROXY_CONSTRUCTOR_MAP_INDEX, Map, proxy_constructor_map) \
234 V(PROXY_FUNCTION_INDEX, JSFunction, proxy_function) \
235 V(PROXY_FUNCTION_MAP_INDEX, Map, proxy_function_map) \
236 V(PROXY_MAP_INDEX, Map, proxy_map) \
237 V(REGEXP_FUNCTION_INDEX, JSFunction, regexp_function) \
238 V(REGEXP_RESULT_MAP_INDEX, Map, regexp_result_map) \
239 V(SCRIPT_CONTEXT_TABLE_INDEX, ScriptContextTable, script_context_table) \
240 V(SCRIPT_FUNCTION_INDEX, JSFunction, script_function) \
241 V(SECURITY_TOKEN_INDEX, Object, security_token) \
242 V(SELF_WEAK_CELL_INDEX, WeakCell, self_weak_cell) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000243 V(SET_ITERATOR_MAP_INDEX, Map, set_iterator_map) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000244 V(SHARED_ARRAY_BUFFER_FUN_INDEX, JSFunction, shared_array_buffer_fun) \
245 V(SLOPPY_ARGUMENTS_MAP_INDEX, Map, sloppy_arguments_map) \
246 V(SLOPPY_FUNCTION_MAP_INDEX, Map, sloppy_function_map) \
247 V(SLOPPY_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map, \
248 sloppy_function_without_prototype_map) \
249 V(SLOPPY_FUNCTION_WITH_READONLY_PROTOTYPE_MAP_INDEX, Map, \
250 sloppy_function_with_readonly_prototype_map) \
251 V(WASM_FUNCTION_MAP_INDEX, Map, wasm_function_map) \
252 V(SLOPPY_GENERATOR_FUNCTION_MAP_INDEX, Map, sloppy_generator_function_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(STRICT_FUNCTION_MAP_INDEX, Map, strict_function_map) \
256 V(STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map, \
257 strict_function_without_prototype_map) \
258 V(STRICT_GENERATOR_FUNCTION_MAP_INDEX, Map, strict_generator_function_map) \
259 V(STRING_FUNCTION_INDEX, JSFunction, string_function) \
260 V(STRING_FUNCTION_PROTOTYPE_MAP_INDEX, Map, string_function_prototype_map) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000261 V(SYMBOL_FUNCTION_INDEX, JSFunction, symbol_function) \
262 V(UINT16_ARRAY_FUN_INDEX, JSFunction, uint16_array_fun) \
263 V(UINT16X8_FUNCTION_INDEX, JSFunction, uint16x8_function) \
264 V(UINT32_ARRAY_FUN_INDEX, JSFunction, uint32_array_fun) \
265 V(UINT32X4_FUNCTION_INDEX, JSFunction, uint32x4_function) \
266 V(UINT8_ARRAY_FUN_INDEX, JSFunction, uint8_array_fun) \
267 V(UINT8_CLAMPED_ARRAY_FUN_INDEX, JSFunction, uint8_clamped_array_fun) \
268 V(UINT8X16_FUNCTION_INDEX, JSFunction, uint8x16_function) \
269 NATIVE_CONTEXT_INTRINSIC_FUNCTIONS(V) \
270 NATIVE_CONTEXT_IMPORTED_FIELDS(V)
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400271
272// A table of all script contexts. Every loaded top-level script with top-level
273// lexical declarations contributes its ScriptContext into this table.
274//
275// The table is a fixed array, its first slot is the current used count and
276// the subsequent slots 1..used contain ScriptContexts.
277class ScriptContextTable : public FixedArray {
278 public:
279 // Conversions.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000280 static inline ScriptContextTable* cast(Object* context);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400281
282 struct LookupResult {
283 int context_index;
284 int slot_index;
285 VariableMode mode;
286 InitializationFlag init_flag;
287 MaybeAssignedFlag maybe_assigned_flag;
288 };
289
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000290 inline int used() const;
291 inline void set_used(int used);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400292
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000293 static inline Handle<Context> GetContext(Handle<ScriptContextTable> table,
294 int i);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400295
296 // Lookup a variable `name` in a ScriptContextTable.
297 // If it returns true, the variable is found and `result` contains
298 // valid information about its location.
299 // If it returns false, `result` is untouched.
300 MUST_USE_RESULT
301 static bool Lookup(Handle<ScriptContextTable> table, Handle<String> name,
302 LookupResult* result);
303
304 MUST_USE_RESULT
305 static Handle<ScriptContextTable> Extend(Handle<ScriptContextTable> table,
306 Handle<Context> script_context);
307
308 static int GetContextOffset(int context_index) {
309 return kFirstContextOffset + context_index * kPointerSize;
310 }
311
312 private:
313 static const int kUsedSlot = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000314 static const int kFirstContextSlot = kUsedSlot + 1;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400315 static const int kFirstContextOffset =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000316 FixedArray::kHeaderSize + kFirstContextSlot * kPointerSize;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400317
318 DISALLOW_IMPLICIT_CONSTRUCTORS(ScriptContextTable);
319};
Steve Blocka7e24c12009-10-30 11:49:00 +0000320
321// JSFunctions are pairs (context, function code), sometimes also called
322// closures. A Context object is used to represent function contexts and
323// dynamically pushed 'with' contexts (or 'scopes' in ECMA-262 speak).
324//
325// At runtime, the contexts build a stack in parallel to the execution
326// stack, with the top-most context being the current context. All contexts
327// have the following slots:
328//
329// [ closure ] This is the current function. It is the same for all
330// contexts inside a function. It provides access to the
331// incoming context (i.e., the outer context, which may
332// or may not become the current function's context), and
333// it provides access to the functions code and thus it's
334// scope information, which in turn contains the names of
335// statically allocated context slots. The names are needed
336// for dynamic lookups in the presence of 'with' or 'eval'.
337//
Steve Blocka7e24c12009-10-30 11:49:00 +0000338// [ previous ] A pointer to the previous context. It is NULL for
339// function contexts, and non-NULL for 'with' contexts.
340// Used to implement the 'with' statement.
341//
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000342// [ extension ] A pointer to an extension JSObject, or "the hole". Used to
Steve Blocka7e24c12009-10-30 11:49:00 +0000343// implement 'with' statements and dynamic declarations
344// (through 'eval'). The object in a 'with' statement is
345// stored in the extension slot of a 'with' context.
346// Dynamically declared variables/functions are also added
347// to lazily allocated extension object. Context::Lookup
348// searches the extension object for properties.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000349// For script and block contexts, contains the respective
350// ScopeInfo. For block contexts representing sloppy declaration
351// block scopes, it may also be a struct being a
352// SloppyBlockWithEvalContextExtension, pairing the ScopeInfo
353// with an extension object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000354// For module contexts, points back to the respective JSModule.
Steve Blocka7e24c12009-10-30 11:49:00 +0000355//
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000356// [ global_object ] A pointer to the global object. Provided for quick
Steve Blocka7e24c12009-10-30 11:49:00 +0000357// access to the global object from inside the code (since
358// we always have a context pointer).
359//
360// In addition, function contexts may have statically allocated context slots
361// to store local variables/functions that are accessed from inner functions
362// (via static context addresses) or through 'eval' (dynamic context lookups).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000363// The native context contains additional slots for fast access to native
364// properties.
365//
366// Finally, with Harmony scoping, the JSFunction representing a top level
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400367// script will have the ScriptContext rather than a FunctionContext.
368// Script contexts from all top-level scripts are gathered in
369// ScriptContextTable.
Steve Blocka7e24c12009-10-30 11:49:00 +0000370
371class Context: public FixedArray {
372 public:
373 // Conversions.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000374 static inline Context* cast(Object* context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000375
376 // The default context slot layout; indices are FixedArray slot indices.
377 enum {
378 // These slots are in all contexts.
379 CLOSURE_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000380 PREVIOUS_INDEX,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000381 // The extension slot is used for either the global object (in native
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000382 // contexts), eval extension object (function contexts), subject of with
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100383 // (with contexts), or the variable name (catch contexts), the serialized
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000384 // scope info (block contexts), or the module instance (module contexts).
Steve Blocka7e24c12009-10-30 11:49:00 +0000385 EXTENSION_INDEX,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000386 NATIVE_CONTEXT_INDEX,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000387
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000388 // These slots are only in native contexts.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000389#define NATIVE_CONTEXT_SLOT(index, type, name) index,
390 NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_SLOT)
391#undef NATIVE_CONTEXT_SLOT
Ben Murdochf87a2032010-10-22 12:50:53 +0100392
393 // Properties from here are treated as weak references by the full GC.
394 // Scavenge treats them as strong references.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100395 OPTIMIZED_FUNCTIONS_LIST, // Weak.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000396 OPTIMIZED_CODE_LIST, // Weak.
397 DEOPTIMIZED_CODE_LIST, // Weak.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000398 NEXT_CONTEXT_LINK, // Weak.
Ben Murdochf87a2032010-10-22 12:50:53 +0100399
400 // Total number of slots.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000401 NATIVE_CONTEXT_SLOTS,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000402 FIRST_WEAK_SLOT = OPTIMIZED_FUNCTIONS_LIST,
403 FIRST_JS_ARRAY_MAP_SLOT = JS_ARRAY_FAST_SMI_ELEMENTS_MAP_INDEX,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000404
405 MIN_CONTEXT_SLOTS = GLOBAL_PROXY_INDEX,
406 // This slot holds the thrown value in catch contexts.
407 THROWN_OBJECT_INDEX = MIN_CONTEXT_SLOTS,
Ben Murdochda12d292016-06-02 14:46:10 +0100408
409 // These slots hold values in debug evaluate contexts.
410 WRAPPED_CONTEXT_INDEX = MIN_CONTEXT_SLOTS,
411 WHITE_LIST_INDEX = MIN_CONTEXT_SLOTS + 1
Steve Blocka7e24c12009-10-30 11:49:00 +0000412 };
413
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000414 void IncrementErrorsThrown();
415 int GetErrorsThrown();
416
Steve Blocka7e24c12009-10-30 11:49:00 +0000417 // Direct slot access.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000418 inline JSFunction* closure();
419 inline void set_closure(JSFunction* closure);
Steve Blocka7e24c12009-10-30 11:49:00 +0000420
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000421 inline Context* previous();
422 inline void set_previous(Context* context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000423
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000424 inline bool has_extension();
425 inline HeapObject* extension();
426 inline void set_extension(HeapObject* object);
427 JSObject* extension_object();
428 JSReceiver* extension_receiver();
429 ScopeInfo* scope_info();
430 String* catch_name();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000431
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000432 inline JSModule* module();
433 inline void set_module(JSModule* module);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000434
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000435 // Get the context where var declarations will be hoisted to, which
436 // may be the context itself.
437 Context* declaration_context();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000438 bool is_declaration_context();
Steve Blocka7e24c12009-10-30 11:49:00 +0000439
Ben Murdoch097c5b22016-05-18 11:27:45 +0100440 // Get the next closure's context on the context chain.
441 Context* closure_context();
442
Steve Blocka7e24c12009-10-30 11:49:00 +0000443 // Returns a JSGlobalProxy object or null.
444 JSObject* global_proxy();
445 void set_global_proxy(JSObject* global);
446
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000447 // Get the JSGlobalObject object.
448 JSGlobalObject* global_object();
Steve Blocka7e24c12009-10-30 11:49:00 +0000449
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400450 // Get the script context by traversing the context chain.
451 Context* script_context();
Steve Blocka7e24c12009-10-30 11:49:00 +0000452
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000453 // Compute the native context.
454 inline Context* native_context();
455 inline void set_native_context(Context* context);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000456
457 // Predicates for context types. IsNativeContext is also defined on Object
458 // because we frequently have to know if arbitrary objects are natives
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000459 // contexts.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000460 inline bool IsNativeContext();
461 inline bool IsFunctionContext();
462 inline bool IsCatchContext();
463 inline bool IsWithContext();
Ben Murdochda12d292016-06-02 14:46:10 +0100464 inline bool IsDebugEvaluateContext();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000465 inline bool IsBlockContext();
466 inline bool IsModuleContext();
467 inline bool IsScriptContext();
Steve Blocka7e24c12009-10-30 11:49:00 +0000468
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000469 inline bool HasSameSecurityTokenAs(Context* that);
470
471 // Initializes global variable bindings in given script context.
472 void InitializeGlobalSlots();
Steve Blocka7e24c12009-10-30 11:49:00 +0000473
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000474 // A native context holds a list of all functions with optimized code.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100475 void AddOptimizedFunction(JSFunction* function);
476 void RemoveOptimizedFunction(JSFunction* function);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000477 void SetOptimizedFunctionsListHead(Object* head);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100478 Object* OptimizedFunctionsListHead();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100479
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000480 // The native context also stores a list of all optimized code and a
481 // list of all deoptimized code, which are needed by the deoptimizer.
482 void AddOptimizedCode(Code* code);
483 void SetOptimizedCodeListHead(Object* head);
484 Object* OptimizedCodeListHead();
485 void SetDeoptimizedCodeListHead(Object* head);
486 Object* DeoptimizedCodeListHead();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100487
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000488 Handle<Object> ErrorMessageForCodeGenerationFromStrings();
489
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000490 static int ImportedFieldIndexForName(Handle<String> name);
491 static int IntrinsicIndexForName(Handle<String> name);
492
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000493#define NATIVE_CONTEXT_FIELD_ACCESSORS(index, type, name) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000494 inline void set_##name(type* value); \
495 inline bool is_##name(type* value); \
496 inline type* name();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000497 NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_FIELD_ACCESSORS)
498#undef NATIVE_CONTEXT_FIELD_ACCESSORS
Steve Blocka7e24c12009-10-30 11:49:00 +0000499
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000500 // Lookup the slot called name, starting with the current context.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100501 // There are three possibilities:
Steve Blocka7e24c12009-10-30 11:49:00 +0000502 //
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100503 // 1) result->IsContext():
504 // The binding was found in a context. *index is always the
505 // non-negative slot index. *attributes is NONE for var and let
506 // declarations, READ_ONLY for const declarations (never ABSENT).
Steve Blocka7e24c12009-10-30 11:49:00 +0000507 //
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100508 // 2) result->IsJSObject():
509 // The binding was found as a named property in a context extension
510 // object (i.e., was introduced via eval), as a property on the subject
511 // of with, or as a property of the global object. *index is -1 and
512 // *attributes is not ABSENT.
Steve Blocka7e24c12009-10-30 11:49:00 +0000513 //
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100514 // 3) result.is_null():
515 // There was no binding found, *index is always -1 and *attributes is
516 // always ABSENT.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000517 Handle<Object> Lookup(Handle<String> name,
518 ContextLookupFlags flags,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100519 int* index,
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000520 PropertyAttributes* attributes,
521 BindingFlags* binding_flags);
Steve Blocka7e24c12009-10-30 11:49:00 +0000522
Steve Blocka7e24c12009-10-30 11:49:00 +0000523 // Code generation support.
524 static int SlotOffset(int index) {
525 return kHeaderSize + index * kPointerSize - kHeapObjectTag;
526 }
527
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000528 static int FunctionMapIndex(LanguageMode language_mode, FunctionKind kind) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000529 if (IsGeneratorFunction(kind)) {
Ben Murdochda12d292016-06-02 14:46:10 +0100530 return is_strict(language_mode) ? STRICT_GENERATOR_FUNCTION_MAP_INDEX
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000531 : SLOPPY_GENERATOR_FUNCTION_MAP_INDEX;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000532 }
533
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000534 if (IsClassConstructor(kind)) {
535 // Use strict function map (no own "caller" / "arguments")
Ben Murdochda12d292016-06-02 14:46:10 +0100536 return STRICT_FUNCTION_MAP_INDEX;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000537 }
538
539 if (IsArrowFunction(kind) || IsConciseMethod(kind) ||
540 IsAccessorFunction(kind)) {
Ben Murdochda12d292016-06-02 14:46:10 +0100541 return STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000542 }
543
Ben Murdochda12d292016-06-02 14:46:10 +0100544 return is_strict(language_mode) ? STRICT_FUNCTION_MAP_INDEX
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000545 : SLOPPY_FUNCTION_MAP_INDEX;
546 }
547
Ben Murdochda12d292016-06-02 14:46:10 +0100548 static int ArrayMapIndex(ElementsKind elements_kind) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000549 DCHECK(IsFastElementsKind(elements_kind));
Ben Murdochda12d292016-06-02 14:46:10 +0100550 return elements_kind + FIRST_JS_ARRAY_MAP_SLOT;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000551 }
552
553 static const int kSize = kHeaderSize + NATIVE_CONTEXT_SLOTS * kPointerSize;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000554 static const int kNotFound = -1;
Ben Murdochf87a2032010-10-22 12:50:53 +0100555
556 // GC support.
557 typedef FixedBodyDescriptor<
558 kHeaderSize, kSize, kSize> ScavengeBodyDescriptor;
559
560 typedef FixedBodyDescriptor<
561 kHeaderSize,
562 kHeaderSize + FIRST_WEAK_SLOT * kPointerSize,
563 kSize> MarkCompactBodyDescriptor;
564
Steve Blocka7e24c12009-10-30 11:49:00 +0000565 private:
Steve Blocka7e24c12009-10-30 11:49:00 +0000566#ifdef DEBUG
567 // Bootstrapping-aware type checks.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000568 static bool IsBootstrappingOrNativeContext(Isolate* isolate, Object* object);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000569 static bool IsBootstrappingOrValidParentContext(Object* object, Context* kid);
Steve Blocka7e24c12009-10-30 11:49:00 +0000570#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000571
572 STATIC_ASSERT(kHeaderSize == Internals::kContextHeaderSize);
573 STATIC_ASSERT(EMBEDDER_DATA_INDEX == Internals::kContextEmbedderDataIndex);
Steve Blocka7e24c12009-10-30 11:49:00 +0000574};
575
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000576} // namespace internal
577} // namespace v8
Steve Blocka7e24c12009-10-30 11:49:00 +0000578
579#endif // V8_CONTEXTS_H_