blob: e0d8a14a5526c307e01e761c012308afef70edcb [file] [log] [blame]
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001// Copyright 2012 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +000031#include "heap.h"
32#include "objects.h"
33
kasperl@chromium.org71affb52009-05-26 05:44:31 +000034namespace v8 {
35namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036
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
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +000047// ES5 10.2 defines lexical environments with mutable and immutable bindings.
48// Immutable bindings have two states, initialized and uninitialized, and
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +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.
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +000062//
63// The harmony proposal for block scoped bindings also introduces the
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +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.
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +000079enum BindingFlags {
80 MUTABLE_IS_INITIALIZED,
81 MUTABLE_CHECK_INITIALIZED,
82 IMMUTABLE_IS_INITIALIZED,
83 IMMUTABLE_CHECK_INITIALIZED,
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000084 IMMUTABLE_IS_INITIALIZED_HARMONY,
85 IMMUTABLE_CHECK_INITIALIZED_HARMONY,
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +000086 MISSING_BINDING
87};
88
89
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000099#define GLOBAL_CONTEXT_FIELDS(V) \
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000100 V(GLOBAL_PROXY_INDEX, JSObject, global_proxy_object) \
101 V(SECURITY_TOKEN_INDEX, Object, security_token) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000102 V(BOOLEAN_FUNCTION_INDEX, JSFunction, boolean_function) \
103 V(NUMBER_FUNCTION_INDEX, JSFunction, number_function) \
104 V(STRING_FUNCTION_INDEX, JSFunction, string_function) \
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000105 V(STRING_FUNCTION_PROTOTYPE_MAP_INDEX, Map, string_function_prototype_map) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000106 V(OBJECT_FUNCTION_INDEX, JSFunction, object_function) \
svenpanne@chromium.org3c93e772012-01-02 09:26:59 +0000107 V(INTERNAL_ARRAY_FUNCTION_INDEX, JSFunction, internal_array_function) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000108 V(ARRAY_FUNCTION_INDEX, JSFunction, array_function) \
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +0000109 V(JS_ARRAY_MAPS_INDEX, Object, js_array_maps) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000110 V(DATE_FUNCTION_INDEX, JSFunction, date_function) \
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000111 V(JSON_OBJECT_INDEX, JSObject, json_object) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000112 V(REGEXP_FUNCTION_INDEX, JSFunction, regexp_function) \
113 V(INITIAL_OBJECT_PROTOTYPE_INDEX, JSObject, initial_object_prototype) \
114 V(CREATE_DATE_FUN_INDEX, JSFunction, create_date_fun) \
115 V(TO_NUMBER_FUN_INDEX, JSFunction, to_number_fun) \
116 V(TO_STRING_FUN_INDEX, JSFunction, to_string_fun) \
117 V(TO_DETAIL_STRING_FUN_INDEX, JSFunction, to_detail_string_fun) \
118 V(TO_OBJECT_FUN_INDEX, JSFunction, to_object_fun) \
119 V(TO_INTEGER_FUN_INDEX, JSFunction, to_integer_fun) \
120 V(TO_UINT32_FUN_INDEX, JSFunction, to_uint32_fun) \
121 V(TO_INT32_FUN_INDEX, JSFunction, to_int32_fun) \
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000122 V(GLOBAL_EVAL_FUN_INDEX, JSFunction, global_eval_fun) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000123 V(INSTANTIATE_FUN_INDEX, JSFunction, instantiate_fun) \
124 V(CONFIGURE_INSTANCE_FUN_INDEX, JSFunction, configure_instance_fun) \
125 V(FUNCTION_MAP_INDEX, Map, function_map) \
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000126 V(STRICT_MODE_FUNCTION_MAP_INDEX, Map, strict_mode_function_map) \
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000127 V(FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map, function_without_prototype_map) \
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000128 V(STRICT_MODE_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map, \
129 strict_mode_function_without_prototype_map) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000130 V(FUNCTION_INSTANCE_MAP_INDEX, Map, function_instance_map) \
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000131 V(STRICT_MODE_FUNCTION_INSTANCE_MAP_INDEX, Map, \
132 strict_mode_function_instance_map) \
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000133 V(REGEXP_RESULT_MAP_INDEX, Map, regexp_result_map)\
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000134 V(ARGUMENTS_BOILERPLATE_INDEX, JSObject, arguments_boilerplate) \
whesse@chromium.org7b260152011-06-20 15:33:18 +0000135 V(ALIASED_ARGUMENTS_BOILERPLATE_INDEX, JSObject, \
136 aliased_arguments_boilerplate) \
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000137 V(STRICT_MODE_ARGUMENTS_BOILERPLATE_INDEX, JSObject, \
138 strict_mode_arguments_boilerplate) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000139 V(MESSAGE_LISTENERS_INDEX, JSObject, message_listeners) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000140 V(MAKE_MESSAGE_FUN_INDEX, JSFunction, make_message_fun) \
141 V(GET_STACK_TRACE_LINE_INDEX, JSFunction, get_stack_trace_line_fun) \
142 V(CONFIGURE_GLOBAL_INDEX, JSFunction, configure_global_fun) \
143 V(FUNCTION_CACHE_INDEX, JSObject, function_cache) \
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000144 V(JSFUNCTION_RESULT_CACHES_INDEX, FixedArray, jsfunction_result_caches) \
ricow@chromium.org65fae842010-08-25 15:26:24 +0000145 V(NORMALIZED_MAP_CACHE_INDEX, NormalizedMapCache, normalized_map_cache) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000146 V(RUNTIME_CONTEXT_INDEX, Context, runtime_context) \
147 V(CALL_AS_FUNCTION_DELEGATE_INDEX, JSFunction, call_as_function_delegate) \
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +0000148 V(CALL_AS_CONSTRUCTOR_DELEGATE_INDEX, JSFunction, \
149 call_as_constructor_delegate) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000150 V(SCRIPT_FUNCTION_INDEX, JSFunction, script_function) \
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000151 V(OPAQUE_REFERENCE_FUNCTION_INDEX, JSFunction, opaque_reference_function) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000152 V(CONTEXT_EXTENSION_FUNCTION_INDEX, JSFunction, context_extension_function) \
ager@chromium.org236ad962008-09-25 09:45:57 +0000153 V(OUT_OF_MEMORY_INDEX, Object, out_of_memory) \
ager@chromium.org9085a012009-05-11 19:22:57 +0000154 V(MAP_CACHE_INDEX, Object, map_cache) \
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000155 V(CONTEXT_DATA_INDEX, Object, data) \
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000156 V(ALLOW_CODE_GEN_FROM_STRINGS_INDEX, Object, allow_code_gen_from_strings) \
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000157 V(TO_COMPLETE_PROPERTY_DESCRIPTOR_INDEX, JSFunction, \
158 to_complete_property_descriptor) \
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000159 V(DERIVED_HAS_TRAP_INDEX, JSFunction, derived_has_trap) \
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000160 V(DERIVED_GET_TRAP_INDEX, JSFunction, derived_get_trap) \
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000161 V(DERIVED_SET_TRAP_INDEX, JSFunction, derived_set_trap) \
162 V(PROXY_ENUMERATE, JSFunction, proxy_enumerate) \
163 V(RANDOM_SEED_INDEX, ByteArray, random_seed)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000164
165// JSFunctions are pairs (context, function code), sometimes also called
166// closures. A Context object is used to represent function contexts and
167// dynamically pushed 'with' contexts (or 'scopes' in ECMA-262 speak).
168//
169// At runtime, the contexts build a stack in parallel to the execution
170// stack, with the top-most context being the current context. All contexts
171// have the following slots:
172//
173// [ closure ] This is the current function. It is the same for all
174// contexts inside a function. It provides access to the
175// incoming context (i.e., the outer context, which may
176// or may not become the current function's context), and
177// it provides access to the functions code and thus it's
178// scope information, which in turn contains the names of
179// statically allocated context slots. The names are needed
180// for dynamic lookups in the presence of 'with' or 'eval'.
181//
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000182// [ previous ] A pointer to the previous context. It is NULL for
183// function contexts, and non-NULL for 'with' contexts.
184// Used to implement the 'with' statement.
185//
186// [ extension ] A pointer to an extension JSObject, or NULL. Used to
187// implement 'with' statements and dynamic declarations
188// (through 'eval'). The object in a 'with' statement is
189// stored in the extension slot of a 'with' context.
190// Dynamically declared variables/functions are also added
191// to lazily allocated extension object. Context::Lookup
192// searches the extension object for properties.
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000193// For block contexts, contains the respective ScopeInfo.
194// For module contexts, points back to the respective JSModule.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000195//
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.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000218 PREVIOUS_INDEX,
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000219 // The extension slot is used for either the global object (in global
220 // contexts), eval extension object (function contexts), subject of with
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000221 // (with contexts), or the variable name (catch contexts), the serialized
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000222 // scope info (block contexts), or the module instance (module contexts).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000223 EXTENSION_INDEX,
224 GLOBAL_INDEX,
225 MIN_CONTEXT_SLOTS,
226
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000227 // This slot holds the thrown value in catch contexts.
228 THROWN_OBJECT_INDEX = MIN_CONTEXT_SLOTS,
229
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000230 // These slots are only in global contexts.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000231 GLOBAL_PROXY_INDEX = MIN_CONTEXT_SLOTS,
232 SECURITY_TOKEN_INDEX,
233 ARGUMENTS_BOILERPLATE_INDEX,
whesse@chromium.org7b260152011-06-20 15:33:18 +0000234 ALIASED_ARGUMENTS_BOILERPLATE_INDEX,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000235 STRICT_MODE_ARGUMENTS_BOILERPLATE_INDEX,
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000236 REGEXP_RESULT_MAP_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000237 FUNCTION_MAP_INDEX,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000238 STRICT_MODE_FUNCTION_MAP_INDEX,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000239 FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000240 STRICT_MODE_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000241 FUNCTION_INSTANCE_MAP_INDEX,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000242 STRICT_MODE_FUNCTION_INSTANCE_MAP_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000243 INITIAL_OBJECT_PROTOTYPE_INDEX,
244 BOOLEAN_FUNCTION_INDEX,
245 NUMBER_FUNCTION_INDEX,
246 STRING_FUNCTION_INDEX,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000247 STRING_FUNCTION_PROTOTYPE_MAP_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000248 OBJECT_FUNCTION_INDEX,
svenpanne@chromium.org3c93e772012-01-02 09:26:59 +0000249 INTERNAL_ARRAY_FUNCTION_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000250 ARRAY_FUNCTION_INDEX,
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +0000251 JS_ARRAY_MAPS_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000252 DATE_FUNCTION_INDEX,
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000253 JSON_OBJECT_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000254 REGEXP_FUNCTION_INDEX,
255 CREATE_DATE_FUN_INDEX,
256 TO_NUMBER_FUN_INDEX,
257 TO_STRING_FUN_INDEX,
258 TO_DETAIL_STRING_FUN_INDEX,
259 TO_OBJECT_FUN_INDEX,
260 TO_INTEGER_FUN_INDEX,
261 TO_UINT32_FUN_INDEX,
262 TO_INT32_FUN_INDEX,
263 TO_BOOLEAN_FUN_INDEX,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000264 GLOBAL_EVAL_FUN_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000265 INSTANTIATE_FUN_INDEX,
266 CONFIGURE_INSTANCE_FUN_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000267 MESSAGE_LISTENERS_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000268 MAKE_MESSAGE_FUN_INDEX,
269 GET_STACK_TRACE_LINE_INDEX,
270 CONFIGURE_GLOBAL_INDEX,
271 FUNCTION_CACHE_INDEX,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000272 JSFUNCTION_RESULT_CACHES_INDEX,
ricow@chromium.org65fae842010-08-25 15:26:24 +0000273 NORMALIZED_MAP_CACHE_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000274 RUNTIME_CONTEXT_INDEX,
275 CALL_AS_FUNCTION_DELEGATE_INDEX,
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +0000276 CALL_AS_CONSTRUCTOR_DELEGATE_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000277 SCRIPT_FUNCTION_INDEX,
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000278 OPAQUE_REFERENCE_FUNCTION_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000279 CONTEXT_EXTENSION_FUNCTION_INDEX,
280 OUT_OF_MEMORY_INDEX,
ager@chromium.org9085a012009-05-11 19:22:57 +0000281 CONTEXT_DATA_INDEX,
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000282 ALLOW_CODE_GEN_FROM_STRINGS_INDEX,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000283 TO_COMPLETE_PROPERTY_DESCRIPTOR_INDEX,
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000284 DERIVED_HAS_TRAP_INDEX,
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000285 DERIVED_GET_TRAP_INDEX,
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000286 DERIVED_SET_TRAP_INDEX,
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000287 PROXY_ENUMERATE,
288 RANDOM_SEED_INDEX,
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000289
290 // Properties from here are treated as weak references by the full GC.
291 // Scavenge treats them as strong references.
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000292 OPTIMIZED_FUNCTIONS_LIST, // Weak.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000293 MAP_CACHE_INDEX, // Weak.
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000294 NEXT_CONTEXT_LINK, // Weak.
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000295
296 // Total number of slots.
297 GLOBAL_CONTEXT_SLOTS,
298
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000299 FIRST_WEAK_SLOT = OPTIMIZED_FUNCTIONS_LIST
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000300 };
301
302 // Direct slot access.
303 JSFunction* closure() { return JSFunction::cast(get(CLOSURE_INDEX)); }
304 void set_closure(JSFunction* closure) { set(CLOSURE_INDEX, closure); }
305
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000306 Context* previous() {
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000307 Object* result = unchecked_previous();
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000308 ASSERT(IsBootstrappingOrValidParentContext(result, this));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000309 return reinterpret_cast<Context*>(result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000310 }
311 void set_previous(Context* context) { set(PREVIOUS_INDEX, context); }
312
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000313 bool has_extension() { return extension() != NULL; }
314 Object* extension() { return get(EXTENSION_INDEX); }
315 void set_extension(Object* object) { set(EXTENSION_INDEX, object); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000316
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000317 JSModule* module() { return JSModule::cast(get(EXTENSION_INDEX)); }
318 void set_module(JSModule* module) { set(EXTENSION_INDEX, module); }
319
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000320 // Get the context where var declarations will be hoisted to, which
321 // may be the context itself.
322 Context* declaration_context();
323
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000324 GlobalObject* global() {
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000325 Object* result = get(GLOBAL_INDEX);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000326 ASSERT(IsBootstrappingOrGlobalObject(result));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000327 return reinterpret_cast<GlobalObject*>(result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000328 }
329 void set_global(GlobalObject* global) { set(GLOBAL_INDEX, global); }
330
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000331 // Returns a JSGlobalProxy object or null.
332 JSObject* global_proxy();
333 void set_global_proxy(JSObject* global);
334
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000335 // The builtins object.
336 JSBuiltinsObject* builtins();
337
338 // Compute the global context by traversing the context chain.
339 Context* global_context();
340
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000341 // Predicates for context types. IsGlobalContext is defined on Object
342 // because we frequently have to know if arbitrary objects are global
343 // contexts.
344 bool IsFunctionContext() {
345 Map* map = this->map();
346 return map == map->GetHeap()->function_context_map();
347 }
348 bool IsCatchContext() {
349 Map* map = this->map();
350 return map == map->GetHeap()->catch_context_map();
351 }
352 bool IsWithContext() {
353 Map* map = this->map();
354 return map == map->GetHeap()->with_context_map();
355 }
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000356 bool IsBlockContext() {
357 Map* map = this->map();
358 return map == map->GetHeap()->block_context_map();
359 }
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000360 bool IsModuleContext() {
361 Map* map = this->map();
362 return map == map->GetHeap()->module_context_map();
363 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000364
365 // Tells whether the global context is marked with out of memory.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000366 inline bool has_out_of_memory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000367
368 // Mark the global context with out of memory.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000369 inline void mark_out_of_memory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000370
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000371 // A global context hold a list of all functions which have been optimized.
372 void AddOptimizedFunction(JSFunction* function);
373 void RemoveOptimizedFunction(JSFunction* function);
374 Object* OptimizedFunctionsListHead();
375 void ClearOptimizedFunctions();
376
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000377#define GLOBAL_CONTEXT_FIELD_ACCESSORS(index, type, name) \
378 void set_##name(type* value) { \
379 ASSERT(IsGlobalContext()); \
380 set(index, value); \
381 } \
382 type* name() { \
383 ASSERT(IsGlobalContext()); \
384 return type::cast(get(index)); \
385 }
386 GLOBAL_CONTEXT_FIELDS(GLOBAL_CONTEXT_FIELD_ACCESSORS)
387#undef GLOBAL_CONTEXT_FIELD_ACCESSORS
388
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000389 // Lookup the slot called name, starting with the current context.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000390 // There are three possibilities:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000391 //
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000392 // 1) result->IsContext():
393 // The binding was found in a context. *index is always the
394 // non-negative slot index. *attributes is NONE for var and let
395 // declarations, READ_ONLY for const declarations (never ABSENT).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000396 //
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000397 // 2) result->IsJSObject():
398 // The binding was found as a named property in a context extension
399 // object (i.e., was introduced via eval), as a property on the subject
400 // of with, or as a property of the global object. *index is -1 and
401 // *attributes is not ABSENT.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000402 //
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000403 // 3) result.is_null():
404 // There was no binding found, *index is always -1 and *attributes is
405 // always ABSENT.
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +0000406 Handle<Object> Lookup(Handle<String> name,
407 ContextLookupFlags flags,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000408 int* index,
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +0000409 PropertyAttributes* attributes,
410 BindingFlags* binding_flags);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000411
412 // Code generation support.
413 static int SlotOffset(int index) {
414 return kHeaderSize + index * kPointerSize - kHeapObjectTag;
415 }
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000416
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000417 static const int kSize = kHeaderSize + GLOBAL_CONTEXT_SLOTS * kPointerSize;
418
419 // GC support.
420 typedef FixedBodyDescriptor<
421 kHeaderSize, kSize, kSize> ScavengeBodyDescriptor;
422
423 typedef FixedBodyDescriptor<
424 kHeaderSize,
425 kHeaderSize + FIRST_WEAK_SLOT * kPointerSize,
426 kSize> MarkCompactBodyDescriptor;
427
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000428 private:
429 // Unchecked access to the slots.
430 Object* unchecked_previous() { return get(PREVIOUS_INDEX); }
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000431
432#ifdef DEBUG
433 // Bootstrapping-aware type checks.
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000434 static bool IsBootstrappingOrValidParentContext(Object* object, Context* kid);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000435 static bool IsBootstrappingOrGlobalObject(Object* object);
436#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000437};
438
439} } // namespace v8::internal
440
441#endif // V8_CONTEXTS_H_