Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / v8 / src / contexts.h
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 //     * Redistributions of source code must retain the above copyright
7 //       notice, this list of conditions and the following disclaimer.
8 //     * Redistributions in binary form must reproduce the above
9 //       copyright notice, this list of conditions and the following
10 //       disclaimer in the documentation and/or other materials provided
11 //       with the distribution.
12 //     * Neither the name of Google Inc. nor the names of its
13 //       contributors may be used to endorse or promote products derived
14 //       from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 #ifndef V8_CONTEXTS_H_
29 #define V8_CONTEXTS_H_
30
31 #include "heap.h"
32 #include "objects.h"
33
34 namespace v8 {
35 namespace internal {
36
37
38 enum ContextLookupFlags {
39   FOLLOW_CONTEXT_CHAIN = 1,
40   FOLLOW_PROTOTYPE_CHAIN = 2,
41
42   DONT_FOLLOW_CHAINS = 0,
43   FOLLOW_CHAINS = FOLLOW_CONTEXT_CHAIN | FOLLOW_PROTOTYPE_CHAIN
44 };
45
46
47 // ES5 10.2 defines lexical environments with mutable and immutable bindings.
48 // Immutable bindings have two states, initialized and uninitialized, and
49 // their state is changed by the InitializeImmutableBinding method. The
50 // BindingFlags enum represents information if a binding has definitely been
51 // initialized. A mutable binding does not need to be checked and thus has
52 // the BindingFlag MUTABLE_IS_INITIALIZED.
53 //
54 // There are two possibilities for immutable bindings
55 //  * 'const' declared variables. They are initialized when evaluating the
56 //    corresponding declaration statement. They need to be checked for being
57 //    initialized and thus get the flag IMMUTABLE_CHECK_INITIALIZED.
58 //  * The function name of a named function literal. The binding is immediately
59 //    initialized when entering the function and thus does not need to be
60 //    checked. it gets the BindingFlag IMMUTABLE_IS_INITIALIZED.
61 // Accessing an uninitialized binding produces the undefined value.
62 //
63 // The harmony proposal for block scoped bindings also introduces the
64 // uninitialized state for mutable bindings.
65 //  * A 'let' declared variable. They are initialized when evaluating the
66 //    corresponding declaration statement. They need to be checked for being
67 //    initialized and thus get the flag MUTABLE_CHECK_INITIALIZED.
68 //  * A 'var' declared variable. It is initialized immediately upon creation
69 //    and thus doesn't need to be checked. It gets the flag
70 //    MUTABLE_IS_INITIALIZED.
71 //  * Catch bound variables, function parameters and variables introduced by
72 //    function declarations are initialized immediately and do not need to be
73 //    checked. Thus they get the flag MUTABLE_IS_INITIALIZED.
74 // Immutable bindings in harmony mode get the _HARMONY flag variants. Accessing
75 // an uninitialized binding produces a reference error.
76 //
77 // In V8 uninitialized bindings are set to the hole value upon creation and set
78 // to a different value upon initialization.
79 enum BindingFlags {
80   MUTABLE_IS_INITIALIZED,
81   MUTABLE_CHECK_INITIALIZED,
82   IMMUTABLE_IS_INITIALIZED,
83   IMMUTABLE_CHECK_INITIALIZED,
84   IMMUTABLE_IS_INITIALIZED_HARMONY,
85   IMMUTABLE_CHECK_INITIALIZED_HARMONY,
86   MISSING_BINDING
87 };
88
89
90 // Heap-allocated activation contexts.
91 //
92 // Contexts are implemented as FixedArray objects; the Context
93 // class is a convenience interface casted on a FixedArray object.
94 //
95 // Note: Context must have no virtual functions and Context objects
96 // must always be allocated via Heap::AllocateContext() or
97 // Factory::NewContext.
98
99 #define NATIVE_CONTEXT_FIELDS(V) \
100   V(GLOBAL_PROXY_INDEX, JSObject, global_proxy_object) \
101   V(SECURITY_TOKEN_INDEX, Object, security_token) \
102   V(BOOLEAN_FUNCTION_INDEX, JSFunction, boolean_function) \
103   V(NUMBER_FUNCTION_INDEX, JSFunction, number_function) \
104   V(FLOAT32x4_FUNCTION_INDEX, JSFunction, float32x4_function) \
105   V(FLOAT32x4_FUNCTION_PROTOTYPE_MAP_INDEX, Map, \
106     float32x4_function_prototype_map) \
107   V(INT32x4_FUNCTION_INDEX, JSFunction, int32x4_function) \
108   V(INT32x4_FUNCTION_PROTOTYPE_MAP_INDEX, Map, \
109     int32x4_function_prototype_map) \
110   V(STRING_FUNCTION_INDEX, JSFunction, string_function) \
111   V(STRING_FUNCTION_PROTOTYPE_MAP_INDEX, Map, string_function_prototype_map) \
112   V(SYMBOL_FUNCTION_INDEX, JSFunction, symbol_function) \
113   V(OBJECT_FUNCTION_INDEX, JSFunction, object_function) \
114   V(INTERNAL_ARRAY_FUNCTION_INDEX, JSFunction, internal_array_function) \
115   V(ARRAY_FUNCTION_INDEX, JSFunction, array_function) \
116   V(JS_ARRAY_MAPS_INDEX, Object, js_array_maps) \
117   V(DATE_FUNCTION_INDEX, JSFunction, date_function) \
118   V(JSON_OBJECT_INDEX, JSObject, json_object) \
119   V(SIMD_OBJECT_INDEX, JSObject, simd_object) \
120   V(REGEXP_FUNCTION_INDEX, JSFunction, regexp_function) \
121   V(INITIAL_OBJECT_PROTOTYPE_INDEX, JSObject, initial_object_prototype) \
122   V(INITIAL_ARRAY_PROTOTYPE_INDEX, JSObject, initial_array_prototype) \
123   V(CREATE_DATE_FUN_INDEX, JSFunction,  create_date_fun) \
124   V(TO_NUMBER_FUN_INDEX, JSFunction, to_number_fun) \
125   V(TO_STRING_FUN_INDEX, JSFunction, to_string_fun) \
126   V(TO_DETAIL_STRING_FUN_INDEX, JSFunction, to_detail_string_fun) \
127   V(TO_OBJECT_FUN_INDEX, JSFunction, to_object_fun) \
128   V(TO_INTEGER_FUN_INDEX, JSFunction, to_integer_fun) \
129   V(TO_UINT32_FUN_INDEX, JSFunction, to_uint32_fun) \
130   V(TO_INT32_FUN_INDEX, JSFunction, to_int32_fun) \
131   V(GLOBAL_EVAL_FUN_INDEX, JSFunction, global_eval_fun) \
132   V(INSTANTIATE_FUN_INDEX, JSFunction, instantiate_fun) \
133   V(CONFIGURE_INSTANCE_FUN_INDEX, JSFunction, configure_instance_fun) \
134   V(ARRAY_BUFFER_FUN_INDEX, JSFunction, array_buffer_fun) \
135   V(UINT8_ARRAY_FUN_INDEX, JSFunction, uint8_array_fun) \
136   V(INT8_ARRAY_FUN_INDEX, JSFunction, int8_array_fun) \
137   V(UINT16_ARRAY_FUN_INDEX, JSFunction, uint16_array_fun) \
138   V(INT16_ARRAY_FUN_INDEX, JSFunction, int16_array_fun) \
139   V(UINT32_ARRAY_FUN_INDEX, JSFunction, uint32_array_fun) \
140   V(INT32_ARRAY_FUN_INDEX, JSFunction, int32_array_fun) \
141   V(FLOAT32_ARRAY_FUN_INDEX, JSFunction, float32_array_fun) \
142   V(FLOAT64_ARRAY_FUN_INDEX, JSFunction, float64_array_fun) \
143   V(FLOAT32x4_ARRAY_FUN_INDEX, JSFunction, float32x4_array_fun) \
144   V(INT32x4_ARRAY_FUN_INDEX, JSFunction, int32x4_array_fun) \
145   V(UINT8_CLAMPED_ARRAY_FUN_INDEX, JSFunction, uint8_clamped_array_fun) \
146   V(DATA_VIEW_FUN_INDEX, JSFunction, data_view_fun) \
147   V(FUNCTION_MAP_INDEX, Map, function_map) \
148   V(STRICT_MODE_FUNCTION_MAP_INDEX, Map, strict_mode_function_map) \
149   V(FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map, function_without_prototype_map) \
150   V(STRICT_MODE_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map, \
151     strict_mode_function_without_prototype_map) \
152   V(REGEXP_RESULT_MAP_INDEX, Map, regexp_result_map)\
153   V(ARGUMENTS_BOILERPLATE_INDEX, JSObject, arguments_boilerplate) \
154   V(ALIASED_ARGUMENTS_BOILERPLATE_INDEX, JSObject, \
155     aliased_arguments_boilerplate) \
156   V(STRICT_MODE_ARGUMENTS_BOILERPLATE_INDEX, JSObject, \
157     strict_mode_arguments_boilerplate) \
158   V(MESSAGE_LISTENERS_INDEX, JSObject, message_listeners) \
159   V(MAKE_MESSAGE_FUN_INDEX, JSFunction, make_message_fun) \
160   V(GET_STACK_TRACE_LINE_INDEX, JSFunction, get_stack_trace_line_fun) \
161   V(CONFIGURE_GLOBAL_INDEX, JSFunction, configure_global_fun) \
162   V(FUNCTION_CACHE_INDEX, JSObject, function_cache) \
163   V(JSFUNCTION_RESULT_CACHES_INDEX, FixedArray, jsfunction_result_caches) \
164   V(NORMALIZED_MAP_CACHE_INDEX, NormalizedMapCache, normalized_map_cache) \
165   V(RUNTIME_CONTEXT_INDEX, Context, runtime_context) \
166   V(CALL_AS_FUNCTION_DELEGATE_INDEX, JSFunction, call_as_function_delegate) \
167   V(CALL_AS_CONSTRUCTOR_DELEGATE_INDEX, JSFunction, \
168     call_as_constructor_delegate) \
169   V(SCRIPT_FUNCTION_INDEX, JSFunction, script_function) \
170   V(OPAQUE_REFERENCE_FUNCTION_INDEX, JSFunction, opaque_reference_function) \
171   V(CONTEXT_EXTENSION_FUNCTION_INDEX, JSFunction, context_extension_function) \
172   V(OUT_OF_MEMORY_INDEX, Object, out_of_memory) \
173   V(MAP_CACHE_INDEX, Object, map_cache) \
174   V(EMBEDDER_DATA_INDEX, FixedArray, embedder_data) \
175   V(ALLOW_CODE_GEN_FROM_STRINGS_INDEX, Object, allow_code_gen_from_strings) \
176   V(ERROR_MESSAGE_FOR_CODE_GEN_FROM_STRINGS_INDEX, Object, \
177     error_message_for_code_gen_from_strings) \
178   V(RUN_MICROTASKS_INDEX, JSFunction, run_microtasks) \
179   V(TO_COMPLETE_PROPERTY_DESCRIPTOR_INDEX, JSFunction, \
180     to_complete_property_descriptor) \
181   V(DERIVED_HAS_TRAP_INDEX, JSFunction, derived_has_trap) \
182   V(DERIVED_GET_TRAP_INDEX, JSFunction, derived_get_trap) \
183   V(DERIVED_SET_TRAP_INDEX, JSFunction, derived_set_trap) \
184   V(PROXY_ENUMERATE_INDEX, JSFunction, proxy_enumerate) \
185   V(OBSERVERS_NOTIFY_CHANGE_INDEX, JSFunction, observers_notify_change) \
186   V(OBSERVERS_ENQUEUE_SPLICE_INDEX, JSFunction, observers_enqueue_splice) \
187   V(OBSERVERS_BEGIN_SPLICE_INDEX, JSFunction, \
188     observers_begin_perform_splice) \
189   V(OBSERVERS_END_SPLICE_INDEX, JSFunction, \
190     observers_end_perform_splice) \
191   V(GENERATOR_FUNCTION_MAP_INDEX, Map, generator_function_map) \
192   V(STRICT_MODE_GENERATOR_FUNCTION_MAP_INDEX, Map, \
193     strict_mode_generator_function_map) \
194   V(GENERATOR_OBJECT_PROTOTYPE_MAP_INDEX, Map, \
195     generator_object_prototype_map) \
196   V(GENERATOR_RESULT_MAP_INDEX, Map, generator_result_map)
197
198 // JSFunctions are pairs (context, function code), sometimes also called
199 // closures. A Context object is used to represent function contexts and
200 // dynamically pushed 'with' contexts (or 'scopes' in ECMA-262 speak).
201 //
202 // At runtime, the contexts build a stack in parallel to the execution
203 // stack, with the top-most context being the current context. All contexts
204 // have the following slots:
205 //
206 // [ closure   ]  This is the current function. It is the same for all
207 //                contexts inside a function. It provides access to the
208 //                incoming context (i.e., the outer context, which may
209 //                or may not become the current function's context), and
210 //                it provides access to the functions code and thus it's
211 //                scope information, which in turn contains the names of
212 //                statically allocated context slots. The names are needed
213 //                for dynamic lookups in the presence of 'with' or 'eval'.
214 //
215 // [ previous  ]  A pointer to the previous context. It is NULL for
216 //                function contexts, and non-NULL for 'with' contexts.
217 //                Used to implement the 'with' statement.
218 //
219 // [ extension ]  A pointer to an extension JSObject, or NULL. Used to
220 //                implement 'with' statements and dynamic declarations
221 //                (through 'eval'). The object in a 'with' statement is
222 //                stored in the extension slot of a 'with' context.
223 //                Dynamically declared variables/functions are also added
224 //                to lazily allocated extension object. Context::Lookup
225 //                searches the extension object for properties.
226 //                For global and block contexts, contains the respective
227 //                ScopeInfo.
228 //                For module contexts, points back to the respective JSModule.
229 //
230 // [ global_object ]  A pointer to the global object. Provided for quick
231 //                access to the global object from inside the code (since
232 //                we always have a context pointer).
233 //
234 // In addition, function contexts may have statically allocated context slots
235 // to store local variables/functions that are accessed from inner functions
236 // (via static context addresses) or through 'eval' (dynamic context lookups).
237 // Finally, the native context contains additional slots for fast access to
238 // native properties.
239
240 class Context: public FixedArray {
241  public:
242   // Conversions.
243   static Context* cast(Object* context) {
244     ASSERT(context->IsContext());
245     return reinterpret_cast<Context*>(context);
246   }
247
248   // The default context slot layout; indices are FixedArray slot indices.
249   enum {
250     // These slots are in all contexts.
251     CLOSURE_INDEX,
252     PREVIOUS_INDEX,
253     // The extension slot is used for either the global object (in global
254     // contexts), eval extension object (function contexts), subject of with
255     // (with contexts), or the variable name (catch contexts), the serialized
256     // scope info (block contexts), or the module instance (module contexts).
257     EXTENSION_INDEX,
258     GLOBAL_OBJECT_INDEX,
259     MIN_CONTEXT_SLOTS,
260
261     // This slot holds the thrown value in catch contexts.
262     THROWN_OBJECT_INDEX = MIN_CONTEXT_SLOTS,
263
264     // These slots are only in native contexts.
265     GLOBAL_PROXY_INDEX = MIN_CONTEXT_SLOTS,
266     SECURITY_TOKEN_INDEX,
267     ARGUMENTS_BOILERPLATE_INDEX,
268     ALIASED_ARGUMENTS_BOILERPLATE_INDEX,
269     STRICT_MODE_ARGUMENTS_BOILERPLATE_INDEX,
270     REGEXP_RESULT_MAP_INDEX,
271     FUNCTION_MAP_INDEX,
272     STRICT_MODE_FUNCTION_MAP_INDEX,
273     FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX,
274     STRICT_MODE_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX,
275     INITIAL_OBJECT_PROTOTYPE_INDEX,
276     INITIAL_ARRAY_PROTOTYPE_INDEX,
277     BOOLEAN_FUNCTION_INDEX,
278     NUMBER_FUNCTION_INDEX,
279     FLOAT32x4_FUNCTION_INDEX,
280     FLOAT32x4_FUNCTION_PROTOTYPE_MAP_INDEX,
281     INT32x4_FUNCTION_INDEX,
282     INT32x4_FUNCTION_PROTOTYPE_MAP_INDEX,
283     STRING_FUNCTION_INDEX,
284     STRING_FUNCTION_PROTOTYPE_MAP_INDEX,
285     SYMBOL_FUNCTION_INDEX,
286     OBJECT_FUNCTION_INDEX,
287     INTERNAL_ARRAY_FUNCTION_INDEX,
288     ARRAY_FUNCTION_INDEX,
289     JS_ARRAY_MAPS_INDEX,
290     DATE_FUNCTION_INDEX,
291     JSON_OBJECT_INDEX,
292     SIMD_OBJECT_INDEX,
293     REGEXP_FUNCTION_INDEX,
294     CREATE_DATE_FUN_INDEX,
295     TO_NUMBER_FUN_INDEX,
296     TO_STRING_FUN_INDEX,
297     TO_DETAIL_STRING_FUN_INDEX,
298     TO_OBJECT_FUN_INDEX,
299     TO_INTEGER_FUN_INDEX,
300     TO_UINT32_FUN_INDEX,
301     TO_INT32_FUN_INDEX,
302     TO_BOOLEAN_FUN_INDEX,
303     GLOBAL_EVAL_FUN_INDEX,
304     INSTANTIATE_FUN_INDEX,
305     CONFIGURE_INSTANCE_FUN_INDEX,
306     ARRAY_BUFFER_FUN_INDEX,
307     UINT8_ARRAY_FUN_INDEX,
308     INT8_ARRAY_FUN_INDEX,
309     UINT16_ARRAY_FUN_INDEX,
310     INT16_ARRAY_FUN_INDEX,
311     UINT32_ARRAY_FUN_INDEX,
312     INT32_ARRAY_FUN_INDEX,
313     FLOAT32_ARRAY_FUN_INDEX,
314     FLOAT32x4_ARRAY_FUN_INDEX,
315     INT32x4_ARRAY_FUN_INDEX,
316     FLOAT64_ARRAY_FUN_INDEX,
317     UINT8_CLAMPED_ARRAY_FUN_INDEX,
318     DATA_VIEW_FUN_INDEX,
319     MESSAGE_LISTENERS_INDEX,
320     MAKE_MESSAGE_FUN_INDEX,
321     GET_STACK_TRACE_LINE_INDEX,
322     CONFIGURE_GLOBAL_INDEX,
323     FUNCTION_CACHE_INDEX,
324     JSFUNCTION_RESULT_CACHES_INDEX,
325     NORMALIZED_MAP_CACHE_INDEX,
326     RUNTIME_CONTEXT_INDEX,
327     CALL_AS_FUNCTION_DELEGATE_INDEX,
328     CALL_AS_CONSTRUCTOR_DELEGATE_INDEX,
329     SCRIPT_FUNCTION_INDEX,
330     OPAQUE_REFERENCE_FUNCTION_INDEX,
331     CONTEXT_EXTENSION_FUNCTION_INDEX,
332     OUT_OF_MEMORY_INDEX,
333     EMBEDDER_DATA_INDEX,
334     ALLOW_CODE_GEN_FROM_STRINGS_INDEX,
335     ERROR_MESSAGE_FOR_CODE_GEN_FROM_STRINGS_INDEX,
336     RUN_MICROTASKS_INDEX,
337     TO_COMPLETE_PROPERTY_DESCRIPTOR_INDEX,
338     DERIVED_HAS_TRAP_INDEX,
339     DERIVED_GET_TRAP_INDEX,
340     DERIVED_SET_TRAP_INDEX,
341     PROXY_ENUMERATE_INDEX,
342     OBSERVERS_NOTIFY_CHANGE_INDEX,
343     OBSERVERS_ENQUEUE_SPLICE_INDEX,
344     OBSERVERS_BEGIN_SPLICE_INDEX,
345     OBSERVERS_END_SPLICE_INDEX,
346     GENERATOR_FUNCTION_MAP_INDEX,
347     STRICT_MODE_GENERATOR_FUNCTION_MAP_INDEX,
348     GENERATOR_OBJECT_PROTOTYPE_MAP_INDEX,
349     GENERATOR_RESULT_MAP_INDEX,
350
351     // Properties from here are treated as weak references by the full GC.
352     // Scavenge treats them as strong references.
353     OPTIMIZED_FUNCTIONS_LIST,  // Weak.
354     OPTIMIZED_CODE_LIST,       // Weak.
355     DEOPTIMIZED_CODE_LIST,     // Weak.
356     MAP_CACHE_INDEX,           // Weak.
357     NEXT_CONTEXT_LINK,         // Weak.
358
359     // Total number of slots.
360     NATIVE_CONTEXT_SLOTS,
361
362     FIRST_WEAK_SLOT = OPTIMIZED_FUNCTIONS_LIST
363   };
364
365   // Direct slot access.
366   JSFunction* closure() { return JSFunction::cast(get(CLOSURE_INDEX)); }
367   void set_closure(JSFunction* closure) { set(CLOSURE_INDEX, closure); }
368
369   Context* previous() {
370     Object* result = unchecked_previous();
371     ASSERT(IsBootstrappingOrValidParentContext(result, this));
372     return reinterpret_cast<Context*>(result);
373   }
374   void set_previous(Context* context) { set(PREVIOUS_INDEX, context); }
375
376   bool has_extension() { return extension() != NULL; }
377   Object* extension() { return get(EXTENSION_INDEX); }
378   void set_extension(Object* object) { set(EXTENSION_INDEX, object); }
379
380   JSModule* module() { return JSModule::cast(get(EXTENSION_INDEX)); }
381   void set_module(JSModule* module) { set(EXTENSION_INDEX, module); }
382
383   // Get the context where var declarations will be hoisted to, which
384   // may be the context itself.
385   Context* declaration_context();
386
387   GlobalObject* global_object() {
388     Object* result = get(GLOBAL_OBJECT_INDEX);
389     ASSERT(IsBootstrappingOrGlobalObject(this->GetIsolate(), result));
390     return reinterpret_cast<GlobalObject*>(result);
391   }
392   void set_global_object(GlobalObject* object) {
393     set(GLOBAL_OBJECT_INDEX, object);
394   }
395
396   // Returns a JSGlobalProxy object or null.
397   JSObject* global_proxy();
398   void set_global_proxy(JSObject* global);
399
400   // The builtins object.
401   JSBuiltinsObject* builtins();
402
403   // Get the innermost global context by traversing the context chain.
404   Context* global_context();
405
406   // Compute the native context by traversing the context chain.
407   Context* native_context();
408
409   // Predicates for context types.  IsNativeContext is also defined on Object
410   // because we frequently have to know if arbitrary objects are natives
411   // contexts.
412   bool IsNativeContext() {
413     Map* map = this->map();
414     return map == map->GetHeap()->native_context_map();
415   }
416   bool IsFunctionContext() {
417     Map* map = this->map();
418     return map == map->GetHeap()->function_context_map();
419   }
420   bool IsCatchContext() {
421     Map* map = this->map();
422     return map == map->GetHeap()->catch_context_map();
423   }
424   bool IsWithContext() {
425     Map* map = this->map();
426     return map == map->GetHeap()->with_context_map();
427   }
428   bool IsBlockContext() {
429     Map* map = this->map();
430     return map == map->GetHeap()->block_context_map();
431   }
432   bool IsModuleContext() {
433     Map* map = this->map();
434     return map == map->GetHeap()->module_context_map();
435   }
436   bool IsGlobalContext() {
437     Map* map = this->map();
438     return map == map->GetHeap()->global_context_map();
439   }
440
441   // Tells whether the native context is marked with out of memory.
442   inline bool has_out_of_memory();
443
444   // Mark the native context with out of memory.
445   inline void mark_out_of_memory();
446
447   // A native context holds a list of all functions with optimized code.
448   void AddOptimizedFunction(JSFunction* function);
449   void RemoveOptimizedFunction(JSFunction* function);
450   void SetOptimizedFunctionsListHead(Object* head);
451   Object* OptimizedFunctionsListHead();
452
453   // The native context also stores a list of all optimized code and a
454   // list of all deoptimized code, which are needed by the deoptimizer.
455   void AddOptimizedCode(Code* code);
456   void SetOptimizedCodeListHead(Object* head);
457   Object* OptimizedCodeListHead();
458   void SetDeoptimizedCodeListHead(Object* head);
459   Object* DeoptimizedCodeListHead();
460
461   Handle<Object> ErrorMessageForCodeGenerationFromStrings();
462
463 #define NATIVE_CONTEXT_FIELD_ACCESSORS(index, type, name) \
464   void  set_##name(type* value) {                         \
465     ASSERT(IsNativeContext());                            \
466     set(index, value);                                    \
467   }                                                       \
468   bool is_##name(type* value) {                           \
469     ASSERT(IsNativeContext());                            \
470     return type::cast(get(index)) == value;               \
471   }                                                       \
472   type* name() {                                          \
473     ASSERT(IsNativeContext());                            \
474     return type::cast(get(index));                        \
475   }
476   NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_FIELD_ACCESSORS)
477 #undef NATIVE_CONTEXT_FIELD_ACCESSORS
478
479   // Lookup the slot called name, starting with the current context.
480   // There are three possibilities:
481   //
482   // 1) result->IsContext():
483   //    The binding was found in a context.  *index is always the
484   //    non-negative slot index.  *attributes is NONE for var and let
485   //    declarations, READ_ONLY for const declarations (never ABSENT).
486   //
487   // 2) result->IsJSObject():
488   //    The binding was found as a named property in a context extension
489   //    object (i.e., was introduced via eval), as a property on the subject
490   //    of with, or as a property of the global object.  *index is -1 and
491   //    *attributes is not ABSENT.
492   //
493   // 3) result.is_null():
494   //    There was no binding found, *index is always -1 and *attributes is
495   //    always ABSENT.
496   Handle<Object> Lookup(Handle<String> name,
497                         ContextLookupFlags flags,
498                         int* index,
499                         PropertyAttributes* attributes,
500                         BindingFlags* binding_flags);
501
502   // Code generation support.
503   static int SlotOffset(int index) {
504     return kHeaderSize + index * kPointerSize - kHeapObjectTag;
505   }
506
507   static int FunctionMapIndex(LanguageMode language_mode, bool is_generator) {
508     return is_generator
509       ? (language_mode == CLASSIC_MODE
510          ? GENERATOR_FUNCTION_MAP_INDEX
511          : STRICT_MODE_GENERATOR_FUNCTION_MAP_INDEX)
512       : (language_mode == CLASSIC_MODE
513          ? FUNCTION_MAP_INDEX
514          : STRICT_MODE_FUNCTION_MAP_INDEX);
515   }
516
517   static const int kSize = kHeaderSize + NATIVE_CONTEXT_SLOTS * kPointerSize;
518
519   // GC support.
520   typedef FixedBodyDescriptor<
521       kHeaderSize, kSize, kSize> ScavengeBodyDescriptor;
522
523   typedef FixedBodyDescriptor<
524       kHeaderSize,
525       kHeaderSize + FIRST_WEAK_SLOT * kPointerSize,
526       kSize> MarkCompactBodyDescriptor;
527
528  private:
529   // Unchecked access to the slots.
530   Object* unchecked_previous() { return get(PREVIOUS_INDEX); }
531
532 #ifdef DEBUG
533   // Bootstrapping-aware type checks.
534   static bool IsBootstrappingOrValidParentContext(Object* object, Context* kid);
535   static bool IsBootstrappingOrGlobalObject(Isolate* isolate, Object* object);
536 #endif
537
538   STATIC_CHECK(kHeaderSize == Internals::kContextHeaderSize);
539   STATIC_CHECK(EMBEDDER_DATA_INDEX == Internals::kContextEmbedderDataIndex);
540 };
541
542 } }  // namespace v8::internal
543
544 #endif  // V8_CONTEXTS_H_