blob: 10ef33d1ac55c0929a911461162102b8953f65ad [file] [log] [blame]
Ben Murdoch257744e2011-11-30 15:57:28 +00001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// 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
Kristian Monsen80d68ea2010-09-08 11:05:35 +010031#include "heap.h"
32#include "objects.h"
33
Steve Blocka7e24c12009-10-30 11:49:00 +000034namespace v8 {
35namespace internal {
36
37
38enum 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
Ben Murdoch69a99ed2011-11-30 16:03:39 +000047// ES5 10.2 defines lexical environments with mutable and immutable bindings.
48// Immutable bindings have two states, initialized and uninitialized, and
Ben Murdoch592a9fc2012-03-05 11:04:45 +000049// 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.
Ben Murdoch69a99ed2011-11-30 16:03:39 +000062//
63// The harmony proposal for block scoped bindings also introduces the
Ben Murdoch592a9fc2012-03-05 11:04:45 +000064// 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.
Ben Murdoch69a99ed2011-11-30 16:03:39 +000079enum BindingFlags {
80 MUTABLE_IS_INITIALIZED,
81 MUTABLE_CHECK_INITIALIZED,
82 IMMUTABLE_IS_INITIALIZED,
83 IMMUTABLE_CHECK_INITIALIZED,
Ben Murdoch592a9fc2012-03-05 11:04:45 +000084 IMMUTABLE_IS_INITIALIZED_HARMONY,
85 IMMUTABLE_CHECK_INITIALIZED_HARMONY,
Ben Murdoch69a99ed2011-11-30 16:03:39 +000086 MISSING_BINDING
87};
88
89
Steve Blocka7e24c12009-10-30 11:49:00 +000090// 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
Steve Blocka7e24c12009-10-30 11:49:00 +000099#define GLOBAL_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(STRING_FUNCTION_INDEX, JSFunction, string_function) \
Iain Merrick75681382010-08-19 15:07:18 +0100105 V(STRING_FUNCTION_PROTOTYPE_MAP_INDEX, Map, string_function_prototype_map) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000106 V(OBJECT_FUNCTION_INDEX, JSFunction, object_function) \
107 V(ARRAY_FUNCTION_INDEX, JSFunction, array_function) \
108 V(DATE_FUNCTION_INDEX, JSFunction, date_function) \
109 V(JSON_OBJECT_INDEX, JSObject, json_object) \
110 V(REGEXP_FUNCTION_INDEX, JSFunction, regexp_function) \
111 V(INITIAL_OBJECT_PROTOTYPE_INDEX, JSObject, initial_object_prototype) \
112 V(CREATE_DATE_FUN_INDEX, JSFunction, create_date_fun) \
113 V(TO_NUMBER_FUN_INDEX, JSFunction, to_number_fun) \
114 V(TO_STRING_FUN_INDEX, JSFunction, to_string_fun) \
115 V(TO_DETAIL_STRING_FUN_INDEX, JSFunction, to_detail_string_fun) \
116 V(TO_OBJECT_FUN_INDEX, JSFunction, to_object_fun) \
117 V(TO_INTEGER_FUN_INDEX, JSFunction, to_integer_fun) \
118 V(TO_UINT32_FUN_INDEX, JSFunction, to_uint32_fun) \
119 V(TO_INT32_FUN_INDEX, JSFunction, to_int32_fun) \
Leon Clarkee46be812010-01-19 14:06:41 +0000120 V(GLOBAL_EVAL_FUN_INDEX, JSFunction, global_eval_fun) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000121 V(INSTANTIATE_FUN_INDEX, JSFunction, instantiate_fun) \
122 V(CONFIGURE_INSTANCE_FUN_INDEX, JSFunction, configure_instance_fun) \
123 V(FUNCTION_MAP_INDEX, Map, function_map) \
Steve Block44f0eee2011-05-26 01:26:41 +0100124 V(STRICT_MODE_FUNCTION_MAP_INDEX, Map, strict_mode_function_map) \
Steve Block6ded16b2010-05-10 14:33:55 +0100125 V(FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map, function_without_prototype_map) \
Steve Block44f0eee2011-05-26 01:26:41 +0100126 V(STRICT_MODE_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map, \
127 strict_mode_function_without_prototype_map) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000128 V(FUNCTION_INSTANCE_MAP_INDEX, Map, function_instance_map) \
Steve Block44f0eee2011-05-26 01:26:41 +0100129 V(STRICT_MODE_FUNCTION_INSTANCE_MAP_INDEX, Map, \
130 strict_mode_function_instance_map) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000131 V(JS_ARRAY_MAP_INDEX, Map, js_array_map)\
Steve Block6ded16b2010-05-10 14:33:55 +0100132 V(REGEXP_RESULT_MAP_INDEX, Map, regexp_result_map)\
Steve Blocka7e24c12009-10-30 11:49:00 +0000133 V(ARGUMENTS_BOILERPLATE_INDEX, JSObject, arguments_boilerplate) \
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000134 V(ALIASED_ARGUMENTS_BOILERPLATE_INDEX, JSObject, \
135 aliased_arguments_boilerplate) \
Steve Block44f0eee2011-05-26 01:26:41 +0100136 V(STRICT_MODE_ARGUMENTS_BOILERPLATE_INDEX, JSObject, \
137 strict_mode_arguments_boilerplate) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000138 V(MESSAGE_LISTENERS_INDEX, JSObject, message_listeners) \
139 V(MAKE_MESSAGE_FUN_INDEX, JSFunction, make_message_fun) \
140 V(GET_STACK_TRACE_LINE_INDEX, JSFunction, get_stack_trace_line_fun) \
141 V(CONFIGURE_GLOBAL_INDEX, JSFunction, configure_global_fun) \
142 V(FUNCTION_CACHE_INDEX, JSObject, function_cache) \
Steve Block6ded16b2010-05-10 14:33:55 +0100143 V(JSFUNCTION_RESULT_CACHES_INDEX, FixedArray, jsfunction_result_caches) \
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100144 V(NORMALIZED_MAP_CACHE_INDEX, NormalizedMapCache, normalized_map_cache) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000145 V(RUNTIME_CONTEXT_INDEX, Context, runtime_context) \
146 V(CALL_AS_FUNCTION_DELEGATE_INDEX, JSFunction, call_as_function_delegate) \
147 V(CALL_AS_CONSTRUCTOR_DELEGATE_INDEX, JSFunction, \
148 call_as_constructor_delegate) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000149 V(SCRIPT_FUNCTION_INDEX, JSFunction, script_function) \
Steve Block6ded16b2010-05-10 14:33:55 +0100150 V(OPAQUE_REFERENCE_FUNCTION_INDEX, JSFunction, opaque_reference_function) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000151 V(CONTEXT_EXTENSION_FUNCTION_INDEX, JSFunction, context_extension_function) \
152 V(OUT_OF_MEMORY_INDEX, Object, out_of_memory) \
153 V(MAP_CACHE_INDEX, Object, map_cache) \
Ben Murdoch257744e2011-11-30 15:57:28 +0000154 V(CONTEXT_DATA_INDEX, Object, data) \
155 V(ALLOW_CODE_GEN_FROM_STRINGS_INDEX, Object, allow_code_gen_from_strings) \
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000156 V(TO_COMPLETE_PROPERTY_DESCRIPTOR_INDEX, JSFunction, \
157 to_complete_property_descriptor) \
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000158 V(DERIVED_HAS_TRAP_INDEX, JSFunction, derived_has_trap) \
159 V(DERIVED_GET_TRAP_INDEX, JSFunction, derived_get_trap) \
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000160 V(DERIVED_SET_TRAP_INDEX, JSFunction, derived_set_trap) \
161 V(PROXY_ENUMERATE, JSFunction, proxy_enumerate) \
162 V(RANDOM_SEED_INDEX, ByteArray, random_seed)
Steve Blocka7e24c12009-10-30 11:49:00 +0000163
164// JSFunctions are pairs (context, function code), sometimes also called
165// closures. A Context object is used to represent function contexts and
166// dynamically pushed 'with' contexts (or 'scopes' in ECMA-262 speak).
167//
168// At runtime, the contexts build a stack in parallel to the execution
169// stack, with the top-most context being the current context. All contexts
170// have the following slots:
171//
172// [ closure ] This is the current function. It is the same for all
173// contexts inside a function. It provides access to the
174// incoming context (i.e., the outer context, which may
175// or may not become the current function's context), and
176// it provides access to the functions code and thus it's
177// scope information, which in turn contains the names of
178// statically allocated context slots. The names are needed
179// for dynamic lookups in the presence of 'with' or 'eval'.
180//
Steve Blocka7e24c12009-10-30 11:49:00 +0000181// [ previous ] A pointer to the previous context. It is NULL for
182// function contexts, and non-NULL for 'with' contexts.
183// Used to implement the 'with' statement.
184//
185// [ extension ] A pointer to an extension JSObject, or NULL. Used to
186// implement 'with' statements and dynamic declarations
187// (through 'eval'). The object in a 'with' statement is
188// stored in the extension slot of a 'with' context.
189// Dynamically declared variables/functions are also added
190// to lazily allocated extension object. Context::Lookup
191// searches the extension object for properties.
192//
193// [ global ] A pointer to the global object. Provided for quick
194// access to the global object from inside the code (since
195// we always have a context pointer).
196//
197// In addition, function contexts may have statically allocated context slots
198// to store local variables/functions that are accessed from inner functions
199// (via static context addresses) or through 'eval' (dynamic context lookups).
200// Finally, the global context contains additional slots for fast access to
201// global properties.
Steve Blocka7e24c12009-10-30 11:49:00 +0000202
203class Context: public FixedArray {
204 public:
205 // Conversions.
206 static Context* cast(Object* context) {
207 ASSERT(context->IsContext());
208 return reinterpret_cast<Context*>(context);
209 }
210
211 // The default context slot layout; indices are FixedArray slot indices.
212 enum {
213 // These slots are in all contexts.
214 CLOSURE_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000215 PREVIOUS_INDEX,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000216 // The extension slot is used for either the global object (in global
217 // contexts), eval extension object (function contexts), subject of with
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000218 // (with contexts), or the variable name (catch contexts), the serialized
219 // scope info (block contexts).
Steve Blocka7e24c12009-10-30 11:49:00 +0000220 EXTENSION_INDEX,
221 GLOBAL_INDEX,
222 MIN_CONTEXT_SLOTS,
223
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000224 // This slot holds the thrown value in catch contexts.
225 THROWN_OBJECT_INDEX = MIN_CONTEXT_SLOTS,
226
Steve Blocka7e24c12009-10-30 11:49:00 +0000227 // These slots are only in global contexts.
228 GLOBAL_PROXY_INDEX = MIN_CONTEXT_SLOTS,
229 SECURITY_TOKEN_INDEX,
230 ARGUMENTS_BOILERPLATE_INDEX,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000231 ALIASED_ARGUMENTS_BOILERPLATE_INDEX,
Steve Block44f0eee2011-05-26 01:26:41 +0100232 STRICT_MODE_ARGUMENTS_BOILERPLATE_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000233 JS_ARRAY_MAP_INDEX,
Steve Block6ded16b2010-05-10 14:33:55 +0100234 REGEXP_RESULT_MAP_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000235 FUNCTION_MAP_INDEX,
Steve Block44f0eee2011-05-26 01:26:41 +0100236 STRICT_MODE_FUNCTION_MAP_INDEX,
Steve Block6ded16b2010-05-10 14:33:55 +0100237 FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX,
Steve Block44f0eee2011-05-26 01:26:41 +0100238 STRICT_MODE_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000239 FUNCTION_INSTANCE_MAP_INDEX,
Steve Block44f0eee2011-05-26 01:26:41 +0100240 STRICT_MODE_FUNCTION_INSTANCE_MAP_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000241 INITIAL_OBJECT_PROTOTYPE_INDEX,
242 BOOLEAN_FUNCTION_INDEX,
243 NUMBER_FUNCTION_INDEX,
244 STRING_FUNCTION_INDEX,
Iain Merrick75681382010-08-19 15:07:18 +0100245 STRING_FUNCTION_PROTOTYPE_MAP_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000246 OBJECT_FUNCTION_INDEX,
247 ARRAY_FUNCTION_INDEX,
248 DATE_FUNCTION_INDEX,
249 JSON_OBJECT_INDEX,
250 REGEXP_FUNCTION_INDEX,
251 CREATE_DATE_FUN_INDEX,
252 TO_NUMBER_FUN_INDEX,
253 TO_STRING_FUN_INDEX,
254 TO_DETAIL_STRING_FUN_INDEX,
255 TO_OBJECT_FUN_INDEX,
256 TO_INTEGER_FUN_INDEX,
257 TO_UINT32_FUN_INDEX,
258 TO_INT32_FUN_INDEX,
259 TO_BOOLEAN_FUN_INDEX,
Leon Clarkee46be812010-01-19 14:06:41 +0000260 GLOBAL_EVAL_FUN_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000261 INSTANTIATE_FUN_INDEX,
262 CONFIGURE_INSTANCE_FUN_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000263 MESSAGE_LISTENERS_INDEX,
264 MAKE_MESSAGE_FUN_INDEX,
265 GET_STACK_TRACE_LINE_INDEX,
266 CONFIGURE_GLOBAL_INDEX,
267 FUNCTION_CACHE_INDEX,
Steve Block6ded16b2010-05-10 14:33:55 +0100268 JSFUNCTION_RESULT_CACHES_INDEX,
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100269 NORMALIZED_MAP_CACHE_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000270 RUNTIME_CONTEXT_INDEX,
271 CALL_AS_FUNCTION_DELEGATE_INDEX,
272 CALL_AS_CONSTRUCTOR_DELEGATE_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000273 SCRIPT_FUNCTION_INDEX,
Steve Block6ded16b2010-05-10 14:33:55 +0100274 OPAQUE_REFERENCE_FUNCTION_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000275 CONTEXT_EXTENSION_FUNCTION_INDEX,
276 OUT_OF_MEMORY_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000277 CONTEXT_DATA_INDEX,
Ben Murdoch257744e2011-11-30 15:57:28 +0000278 ALLOW_CODE_GEN_FROM_STRINGS_INDEX,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000279 TO_COMPLETE_PROPERTY_DESCRIPTOR_INDEX,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000280 DERIVED_HAS_TRAP_INDEX,
Ben Murdoch257744e2011-11-30 15:57:28 +0000281 DERIVED_GET_TRAP_INDEX,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000282 DERIVED_SET_TRAP_INDEX,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000283 PROXY_ENUMERATE,
284 RANDOM_SEED_INDEX,
Ben Murdochf87a2032010-10-22 12:50:53 +0100285
286 // Properties from here are treated as weak references by the full GC.
287 // Scavenge treats them as strong references.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100288 OPTIMIZED_FUNCTIONS_LIST, // Weak.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000289 MAP_CACHE_INDEX, // Weak.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100290 NEXT_CONTEXT_LINK, // Weak.
Ben Murdochf87a2032010-10-22 12:50:53 +0100291
292 // Total number of slots.
293 GLOBAL_CONTEXT_SLOTS,
294
Ben Murdochb0fe1622011-05-05 13:52:32 +0100295 FIRST_WEAK_SLOT = OPTIMIZED_FUNCTIONS_LIST
Steve Blocka7e24c12009-10-30 11:49:00 +0000296 };
297
298 // Direct slot access.
299 JSFunction* closure() { return JSFunction::cast(get(CLOSURE_INDEX)); }
300 void set_closure(JSFunction* closure) { set(CLOSURE_INDEX, closure); }
301
Steve Blocka7e24c12009-10-30 11:49:00 +0000302 Context* previous() {
303 Object* result = unchecked_previous();
304 ASSERT(IsBootstrappingOrContext(result));
305 return reinterpret_cast<Context*>(result);
306 }
307 void set_previous(Context* context) { set(PREVIOUS_INDEX, context); }
308
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000309 bool has_extension() { return extension() != NULL; }
310 Object* extension() { return get(EXTENSION_INDEX); }
311 void set_extension(Object* object) { set(EXTENSION_INDEX, object); }
312
313 // Get the context where var declarations will be hoisted to, which
314 // may be the context itself.
315 Context* declaration_context();
Steve Blocka7e24c12009-10-30 11:49:00 +0000316
317 GlobalObject* global() {
318 Object* result = get(GLOBAL_INDEX);
Steve Block44f0eee2011-05-26 01:26:41 +0100319 ASSERT(IsBootstrappingOrGlobalObject(result));
Steve Blocka7e24c12009-10-30 11:49:00 +0000320 return reinterpret_cast<GlobalObject*>(result);
321 }
322 void set_global(GlobalObject* global) { set(GLOBAL_INDEX, global); }
323
324 // Returns a JSGlobalProxy object or null.
325 JSObject* global_proxy();
326 void set_global_proxy(JSObject* global);
327
328 // The builtins object.
329 JSBuiltinsObject* builtins();
330
331 // Compute the global context by traversing the context chain.
332 Context* global_context();
333
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000334 // Predicates for context types. IsGlobalContext is defined on Object
335 // because we frequently have to know if arbitrary objects are global
336 // contexts.
337 bool IsFunctionContext() {
338 Map* map = this->map();
339 return map == map->GetHeap()->function_context_map();
340 }
341 bool IsCatchContext() {
342 Map* map = this->map();
343 return map == map->GetHeap()->catch_context_map();
344 }
345 bool IsWithContext() {
346 Map* map = this->map();
347 return map == map->GetHeap()->with_context_map();
348 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000349 bool IsBlockContext() {
350 Map* map = this->map();
351 return map == map->GetHeap()->block_context_map();
352 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000353
354 // Tells whether the global context is marked with out of memory.
Steve Block44f0eee2011-05-26 01:26:41 +0100355 inline bool has_out_of_memory();
Steve Blocka7e24c12009-10-30 11:49:00 +0000356
357 // Mark the global context with out of memory.
Steve Block44f0eee2011-05-26 01:26:41 +0100358 inline void mark_out_of_memory();
Steve Blocka7e24c12009-10-30 11:49:00 +0000359
Ben Murdochb0fe1622011-05-05 13:52:32 +0100360 // A global context hold a list of all functions which have been optimized.
361 void AddOptimizedFunction(JSFunction* function);
362 void RemoveOptimizedFunction(JSFunction* function);
363 Object* OptimizedFunctionsListHead();
364 void ClearOptimizedFunctions();
365
Steve Blocka7e24c12009-10-30 11:49:00 +0000366#define GLOBAL_CONTEXT_FIELD_ACCESSORS(index, type, name) \
367 void set_##name(type* value) { \
368 ASSERT(IsGlobalContext()); \
369 set(index, value); \
370 } \
371 type* name() { \
372 ASSERT(IsGlobalContext()); \
373 return type::cast(get(index)); \
374 }
375 GLOBAL_CONTEXT_FIELDS(GLOBAL_CONTEXT_FIELD_ACCESSORS)
376#undef GLOBAL_CONTEXT_FIELD_ACCESSORS
377
378 // Lookup the the slot called name, starting with the current context.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000379 // There are three possibilities:
Steve Blocka7e24c12009-10-30 11:49:00 +0000380 //
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000381 // 1) result->IsContext():
382 // The binding was found in a context. *index is always the
383 // non-negative slot index. *attributes is NONE for var and let
384 // declarations, READ_ONLY for const declarations (never ABSENT).
Steve Blocka7e24c12009-10-30 11:49:00 +0000385 //
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000386 // 2) result->IsJSObject():
387 // The binding was found as a named property in a context extension
388 // object (i.e., was introduced via eval), as a property on the subject
389 // of with, or as a property of the global object. *index is -1 and
390 // *attributes is not ABSENT.
Steve Blocka7e24c12009-10-30 11:49:00 +0000391 //
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000392 // 3) result.is_null():
393 // There was no binding found, *index is always -1 and *attributes is
394 // always ABSENT.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000395 Handle<Object> Lookup(Handle<String> name,
396 ContextLookupFlags flags,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000397 int* index,
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000398 PropertyAttributes* attributes,
399 BindingFlags* binding_flags);
Steve Blocka7e24c12009-10-30 11:49:00 +0000400
Steve Blocka7e24c12009-10-30 11:49:00 +0000401 // Code generation support.
402 static int SlotOffset(int index) {
403 return kHeaderSize + index * kPointerSize - kHeapObjectTag;
404 }
405
Ben Murdochf87a2032010-10-22 12:50:53 +0100406 static const int kSize = kHeaderSize + GLOBAL_CONTEXT_SLOTS * kPointerSize;
407
408 // GC support.
409 typedef FixedBodyDescriptor<
410 kHeaderSize, kSize, kSize> ScavengeBodyDescriptor;
411
412 typedef FixedBodyDescriptor<
413 kHeaderSize,
414 kHeaderSize + FIRST_WEAK_SLOT * kPointerSize,
415 kSize> MarkCompactBodyDescriptor;
416
Steve Blocka7e24c12009-10-30 11:49:00 +0000417 private:
418 // Unchecked access to the slots.
419 Object* unchecked_previous() { return get(PREVIOUS_INDEX); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000420
421#ifdef DEBUG
422 // Bootstrapping-aware type checks.
423 static bool IsBootstrappingOrContext(Object* object);
424 static bool IsBootstrappingOrGlobalObject(Object* object);
425#endif
426};
427
428} } // namespace v8::internal
429
430#endif // V8_CONTEXTS_H_