blob: cd3ff1421177fa9e41b691c5b0030ae91b195dfc [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 {
16 FOLLOW_CONTEXT_CHAIN = 1,
17 FOLLOW_PROTOTYPE_CHAIN = 2,
18
19 DONT_FOLLOW_CHAINS = 0,
20 FOLLOW_CHAINS = FOLLOW_CONTEXT_CHAIN | FOLLOW_PROTOTYPE_CHAIN
21};
22
23
Ben Murdoch69a99ed2011-11-30 16:03:39 +000024// ES5 10.2 defines lexical environments with mutable and immutable bindings.
25// Immutable bindings have two states, initialized and uninitialized, and
Ben Murdoch3ef787d2012-04-12 10:51:47 +010026// their state is changed by the InitializeImmutableBinding method. The
27// BindingFlags enum represents information if a binding has definitely been
28// initialized. A mutable binding does not need to be checked and thus has
29// the BindingFlag MUTABLE_IS_INITIALIZED.
30//
31// There are two possibilities for immutable bindings
32// * 'const' declared variables. They are initialized when evaluating the
33// corresponding declaration statement. They need to be checked for being
34// initialized and thus get the flag IMMUTABLE_CHECK_INITIALIZED.
35// * The function name of a named function literal. The binding is immediately
36// initialized when entering the function and thus does not need to be
37// checked. it gets the BindingFlag IMMUTABLE_IS_INITIALIZED.
38// Accessing an uninitialized binding produces the undefined value.
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
44// initialized and thus get the flag MUTABLE_CHECK_INITIALIZED.
45// * A 'var' declared variable. It is initialized immediately upon creation
46// and thus doesn't need to be checked. It gets the flag
47// MUTABLE_IS_INITIALIZED.
48// * Catch bound variables, function parameters and variables introduced by
49// function declarations are initialized immediately and do not need to be
50// checked. Thus they get the flag MUTABLE_IS_INITIALIZED.
51// Immutable bindings in harmony mode get the _HARMONY flag variants. Accessing
52// an uninitialized binding produces a reference error.
53//
54// In V8 uninitialized bindings are set to the hole value upon creation and set
55// to a different value upon initialization.
Ben Murdoch69a99ed2011-11-30 16:03:39 +000056enum BindingFlags {
57 MUTABLE_IS_INITIALIZED,
58 MUTABLE_CHECK_INITIALIZED,
59 IMMUTABLE_IS_INITIALIZED,
60 IMMUTABLE_CHECK_INITIALIZED,
Ben Murdoch3ef787d2012-04-12 10:51:47 +010061 IMMUTABLE_IS_INITIALIZED_HARMONY,
62 IMMUTABLE_CHECK_INITIALIZED_HARMONY,
Ben Murdoch69a99ed2011-11-30 16:03:39 +000063 MISSING_BINDING
64};
65
66
Steve Blocka7e24c12009-10-30 11:49:00 +000067// Heap-allocated activation contexts.
68//
69// Contexts are implemented as FixedArray objects; the Context
70// class is a convenience interface casted on a FixedArray object.
71//
72// Note: Context must have no virtual functions and Context objects
73// must always be allocated via Heap::AllocateContext() or
74// Factory::NewContext.
75
Ben Murdochb8a8cc12014-11-26 15:28:44 +000076#define NATIVE_CONTEXT_FIELDS(V) \
77 V(GLOBAL_PROXY_INDEX, JSObject, global_proxy_object) \
78 V(SECURITY_TOKEN_INDEX, Object, security_token) \
79 V(BOOLEAN_FUNCTION_INDEX, JSFunction, boolean_function) \
80 V(NUMBER_FUNCTION_INDEX, JSFunction, number_function) \
81 V(STRING_FUNCTION_INDEX, JSFunction, string_function) \
82 V(STRING_FUNCTION_PROTOTYPE_MAP_INDEX, Map, string_function_prototype_map) \
83 V(SYMBOL_FUNCTION_INDEX, JSFunction, symbol_function) \
84 V(OBJECT_FUNCTION_INDEX, JSFunction, object_function) \
85 V(INTERNAL_ARRAY_FUNCTION_INDEX, JSFunction, internal_array_function) \
86 V(ARRAY_FUNCTION_INDEX, JSFunction, array_function) \
87 V(JS_ARRAY_MAPS_INDEX, Object, js_array_maps) \
88 V(DATE_FUNCTION_INDEX, JSFunction, date_function) \
89 V(JSON_OBJECT_INDEX, JSObject, json_object) \
90 V(REGEXP_FUNCTION_INDEX, JSFunction, regexp_function) \
91 V(INITIAL_OBJECT_PROTOTYPE_INDEX, JSObject, initial_object_prototype) \
92 V(INITIAL_ARRAY_PROTOTYPE_INDEX, JSObject, initial_array_prototype) \
93 V(CREATE_DATE_FUN_INDEX, JSFunction, create_date_fun) \
94 V(TO_NUMBER_FUN_INDEX, JSFunction, to_number_fun) \
95 V(TO_STRING_FUN_INDEX, JSFunction, to_string_fun) \
96 V(TO_DETAIL_STRING_FUN_INDEX, JSFunction, to_detail_string_fun) \
97 V(TO_OBJECT_FUN_INDEX, JSFunction, to_object_fun) \
98 V(TO_INTEGER_FUN_INDEX, JSFunction, to_integer_fun) \
99 V(TO_UINT32_FUN_INDEX, JSFunction, to_uint32_fun) \
100 V(TO_INT32_FUN_INDEX, JSFunction, to_int32_fun) \
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400101 V(TO_LENGTH_FUN_INDEX, JSFunction, to_length_fun) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000102 V(GLOBAL_EVAL_FUN_INDEX, JSFunction, global_eval_fun) \
103 V(INSTANTIATE_FUN_INDEX, JSFunction, instantiate_fun) \
104 V(CONFIGURE_INSTANCE_FUN_INDEX, JSFunction, configure_instance_fun) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000105 V(ARRAY_BUFFER_FUN_INDEX, JSFunction, array_buffer_fun) \
106 V(UINT8_ARRAY_FUN_INDEX, JSFunction, uint8_array_fun) \
107 V(INT8_ARRAY_FUN_INDEX, JSFunction, int8_array_fun) \
108 V(UINT16_ARRAY_FUN_INDEX, JSFunction, uint16_array_fun) \
109 V(INT16_ARRAY_FUN_INDEX, JSFunction, int16_array_fun) \
110 V(UINT32_ARRAY_FUN_INDEX, JSFunction, uint32_array_fun) \
111 V(INT32_ARRAY_FUN_INDEX, JSFunction, int32_array_fun) \
112 V(FLOAT32_ARRAY_FUN_INDEX, JSFunction, float32_array_fun) \
113 V(FLOAT64_ARRAY_FUN_INDEX, JSFunction, float64_array_fun) \
114 V(UINT8_CLAMPED_ARRAY_FUN_INDEX, JSFunction, uint8_clamped_array_fun) \
115 V(INT8_ARRAY_EXTERNAL_MAP_INDEX, Map, int8_array_external_map) \
116 V(UINT8_ARRAY_EXTERNAL_MAP_INDEX, Map, uint8_array_external_map) \
117 V(INT16_ARRAY_EXTERNAL_MAP_INDEX, Map, int16_array_external_map) \
118 V(UINT16_ARRAY_EXTERNAL_MAP_INDEX, Map, uint16_array_external_map) \
119 V(INT32_ARRAY_EXTERNAL_MAP_INDEX, Map, int32_array_external_map) \
120 V(UINT32_ARRAY_EXTERNAL_MAP_INDEX, Map, uint32_array_external_map) \
121 V(FLOAT32_ARRAY_EXTERNAL_MAP_INDEX, Map, float32_array_external_map) \
122 V(FLOAT64_ARRAY_EXTERNAL_MAP_INDEX, Map, float64_array_external_map) \
123 V(UINT8_CLAMPED_ARRAY_EXTERNAL_MAP_INDEX, Map, \
124 uint8_clamped_array_external_map) \
125 V(DATA_VIEW_FUN_INDEX, JSFunction, data_view_fun) \
126 V(SLOPPY_FUNCTION_MAP_INDEX, Map, sloppy_function_map) \
127 V(SLOPPY_FUNCTION_WITH_READONLY_PROTOTYPE_MAP_INDEX, Map, \
128 sloppy_function_with_readonly_prototype_map) \
129 V(STRICT_FUNCTION_MAP_INDEX, Map, strict_function_map) \
130 V(SLOPPY_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map, \
131 sloppy_function_without_prototype_map) \
132 V(STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map, \
133 strict_function_without_prototype_map) \
134 V(BOUND_FUNCTION_MAP_INDEX, Map, bound_function_map) \
135 V(REGEXP_RESULT_MAP_INDEX, Map, regexp_result_map) \
136 V(SLOPPY_ARGUMENTS_MAP_INDEX, Map, sloppy_arguments_map) \
137 V(ALIASED_ARGUMENTS_MAP_INDEX, Map, aliased_arguments_map) \
138 V(STRICT_ARGUMENTS_MAP_INDEX, Map, strict_arguments_map) \
139 V(MESSAGE_LISTENERS_INDEX, JSObject, message_listeners) \
140 V(MAKE_MESSAGE_FUN_INDEX, JSFunction, make_message_fun) \
141 V(GET_STACK_TRACE_LINE_INDEX, JSFunction, get_stack_trace_line_fun) \
142 V(CONFIGURE_GLOBAL_INDEX, JSFunction, configure_global_fun) \
143 V(FUNCTION_CACHE_INDEX, JSObject, function_cache) \
144 V(JSFUNCTION_RESULT_CACHES_INDEX, FixedArray, jsfunction_result_caches) \
145 V(NORMALIZED_MAP_CACHE_INDEX, Object, normalized_map_cache) \
146 V(RUNTIME_CONTEXT_INDEX, Context, runtime_context) \
147 V(CALL_AS_FUNCTION_DELEGATE_INDEX, JSFunction, call_as_function_delegate) \
148 V(CALL_AS_CONSTRUCTOR_DELEGATE_INDEX, JSFunction, \
149 call_as_constructor_delegate) \
150 V(SCRIPT_FUNCTION_INDEX, JSFunction, script_function) \
151 V(OPAQUE_REFERENCE_FUNCTION_INDEX, JSFunction, opaque_reference_function) \
152 V(CONTEXT_EXTENSION_FUNCTION_INDEX, JSFunction, context_extension_function) \
153 V(MAP_CACHE_INDEX, Object, map_cache) \
154 V(EMBEDDER_DATA_INDEX, FixedArray, embedder_data) \
155 V(ALLOW_CODE_GEN_FROM_STRINGS_INDEX, Object, allow_code_gen_from_strings) \
156 V(ERROR_MESSAGE_FOR_CODE_GEN_FROM_STRINGS_INDEX, Object, \
157 error_message_for_code_gen_from_strings) \
158 V(IS_PROMISE_INDEX, JSFunction, is_promise) \
159 V(PROMISE_CREATE_INDEX, JSFunction, promise_create) \
160 V(PROMISE_RESOLVE_INDEX, JSFunction, promise_resolve) \
161 V(PROMISE_REJECT_INDEX, JSFunction, promise_reject) \
162 V(PROMISE_CHAIN_INDEX, JSFunction, promise_chain) \
163 V(PROMISE_CATCH_INDEX, JSFunction, promise_catch) \
164 V(PROMISE_THEN_INDEX, JSFunction, promise_then) \
165 V(TO_COMPLETE_PROPERTY_DESCRIPTOR_INDEX, JSFunction, \
166 to_complete_property_descriptor) \
167 V(DERIVED_HAS_TRAP_INDEX, JSFunction, derived_has_trap) \
168 V(DERIVED_GET_TRAP_INDEX, JSFunction, derived_get_trap) \
169 V(DERIVED_SET_TRAP_INDEX, JSFunction, derived_set_trap) \
170 V(PROXY_ENUMERATE_INDEX, JSFunction, proxy_enumerate) \
171 V(OBSERVERS_NOTIFY_CHANGE_INDEX, JSFunction, observers_notify_change) \
172 V(OBSERVERS_ENQUEUE_SPLICE_INDEX, JSFunction, observers_enqueue_splice) \
173 V(OBSERVERS_BEGIN_SPLICE_INDEX, JSFunction, observers_begin_perform_splice) \
174 V(OBSERVERS_END_SPLICE_INDEX, JSFunction, observers_end_perform_splice) \
175 V(NATIVE_OBJECT_OBSERVE_INDEX, JSFunction, native_object_observe) \
176 V(NATIVE_OBJECT_GET_NOTIFIER_INDEX, JSFunction, native_object_get_notifier) \
177 V(NATIVE_OBJECT_NOTIFIER_PERFORM_CHANGE, JSFunction, \
178 native_object_notifier_perform_change) \
179 V(SLOPPY_GENERATOR_FUNCTION_MAP_INDEX, Map, sloppy_generator_function_map) \
180 V(STRICT_GENERATOR_FUNCTION_MAP_INDEX, Map, strict_generator_function_map) \
181 V(GENERATOR_OBJECT_PROTOTYPE_MAP_INDEX, Map, generator_object_prototype_map) \
182 V(ITERATOR_RESULT_MAP_INDEX, Map, iterator_result_map) \
183 V(MAP_ITERATOR_MAP_INDEX, Map, map_iterator_map) \
184 V(SET_ITERATOR_MAP_INDEX, Map, set_iterator_map) \
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400185 V(ARRAY_VALUES_ITERATOR_INDEX, JSFunction, array_values_iterator) \
186 V(SCRIPT_CONTEXT_TABLE_INDEX, ScriptContextTable, script_context_table)
187
188
189// A table of all script contexts. Every loaded top-level script with top-level
190// lexical declarations contributes its ScriptContext into this table.
191//
192// The table is a fixed array, its first slot is the current used count and
193// the subsequent slots 1..used contain ScriptContexts.
194class ScriptContextTable : public FixedArray {
195 public:
196 // Conversions.
197 static ScriptContextTable* cast(Object* context) {
198 DCHECK(context->IsScriptContextTable());
199 return reinterpret_cast<ScriptContextTable*>(context);
200 }
201
202 struct LookupResult {
203 int context_index;
204 int slot_index;
205 VariableMode mode;
206 InitializationFlag init_flag;
207 MaybeAssignedFlag maybe_assigned_flag;
208 };
209
210 int used() const { return Smi::cast(get(kUsedSlot))->value(); }
211
212 void set_used(int used) { set(kUsedSlot, Smi::FromInt(used)); }
213
214 static Handle<Context> GetContext(Handle<ScriptContextTable> table, int i) {
215 DCHECK(i < table->used());
216 return Handle<Context>::cast(FixedArray::get(table, i + 1));
217 }
218
219 // Lookup a variable `name` in a ScriptContextTable.
220 // If it returns true, the variable is found and `result` contains
221 // valid information about its location.
222 // If it returns false, `result` is untouched.
223 MUST_USE_RESULT
224 static bool Lookup(Handle<ScriptContextTable> table, Handle<String> name,
225 LookupResult* result);
226
227 MUST_USE_RESULT
228 static Handle<ScriptContextTable> Extend(Handle<ScriptContextTable> table,
229 Handle<Context> script_context);
230
231 static int GetContextOffset(int context_index) {
232 return kFirstContextOffset + context_index * kPointerSize;
233 }
234
235 private:
236 static const int kUsedSlot = 0;
237 static const int kFirstContextOffset =
238 FixedArray::kHeaderSize + (kUsedSlot + 1) * kPointerSize;
239
240 DISALLOW_IMPLICIT_CONSTRUCTORS(ScriptContextTable);
241};
Steve Blocka7e24c12009-10-30 11:49:00 +0000242
243// JSFunctions are pairs (context, function code), sometimes also called
244// closures. A Context object is used to represent function contexts and
245// dynamically pushed 'with' contexts (or 'scopes' in ECMA-262 speak).
246//
247// At runtime, the contexts build a stack in parallel to the execution
248// stack, with the top-most context being the current context. All contexts
249// have the following slots:
250//
251// [ closure ] This is the current function. It is the same for all
252// contexts inside a function. It provides access to the
253// incoming context (i.e., the outer context, which may
254// or may not become the current function's context), and
255// it provides access to the functions code and thus it's
256// scope information, which in turn contains the names of
257// statically allocated context slots. The names are needed
258// for dynamic lookups in the presence of 'with' or 'eval'.
259//
Steve Blocka7e24c12009-10-30 11:49:00 +0000260// [ previous ] A pointer to the previous context. It is NULL for
261// function contexts, and non-NULL for 'with' contexts.
262// Used to implement the 'with' statement.
263//
264// [ extension ] A pointer to an extension JSObject, or NULL. Used to
265// implement 'with' statements and dynamic declarations
266// (through 'eval'). The object in a 'with' statement is
267// stored in the extension slot of a 'with' context.
268// Dynamically declared variables/functions are also added
269// to lazily allocated extension object. Context::Lookup
270// searches the extension object for properties.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000271// For global and block contexts, contains the respective
272// ScopeInfo.
273// For module contexts, points back to the respective JSModule.
Steve Blocka7e24c12009-10-30 11:49:00 +0000274//
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000275// [ global_object ] A pointer to the global object. Provided for quick
Steve Blocka7e24c12009-10-30 11:49:00 +0000276// access to the global object from inside the code (since
277// we always have a context pointer).
278//
279// In addition, function contexts may have statically allocated context slots
280// to store local variables/functions that are accessed from inner functions
281// (via static context addresses) or through 'eval' (dynamic context lookups).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000282// The native context contains additional slots for fast access to native
283// properties.
284//
285// Finally, with Harmony scoping, the JSFunction representing a top level
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400286// script will have the ScriptContext rather than a FunctionContext.
287// Script contexts from all top-level scripts are gathered in
288// ScriptContextTable.
Steve Blocka7e24c12009-10-30 11:49:00 +0000289
290class Context: public FixedArray {
291 public:
292 // Conversions.
293 static Context* cast(Object* context) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000294 DCHECK(context->IsContext());
Steve Blocka7e24c12009-10-30 11:49:00 +0000295 return reinterpret_cast<Context*>(context);
296 }
297
298 // The default context slot layout; indices are FixedArray slot indices.
299 enum {
300 // These slots are in all contexts.
301 CLOSURE_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000302 PREVIOUS_INDEX,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000303 // The extension slot is used for either the global object (in global
304 // contexts), eval extension object (function contexts), subject of with
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100305 // (with contexts), or the variable name (catch contexts), the serialized
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000306 // scope info (block contexts), or the module instance (module contexts).
Steve Blocka7e24c12009-10-30 11:49:00 +0000307 EXTENSION_INDEX,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000308 GLOBAL_OBJECT_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000309 MIN_CONTEXT_SLOTS,
310
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000311 // This slot holds the thrown value in catch contexts.
312 THROWN_OBJECT_INDEX = MIN_CONTEXT_SLOTS,
313
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000314 // These slots are only in native contexts.
Steve Blocka7e24c12009-10-30 11:49:00 +0000315 GLOBAL_PROXY_INDEX = MIN_CONTEXT_SLOTS,
316 SECURITY_TOKEN_INDEX,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000317 SLOPPY_ARGUMENTS_MAP_INDEX,
318 ALIASED_ARGUMENTS_MAP_INDEX,
319 STRICT_ARGUMENTS_MAP_INDEX,
Steve Block6ded16b2010-05-10 14:33:55 +0100320 REGEXP_RESULT_MAP_INDEX,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000321 SLOPPY_FUNCTION_MAP_INDEX,
322 SLOPPY_FUNCTION_WITH_READONLY_PROTOTYPE_MAP_INDEX,
323 STRICT_FUNCTION_MAP_INDEX,
324 SLOPPY_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX,
325 STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX,
326 BOUND_FUNCTION_MAP_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000327 INITIAL_OBJECT_PROTOTYPE_INDEX,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000328 INITIAL_ARRAY_PROTOTYPE_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000329 BOOLEAN_FUNCTION_INDEX,
330 NUMBER_FUNCTION_INDEX,
331 STRING_FUNCTION_INDEX,
Iain Merrick75681382010-08-19 15:07:18 +0100332 STRING_FUNCTION_PROTOTYPE_MAP_INDEX,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000333 SYMBOL_FUNCTION_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000334 OBJECT_FUNCTION_INDEX,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100335 INTERNAL_ARRAY_FUNCTION_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000336 ARRAY_FUNCTION_INDEX,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000337 JS_ARRAY_MAPS_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000338 DATE_FUNCTION_INDEX,
339 JSON_OBJECT_INDEX,
340 REGEXP_FUNCTION_INDEX,
341 CREATE_DATE_FUN_INDEX,
342 TO_NUMBER_FUN_INDEX,
343 TO_STRING_FUN_INDEX,
344 TO_DETAIL_STRING_FUN_INDEX,
345 TO_OBJECT_FUN_INDEX,
346 TO_INTEGER_FUN_INDEX,
347 TO_UINT32_FUN_INDEX,
348 TO_INT32_FUN_INDEX,
349 TO_BOOLEAN_FUN_INDEX,
Leon Clarkee46be812010-01-19 14:06:41 +0000350 GLOBAL_EVAL_FUN_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000351 INSTANTIATE_FUN_INDEX,
352 CONFIGURE_INSTANCE_FUN_INDEX,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000353 ARRAY_BUFFER_FUN_INDEX,
354 UINT8_ARRAY_FUN_INDEX,
355 INT8_ARRAY_FUN_INDEX,
356 UINT16_ARRAY_FUN_INDEX,
357 INT16_ARRAY_FUN_INDEX,
358 UINT32_ARRAY_FUN_INDEX,
359 INT32_ARRAY_FUN_INDEX,
360 FLOAT32_ARRAY_FUN_INDEX,
361 FLOAT64_ARRAY_FUN_INDEX,
362 UINT8_CLAMPED_ARRAY_FUN_INDEX,
363 INT8_ARRAY_EXTERNAL_MAP_INDEX,
364 UINT8_ARRAY_EXTERNAL_MAP_INDEX,
365 INT16_ARRAY_EXTERNAL_MAP_INDEX,
366 UINT16_ARRAY_EXTERNAL_MAP_INDEX,
367 INT32_ARRAY_EXTERNAL_MAP_INDEX,
368 UINT32_ARRAY_EXTERNAL_MAP_INDEX,
369 FLOAT32_ARRAY_EXTERNAL_MAP_INDEX,
370 FLOAT64_ARRAY_EXTERNAL_MAP_INDEX,
371 UINT8_CLAMPED_ARRAY_EXTERNAL_MAP_INDEX,
372 DATA_VIEW_FUN_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000373 MESSAGE_LISTENERS_INDEX,
374 MAKE_MESSAGE_FUN_INDEX,
375 GET_STACK_TRACE_LINE_INDEX,
376 CONFIGURE_GLOBAL_INDEX,
377 FUNCTION_CACHE_INDEX,
Steve Block6ded16b2010-05-10 14:33:55 +0100378 JSFUNCTION_RESULT_CACHES_INDEX,
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100379 NORMALIZED_MAP_CACHE_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000380 RUNTIME_CONTEXT_INDEX,
381 CALL_AS_FUNCTION_DELEGATE_INDEX,
382 CALL_AS_CONSTRUCTOR_DELEGATE_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000383 SCRIPT_FUNCTION_INDEX,
Steve Block6ded16b2010-05-10 14:33:55 +0100384 OPAQUE_REFERENCE_FUNCTION_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000385 CONTEXT_EXTENSION_FUNCTION_INDEX,
386 OUT_OF_MEMORY_INDEX,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000387 EMBEDDER_DATA_INDEX,
Ben Murdoch257744e2011-11-30 15:57:28 +0000388 ALLOW_CODE_GEN_FROM_STRINGS_INDEX,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000389 ERROR_MESSAGE_FOR_CODE_GEN_FROM_STRINGS_INDEX,
390 RUN_MICROTASKS_INDEX,
391 ENQUEUE_MICROTASK_INDEX,
392 IS_PROMISE_INDEX,
393 PROMISE_CREATE_INDEX,
394 PROMISE_RESOLVE_INDEX,
395 PROMISE_REJECT_INDEX,
396 PROMISE_CHAIN_INDEX,
397 PROMISE_CATCH_INDEX,
398 PROMISE_THEN_INDEX,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100399 TO_COMPLETE_PROPERTY_DESCRIPTOR_INDEX,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000400 DERIVED_HAS_TRAP_INDEX,
Ben Murdoch257744e2011-11-30 15:57:28 +0000401 DERIVED_GET_TRAP_INDEX,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000402 DERIVED_SET_TRAP_INDEX,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000403 PROXY_ENUMERATE_INDEX,
404 OBSERVERS_NOTIFY_CHANGE_INDEX,
405 OBSERVERS_ENQUEUE_SPLICE_INDEX,
406 OBSERVERS_BEGIN_SPLICE_INDEX,
407 OBSERVERS_END_SPLICE_INDEX,
408 NATIVE_OBJECT_OBSERVE_INDEX,
409 NATIVE_OBJECT_GET_NOTIFIER_INDEX,
410 NATIVE_OBJECT_NOTIFIER_PERFORM_CHANGE,
411 SLOPPY_GENERATOR_FUNCTION_MAP_INDEX,
412 STRICT_GENERATOR_FUNCTION_MAP_INDEX,
413 GENERATOR_OBJECT_PROTOTYPE_MAP_INDEX,
414 ITERATOR_RESULT_MAP_INDEX,
415 MAP_ITERATOR_MAP_INDEX,
416 SET_ITERATOR_MAP_INDEX,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000417 ARRAY_VALUES_ITERATOR_INDEX,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400418 SCRIPT_CONTEXT_TABLE_INDEX,
419 MAP_CACHE_INDEX,
420 TO_LENGTH_FUN_INDEX,
Ben Murdochf87a2032010-10-22 12:50:53 +0100421
422 // Properties from here are treated as weak references by the full GC.
423 // Scavenge treats them as strong references.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100424 OPTIMIZED_FUNCTIONS_LIST, // Weak.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000425 OPTIMIZED_CODE_LIST, // Weak.
426 DEOPTIMIZED_CODE_LIST, // Weak.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000427 NEXT_CONTEXT_LINK, // Weak.
Ben Murdochf87a2032010-10-22 12:50:53 +0100428
429 // Total number of slots.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000430 NATIVE_CONTEXT_SLOTS,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100431 FIRST_WEAK_SLOT = OPTIMIZED_FUNCTIONS_LIST
Steve Blocka7e24c12009-10-30 11:49:00 +0000432 };
433
434 // Direct slot access.
435 JSFunction* closure() { return JSFunction::cast(get(CLOSURE_INDEX)); }
436 void set_closure(JSFunction* closure) { set(CLOSURE_INDEX, closure); }
437
Steve Blocka7e24c12009-10-30 11:49:00 +0000438 Context* previous() {
439 Object* result = unchecked_previous();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000440 DCHECK(IsBootstrappingOrValidParentContext(result, this));
Steve Blocka7e24c12009-10-30 11:49:00 +0000441 return reinterpret_cast<Context*>(result);
442 }
443 void set_previous(Context* context) { set(PREVIOUS_INDEX, context); }
444
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000445 bool has_extension() { return extension() != NULL; }
446 Object* extension() { return get(EXTENSION_INDEX); }
447 void set_extension(Object* object) { set(EXTENSION_INDEX, object); }
448
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000449 JSModule* module() { return JSModule::cast(get(EXTENSION_INDEX)); }
450 void set_module(JSModule* module) { set(EXTENSION_INDEX, module); }
451
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000452 // Get the context where var declarations will be hoisted to, which
453 // may be the context itself.
454 Context* declaration_context();
Steve Blocka7e24c12009-10-30 11:49:00 +0000455
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000456 GlobalObject* global_object() {
457 Object* result = get(GLOBAL_OBJECT_INDEX);
458 DCHECK(IsBootstrappingOrGlobalObject(this->GetIsolate(), result));
Steve Blocka7e24c12009-10-30 11:49:00 +0000459 return reinterpret_cast<GlobalObject*>(result);
460 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000461 void set_global_object(GlobalObject* object) {
462 set(GLOBAL_OBJECT_INDEX, object);
463 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000464
465 // Returns a JSGlobalProxy object or null.
466 JSObject* global_proxy();
467 void set_global_proxy(JSObject* global);
468
469 // The builtins object.
470 JSBuiltinsObject* builtins();
471
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400472 // Get the script context by traversing the context chain.
473 Context* script_context();
Steve Blocka7e24c12009-10-30 11:49:00 +0000474
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000475 // Compute the native context by traversing the context chain.
476 Context* native_context();
477
478 // Predicates for context types. IsNativeContext is also defined on Object
479 // because we frequently have to know if arbitrary objects are natives
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000480 // contexts.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000481 bool IsNativeContext() {
482 Map* map = this->map();
483 return map == map->GetHeap()->native_context_map();
484 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000485 bool IsFunctionContext() {
486 Map* map = this->map();
487 return map == map->GetHeap()->function_context_map();
488 }
489 bool IsCatchContext() {
490 Map* map = this->map();
491 return map == map->GetHeap()->catch_context_map();
492 }
493 bool IsWithContext() {
494 Map* map = this->map();
495 return map == map->GetHeap()->with_context_map();
496 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000497 bool IsBlockContext() {
498 Map* map = this->map();
499 return map == map->GetHeap()->block_context_map();
500 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100501 bool IsModuleContext() {
502 Map* map = this->map();
503 return map == map->GetHeap()->module_context_map();
504 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400505 bool IsScriptContext() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000506 Map* map = this->map();
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400507 return map == map->GetHeap()->script_context_map();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000508 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000509
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000510 bool HasSameSecurityTokenAs(Context* that) {
511 return this->global_object()->native_context()->security_token() ==
512 that->global_object()->native_context()->security_token();
513 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000514
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000515 // A native context holds a list of all functions with optimized code.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100516 void AddOptimizedFunction(JSFunction* function);
517 void RemoveOptimizedFunction(JSFunction* function);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000518 void SetOptimizedFunctionsListHead(Object* head);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100519 Object* OptimizedFunctionsListHead();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100520
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000521 // The native context also stores a list of all optimized code and a
522 // list of all deoptimized code, which are needed by the deoptimizer.
523 void AddOptimizedCode(Code* code);
524 void SetOptimizedCodeListHead(Object* head);
525 Object* OptimizedCodeListHead();
526 void SetDeoptimizedCodeListHead(Object* head);
527 Object* DeoptimizedCodeListHead();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100528
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000529 Handle<Object> ErrorMessageForCodeGenerationFromStrings();
530
531#define NATIVE_CONTEXT_FIELD_ACCESSORS(index, type, name) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000532 void set_##name(type* value) { \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000533 DCHECK(IsNativeContext()); \
Steve Blocka7e24c12009-10-30 11:49:00 +0000534 set(index, value); \
535 } \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000536 bool is_##name(type* value) { \
537 DCHECK(IsNativeContext()); \
538 return type::cast(get(index)) == value; \
539 } \
Steve Blocka7e24c12009-10-30 11:49:00 +0000540 type* name() { \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000541 DCHECK(IsNativeContext()); \
Steve Blocka7e24c12009-10-30 11:49:00 +0000542 return type::cast(get(index)); \
543 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000544 NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_FIELD_ACCESSORS)
545#undef NATIVE_CONTEXT_FIELD_ACCESSORS
Steve Blocka7e24c12009-10-30 11:49:00 +0000546
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000547 // Lookup the slot called name, starting with the current context.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100548 // There are three possibilities:
Steve Blocka7e24c12009-10-30 11:49:00 +0000549 //
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100550 // 1) result->IsContext():
551 // The binding was found in a context. *index is always the
552 // non-negative slot index. *attributes is NONE for var and let
553 // declarations, READ_ONLY for const declarations (never ABSENT).
Steve Blocka7e24c12009-10-30 11:49:00 +0000554 //
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100555 // 2) result->IsJSObject():
556 // The binding was found as a named property in a context extension
557 // object (i.e., was introduced via eval), as a property on the subject
558 // of with, or as a property of the global object. *index is -1 and
559 // *attributes is not ABSENT.
Steve Blocka7e24c12009-10-30 11:49:00 +0000560 //
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100561 // 3) result.is_null():
562 // There was no binding found, *index is always -1 and *attributes is
563 // always ABSENT.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000564 Handle<Object> Lookup(Handle<String> name,
565 ContextLookupFlags flags,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100566 int* index,
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000567 PropertyAttributes* attributes,
568 BindingFlags* binding_flags);
Steve Blocka7e24c12009-10-30 11:49:00 +0000569
Steve Blocka7e24c12009-10-30 11:49:00 +0000570 // Code generation support.
571 static int SlotOffset(int index) {
572 return kHeaderSize + index * kPointerSize - kHeapObjectTag;
573 }
574
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000575 static int FunctionMapIndex(StrictMode strict_mode, FunctionKind kind) {
576 if (IsGeneratorFunction(kind)) {
577 return strict_mode == SLOPPY ? SLOPPY_GENERATOR_FUNCTION_MAP_INDEX
578 : STRICT_GENERATOR_FUNCTION_MAP_INDEX;
579 }
580
581 if (IsArrowFunction(kind) || IsConciseMethod(kind)) {
582 return strict_mode == SLOPPY
583 ? SLOPPY_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX
584 : STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX;
585 }
586
587 return strict_mode == SLOPPY ? SLOPPY_FUNCTION_MAP_INDEX
588 : STRICT_FUNCTION_MAP_INDEX;
589 }
590
591 static const int kSize = kHeaderSize + NATIVE_CONTEXT_SLOTS * kPointerSize;
Ben Murdochf87a2032010-10-22 12:50:53 +0100592
593 // GC support.
594 typedef FixedBodyDescriptor<
595 kHeaderSize, kSize, kSize> ScavengeBodyDescriptor;
596
597 typedef FixedBodyDescriptor<
598 kHeaderSize,
599 kHeaderSize + FIRST_WEAK_SLOT * kPointerSize,
600 kSize> MarkCompactBodyDescriptor;
601
Steve Blocka7e24c12009-10-30 11:49:00 +0000602 private:
603 // Unchecked access to the slots.
604 Object* unchecked_previous() { return get(PREVIOUS_INDEX); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000605
606#ifdef DEBUG
607 // Bootstrapping-aware type checks.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000608 static bool IsBootstrappingOrValidParentContext(Object* object, Context* kid);
609 static bool IsBootstrappingOrGlobalObject(Isolate* isolate, Object* object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000610#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000611
612 STATIC_ASSERT(kHeaderSize == Internals::kContextHeaderSize);
613 STATIC_ASSERT(EMBEDDER_DATA_INDEX == Internals::kContextEmbedderDataIndex);
Steve Blocka7e24c12009-10-30 11:49:00 +0000614};
615
616} } // namespace v8::internal
617
618#endif // V8_CONTEXTS_H_