blob: af5cb036c615bb212a91aae4cd1a3bff808ec1ea [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 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 Murdoch3ef787d2012-04-12 10:51:47 +010049// 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 Murdoch3ef787d2012-04-12 10:51:47 +010064// 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 Murdoch3ef787d2012-04-12 10:51:47 +010084 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) \
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100107 V(INTERNAL_ARRAY_FUNCTION_INDEX, JSFunction, internal_array_function) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000108 V(ARRAY_FUNCTION_INDEX, JSFunction, array_function) \
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100109 V(SMI_JS_ARRAY_MAP_INDEX, Object, smi_js_array_map) \
110 V(DOUBLE_JS_ARRAY_MAP_INDEX, Object, double_js_array_map) \
111 V(OBJECT_JS_ARRAY_MAP_INDEX, Object, object_js_array_map) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000112 V(DATE_FUNCTION_INDEX, JSFunction, date_function) \
113 V(JSON_OBJECT_INDEX, JSObject, json_object) \
114 V(REGEXP_FUNCTION_INDEX, JSFunction, regexp_function) \
115 V(INITIAL_OBJECT_PROTOTYPE_INDEX, JSObject, initial_object_prototype) \
116 V(CREATE_DATE_FUN_INDEX, JSFunction, create_date_fun) \
117 V(TO_NUMBER_FUN_INDEX, JSFunction, to_number_fun) \
118 V(TO_STRING_FUN_INDEX, JSFunction, to_string_fun) \
119 V(TO_DETAIL_STRING_FUN_INDEX, JSFunction, to_detail_string_fun) \
120 V(TO_OBJECT_FUN_INDEX, JSFunction, to_object_fun) \
121 V(TO_INTEGER_FUN_INDEX, JSFunction, to_integer_fun) \
122 V(TO_UINT32_FUN_INDEX, JSFunction, to_uint32_fun) \
123 V(TO_INT32_FUN_INDEX, JSFunction, to_int32_fun) \
Leon Clarkee46be812010-01-19 14:06:41 +0000124 V(GLOBAL_EVAL_FUN_INDEX, JSFunction, global_eval_fun) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000125 V(INSTANTIATE_FUN_INDEX, JSFunction, instantiate_fun) \
126 V(CONFIGURE_INSTANCE_FUN_INDEX, JSFunction, configure_instance_fun) \
127 V(FUNCTION_MAP_INDEX, Map, function_map) \
Steve Block44f0eee2011-05-26 01:26:41 +0100128 V(STRICT_MODE_FUNCTION_MAP_INDEX, Map, strict_mode_function_map) \
Steve Block6ded16b2010-05-10 14:33:55 +0100129 V(FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map, function_without_prototype_map) \
Steve Block44f0eee2011-05-26 01:26:41 +0100130 V(STRICT_MODE_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map, \
131 strict_mode_function_without_prototype_map) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000132 V(FUNCTION_INSTANCE_MAP_INDEX, Map, function_instance_map) \
Steve Block44f0eee2011-05-26 01:26:41 +0100133 V(STRICT_MODE_FUNCTION_INSTANCE_MAP_INDEX, Map, \
134 strict_mode_function_instance_map) \
Steve Block6ded16b2010-05-10 14:33:55 +0100135 V(REGEXP_RESULT_MAP_INDEX, Map, regexp_result_map)\
Steve Blocka7e24c12009-10-30 11:49:00 +0000136 V(ARGUMENTS_BOILERPLATE_INDEX, JSObject, arguments_boilerplate) \
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000137 V(ALIASED_ARGUMENTS_BOILERPLATE_INDEX, JSObject, \
138 aliased_arguments_boilerplate) \
Steve Block44f0eee2011-05-26 01:26:41 +0100139 V(STRICT_MODE_ARGUMENTS_BOILERPLATE_INDEX, JSObject, \
140 strict_mode_arguments_boilerplate) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000141 V(MESSAGE_LISTENERS_INDEX, JSObject, message_listeners) \
142 V(MAKE_MESSAGE_FUN_INDEX, JSFunction, make_message_fun) \
143 V(GET_STACK_TRACE_LINE_INDEX, JSFunction, get_stack_trace_line_fun) \
144 V(CONFIGURE_GLOBAL_INDEX, JSFunction, configure_global_fun) \
145 V(FUNCTION_CACHE_INDEX, JSObject, function_cache) \
Steve Block6ded16b2010-05-10 14:33:55 +0100146 V(JSFUNCTION_RESULT_CACHES_INDEX, FixedArray, jsfunction_result_caches) \
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100147 V(NORMALIZED_MAP_CACHE_INDEX, NormalizedMapCache, normalized_map_cache) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000148 V(RUNTIME_CONTEXT_INDEX, Context, runtime_context) \
149 V(CALL_AS_FUNCTION_DELEGATE_INDEX, JSFunction, call_as_function_delegate) \
150 V(CALL_AS_CONSTRUCTOR_DELEGATE_INDEX, JSFunction, \
151 call_as_constructor_delegate) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000152 V(SCRIPT_FUNCTION_INDEX, JSFunction, script_function) \
Steve Block6ded16b2010-05-10 14:33:55 +0100153 V(OPAQUE_REFERENCE_FUNCTION_INDEX, JSFunction, opaque_reference_function) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000154 V(CONTEXT_EXTENSION_FUNCTION_INDEX, JSFunction, context_extension_function) \
155 V(OUT_OF_MEMORY_INDEX, Object, out_of_memory) \
156 V(MAP_CACHE_INDEX, Object, map_cache) \
Ben Murdoch257744e2011-11-30 15:57:28 +0000157 V(CONTEXT_DATA_INDEX, Object, data) \
158 V(ALLOW_CODE_GEN_FROM_STRINGS_INDEX, Object, allow_code_gen_from_strings) \
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100159 V(TO_COMPLETE_PROPERTY_DESCRIPTOR_INDEX, JSFunction, \
160 to_complete_property_descriptor) \
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000161 V(DERIVED_HAS_TRAP_INDEX, JSFunction, derived_has_trap) \
162 V(DERIVED_GET_TRAP_INDEX, JSFunction, derived_get_trap) \
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100163 V(DERIVED_SET_TRAP_INDEX, JSFunction, derived_set_trap) \
164 V(PROXY_ENUMERATE, JSFunction, proxy_enumerate) \
165 V(RANDOM_SEED_INDEX, ByteArray, random_seed)
Steve Blocka7e24c12009-10-30 11:49:00 +0000166
167// JSFunctions are pairs (context, function code), sometimes also called
168// closures. A Context object is used to represent function contexts and
169// dynamically pushed 'with' contexts (or 'scopes' in ECMA-262 speak).
170//
171// At runtime, the contexts build a stack in parallel to the execution
172// stack, with the top-most context being the current context. All contexts
173// have the following slots:
174//
175// [ closure ] This is the current function. It is the same for all
176// contexts inside a function. It provides access to the
177// incoming context (i.e., the outer context, which may
178// or may not become the current function's context), and
179// it provides access to the functions code and thus it's
180// scope information, which in turn contains the names of
181// statically allocated context slots. The names are needed
182// for dynamic lookups in the presence of 'with' or 'eval'.
183//
Steve Blocka7e24c12009-10-30 11:49:00 +0000184// [ previous ] A pointer to the previous context. It is NULL for
185// function contexts, and non-NULL for 'with' contexts.
186// Used to implement the 'with' statement.
187//
188// [ extension ] A pointer to an extension JSObject, or NULL. Used to
189// implement 'with' statements and dynamic declarations
190// (through 'eval'). The object in a 'with' statement is
191// stored in the extension slot of a 'with' context.
192// Dynamically declared variables/functions are also added
193// to lazily allocated extension object. Context::Lookup
194// searches the extension object for properties.
195//
196// [ global ] A pointer to the global object. Provided for quick
197// access to the global object from inside the code (since
198// we always have a context pointer).
199//
200// In addition, function contexts may have statically allocated context slots
201// to store local variables/functions that are accessed from inner functions
202// (via static context addresses) or through 'eval' (dynamic context lookups).
203// Finally, the global context contains additional slots for fast access to
204// global properties.
Steve Blocka7e24c12009-10-30 11:49:00 +0000205
206class Context: public FixedArray {
207 public:
208 // Conversions.
209 static Context* cast(Object* context) {
210 ASSERT(context->IsContext());
211 return reinterpret_cast<Context*>(context);
212 }
213
214 // The default context slot layout; indices are FixedArray slot indices.
215 enum {
216 // These slots are in all contexts.
217 CLOSURE_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000218 PREVIOUS_INDEX,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000219 // The extension slot is used for either the global object (in global
220 // contexts), eval extension object (function contexts), subject of with
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100221 // (with contexts), or the variable name (catch contexts), the serialized
222 // scope info (block contexts).
Steve Blocka7e24c12009-10-30 11:49:00 +0000223 EXTENSION_INDEX,
224 GLOBAL_INDEX,
225 MIN_CONTEXT_SLOTS,
226
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000227 // This slot holds the thrown value in catch contexts.
228 THROWN_OBJECT_INDEX = MIN_CONTEXT_SLOTS,
229
Steve Blocka7e24c12009-10-30 11:49:00 +0000230 // These slots are only in global contexts.
231 GLOBAL_PROXY_INDEX = MIN_CONTEXT_SLOTS,
232 SECURITY_TOKEN_INDEX,
233 ARGUMENTS_BOILERPLATE_INDEX,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000234 ALIASED_ARGUMENTS_BOILERPLATE_INDEX,
Steve Block44f0eee2011-05-26 01:26:41 +0100235 STRICT_MODE_ARGUMENTS_BOILERPLATE_INDEX,
Steve Block6ded16b2010-05-10 14:33:55 +0100236 REGEXP_RESULT_MAP_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000237 FUNCTION_MAP_INDEX,
Steve Block44f0eee2011-05-26 01:26:41 +0100238 STRICT_MODE_FUNCTION_MAP_INDEX,
Steve Block6ded16b2010-05-10 14:33:55 +0100239 FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX,
Steve Block44f0eee2011-05-26 01:26:41 +0100240 STRICT_MODE_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000241 FUNCTION_INSTANCE_MAP_INDEX,
Steve Block44f0eee2011-05-26 01:26:41 +0100242 STRICT_MODE_FUNCTION_INSTANCE_MAP_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000243 INITIAL_OBJECT_PROTOTYPE_INDEX,
244 BOOLEAN_FUNCTION_INDEX,
245 NUMBER_FUNCTION_INDEX,
246 STRING_FUNCTION_INDEX,
Iain Merrick75681382010-08-19 15:07:18 +0100247 STRING_FUNCTION_PROTOTYPE_MAP_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000248 OBJECT_FUNCTION_INDEX,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100249 INTERNAL_ARRAY_FUNCTION_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000250 ARRAY_FUNCTION_INDEX,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100251 SMI_JS_ARRAY_MAP_INDEX,
252 DOUBLE_JS_ARRAY_MAP_INDEX,
253 OBJECT_JS_ARRAY_MAP_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000254 DATE_FUNCTION_INDEX,
255 JSON_OBJECT_INDEX,
256 REGEXP_FUNCTION_INDEX,
257 CREATE_DATE_FUN_INDEX,
258 TO_NUMBER_FUN_INDEX,
259 TO_STRING_FUN_INDEX,
260 TO_DETAIL_STRING_FUN_INDEX,
261 TO_OBJECT_FUN_INDEX,
262 TO_INTEGER_FUN_INDEX,
263 TO_UINT32_FUN_INDEX,
264 TO_INT32_FUN_INDEX,
265 TO_BOOLEAN_FUN_INDEX,
Leon Clarkee46be812010-01-19 14:06:41 +0000266 GLOBAL_EVAL_FUN_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000267 INSTANTIATE_FUN_INDEX,
268 CONFIGURE_INSTANCE_FUN_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000269 MESSAGE_LISTENERS_INDEX,
270 MAKE_MESSAGE_FUN_INDEX,
271 GET_STACK_TRACE_LINE_INDEX,
272 CONFIGURE_GLOBAL_INDEX,
273 FUNCTION_CACHE_INDEX,
Steve Block6ded16b2010-05-10 14:33:55 +0100274 JSFUNCTION_RESULT_CACHES_INDEX,
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100275 NORMALIZED_MAP_CACHE_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000276 RUNTIME_CONTEXT_INDEX,
277 CALL_AS_FUNCTION_DELEGATE_INDEX,
278 CALL_AS_CONSTRUCTOR_DELEGATE_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000279 SCRIPT_FUNCTION_INDEX,
Steve Block6ded16b2010-05-10 14:33:55 +0100280 OPAQUE_REFERENCE_FUNCTION_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000281 CONTEXT_EXTENSION_FUNCTION_INDEX,
282 OUT_OF_MEMORY_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000283 CONTEXT_DATA_INDEX,
Ben Murdoch257744e2011-11-30 15:57:28 +0000284 ALLOW_CODE_GEN_FROM_STRINGS_INDEX,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100285 TO_COMPLETE_PROPERTY_DESCRIPTOR_INDEX,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000286 DERIVED_HAS_TRAP_INDEX,
Ben Murdoch257744e2011-11-30 15:57:28 +0000287 DERIVED_GET_TRAP_INDEX,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000288 DERIVED_SET_TRAP_INDEX,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100289 PROXY_ENUMERATE,
290 RANDOM_SEED_INDEX,
Ben Murdochf87a2032010-10-22 12:50:53 +0100291
292 // Properties from here are treated as weak references by the full GC.
293 // Scavenge treats them as strong references.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100294 OPTIMIZED_FUNCTIONS_LIST, // Weak.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000295 MAP_CACHE_INDEX, // Weak.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100296 NEXT_CONTEXT_LINK, // Weak.
Ben Murdochf87a2032010-10-22 12:50:53 +0100297
298 // Total number of slots.
299 GLOBAL_CONTEXT_SLOTS,
300
Ben Murdochb0fe1622011-05-05 13:52:32 +0100301 FIRST_WEAK_SLOT = OPTIMIZED_FUNCTIONS_LIST
Steve Blocka7e24c12009-10-30 11:49:00 +0000302 };
303
304 // Direct slot access.
305 JSFunction* closure() { return JSFunction::cast(get(CLOSURE_INDEX)); }
306 void set_closure(JSFunction* closure) { set(CLOSURE_INDEX, closure); }
307
Steve Blocka7e24c12009-10-30 11:49:00 +0000308 Context* previous() {
309 Object* result = unchecked_previous();
310 ASSERT(IsBootstrappingOrContext(result));
311 return reinterpret_cast<Context*>(result);
312 }
313 void set_previous(Context* context) { set(PREVIOUS_INDEX, context); }
314
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000315 bool has_extension() { return extension() != NULL; }
316 Object* extension() { return get(EXTENSION_INDEX); }
317 void set_extension(Object* object) { set(EXTENSION_INDEX, object); }
318
319 // Get the context where var declarations will be hoisted to, which
320 // may be the context itself.
321 Context* declaration_context();
Steve Blocka7e24c12009-10-30 11:49:00 +0000322
323 GlobalObject* global() {
324 Object* result = get(GLOBAL_INDEX);
Steve Block44f0eee2011-05-26 01:26:41 +0100325 ASSERT(IsBootstrappingOrGlobalObject(result));
Steve Blocka7e24c12009-10-30 11:49:00 +0000326 return reinterpret_cast<GlobalObject*>(result);
327 }
328 void set_global(GlobalObject* global) { set(GLOBAL_INDEX, global); }
329
330 // Returns a JSGlobalProxy object or null.
331 JSObject* global_proxy();
332 void set_global_proxy(JSObject* global);
333
334 // The builtins object.
335 JSBuiltinsObject* builtins();
336
337 // Compute the global context by traversing the context chain.
338 Context* global_context();
339
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000340 // Predicates for context types. IsGlobalContext is defined on Object
341 // because we frequently have to know if arbitrary objects are global
342 // contexts.
343 bool IsFunctionContext() {
344 Map* map = this->map();
345 return map == map->GetHeap()->function_context_map();
346 }
347 bool IsCatchContext() {
348 Map* map = this->map();
349 return map == map->GetHeap()->catch_context_map();
350 }
351 bool IsWithContext() {
352 Map* map = this->map();
353 return map == map->GetHeap()->with_context_map();
354 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000355 bool IsBlockContext() {
356 Map* map = this->map();
357 return map == map->GetHeap()->block_context_map();
358 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100359 bool IsModuleContext() {
360 Map* map = this->map();
361 return map == map->GetHeap()->module_context_map();
362 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000363
364 // Tells whether the global context is marked with out of memory.
Steve Block44f0eee2011-05-26 01:26:41 +0100365 inline bool has_out_of_memory();
Steve Blocka7e24c12009-10-30 11:49:00 +0000366
367 // Mark the global context with out of memory.
Steve Block44f0eee2011-05-26 01:26:41 +0100368 inline void mark_out_of_memory();
Steve Blocka7e24c12009-10-30 11:49:00 +0000369
Ben Murdochb0fe1622011-05-05 13:52:32 +0100370 // A global context hold a list of all functions which have been optimized.
371 void AddOptimizedFunction(JSFunction* function);
372 void RemoveOptimizedFunction(JSFunction* function);
373 Object* OptimizedFunctionsListHead();
374 void ClearOptimizedFunctions();
375
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100376 static int GetContextMapIndexFromElementsKind(
377 ElementsKind elements_kind) {
378 if (elements_kind == FAST_DOUBLE_ELEMENTS) {
379 return Context::DOUBLE_JS_ARRAY_MAP_INDEX;
380 } else if (elements_kind == FAST_ELEMENTS) {
381 return Context::OBJECT_JS_ARRAY_MAP_INDEX;
382 } else {
383 ASSERT(elements_kind == FAST_SMI_ONLY_ELEMENTS);
384 return Context::SMI_JS_ARRAY_MAP_INDEX;
385 }
386 }
387
Steve Blocka7e24c12009-10-30 11:49:00 +0000388#define GLOBAL_CONTEXT_FIELD_ACCESSORS(index, type, name) \
389 void set_##name(type* value) { \
390 ASSERT(IsGlobalContext()); \
391 set(index, value); \
392 } \
393 type* name() { \
394 ASSERT(IsGlobalContext()); \
395 return type::cast(get(index)); \
396 }
397 GLOBAL_CONTEXT_FIELDS(GLOBAL_CONTEXT_FIELD_ACCESSORS)
398#undef GLOBAL_CONTEXT_FIELD_ACCESSORS
399
400 // Lookup the the slot called name, starting with the current context.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100401 // There are three possibilities:
Steve Blocka7e24c12009-10-30 11:49:00 +0000402 //
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100403 // 1) result->IsContext():
404 // The binding was found in a context. *index is always the
405 // non-negative slot index. *attributes is NONE for var and let
406 // declarations, READ_ONLY for const declarations (never ABSENT).
Steve Blocka7e24c12009-10-30 11:49:00 +0000407 //
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100408 // 2) result->IsJSObject():
409 // The binding was found as a named property in a context extension
410 // object (i.e., was introduced via eval), as a property on the subject
411 // of with, or as a property of the global object. *index is -1 and
412 // *attributes is not ABSENT.
Steve Blocka7e24c12009-10-30 11:49:00 +0000413 //
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100414 // 3) result.is_null():
415 // There was no binding found, *index is always -1 and *attributes is
416 // always ABSENT.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000417 Handle<Object> Lookup(Handle<String> name,
418 ContextLookupFlags flags,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100419 int* index,
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000420 PropertyAttributes* attributes,
421 BindingFlags* binding_flags);
Steve Blocka7e24c12009-10-30 11:49:00 +0000422
Steve Blocka7e24c12009-10-30 11:49:00 +0000423 // Code generation support.
424 static int SlotOffset(int index) {
425 return kHeaderSize + index * kPointerSize - kHeapObjectTag;
426 }
427
Ben Murdochf87a2032010-10-22 12:50:53 +0100428 static const int kSize = kHeaderSize + GLOBAL_CONTEXT_SLOTS * kPointerSize;
429
430 // GC support.
431 typedef FixedBodyDescriptor<
432 kHeaderSize, kSize, kSize> ScavengeBodyDescriptor;
433
434 typedef FixedBodyDescriptor<
435 kHeaderSize,
436 kHeaderSize + FIRST_WEAK_SLOT * kPointerSize,
437 kSize> MarkCompactBodyDescriptor;
438
Steve Blocka7e24c12009-10-30 11:49:00 +0000439 private:
440 // Unchecked access to the slots.
441 Object* unchecked_previous() { return get(PREVIOUS_INDEX); }
Steve Blocka7e24c12009-10-30 11:49:00 +0000442
443#ifdef DEBUG
444 // Bootstrapping-aware type checks.
445 static bool IsBootstrappingOrContext(Object* object);
446 static bool IsBootstrappingOrGlobalObject(Object* object);
447#endif
448};
449
450} } // namespace v8::internal
451
452#endif // V8_CONTEXTS_H_