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