[stubs] Unify (and optimize) implementation of ToObject.
[platform/upstream/v8.git] / src / contexts.h
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,
17   FOLLOW_PROTOTYPE_CHAIN = 2,
18
19   DONT_FOLLOW_CHAINS = 0,
20   FOLLOW_CHAINS = FOLLOW_CONTEXT_CHAIN | FOLLOW_PROTOTYPE_CHAIN
21 };
22
23
24 // ES5 10.2 defines lexical environments with mutable and immutable bindings.
25 // Immutable bindings have two states, initialized and uninitialized, and
26 // 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.
39 //
40 // The harmony proposal for block scoped bindings also introduces the
41 // 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.
56 enum BindingFlags {
57   MUTABLE_IS_INITIALIZED,
58   MUTABLE_CHECK_INITIALIZED,
59   IMMUTABLE_IS_INITIALIZED,
60   IMMUTABLE_CHECK_INITIALIZED,
61   IMMUTABLE_IS_INITIALIZED_HARMONY,
62   IMMUTABLE_CHECK_INITIALIZED_HARMONY,
63   MISSING_BINDING
64 };
65
66
67 // 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
76 #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(FLOAT32X4_FUNCTION_INDEX, JSFunction, float32x4_function)                  \
85   V(OBJECT_FUNCTION_INDEX, JSFunction, object_function)                        \
86   V(JS_OBJECT_STRONG_MAP_INDEX, Map, js_object_strong_map)                     \
87   V(INTERNAL_ARRAY_FUNCTION_INDEX, JSFunction, internal_array_function)        \
88   V(ARRAY_FUNCTION_INDEX, JSFunction, array_function)                          \
89   V(JS_ARRAY_MAPS_INDEX, Object, js_array_maps)                                \
90   V(JS_ARRAY_STRONG_MAPS_INDEX, Object, js_array_strong_maps)                  \
91   V(DATE_FUNCTION_INDEX, JSFunction, date_function)                            \
92   V(JSON_OBJECT_INDEX, JSObject, json_object)                                  \
93   V(REGEXP_FUNCTION_INDEX, JSFunction, regexp_function)                        \
94   V(INITIAL_OBJECT_PROTOTYPE_INDEX, JSObject, initial_object_prototype)        \
95   V(INITIAL_ARRAY_PROTOTYPE_INDEX, JSObject, initial_array_prototype)          \
96   V(CREATE_DATE_FUN_INDEX, JSFunction, create_date_fun)                        \
97   V(TO_NUMBER_FUN_INDEX, JSFunction, to_number_fun)                            \
98   V(TO_STRING_FUN_INDEX, JSFunction, to_string_fun)                            \
99   V(TO_DETAIL_STRING_FUN_INDEX, JSFunction, to_detail_string_fun)              \
100   V(TO_INTEGER_FUN_INDEX, JSFunction, to_integer_fun)                          \
101   V(TO_UINT32_FUN_INDEX, JSFunction, to_uint32_fun)                            \
102   V(TO_INT32_FUN_INDEX, JSFunction, to_int32_fun)                              \
103   V(TO_LENGTH_FUN_INDEX, JSFunction, to_length_fun)                            \
104   V(GLOBAL_EVAL_FUN_INDEX, JSFunction, global_eval_fun)                        \
105   V(ARRAY_BUFFER_FUN_INDEX, JSFunction, array_buffer_fun)                      \
106   V(SHARED_ARRAY_BUFFER_FUN_INDEX, JSFunction, shared_array_buffer_fun)        \
107   V(ARRAY_BUFFER_MAP_INDEX, Map, array_buffer_map)                             \
108   V(UINT8_ARRAY_FUN_INDEX, JSFunction, uint8_array_fun)                        \
109   V(INT8_ARRAY_FUN_INDEX, JSFunction, int8_array_fun)                          \
110   V(UINT16_ARRAY_FUN_INDEX, JSFunction, uint16_array_fun)                      \
111   V(INT16_ARRAY_FUN_INDEX, JSFunction, int16_array_fun)                        \
112   V(UINT32_ARRAY_FUN_INDEX, JSFunction, uint32_array_fun)                      \
113   V(INT32_ARRAY_FUN_INDEX, JSFunction, int32_array_fun)                        \
114   V(FLOAT32_ARRAY_FUN_INDEX, JSFunction, float32_array_fun)                    \
115   V(FLOAT64_ARRAY_FUN_INDEX, JSFunction, float64_array_fun)                    \
116   V(UINT8_CLAMPED_ARRAY_FUN_INDEX, JSFunction, uint8_clamped_array_fun)        \
117   V(DATA_VIEW_FUN_INDEX, JSFunction, data_view_fun)                            \
118   V(SLOPPY_FUNCTION_MAP_INDEX, Map, sloppy_function_map)                       \
119   V(SLOPPY_FUNCTION_WITH_READONLY_PROTOTYPE_MAP_INDEX, Map,                    \
120     sloppy_function_with_readonly_prototype_map)                               \
121   V(STRICT_FUNCTION_MAP_INDEX, Map, strict_function_map)                       \
122   V(STRONG_FUNCTION_MAP_INDEX, Map, strong_function_map)                       \
123   V(SLOPPY_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map,                          \
124     sloppy_function_without_prototype_map)                                     \
125   V(STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map,                          \
126     strict_function_without_prototype_map)                                     \
127   V(STRONG_CONSTRUCTOR_MAP_INDEX, Map, strong_constructor_map)                 \
128   V(BOUND_FUNCTION_MAP_INDEX, Map, bound_function_map)                         \
129   V(REGEXP_RESULT_MAP_INDEX, Map, regexp_result_map)                           \
130   V(SLOPPY_ARGUMENTS_MAP_INDEX, Map, sloppy_arguments_map)                     \
131   V(FAST_ALIASED_ARGUMENTS_MAP_INDEX, Map, fast_aliased_arguments_map)         \
132   V(SLOW_ALIASED_ARGUMENTS_MAP_INDEX, Map, slow_aliased_arguments_map)         \
133   V(STRICT_ARGUMENTS_MAP_INDEX, Map, strict_arguments_map)                     \
134   V(MESSAGE_LISTENERS_INDEX, JSObject, message_listeners)                      \
135   V(MAKE_MESSAGE_FUN_INDEX, JSFunction, make_message_fun)                      \
136   V(GET_STACK_TRACE_LINE_INDEX, JSFunction, get_stack_trace_line_fun)          \
137   V(CONFIGURE_GLOBAL_INDEX, JSFunction, configure_global_fun)                  \
138   V(FUNCTION_CACHE_INDEX, ObjectHashTable, function_cache)                     \
139   V(JSFUNCTION_RESULT_CACHES_INDEX, FixedArray, jsfunction_result_caches)      \
140   V(NORMALIZED_MAP_CACHE_INDEX, Object, normalized_map_cache)                  \
141   V(RUNTIME_CONTEXT_INDEX, Context, runtime_context)                           \
142   V(CALL_AS_FUNCTION_DELEGATE_INDEX, JSFunction, call_as_function_delegate)    \
143   V(CALL_AS_CONSTRUCTOR_DELEGATE_INDEX, JSFunction,                            \
144     call_as_constructor_delegate)                                              \
145   V(SCRIPT_FUNCTION_INDEX, JSFunction, script_function)                        \
146   V(OPAQUE_REFERENCE_FUNCTION_INDEX, JSFunction, opaque_reference_function)    \
147   V(CONTEXT_EXTENSION_FUNCTION_INDEX, JSFunction, context_extension_function)  \
148   V(MAP_CACHE_INDEX, Object, map_cache)                                        \
149   V(STRONG_MAP_CACHE_INDEX, Object, strong_map_cache)                          \
150   V(EMBEDDER_DATA_INDEX, FixedArray, embedder_data)                            \
151   V(ALLOW_CODE_GEN_FROM_STRINGS_INDEX, Object, allow_code_gen_from_strings)    \
152   V(ERROR_MESSAGE_FOR_CODE_GEN_FROM_STRINGS_INDEX, Object,                     \
153     error_message_for_code_gen_from_strings)                                   \
154   V(PROMISE_STATUS_INDEX, Symbol, promise_status)                              \
155   V(PROMISE_VALUE_INDEX, Symbol, promise_value)                                \
156   V(PROMISE_CREATE_INDEX, JSFunction, promise_create)                          \
157   V(PROMISE_RESOLVE_INDEX, JSFunction, promise_resolve)                        \
158   V(PROMISE_REJECT_INDEX, JSFunction, promise_reject)                          \
159   V(PROMISE_CHAIN_INDEX, JSFunction, promise_chain)                            \
160   V(PROMISE_CATCH_INDEX, JSFunction, promise_catch)                            \
161   V(PROMISE_THEN_INDEX, JSFunction, promise_then)                              \
162   V(TO_COMPLETE_PROPERTY_DESCRIPTOR_INDEX, JSFunction,                         \
163     to_complete_property_descriptor)                                           \
164   V(DERIVED_HAS_TRAP_INDEX, JSFunction, derived_has_trap)                      \
165   V(DERIVED_GET_TRAP_INDEX, JSFunction, derived_get_trap)                      \
166   V(DERIVED_SET_TRAP_INDEX, JSFunction, derived_set_trap)                      \
167   V(PROXY_ENUMERATE_INDEX, JSFunction, proxy_enumerate)                        \
168   V(OBSERVERS_NOTIFY_CHANGE_INDEX, JSFunction, observers_notify_change)        \
169   V(OBSERVERS_ENQUEUE_SPLICE_INDEX, JSFunction, observers_enqueue_splice)      \
170   V(OBSERVERS_BEGIN_SPLICE_INDEX, JSFunction, observers_begin_perform_splice)  \
171   V(OBSERVERS_END_SPLICE_INDEX, JSFunction, observers_end_perform_splice)      \
172   V(NATIVE_OBJECT_OBSERVE_INDEX, JSFunction, native_object_observe)            \
173   V(NATIVE_OBJECT_GET_NOTIFIER_INDEX, JSFunction, native_object_get_notifier)  \
174   V(NATIVE_OBJECT_NOTIFIER_PERFORM_CHANGE, JSFunction,                         \
175     native_object_notifier_perform_change)                                     \
176   V(SLOPPY_GENERATOR_FUNCTION_MAP_INDEX, Map, sloppy_generator_function_map)   \
177   V(STRICT_GENERATOR_FUNCTION_MAP_INDEX, Map, strict_generator_function_map)   \
178   V(STRONG_GENERATOR_FUNCTION_MAP_INDEX, Map, strong_generator_function_map)   \
179   V(GENERATOR_OBJECT_PROTOTYPE_MAP_INDEX, Map, generator_object_prototype_map) \
180   V(ITERATOR_RESULT_MAP_INDEX, Map, iterator_result_map)                       \
181   V(JS_MAP_FUN_INDEX, JSFunction, js_map_fun)                                  \
182   V(JS_MAP_MAP_INDEX, Map, js_map_map)                                         \
183   V(JS_SET_FUN_INDEX, JSFunction, js_set_fun)                                  \
184   V(JS_SET_MAP_INDEX, Map, js_set_map)                                         \
185   V(MAP_GET_METHOD_INDEX, JSFunction, map_get)                                 \
186   V(MAP_SET_METHOD_INDEX, JSFunction, map_set)                                 \
187   V(MAP_HAS_METHOD_INDEX, JSFunction, map_has)                                 \
188   V(MAP_DELETE_METHOD_INDEX, JSFunction, map_delete)                           \
189   V(SET_ADD_METHOD_INDEX, JSFunction, set_add)                                 \
190   V(SET_HAS_METHOD_INDEX, JSFunction, set_has)                                 \
191   V(SET_DELETE_METHOD_INDEX, JSFunction, set_delete)                           \
192   V(MAP_FROM_ARRAY_INDEX, JSFunction, map_from_array)                          \
193   V(SET_FROM_ARRAY_INDEX, JSFunction, set_from_array)                          \
194   V(MAP_ITERATOR_MAP_INDEX, Map, map_iterator_map)                             \
195   V(SET_ITERATOR_MAP_INDEX, Map, set_iterator_map)                             \
196   V(ARRAY_VALUES_ITERATOR_INDEX, JSFunction, array_values_iterator)            \
197   V(SCRIPT_CONTEXT_TABLE_INDEX, ScriptContextTable, script_context_table)      \
198   V(NATIVES_UTILS_OBJECT_INDEX, Object, natives_utils_object)                  \
199   V(EXTRAS_EXPORTS_OBJECT_INDEX, JSObject, extras_exports_object)
200
201
202 // A table of all script contexts. Every loaded top-level script with top-level
203 // lexical declarations contributes its ScriptContext into this table.
204 //
205 // The table is a fixed array, its first slot is the current used count and
206 // the subsequent slots 1..used contain ScriptContexts.
207 class ScriptContextTable : public FixedArray {
208  public:
209   // Conversions.
210   static ScriptContextTable* cast(Object* context) {
211     DCHECK(context->IsScriptContextTable());
212     return reinterpret_cast<ScriptContextTable*>(context);
213   }
214
215   struct LookupResult {
216     int context_index;
217     int slot_index;
218     VariableMode mode;
219     VariableLocation location;
220     InitializationFlag init_flag;
221     MaybeAssignedFlag maybe_assigned_flag;
222   };
223
224   int used() const { return Smi::cast(get(kUsedSlot))->value(); }
225
226   void set_used(int used) { set(kUsedSlot, Smi::FromInt(used)); }
227
228   static Handle<Context> GetContext(Handle<ScriptContextTable> table, int i) {
229     DCHECK(i < table->used());
230     return Handle<Context>::cast(FixedArray::get(table, i + 1));
231   }
232
233   // Lookup a variable `name` in a ScriptContextTable.
234   // If it returns true, the variable is found and `result` contains
235   // valid information about its location.
236   // If it returns false, `result` is untouched.
237   MUST_USE_RESULT
238   static bool Lookup(Handle<ScriptContextTable> table, Handle<String> name,
239                      LookupResult* result);
240
241   MUST_USE_RESULT
242   static Handle<ScriptContextTable> Extend(Handle<ScriptContextTable> table,
243                                            Handle<Context> script_context);
244
245   static int GetContextOffset(int context_index) {
246     return kFirstContextOffset + context_index * kPointerSize;
247   }
248
249  private:
250   static const int kUsedSlot = 0;
251   static const int kFirstContextOffset =
252       FixedArray::kHeaderSize + (kUsedSlot + 1) * kPointerSize;
253
254   DISALLOW_IMPLICIT_CONSTRUCTORS(ScriptContextTable);
255 };
256
257 // JSFunctions are pairs (context, function code), sometimes also called
258 // closures. A Context object is used to represent function contexts and
259 // dynamically pushed 'with' contexts (or 'scopes' in ECMA-262 speak).
260 //
261 // At runtime, the contexts build a stack in parallel to the execution
262 // stack, with the top-most context being the current context. All contexts
263 // have the following slots:
264 //
265 // [ closure   ]  This is the current function. It is the same for all
266 //                contexts inside a function. It provides access to the
267 //                incoming context (i.e., the outer context, which may
268 //                or may not become the current function's context), and
269 //                it provides access to the functions code and thus it's
270 //                scope information, which in turn contains the names of
271 //                statically allocated context slots. The names are needed
272 //                for dynamic lookups in the presence of 'with' or 'eval'.
273 //
274 // [ previous  ]  A pointer to the previous context. It is NULL for
275 //                function contexts, and non-NULL for 'with' contexts.
276 //                Used to implement the 'with' statement.
277 //
278 // [ extension ]  A pointer to an extension JSObject, or NULL. Used to
279 //                implement 'with' statements and dynamic declarations
280 //                (through 'eval'). The object in a 'with' statement is
281 //                stored in the extension slot of a 'with' context.
282 //                Dynamically declared variables/functions are also added
283 //                to lazily allocated extension object. Context::Lookup
284 //                searches the extension object for properties.
285 //                For global and block contexts, contains the respective
286 //                ScopeInfo.
287 //                For module contexts, points back to the respective JSModule.
288 //
289 // [ global_object ]  A pointer to the global object. Provided for quick
290 //                access to the global object from inside the code (since
291 //                we always have a context pointer).
292 //
293 // In addition, function contexts may have statically allocated context slots
294 // to store local variables/functions that are accessed from inner functions
295 // (via static context addresses) or through 'eval' (dynamic context lookups).
296 // The native context contains additional slots for fast access to native
297 // properties.
298 //
299 // Finally, with Harmony scoping, the JSFunction representing a top level
300 // script will have the ScriptContext rather than a FunctionContext.
301 // Script contexts from all top-level scripts are gathered in
302 // ScriptContextTable.
303
304 class Context: public FixedArray {
305  public:
306   // Conversions.
307   static Context* cast(Object* context) {
308     DCHECK(context->IsContext());
309     return reinterpret_cast<Context*>(context);
310   }
311
312   // The default context slot layout; indices are FixedArray slot indices.
313   enum {
314     // These slots are in all contexts.
315     CLOSURE_INDEX,
316     PREVIOUS_INDEX,
317     // The extension slot is used for either the global object (in global
318     // contexts), eval extension object (function contexts), subject of with
319     // (with contexts), or the variable name (catch contexts), the serialized
320     // scope info (block contexts), or the module instance (module contexts).
321     EXTENSION_INDEX,
322     GLOBAL_OBJECT_INDEX,
323     MIN_CONTEXT_SLOTS,
324
325     // This slot holds the thrown value in catch contexts.
326     THROWN_OBJECT_INDEX = MIN_CONTEXT_SLOTS,
327
328     // These slots are only in native contexts.
329     GLOBAL_PROXY_INDEX = MIN_CONTEXT_SLOTS,
330     SECURITY_TOKEN_INDEX,
331     SLOPPY_ARGUMENTS_MAP_INDEX,
332     FAST_ALIASED_ARGUMENTS_MAP_INDEX,
333     SLOW_ALIASED_ARGUMENTS_MAP_INDEX,
334     STRICT_ARGUMENTS_MAP_INDEX,
335     REGEXP_RESULT_MAP_INDEX,
336     SLOPPY_FUNCTION_MAP_INDEX,
337     SLOPPY_FUNCTION_WITH_READONLY_PROTOTYPE_MAP_INDEX,
338     STRICT_FUNCTION_MAP_INDEX,
339     STRONG_FUNCTION_MAP_INDEX,
340     SLOPPY_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX,
341     STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX,
342     STRONG_CONSTRUCTOR_MAP_INDEX,
343     BOUND_FUNCTION_MAP_INDEX,
344     INITIAL_OBJECT_PROTOTYPE_INDEX,
345     INITIAL_ARRAY_PROTOTYPE_INDEX,
346     BOOLEAN_FUNCTION_INDEX,
347     NUMBER_FUNCTION_INDEX,
348     STRING_FUNCTION_INDEX,
349     STRING_FUNCTION_PROTOTYPE_MAP_INDEX,
350     SYMBOL_FUNCTION_INDEX,
351     FLOAT32X4_FUNCTION_INDEX,
352     OBJECT_FUNCTION_INDEX,
353     JS_OBJECT_STRONG_MAP_INDEX,
354     INTERNAL_ARRAY_FUNCTION_INDEX,
355     ARRAY_FUNCTION_INDEX,
356     JS_ARRAY_MAPS_INDEX,
357     JS_ARRAY_STRONG_MAPS_INDEX,
358     DATE_FUNCTION_INDEX,
359     JSON_OBJECT_INDEX,
360     REGEXP_FUNCTION_INDEX,
361     CREATE_DATE_FUN_INDEX,
362     TO_NUMBER_FUN_INDEX,
363     TO_STRING_FUN_INDEX,
364     TO_DETAIL_STRING_FUN_INDEX,
365     TO_INTEGER_FUN_INDEX,
366     TO_UINT32_FUN_INDEX,
367     TO_INT32_FUN_INDEX,
368     TO_BOOLEAN_FUN_INDEX,
369     GLOBAL_EVAL_FUN_INDEX,
370     ARRAY_BUFFER_FUN_INDEX,
371     ARRAY_BUFFER_MAP_INDEX,
372     UINT8_ARRAY_FUN_INDEX,
373     INT8_ARRAY_FUN_INDEX,
374     UINT16_ARRAY_FUN_INDEX,
375     INT16_ARRAY_FUN_INDEX,
376     UINT32_ARRAY_FUN_INDEX,
377     INT32_ARRAY_FUN_INDEX,
378     FLOAT32_ARRAY_FUN_INDEX,
379     FLOAT64_ARRAY_FUN_INDEX,
380     UINT8_CLAMPED_ARRAY_FUN_INDEX,
381     INT8_ARRAY_EXTERNAL_MAP_INDEX,
382     UINT8_ARRAY_EXTERNAL_MAP_INDEX,
383     INT16_ARRAY_EXTERNAL_MAP_INDEX,
384     UINT16_ARRAY_EXTERNAL_MAP_INDEX,
385     INT32_ARRAY_EXTERNAL_MAP_INDEX,
386     UINT32_ARRAY_EXTERNAL_MAP_INDEX,
387     FLOAT32_ARRAY_EXTERNAL_MAP_INDEX,
388     FLOAT64_ARRAY_EXTERNAL_MAP_INDEX,
389     UINT8_CLAMPED_ARRAY_EXTERNAL_MAP_INDEX,
390     DATA_VIEW_FUN_INDEX,
391     SHARED_ARRAY_BUFFER_FUN_INDEX,
392     MESSAGE_LISTENERS_INDEX,
393     MAKE_MESSAGE_FUN_INDEX,
394     GET_STACK_TRACE_LINE_INDEX,
395     CONFIGURE_GLOBAL_INDEX,
396     FUNCTION_CACHE_INDEX,
397     JSFUNCTION_RESULT_CACHES_INDEX,
398     NORMALIZED_MAP_CACHE_INDEX,
399     RUNTIME_CONTEXT_INDEX,
400     CALL_AS_FUNCTION_DELEGATE_INDEX,
401     CALL_AS_CONSTRUCTOR_DELEGATE_INDEX,
402     SCRIPT_FUNCTION_INDEX,
403     OPAQUE_REFERENCE_FUNCTION_INDEX,
404     CONTEXT_EXTENSION_FUNCTION_INDEX,
405     OUT_OF_MEMORY_INDEX,
406     EMBEDDER_DATA_INDEX,
407     ALLOW_CODE_GEN_FROM_STRINGS_INDEX,
408     ERROR_MESSAGE_FOR_CODE_GEN_FROM_STRINGS_INDEX,
409     RUN_MICROTASKS_INDEX,
410     ENQUEUE_MICROTASK_INDEX,
411     PROMISE_STATUS_INDEX,
412     PROMISE_VALUE_INDEX,
413     PROMISE_CREATE_INDEX,
414     PROMISE_RESOLVE_INDEX,
415     PROMISE_REJECT_INDEX,
416     PROMISE_CHAIN_INDEX,
417     PROMISE_CATCH_INDEX,
418     PROMISE_THEN_INDEX,
419     TO_COMPLETE_PROPERTY_DESCRIPTOR_INDEX,
420     DERIVED_HAS_TRAP_INDEX,
421     DERIVED_GET_TRAP_INDEX,
422     DERIVED_SET_TRAP_INDEX,
423     PROXY_ENUMERATE_INDEX,
424     OBSERVERS_NOTIFY_CHANGE_INDEX,
425     OBSERVERS_ENQUEUE_SPLICE_INDEX,
426     OBSERVERS_BEGIN_SPLICE_INDEX,
427     OBSERVERS_END_SPLICE_INDEX,
428     NATIVE_OBJECT_OBSERVE_INDEX,
429     NATIVE_OBJECT_GET_NOTIFIER_INDEX,
430     NATIVE_OBJECT_NOTIFIER_PERFORM_CHANGE,
431     SLOPPY_GENERATOR_FUNCTION_MAP_INDEX,
432     STRICT_GENERATOR_FUNCTION_MAP_INDEX,
433     STRONG_GENERATOR_FUNCTION_MAP_INDEX,
434     GENERATOR_OBJECT_PROTOTYPE_MAP_INDEX,
435     ITERATOR_RESULT_MAP_INDEX,
436     JS_MAP_FUN_INDEX,
437     JS_MAP_MAP_INDEX,
438     JS_SET_FUN_INDEX,
439     JS_SET_MAP_INDEX,
440     MAP_GET_METHOD_INDEX,
441     MAP_SET_METHOD_INDEX,
442     MAP_HAS_METHOD_INDEX,
443     MAP_DELETE_METHOD_INDEX,
444     SET_ADD_METHOD_INDEX,
445     SET_HAS_METHOD_INDEX,
446     SET_DELETE_METHOD_INDEX,
447     MAP_FROM_ARRAY_INDEX,
448     SET_FROM_ARRAY_INDEX,
449     MAP_ITERATOR_MAP_INDEX,
450     SET_ITERATOR_MAP_INDEX,
451     ARRAY_VALUES_ITERATOR_INDEX,
452     SCRIPT_CONTEXT_TABLE_INDEX,
453     MAP_CACHE_INDEX,
454     STRONG_MAP_CACHE_INDEX,
455     TO_LENGTH_FUN_INDEX,
456     NATIVES_UTILS_OBJECT_INDEX,
457     EXTRAS_EXPORTS_OBJECT_INDEX,
458     CODE_STUB_EXPORTS_OBJECT_INDEX,
459
460     // Properties from here are treated as weak references by the full GC.
461     // Scavenge treats them as strong references.
462     OPTIMIZED_FUNCTIONS_LIST,  // Weak.
463     OPTIMIZED_CODE_LIST,       // Weak.
464     DEOPTIMIZED_CODE_LIST,     // Weak.
465     NEXT_CONTEXT_LINK,         // Weak.
466
467     // Total number of slots.
468     NATIVE_CONTEXT_SLOTS,
469     FIRST_WEAK_SLOT = OPTIMIZED_FUNCTIONS_LIST
470   };
471
472   // Direct slot access.
473   JSFunction* closure() { return JSFunction::cast(get(CLOSURE_INDEX)); }
474   void set_closure(JSFunction* closure) { set(CLOSURE_INDEX, closure); }
475
476   Context* previous() {
477     Object* result = unchecked_previous();
478     DCHECK(IsBootstrappingOrValidParentContext(result, this));
479     return reinterpret_cast<Context*>(result);
480   }
481   void set_previous(Context* context) { set(PREVIOUS_INDEX, context); }
482
483   bool has_extension() { return extension() != NULL; }
484   Object* extension() { return get(EXTENSION_INDEX); }
485   void set_extension(Object* object) { set(EXTENSION_INDEX, object); }
486
487   JSModule* module() { return JSModule::cast(get(EXTENSION_INDEX)); }
488   void set_module(JSModule* module) { set(EXTENSION_INDEX, module); }
489
490   // Get the context where var declarations will be hoisted to, which
491   // may be the context itself.
492   Context* declaration_context();
493
494   GlobalObject* global_object() {
495     Object* result = get(GLOBAL_OBJECT_INDEX);
496     DCHECK(IsBootstrappingOrGlobalObject(this->GetIsolate(), result));
497     return reinterpret_cast<GlobalObject*>(result);
498   }
499   void set_global_object(GlobalObject* object) {
500     set(GLOBAL_OBJECT_INDEX, object);
501   }
502
503   // Returns a JSGlobalProxy object or null.
504   JSObject* global_proxy();
505   void set_global_proxy(JSObject* global);
506
507   // The builtins object.
508   JSBuiltinsObject* builtins();
509
510   // Get the script context by traversing the context chain.
511   Context* script_context();
512
513   // Compute the native context by traversing the context chain.
514   Context* native_context();
515
516   // Predicates for context types.  IsNativeContext is also defined on Object
517   // because we frequently have to know if arbitrary objects are natives
518   // contexts.
519   bool IsNativeContext() {
520     Map* map = this->map();
521     return map == map->GetHeap()->native_context_map();
522   }
523   bool IsFunctionContext() {
524     Map* map = this->map();
525     return map == map->GetHeap()->function_context_map();
526   }
527   bool IsCatchContext() {
528     Map* map = this->map();
529     return map == map->GetHeap()->catch_context_map();
530   }
531   bool IsWithContext() {
532     Map* map = this->map();
533     return map == map->GetHeap()->with_context_map();
534   }
535   bool IsBlockContext() {
536     Map* map = this->map();
537     return map == map->GetHeap()->block_context_map();
538   }
539   bool IsModuleContext() {
540     Map* map = this->map();
541     return map == map->GetHeap()->module_context_map();
542   }
543   bool IsScriptContext() {
544     Map* map = this->map();
545     return map == map->GetHeap()->script_context_map();
546   }
547
548   bool HasSameSecurityTokenAs(Context* that) {
549     return this->global_object()->native_context()->security_token() ==
550         that->global_object()->native_context()->security_token();
551   }
552
553   // Initializes global variable bindings in given script context.
554   void InitializeGlobalSlots();
555
556   // A native context holds a list of all functions with optimized code.
557   void AddOptimizedFunction(JSFunction* function);
558   void RemoveOptimizedFunction(JSFunction* function);
559   void SetOptimizedFunctionsListHead(Object* head);
560   Object* OptimizedFunctionsListHead();
561
562   // The native context also stores a list of all optimized code and a
563   // list of all deoptimized code, which are needed by the deoptimizer.
564   void AddOptimizedCode(Code* code);
565   void SetOptimizedCodeListHead(Object* head);
566   Object* OptimizedCodeListHead();
567   void SetDeoptimizedCodeListHead(Object* head);
568   Object* DeoptimizedCodeListHead();
569
570   Handle<Object> ErrorMessageForCodeGenerationFromStrings();
571
572 #define NATIVE_CONTEXT_FIELD_ACCESSORS(index, type, name) \
573   void  set_##name(type* value) {                         \
574     DCHECK(IsNativeContext());                            \
575     set(index, value);                                    \
576   }                                                       \
577   bool is_##name(type* value) {                           \
578     DCHECK(IsNativeContext());                            \
579     return type::cast(get(index)) == value;               \
580   }                                                       \
581   type* name() {                                          \
582     DCHECK(IsNativeContext());                            \
583     return type::cast(get(index));                        \
584   }
585   NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_FIELD_ACCESSORS)
586 #undef NATIVE_CONTEXT_FIELD_ACCESSORS
587
588   // Lookup the slot called name, starting with the current context.
589   // There are three possibilities:
590   //
591   // 1) result->IsContext():
592   //    The binding was found in a context.  *index is always the
593   //    non-negative slot index.  *attributes is NONE for var and let
594   //    declarations, READ_ONLY for const declarations (never ABSENT).
595   //
596   // 2) result->IsJSObject():
597   //    The binding was found as a named property in a context extension
598   //    object (i.e., was introduced via eval), as a property on the subject
599   //    of with, or as a property of the global object.  *index is -1 and
600   //    *attributes is not ABSENT.
601   //
602   // 3) result.is_null():
603   //    There was no binding found, *index is always -1 and *attributes is
604   //    always ABSENT.
605   Handle<Object> Lookup(Handle<String> name,
606                         ContextLookupFlags flags,
607                         int* index,
608                         PropertyAttributes* attributes,
609                         BindingFlags* binding_flags);
610
611   // Code generation support.
612   static int SlotOffset(int index) {
613     return kHeaderSize + index * kPointerSize - kHeapObjectTag;
614   }
615
616   static int FunctionMapIndex(LanguageMode language_mode, FunctionKind kind) {
617     if (IsGeneratorFunction(kind)) {
618       return is_strong(language_mode) ? STRONG_GENERATOR_FUNCTION_MAP_INDEX :
619              is_strict(language_mode) ? STRICT_GENERATOR_FUNCTION_MAP_INDEX
620                                       : SLOPPY_GENERATOR_FUNCTION_MAP_INDEX;
621     }
622
623     if (IsConstructor(kind)) {
624       // Use strict function map (no own "caller" / "arguments")
625       return is_strong(language_mode) ? STRONG_CONSTRUCTOR_MAP_INDEX
626                                       : STRICT_FUNCTION_MAP_INDEX;
627     }
628
629     if (IsArrowFunction(kind) || IsConciseMethod(kind) ||
630         IsAccessorFunction(kind)) {
631       return is_strong(language_mode)
632                  ? STRONG_FUNCTION_MAP_INDEX
633                  : STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX;
634     }
635
636     return is_strong(language_mode) ? STRONG_FUNCTION_MAP_INDEX :
637            is_strict(language_mode) ? STRICT_FUNCTION_MAP_INDEX
638                                     : SLOPPY_FUNCTION_MAP_INDEX;
639   }
640
641   static const int kSize = kHeaderSize + NATIVE_CONTEXT_SLOTS * kPointerSize;
642
643   // GC support.
644   typedef FixedBodyDescriptor<
645       kHeaderSize, kSize, kSize> ScavengeBodyDescriptor;
646
647   typedef FixedBodyDescriptor<
648       kHeaderSize,
649       kHeaderSize + FIRST_WEAK_SLOT * kPointerSize,
650       kSize> MarkCompactBodyDescriptor;
651
652  private:
653   // Unchecked access to the slots.
654   Object* unchecked_previous() { return get(PREVIOUS_INDEX); }
655
656 #ifdef DEBUG
657   // Bootstrapping-aware type checks.
658   static bool IsBootstrappingOrValidParentContext(Object* object, Context* kid);
659   static bool IsBootstrappingOrGlobalObject(Isolate* isolate, Object* object);
660 #endif
661
662   STATIC_ASSERT(kHeaderSize == Internals::kContextHeaderSize);
663   STATIC_ASSERT(EMBEDDER_DATA_INDEX == Internals::kContextEmbedderDataIndex);
664 };
665
666 } }  // namespace v8::internal
667
668 #endif  // V8_CONTEXTS_H_