1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_CONTEXTS_H_
6 #define V8_CONTEXTS_H_
7 
8 #include "src/heap/heap.h"
9 #include "src/objects.h"
10 
11 namespace v8 {
12 namespace internal {
13 
14 
15 enum ContextLookupFlags {
16   FOLLOW_CONTEXT_CHAIN = 1 << 0,
17   FOLLOW_PROTOTYPE_CHAIN = 1 << 1,
18   STOP_AT_DECLARATION_SCOPE = 1 << 2,
19   SKIP_WITH_CONTEXT = 1 << 3,
20 
21   DONT_FOLLOW_CHAINS = 0,
22   FOLLOW_CHAINS = FOLLOW_CONTEXT_CHAIN | FOLLOW_PROTOTYPE_CHAIN,
23   LEXICAL_TEST =
24       FOLLOW_CONTEXT_CHAIN | STOP_AT_DECLARATION_SCOPE | SKIP_WITH_CONTEXT,
25 };
26 
27 
28 // ES5 10.2 defines lexical environments with mutable and immutable bindings.
29 // Immutable bindings have two states, initialized and uninitialized, and
30 // 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.
43 //
44 // The harmony proposal for block scoped bindings also introduces the
45 // 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.
60 enum BindingFlags {
61   MUTABLE_IS_INITIALIZED,
62   MUTABLE_CHECK_INITIALIZED,
63   IMMUTABLE_IS_INITIALIZED,
64   IMMUTABLE_CHECK_INITIALIZED,
65   IMMUTABLE_IS_INITIALIZED_HARMONY,
66   IMMUTABLE_CHECK_INITIALIZED_HARMONY,
67   MISSING_BINDING
68 };
69 
70 
71 // 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 
80 #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)                 \
96   V(SPREAD_ITERABLE_INDEX, JSFunction, spread_iterable)
97 
98 
99 #define NATIVE_CONTEXT_JS_BUILTINS(V)                   \
100   V(CONCAT_ITERABLE_TO_ARRAY_BUILTIN_INDEX, JSFunction, \
101     concat_iterable_to_array_builtin)
102 
103 
104 #define NATIVE_CONTEXT_IMPORTED_FIELDS(V)                                     \
105   V(ARRAY_CONCAT_INDEX, JSFunction, array_concat)                             \
106   V(ARRAY_POP_INDEX, JSFunction, array_pop)                                   \
107   V(ARRAY_PUSH_INDEX, JSFunction, array_push)                                 \
108   V(ARRAY_SHIFT_INDEX, JSFunction, array_shift)                               \
109   V(ARRAY_SPLICE_INDEX, JSFunction, array_splice)                             \
110   V(ARRAY_SLICE_INDEX, JSFunction, array_slice)                               \
111   V(ARRAY_UNSHIFT_INDEX, JSFunction, array_unshift)                           \
112   V(ARRAY_VALUES_ITERATOR_INDEX, JSFunction, array_values_iterator)           \
113   V(DERIVED_GET_TRAP_INDEX, JSFunction, derived_get_trap)                     \
114   V(ERROR_FUNCTION_INDEX, JSFunction, error_function)                         \
115   V(EVAL_ERROR_FUNCTION_INDEX, JSFunction, eval_error_function)               \
116   V(GET_STACK_TRACE_LINE_INDEX, JSFunction, get_stack_trace_line_fun)         \
117   V(GLOBAL_EVAL_FUN_INDEX, JSFunction, global_eval_fun)                       \
118   V(JSON_SERIALIZE_ADAPTER_INDEX, JSFunction, json_serialize_adapter)         \
119   V(MAKE_ERROR_FUNCTION_INDEX, JSFunction, make_error_function)               \
120   V(MAP_DELETE_METHOD_INDEX, JSFunction, map_delete)                          \
121   V(MAP_GET_METHOD_INDEX, JSFunction, map_get)                                \
122   V(MAP_HAS_METHOD_INDEX, JSFunction, map_has)                                \
123   V(MAP_SET_METHOD_INDEX, JSFunction, map_set)                                \
124   V(MESSAGE_GET_COLUMN_NUMBER_INDEX, JSFunction, message_get_column_number)   \
125   V(MESSAGE_GET_LINE_NUMBER_INDEX, JSFunction, message_get_line_number)       \
126   V(MESSAGE_GET_SOURCE_LINE_INDEX, JSFunction, message_get_source_line)       \
127   V(NATIVE_OBJECT_GET_NOTIFIER_INDEX, JSFunction, native_object_get_notifier) \
128   V(NATIVE_OBJECT_NOTIFIER_PERFORM_CHANGE, JSFunction,                        \
129     native_object_notifier_perform_change)                                    \
130   V(NATIVE_OBJECT_OBSERVE_INDEX, JSFunction, native_object_observe)           \
131   V(NO_SIDE_EFFECTS_TO_STRING_FUN_INDEX, JSFunction,                          \
132     no_side_effects_to_string_fun)                                            \
133   V(OBJECT_VALUE_OF, JSFunction, object_value_of)                             \
134   V(OBJECT_TO_STRING, JSFunction, object_to_string)                           \
135   V(OBSERVERS_BEGIN_SPLICE_INDEX, JSFunction, observers_begin_perform_splice) \
136   V(OBSERVERS_END_SPLICE_INDEX, JSFunction, observers_end_perform_splice)     \
137   V(OBSERVERS_ENQUEUE_SPLICE_INDEX, JSFunction, observers_enqueue_splice)     \
138   V(OBSERVERS_NOTIFY_CHANGE_INDEX, JSFunction, observers_notify_change)       \
139   V(PROMISE_CATCH_INDEX, JSFunction, promise_catch)                           \
140   V(PROMISE_CHAIN_INDEX, JSFunction, promise_chain)                           \
141   V(PROMISE_CREATE_INDEX, JSFunction, promise_create)                         \
142   V(PROMISE_FUNCTION_INDEX, JSFunction, promise_function)                     \
143   V(PROMISE_HAS_USER_DEFINED_REJECT_HANDLER_INDEX, JSFunction,                \
144     promise_has_user_defined_reject_handler)                                  \
145   V(PROMISE_REJECT_INDEX, JSFunction, promise_reject)                         \
146   V(PROMISE_RESOLVE_INDEX, JSFunction, promise_resolve)                       \
147   V(PROMISE_THEN_INDEX, JSFunction, promise_then)                             \
148   V(PROXY_ENUMERATE_INDEX, JSFunction, proxy_enumerate)                       \
149   V(RANGE_ERROR_FUNCTION_INDEX, JSFunction, range_error_function)             \
150   V(REFERENCE_ERROR_FUNCTION_INDEX, JSFunction, reference_error_function)     \
151   V(SET_ADD_METHOD_INDEX, JSFunction, set_add)                                \
152   V(SET_DELETE_METHOD_INDEX, JSFunction, set_delete)                          \
153   V(SET_HAS_METHOD_INDEX, JSFunction, set_has)                                \
154   V(STACK_OVERFLOW_BOILERPLATE_INDEX, JSObject, stack_overflow_boilerplate)   \
155   V(SYNTAX_ERROR_FUNCTION_INDEX, JSFunction, syntax_error_function)           \
156   V(TYPE_ERROR_FUNCTION_INDEX, JSFunction, type_error_function)               \
157   V(URI_ERROR_FUNCTION_INDEX, JSFunction, uri_error_function)                 \
158   NATIVE_CONTEXT_JS_BUILTINS(V)
159 
160 #define NATIVE_CONTEXT_FIELDS(V)                                               \
161   V(GLOBAL_PROXY_INDEX, JSObject, global_proxy_object)                         \
162   V(EMBEDDER_DATA_INDEX, FixedArray, embedder_data)                            \
163   /* Below is alpha-sorted */                                                  \
164   V(ALLOW_CODE_GEN_FROM_STRINGS_INDEX, Object, allow_code_gen_from_strings)    \
165   V(ARRAY_BUFFER_FUN_INDEX, JSFunction, array_buffer_fun)                      \
166   V(ARRAY_BUFFER_MAP_INDEX, Map, array_buffer_map)                             \
167   V(ARRAY_FUNCTION_INDEX, JSFunction, array_function)                          \
168   V(BOOL16X8_FUNCTION_INDEX, JSFunction, bool16x8_function)                    \
169   V(BOOL32X4_FUNCTION_INDEX, JSFunction, bool32x4_function)                    \
170   V(BOOL8X16_FUNCTION_INDEX, JSFunction, bool8x16_function)                    \
171   V(BOOLEAN_FUNCTION_INDEX, JSFunction, boolean_function)                      \
172   V(BOUND_FUNCTION_WITH_CONSTRUCTOR_MAP_INDEX, Map,                            \
173     bound_function_with_constructor_map)                                       \
174   V(BOUND_FUNCTION_WITHOUT_CONSTRUCTOR_MAP_INDEX, Map,                         \
175     bound_function_without_constructor_map)                                    \
176   V(CALL_AS_CONSTRUCTOR_DELEGATE_INDEX, JSFunction,                            \
177     call_as_constructor_delegate)                                              \
178   V(CALL_AS_FUNCTION_DELEGATE_INDEX, JSFunction, call_as_function_delegate)    \
179   V(CONTEXT_EXTENSION_FUNCTION_INDEX, JSFunction, context_extension_function)  \
180   V(DATA_VIEW_FUN_INDEX, JSFunction, data_view_fun)                            \
181   V(DATE_FUNCTION_INDEX, JSFunction, date_function)                            \
182   V(ERROR_MESSAGE_FOR_CODE_GEN_FROM_STRINGS_INDEX, Object,                     \
183     error_message_for_code_gen_from_strings)                                   \
184   V(ERRORS_THROWN_INDEX, Smi, errors_thrown)                                   \
185   V(EXTRAS_EXPORTS_OBJECT_INDEX, JSObject, extras_binding_object)              \
186   V(EXTRAS_UTILS_OBJECT_INDEX, JSObject, extras_utils_object)                  \
187   V(FAST_ALIASED_ARGUMENTS_MAP_INDEX, Map, fast_aliased_arguments_map)         \
188   V(FLOAT32_ARRAY_FUN_INDEX, JSFunction, float32_array_fun)                    \
189   V(FLOAT32X4_FUNCTION_INDEX, JSFunction, float32x4_function)                  \
190   V(FLOAT64_ARRAY_FUN_INDEX, JSFunction, float64_array_fun)                    \
191   V(FUNCTION_CACHE_INDEX, ObjectHashTable, function_cache)                     \
192   V(FUNCTION_FUNCTION_INDEX, JSFunction, function_function)                    \
193   V(GENERATOR_FUNCTION_FUNCTION_INDEX, JSFunction,                             \
194     generator_function_function)                                               \
195   V(GENERATOR_OBJECT_PROTOTYPE_MAP_INDEX, Map, generator_object_prototype_map) \
196   V(INITIAL_ARRAY_PROTOTYPE_INDEX, JSObject, initial_array_prototype)          \
197   V(INITIAL_OBJECT_PROTOTYPE_INDEX, JSObject, initial_object_prototype)        \
198   V(INT16_ARRAY_FUN_INDEX, JSFunction, int16_array_fun)                        \
199   V(INT16X8_FUNCTION_INDEX, JSFunction, int16x8_function)                      \
200   V(INT32_ARRAY_FUN_INDEX, JSFunction, int32_array_fun)                        \
201   V(INT32X4_FUNCTION_INDEX, JSFunction, int32x4_function)                      \
202   V(INT8_ARRAY_FUN_INDEX, JSFunction, int8_array_fun)                          \
203   V(INT8X16_FUNCTION_INDEX, JSFunction, int8x16_function)                      \
204   V(INTERNAL_ARRAY_FUNCTION_INDEX, JSFunction, internal_array_function)        \
205   V(ITERATOR_RESULT_MAP_INDEX, Map, iterator_result_map)                       \
206   V(JS_ARRAY_FAST_SMI_ELEMENTS_MAP_INDEX, Map,                                 \
207     js_array_fast_smi_elements_map_index)                                      \
208   V(JS_ARRAY_FAST_HOLEY_SMI_ELEMENTS_MAP_INDEX, Map,                           \
209     js_array_fast_holey_smi_elements_map_index)                                \
210   V(JS_ARRAY_FAST_ELEMENTS_MAP_INDEX, Map, js_array_fast_elements_map_index)   \
211   V(JS_ARRAY_FAST_HOLEY_ELEMENTS_MAP_INDEX, Map,                               \
212     js_array_fast_holey_elements_map_index)                                    \
213   V(JS_ARRAY_FAST_DOUBLE_ELEMENTS_MAP_INDEX, Map,                              \
214     js_array_fast_double_elements_map_index)                                   \
215   V(JS_ARRAY_FAST_HOLEY_DOUBLE_ELEMENTS_MAP_INDEX, Map,                        \
216     js_array_fast_holey_double_elements_map_index)                             \
217   V(JS_ARRAY_FAST_SMI_ELEMENTS_STRONG_MAP_INDEX, Map,                          \
218     js_array_fast_smi_elements_strong_map_index)                               \
219   V(JS_ARRAY_FAST_HOLEY_SMI_ELEMENTS_STRONG_MAP_INDEX, Map,                    \
220     js_array_fast_holey_smi_elements_strong_map_index)                         \
221   V(JS_ARRAY_FAST_ELEMENTS_STRONG_MAP_INDEX, Map,                              \
222     js_array_fast_elements_strong_map_index)                                   \
223   V(JS_ARRAY_FAST_HOLEY_ELEMENTS_STRONG_MAP_INDEX, Map,                        \
224     js_array_fast_holey_elements_strong_map_index)                             \
225   V(JS_ARRAY_FAST_DOUBLE_ELEMENTS_STRONG_MAP_INDEX, Map,                       \
226     js_array_fast_double_elements_strong_map_index)                            \
227   V(JS_ARRAY_FAST_HOLEY_DOUBLE_ELEMENTS_STRONG_MAP_INDEX, Map,                 \
228     js_array_fast_holey_double_elements_strong_map_index)                      \
229   V(JS_MAP_FUN_INDEX, JSFunction, js_map_fun)                                  \
230   V(JS_MAP_MAP_INDEX, Map, js_map_map)                                         \
231   V(JS_OBJECT_STRONG_MAP_INDEX, Map, js_object_strong_map)                     \
232   V(JS_SET_FUN_INDEX, JSFunction, js_set_fun)                                  \
233   V(JS_SET_MAP_INDEX, Map, js_set_map)                                         \
234   V(JS_WEAK_MAP_FUN_INDEX, JSFunction, js_weak_map_fun)                        \
235   V(JS_WEAK_SET_FUN_INDEX, JSFunction, js_weak_set_fun)                        \
236   V(MAP_CACHE_INDEX, Object, map_cache)                                        \
237   V(MAP_ITERATOR_MAP_INDEX, Map, map_iterator_map)                             \
238   V(STRING_ITERATOR_MAP_INDEX, Map, string_iterator_map)                       \
239   V(MESSAGE_LISTENERS_INDEX, JSObject, message_listeners)                      \
240   V(NATIVES_UTILS_OBJECT_INDEX, Object, natives_utils_object)                  \
241   V(NORMALIZED_MAP_CACHE_INDEX, Object, normalized_map_cache)                  \
242   V(NUMBER_FUNCTION_INDEX, JSFunction, number_function)                        \
243   V(OBJECT_FUNCTION_INDEX, JSFunction, object_function)                        \
244   V(OBJECT_FUNCTION_PROTOTYPE_MAP_INDEX, Map, object_function_prototype_map)   \
245   V(OPAQUE_REFERENCE_FUNCTION_INDEX, JSFunction, opaque_reference_function)    \
246   V(PROXY_CALLABLE_MAP_INDEX, Map, proxy_callable_map)                         \
247   V(PROXY_CONSTRUCTOR_MAP_INDEX, Map, proxy_constructor_map)                   \
248   V(PROXY_FUNCTION_INDEX, JSFunction, proxy_function)                          \
249   V(PROXY_FUNCTION_MAP_INDEX, Map, proxy_function_map)                         \
250   V(PROXY_MAP_INDEX, Map, proxy_map)                                           \
251   V(REGEXP_FUNCTION_INDEX, JSFunction, regexp_function)                        \
252   V(REGEXP_RESULT_MAP_INDEX, Map, regexp_result_map)                           \
253   V(SCRIPT_CONTEXT_TABLE_INDEX, ScriptContextTable, script_context_table)      \
254   V(SCRIPT_FUNCTION_INDEX, JSFunction, script_function)                        \
255   V(SECURITY_TOKEN_INDEX, Object, security_token)                              \
256   V(SELF_WEAK_CELL_INDEX, WeakCell, self_weak_cell)                            \
257   V(SET_ITERATOR_MAP_INDEX, Map, set_iterator_map)                             \
258   V(SHARED_ARRAY_BUFFER_FUN_INDEX, JSFunction, shared_array_buffer_fun)        \
259   V(SLOPPY_ARGUMENTS_MAP_INDEX, Map, sloppy_arguments_map)                     \
260   V(SLOPPY_FUNCTION_MAP_INDEX, Map, sloppy_function_map)                       \
261   V(SLOPPY_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map,                          \
262     sloppy_function_without_prototype_map)                                     \
263   V(SLOPPY_FUNCTION_WITH_READONLY_PROTOTYPE_MAP_INDEX, Map,                    \
264     sloppy_function_with_readonly_prototype_map)                               \
265   V(WASM_FUNCTION_MAP_INDEX, Map, wasm_function_map)                           \
266   V(SLOPPY_GENERATOR_FUNCTION_MAP_INDEX, Map, sloppy_generator_function_map)   \
267   V(SLOW_ALIASED_ARGUMENTS_MAP_INDEX, Map, slow_aliased_arguments_map)         \
268   V(STRICT_ARGUMENTS_MAP_INDEX, Map, strict_arguments_map)                     \
269   V(STRICT_FUNCTION_MAP_INDEX, Map, strict_function_map)                       \
270   V(STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map,                          \
271     strict_function_without_prototype_map)                                     \
272   V(STRICT_GENERATOR_FUNCTION_MAP_INDEX, Map, strict_generator_function_map)   \
273   V(STRING_FUNCTION_INDEX, JSFunction, string_function)                        \
274   V(STRING_FUNCTION_PROTOTYPE_MAP_INDEX, Map, string_function_prototype_map)   \
275   V(STRONG_CONSTRUCTOR_MAP_INDEX, Map, strong_constructor_map)                 \
276   V(STRONG_FUNCTION_MAP_INDEX, Map, strong_function_map)                       \
277   V(STRONG_GENERATOR_FUNCTION_MAP_INDEX, Map, strong_generator_function_map)   \
278   V(STRONG_MAP_CACHE_INDEX, Object, strong_map_cache)                          \
279   V(SYMBOL_FUNCTION_INDEX, JSFunction, symbol_function)                        \
280   V(UINT16_ARRAY_FUN_INDEX, JSFunction, uint16_array_fun)                      \
281   V(UINT16X8_FUNCTION_INDEX, JSFunction, uint16x8_function)                    \
282   V(UINT32_ARRAY_FUN_INDEX, JSFunction, uint32_array_fun)                      \
283   V(UINT32X4_FUNCTION_INDEX, JSFunction, uint32x4_function)                    \
284   V(UINT8_ARRAY_FUN_INDEX, JSFunction, uint8_array_fun)                        \
285   V(UINT8_CLAMPED_ARRAY_FUN_INDEX, JSFunction, uint8_clamped_array_fun)        \
286   V(UINT8X16_FUNCTION_INDEX, JSFunction, uint8x16_function)                    \
287   NATIVE_CONTEXT_INTRINSIC_FUNCTIONS(V)                                        \
288   NATIVE_CONTEXT_IMPORTED_FIELDS(V)
289 
290 // A table of all script contexts. Every loaded top-level script with top-level
291 // lexical declarations contributes its ScriptContext into this table.
292 //
293 // The table is a fixed array, its first slot is the current used count and
294 // the subsequent slots 1..used contain ScriptContexts.
295 class ScriptContextTable : public FixedArray {
296  public:
297   // Conversions.
298   static inline ScriptContextTable* cast(Object* context);
299 
300   struct LookupResult {
301     int context_index;
302     int slot_index;
303     VariableMode mode;
304     InitializationFlag init_flag;
305     MaybeAssignedFlag maybe_assigned_flag;
306   };
307 
308   inline int used() const;
309   inline void set_used(int used);
310 
311   static inline Handle<Context> GetContext(Handle<ScriptContextTable> table,
312                                            int i);
313 
314   // Lookup a variable `name` in a ScriptContextTable.
315   // If it returns true, the variable is found and `result` contains
316   // valid information about its location.
317   // If it returns false, `result` is untouched.
318   MUST_USE_RESULT
319   static bool Lookup(Handle<ScriptContextTable> table, Handle<String> name,
320                      LookupResult* result);
321 
322   MUST_USE_RESULT
323   static Handle<ScriptContextTable> Extend(Handle<ScriptContextTable> table,
324                                            Handle<Context> script_context);
325 
GetContextOffset(int context_index)326   static int GetContextOffset(int context_index) {
327     return kFirstContextOffset + context_index * kPointerSize;
328   }
329 
330  private:
331   static const int kUsedSlot = 0;
332   static const int kFirstContextSlot = kUsedSlot + 1;
333   static const int kFirstContextOffset =
334       FixedArray::kHeaderSize + kFirstContextSlot * kPointerSize;
335 
336   DISALLOW_IMPLICIT_CONSTRUCTORS(ScriptContextTable);
337 };
338 
339 // JSFunctions are pairs (context, function code), sometimes also called
340 // closures. A Context object is used to represent function contexts and
341 // dynamically pushed 'with' contexts (or 'scopes' in ECMA-262 speak).
342 //
343 // At runtime, the contexts build a stack in parallel to the execution
344 // stack, with the top-most context being the current context. All contexts
345 // have the following slots:
346 //
347 // [ closure   ]  This is the current function. It is the same for all
348 //                contexts inside a function. It provides access to the
349 //                incoming context (i.e., the outer context, which may
350 //                or may not become the current function's context), and
351 //                it provides access to the functions code and thus it's
352 //                scope information, which in turn contains the names of
353 //                statically allocated context slots. The names are needed
354 //                for dynamic lookups in the presence of 'with' or 'eval'.
355 //
356 // [ previous  ]  A pointer to the previous context. It is NULL for
357 //                function contexts, and non-NULL for 'with' contexts.
358 //                Used to implement the 'with' statement.
359 //
360 // [ extension ]  A pointer to an extension JSObject, or "the hole". Used to
361 //                implement 'with' statements and dynamic declarations
362 //                (through 'eval'). The object in a 'with' statement is
363 //                stored in the extension slot of a 'with' context.
364 //                Dynamically declared variables/functions are also added
365 //                to lazily allocated extension object. Context::Lookup
366 //                searches the extension object for properties.
367 //                For script and block contexts, contains the respective
368 //                ScopeInfo. For block contexts representing sloppy declaration
369 //                block scopes, it may also be a struct being a
370 //                SloppyBlockWithEvalContextExtension, pairing the ScopeInfo
371 //                with an extension object.
372 //                For module contexts, points back to the respective JSModule.
373 //
374 // [ global_object ]  A pointer to the global object. Provided for quick
375 //                access to the global object from inside the code (since
376 //                we always have a context pointer).
377 //
378 // In addition, function contexts may have statically allocated context slots
379 // to store local variables/functions that are accessed from inner functions
380 // (via static context addresses) or through 'eval' (dynamic context lookups).
381 // The native context contains additional slots for fast access to native
382 // properties.
383 //
384 // Finally, with Harmony scoping, the JSFunction representing a top level
385 // script will have the ScriptContext rather than a FunctionContext.
386 // Script contexts from all top-level scripts are gathered in
387 // ScriptContextTable.
388 
389 class Context: public FixedArray {
390  public:
391   // Conversions.
392   static inline Context* cast(Object* context);
393 
394   // The default context slot layout; indices are FixedArray slot indices.
395   enum {
396     // These slots are in all contexts.
397     CLOSURE_INDEX,
398     PREVIOUS_INDEX,
399     // The extension slot is used for either the global object (in native
400     // contexts), eval extension object (function contexts), subject of with
401     // (with contexts), or the variable name (catch contexts), the serialized
402     // scope info (block contexts), or the module instance (module contexts).
403     EXTENSION_INDEX,
404     NATIVE_CONTEXT_INDEX,
405 
406     // These slots are only in native contexts.
407 #define NATIVE_CONTEXT_SLOT(index, type, name) index,
408     NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_SLOT)
409 #undef NATIVE_CONTEXT_SLOT
410 
411     // Properties from here are treated as weak references by the full GC.
412     // Scavenge treats them as strong references.
413     OPTIMIZED_FUNCTIONS_LIST,  // Weak.
414     OPTIMIZED_CODE_LIST,       // Weak.
415     DEOPTIMIZED_CODE_LIST,     // Weak.
416     NEXT_CONTEXT_LINK,         // Weak.
417 
418     // Total number of slots.
419     NATIVE_CONTEXT_SLOTS,
420     FIRST_WEAK_SLOT = OPTIMIZED_FUNCTIONS_LIST,
421     FIRST_JS_ARRAY_MAP_SLOT = JS_ARRAY_FAST_SMI_ELEMENTS_MAP_INDEX,
422     FIRST_JS_ARRAY_STRONG_MAP_SLOT =
423         JS_ARRAY_FAST_SMI_ELEMENTS_STRONG_MAP_INDEX,
424 
425     MIN_CONTEXT_SLOTS = GLOBAL_PROXY_INDEX,
426     // This slot holds the thrown value in catch contexts.
427     THROWN_OBJECT_INDEX = MIN_CONTEXT_SLOTS,
428   };
429 
430   void IncrementErrorsThrown();
431   int GetErrorsThrown();
432 
433   // Direct slot access.
434   inline JSFunction* closure();
435   inline void set_closure(JSFunction* closure);
436 
437   inline Context* previous();
438   inline void set_previous(Context* context);
439 
440   inline bool has_extension();
441   inline HeapObject* extension();
442   inline void set_extension(HeapObject* object);
443   JSObject* extension_object();
444   JSReceiver* extension_receiver();
445   ScopeInfo* scope_info();
446   String* catch_name();
447 
448   inline JSModule* module();
449   inline void set_module(JSModule* module);
450 
451   // Get the context where var declarations will be hoisted to, which
452   // may be the context itself.
453   Context* declaration_context();
454   bool is_declaration_context();
455 
456   // Returns a JSGlobalProxy object or null.
457   JSObject* global_proxy();
458   void set_global_proxy(JSObject* global);
459 
460   // Get the JSGlobalObject object.
461   JSGlobalObject* global_object();
462 
463   // Get the script context by traversing the context chain.
464   Context* script_context();
465 
466   // Compute the native context.
467   inline Context* native_context();
468   inline void set_native_context(Context* context);
469 
470   // Predicates for context types.  IsNativeContext is also defined on Object
471   // because we frequently have to know if arbitrary objects are natives
472   // contexts.
473   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();
480 
481   inline bool HasSameSecurityTokenAs(Context* that);
482 
483   // Initializes global variable bindings in given script context.
484   void InitializeGlobalSlots();
485 
486   // A native context holds a list of all functions with optimized code.
487   void AddOptimizedFunction(JSFunction* function);
488   void RemoveOptimizedFunction(JSFunction* function);
489   void SetOptimizedFunctionsListHead(Object* head);
490   Object* OptimizedFunctionsListHead();
491 
492   // 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();
499 
500   Handle<Object> ErrorMessageForCodeGenerationFromStrings();
501 
502   static int ImportedFieldIndexForName(Handle<String> name);
503   static int IntrinsicIndexForName(Handle<String> name);
504 
505   static bool IsJSBuiltin(Handle<Context> native_context,
506                           Handle<JSFunction> function);
507 
508 #define NATIVE_CONTEXT_FIELD_ACCESSORS(index, type, name) \
509   inline void set_##name(type* value);                    \
510   inline bool is_##name(type* value);                     \
511   inline type* name();
512   NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_FIELD_ACCESSORS)
513 #undef NATIVE_CONTEXT_FIELD_ACCESSORS
514 
515   // Lookup the slot called name, starting with the current context.
516   // There are three possibilities:
517   //
518   // 1) result->IsContext():
519   //    The binding was found in a context.  *index is always the
520   //    non-negative slot index.  *attributes is NONE for var and let
521   //    declarations, READ_ONLY for const declarations (never ABSENT).
522   //
523   // 2) result->IsJSObject():
524   //    The binding was found as a named property in a context extension
525   //    object (i.e., was introduced via eval), as a property on the subject
526   //    of with, or as a property of the global object.  *index is -1 and
527   //    *attributes is not ABSENT.
528   //
529   // 3) result.is_null():
530   //    There was no binding found, *index is always -1 and *attributes is
531   //    always ABSENT.
532   Handle<Object> Lookup(Handle<String> name,
533                         ContextLookupFlags flags,
534                         int* index,
535                         PropertyAttributes* attributes,
536                         BindingFlags* binding_flags);
537 
538   // Code generation support.
SlotOffset(int index)539   static int SlotOffset(int index) {
540     return kHeaderSize + index * kPointerSize - kHeapObjectTag;
541   }
542 
FunctionMapIndex(LanguageMode language_mode,FunctionKind kind)543   static int FunctionMapIndex(LanguageMode language_mode, FunctionKind kind) {
544     if (IsGeneratorFunction(kind)) {
545       return is_strong(language_mode) ? STRONG_GENERATOR_FUNCTION_MAP_INDEX :
546              is_strict(language_mode) ? STRICT_GENERATOR_FUNCTION_MAP_INDEX
547                                       : SLOPPY_GENERATOR_FUNCTION_MAP_INDEX;
548     }
549 
550     if (IsClassConstructor(kind)) {
551       // Use strict function map (no own "caller" / "arguments")
552       return is_strong(language_mode) ? STRONG_CONSTRUCTOR_MAP_INDEX
553                                       : STRICT_FUNCTION_MAP_INDEX;
554     }
555 
556     if (IsArrowFunction(kind) || IsConciseMethod(kind) ||
557         IsAccessorFunction(kind)) {
558       return is_strong(language_mode)
559                  ? STRONG_FUNCTION_MAP_INDEX
560                  : STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX;
561     }
562 
563     return is_strong(language_mode) ? STRONG_FUNCTION_MAP_INDEX :
564            is_strict(language_mode) ? STRICT_FUNCTION_MAP_INDEX
565                                     : SLOPPY_FUNCTION_MAP_INDEX;
566   }
567 
568   static int ArrayMapIndex(ElementsKind elements_kind,
569                            Strength strength = Strength::WEAK) {
570     DCHECK(IsFastElementsKind(elements_kind));
571     return elements_kind + (is_strong(strength) ? FIRST_JS_ARRAY_STRONG_MAP_SLOT
572                                                 : FIRST_JS_ARRAY_MAP_SLOT);
573   }
574 
575   static const int kSize = kHeaderSize + NATIVE_CONTEXT_SLOTS * kPointerSize;
576   static const int kNotFound = -1;
577 
578   // GC support.
579   typedef FixedBodyDescriptor<
580       kHeaderSize, kSize, kSize> ScavengeBodyDescriptor;
581 
582   typedef FixedBodyDescriptor<
583       kHeaderSize,
584       kHeaderSize + FIRST_WEAK_SLOT * kPointerSize,
585       kSize> MarkCompactBodyDescriptor;
586 
587  private:
588 #ifdef DEBUG
589   // Bootstrapping-aware type checks.
590   static bool IsBootstrappingOrNativeContext(Isolate* isolate, Object* object);
591   static bool IsBootstrappingOrValidParentContext(Object* object, Context* kid);
592 #endif
593 
594   STATIC_ASSERT(kHeaderSize == Internals::kContextHeaderSize);
595   STATIC_ASSERT(EMBEDDER_DATA_INDEX == Internals::kContextEmbedderDataIndex);
596 };
597 
598 }  // namespace internal
599 }  // namespace v8
600 
601 #endif  // V8_CONTEXTS_H_
602