blob: 100e1a256e689167dc2176f90935c5e2eb90ca17 [file] [log] [blame]
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001// Copyright 2011 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
47// Heap-allocated activation contexts.
48//
49// Contexts are implemented as FixedArray objects; the Context
50// class is a convenience interface casted on a FixedArray object.
51//
52// Note: Context must have no virtual functions and Context objects
53// must always be allocated via Heap::AllocateContext() or
54// Factory::NewContext.
55
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000056#define GLOBAL_CONTEXT_FIELDS(V) \
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +000057 V(GLOBAL_PROXY_INDEX, JSObject, global_proxy_object) \
58 V(SECURITY_TOKEN_INDEX, Object, security_token) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000059 V(BOOLEAN_FUNCTION_INDEX, JSFunction, boolean_function) \
60 V(NUMBER_FUNCTION_INDEX, JSFunction, number_function) \
61 V(STRING_FUNCTION_INDEX, JSFunction, string_function) \
ager@chromium.orgea4f62e2010-08-16 16:28:43 +000062 V(STRING_FUNCTION_PROTOTYPE_MAP_INDEX, Map, string_function_prototype_map) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000063 V(OBJECT_FUNCTION_INDEX, JSFunction, object_function) \
64 V(ARRAY_FUNCTION_INDEX, JSFunction, array_function) \
65 V(DATE_FUNCTION_INDEX, JSFunction, date_function) \
ager@chromium.org3a37e9b2009-04-27 09:26:21 +000066 V(JSON_OBJECT_INDEX, JSObject, json_object) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000067 V(REGEXP_FUNCTION_INDEX, JSFunction, regexp_function) \
68 V(INITIAL_OBJECT_PROTOTYPE_INDEX, JSObject, initial_object_prototype) \
69 V(CREATE_DATE_FUN_INDEX, JSFunction, create_date_fun) \
70 V(TO_NUMBER_FUN_INDEX, JSFunction, to_number_fun) \
71 V(TO_STRING_FUN_INDEX, JSFunction, to_string_fun) \
72 V(TO_DETAIL_STRING_FUN_INDEX, JSFunction, to_detail_string_fun) \
73 V(TO_OBJECT_FUN_INDEX, JSFunction, to_object_fun) \
74 V(TO_INTEGER_FUN_INDEX, JSFunction, to_integer_fun) \
75 V(TO_UINT32_FUN_INDEX, JSFunction, to_uint32_fun) \
76 V(TO_INT32_FUN_INDEX, JSFunction, to_int32_fun) \
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000077 V(GLOBAL_EVAL_FUN_INDEX, JSFunction, global_eval_fun) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000078 V(INSTANTIATE_FUN_INDEX, JSFunction, instantiate_fun) \
79 V(CONFIGURE_INSTANCE_FUN_INDEX, JSFunction, configure_instance_fun) \
80 V(FUNCTION_MAP_INDEX, Map, function_map) \
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000081 V(STRICT_MODE_FUNCTION_MAP_INDEX, Map, strict_mode_function_map) \
kmillikin@chromium.org4111b802010-05-03 10:34:42 +000082 V(FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map, function_without_prototype_map) \
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000083 V(STRICT_MODE_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map, \
84 strict_mode_function_without_prototype_map) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000085 V(FUNCTION_INSTANCE_MAP_INDEX, Map, function_instance_map) \
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000086 V(STRICT_MODE_FUNCTION_INSTANCE_MAP_INDEX, Map, \
87 strict_mode_function_instance_map) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000088 V(JS_ARRAY_MAP_INDEX, Map, js_array_map)\
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +000089 V(REGEXP_RESULT_MAP_INDEX, Map, regexp_result_map)\
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000090 V(ARGUMENTS_BOILERPLATE_INDEX, JSObject, arguments_boilerplate) \
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000091 V(STRICT_MODE_ARGUMENTS_BOILERPLATE_INDEX, JSObject, \
92 strict_mode_arguments_boilerplate) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000093 V(MESSAGE_LISTENERS_INDEX, JSObject, message_listeners) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000094 V(MAKE_MESSAGE_FUN_INDEX, JSFunction, make_message_fun) \
95 V(GET_STACK_TRACE_LINE_INDEX, JSFunction, get_stack_trace_line_fun) \
96 V(CONFIGURE_GLOBAL_INDEX, JSFunction, configure_global_fun) \
97 V(FUNCTION_CACHE_INDEX, JSObject, function_cache) \
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000098 V(JSFUNCTION_RESULT_CACHES_INDEX, FixedArray, jsfunction_result_caches) \
ricow@chromium.org65fae842010-08-25 15:26:24 +000099 V(NORMALIZED_MAP_CACHE_INDEX, NormalizedMapCache, normalized_map_cache) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000100 V(RUNTIME_CONTEXT_INDEX, Context, runtime_context) \
101 V(CALL_AS_FUNCTION_DELEGATE_INDEX, JSFunction, call_as_function_delegate) \
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +0000102 V(CALL_AS_CONSTRUCTOR_DELEGATE_INDEX, JSFunction, \
103 call_as_constructor_delegate) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000104 V(SCRIPT_FUNCTION_INDEX, JSFunction, script_function) \
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000105 V(OPAQUE_REFERENCE_FUNCTION_INDEX, JSFunction, opaque_reference_function) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000106 V(CONTEXT_EXTENSION_FUNCTION_INDEX, JSFunction, context_extension_function) \
ager@chromium.org236ad962008-09-25 09:45:57 +0000107 V(OUT_OF_MEMORY_INDEX, Object, out_of_memory) \
ager@chromium.org9085a012009-05-11 19:22:57 +0000108 V(MAP_CACHE_INDEX, Object, map_cache) \
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000109 V(CONTEXT_DATA_INDEX, Object, data) \
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000110 V(ALLOW_CODE_GEN_FROM_STRINGS_INDEX, Object, allow_code_gen_from_strings) \
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000111 V(DERIVED_GET_TRAP_INDEX, JSFunction, derived_get_trap) \
112 V(DERIVED_SET_TRAP_INDEX, JSFunction, derived_set_trap)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000113
114// JSFunctions are pairs (context, function code), sometimes also called
115// closures. A Context object is used to represent function contexts and
116// dynamically pushed 'with' contexts (or 'scopes' in ECMA-262 speak).
117//
118// At runtime, the contexts build a stack in parallel to the execution
119// stack, with the top-most context being the current context. All contexts
120// have the following slots:
121//
122// [ closure ] This is the current function. It is the same for all
123// contexts inside a function. It provides access to the
124// incoming context (i.e., the outer context, which may
125// or may not become the current function's context), and
126// it provides access to the functions code and thus it's
127// scope information, which in turn contains the names of
128// statically allocated context slots. The names are needed
129// for dynamic lookups in the presence of 'with' or 'eval'.
130//
131// [ fcontext ] A pointer to the innermost enclosing function context.
132// It is the same for all contexts *allocated* inside a
133// function, and the function context's fcontext points
134// to itself. It is only needed for fast access of the
135// function context (used for declarations, and static
136// context slot access).
137//
138// [ previous ] A pointer to the previous context. It is NULL for
139// function contexts, and non-NULL for 'with' contexts.
140// Used to implement the 'with' statement.
141//
142// [ extension ] A pointer to an extension JSObject, or NULL. Used to
143// implement 'with' statements and dynamic declarations
144// (through 'eval'). The object in a 'with' statement is
145// stored in the extension slot of a 'with' context.
146// Dynamically declared variables/functions are also added
147// to lazily allocated extension object. Context::Lookup
148// searches the extension object for properties.
149//
150// [ global ] A pointer to the global object. Provided for quick
151// access to the global object from inside the code (since
152// we always have a context pointer).
153//
154// In addition, function contexts may have statically allocated context slots
155// to store local variables/functions that are accessed from inner functions
156// (via static context addresses) or through 'eval' (dynamic context lookups).
157// Finally, the global context contains additional slots for fast access to
158// global properties.
159//
160// We may be able to simplify the implementation:
161//
162// - We may be able to get rid of 'fcontext': We can always use the fact that
163// previous == NULL for function contexts and so we can search for them. They
164// are only needed when doing dynamic declarations, and the context chains
165// tend to be very very short (depth of nesting of 'with' statements). At
166// the moment we also use it in generated code for context slot accesses -
167// and there we don't want a loop because of code bloat - but we may not
168// need it there after all (see comment in codegen_*.cc).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000169
170class Context: public FixedArray {
171 public:
172 // Conversions.
173 static Context* cast(Object* context) {
174 ASSERT(context->IsContext());
175 return reinterpret_cast<Context*>(context);
176 }
177
178 // The default context slot layout; indices are FixedArray slot indices.
179 enum {
180 // These slots are in all contexts.
181 CLOSURE_INDEX,
182 FCONTEXT_INDEX,
183 PREVIOUS_INDEX,
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000184 // The extension slot is used for either the global object (in global
185 // contexts), eval extension object (function contexts), subject of with
186 // (with contexts), or the variable name (catch contexts).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000187 EXTENSION_INDEX,
188 GLOBAL_INDEX,
189 MIN_CONTEXT_SLOTS,
190
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000191 // This slot holds the thrown value in catch contexts.
192 THROWN_OBJECT_INDEX = MIN_CONTEXT_SLOTS,
193
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000194 // These slots are only in global contexts.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000195 GLOBAL_PROXY_INDEX = MIN_CONTEXT_SLOTS,
196 SECURITY_TOKEN_INDEX,
197 ARGUMENTS_BOILERPLATE_INDEX,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000198 STRICT_MODE_ARGUMENTS_BOILERPLATE_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000199 JS_ARRAY_MAP_INDEX,
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000200 REGEXP_RESULT_MAP_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000201 FUNCTION_MAP_INDEX,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000202 STRICT_MODE_FUNCTION_MAP_INDEX,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000203 FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000204 STRICT_MODE_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000205 FUNCTION_INSTANCE_MAP_INDEX,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000206 STRICT_MODE_FUNCTION_INSTANCE_MAP_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000207 INITIAL_OBJECT_PROTOTYPE_INDEX,
208 BOOLEAN_FUNCTION_INDEX,
209 NUMBER_FUNCTION_INDEX,
210 STRING_FUNCTION_INDEX,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000211 STRING_FUNCTION_PROTOTYPE_MAP_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000212 OBJECT_FUNCTION_INDEX,
213 ARRAY_FUNCTION_INDEX,
214 DATE_FUNCTION_INDEX,
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000215 JSON_OBJECT_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000216 REGEXP_FUNCTION_INDEX,
217 CREATE_DATE_FUN_INDEX,
218 TO_NUMBER_FUN_INDEX,
219 TO_STRING_FUN_INDEX,
220 TO_DETAIL_STRING_FUN_INDEX,
221 TO_OBJECT_FUN_INDEX,
222 TO_INTEGER_FUN_INDEX,
223 TO_UINT32_FUN_INDEX,
224 TO_INT32_FUN_INDEX,
225 TO_BOOLEAN_FUN_INDEX,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000226 GLOBAL_EVAL_FUN_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000227 INSTANTIATE_FUN_INDEX,
228 CONFIGURE_INSTANCE_FUN_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000229 MESSAGE_LISTENERS_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000230 MAKE_MESSAGE_FUN_INDEX,
231 GET_STACK_TRACE_LINE_INDEX,
232 CONFIGURE_GLOBAL_INDEX,
233 FUNCTION_CACHE_INDEX,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000234 JSFUNCTION_RESULT_CACHES_INDEX,
ricow@chromium.org65fae842010-08-25 15:26:24 +0000235 NORMALIZED_MAP_CACHE_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000236 RUNTIME_CONTEXT_INDEX,
237 CALL_AS_FUNCTION_DELEGATE_INDEX,
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +0000238 CALL_AS_CONSTRUCTOR_DELEGATE_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000239 SCRIPT_FUNCTION_INDEX,
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000240 OPAQUE_REFERENCE_FUNCTION_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000241 CONTEXT_EXTENSION_FUNCTION_INDEX,
242 OUT_OF_MEMORY_INDEX,
ager@chromium.org236ad962008-09-25 09:45:57 +0000243 MAP_CACHE_INDEX,
ager@chromium.org9085a012009-05-11 19:22:57 +0000244 CONTEXT_DATA_INDEX,
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000245 ALLOW_CODE_GEN_FROM_STRINGS_INDEX,
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000246 DERIVED_GET_TRAP_INDEX,
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000247 DERIVED_SET_TRAP_INDEX,
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000248
249 // Properties from here are treated as weak references by the full GC.
250 // Scavenge treats them as strong references.
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000251 OPTIMIZED_FUNCTIONS_LIST, // Weak.
252 NEXT_CONTEXT_LINK, // Weak.
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000253
254 // Total number of slots.
255 GLOBAL_CONTEXT_SLOTS,
256
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000257 FIRST_WEAK_SLOT = OPTIMIZED_FUNCTIONS_LIST
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000258 };
259
260 // Direct slot access.
261 JSFunction* closure() { return JSFunction::cast(get(CLOSURE_INDEX)); }
262 void set_closure(JSFunction* closure) { set(CLOSURE_INDEX, closure); }
263
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000264 Context* fcontext() { return Context::cast(get(FCONTEXT_INDEX)); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000265 void set_fcontext(Context* context) { set(FCONTEXT_INDEX, context); }
266
267 Context* previous() {
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000268 Object* result = unchecked_previous();
269 ASSERT(IsBootstrappingOrContext(result));
270 return reinterpret_cast<Context*>(result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000271 }
272 void set_previous(Context* context) { set(PREVIOUS_INDEX, context); }
273
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000274 bool has_extension() { return extension() != NULL; }
275 Object* extension() { return get(EXTENSION_INDEX); }
276 void set_extension(Object* object) { set(EXTENSION_INDEX, object); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000277
278 GlobalObject* global() {
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000279 Object* result = get(GLOBAL_INDEX);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000280 ASSERT(IsBootstrappingOrGlobalObject(result));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000281 return reinterpret_cast<GlobalObject*>(result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000282 }
283 void set_global(GlobalObject* global) { set(GLOBAL_INDEX, global); }
284
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000285 // Returns a JSGlobalProxy object or null.
286 JSObject* global_proxy();
287 void set_global_proxy(JSObject* global);
288
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000289 // The builtins object.
290 JSBuiltinsObject* builtins();
291
292 // Compute the global context by traversing the context chain.
293 Context* global_context();
294
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000295 // Predicates for context types. IsGlobalContext is defined on Object
296 // because we frequently have to know if arbitrary objects are global
297 // contexts.
298 bool IsFunctionContext() {
299 Map* map = this->map();
300 return map == map->GetHeap()->function_context_map();
301 }
302 bool IsCatchContext() {
303 Map* map = this->map();
304 return map == map->GetHeap()->catch_context_map();
305 }
306 bool IsWithContext() {
307 Map* map = this->map();
308 return map == map->GetHeap()->with_context_map();
309 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000310
311 // Tells whether the global context is marked with out of memory.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000312 inline bool has_out_of_memory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000313
314 // Mark the global context with out of memory.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000315 inline void mark_out_of_memory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000316
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000317 // The exception holder is the object used as a with object in
318 // the implementation of a catch block.
319 bool is_exception_holder(Object* object) {
320 return IsCatchContext() && extension() == object;
321 }
322
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000323 // A global context hold a list of all functions which have been optimized.
324 void AddOptimizedFunction(JSFunction* function);
325 void RemoveOptimizedFunction(JSFunction* function);
326 Object* OptimizedFunctionsListHead();
327 void ClearOptimizedFunctions();
328
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000329#define GLOBAL_CONTEXT_FIELD_ACCESSORS(index, type, name) \
330 void set_##name(type* value) { \
331 ASSERT(IsGlobalContext()); \
332 set(index, value); \
333 } \
334 type* name() { \
335 ASSERT(IsGlobalContext()); \
336 return type::cast(get(index)); \
337 }
338 GLOBAL_CONTEXT_FIELDS(GLOBAL_CONTEXT_FIELD_ACCESSORS)
339#undef GLOBAL_CONTEXT_FIELD_ACCESSORS
340
341 // Lookup the the slot called name, starting with the current context.
342 // There are 4 possible outcomes:
343 //
344 // 1) index_ >= 0 && result->IsContext():
345 // most common case, the result is a Context, and index is the
346 // context slot index, and the slot exists.
347 // attributes == READ_ONLY for the function name variable, NONE otherwise.
348 //
349 // 2) index_ >= 0 && result->IsJSObject():
350 // the result is the JSObject arguments object, the index is the parameter
351 // index, i.e., key into the arguments object, and the property exists.
352 // attributes != ABSENT.
353 //
354 // 3) index_ < 0 && result->IsJSObject():
355 // the result is the JSObject extension context or the global object,
356 // and the name is the property name, and the property exists.
357 // attributes != ABSENT.
358 //
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000359 // 4) index_ < 0 && result.is_null():
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000360 // there was no context found with the corresponding property.
361 // attributes == ABSENT.
362 Handle<Object> Lookup(Handle<String> name, ContextLookupFlags flags,
363 int* index_, PropertyAttributes* attributes);
364
ager@chromium.org381abbb2009-02-25 13:23:22 +0000365 // Determine if a local variable with the given name exists in a
366 // context. Do not consider context extension objects. This is
367 // used for compiling code using eval. If the context surrounding
368 // the eval call does not have a local variable with this name and
369 // does not contain a with statement the property is global unless
370 // it is shadowed by a property in an extension object introduced by
371 // eval.
372 bool GlobalIfNotShadowedByEval(Handle<String> name);
373
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000374 // Determine if any function scope in the context call eval and if
375 // any of those calls are in non-strict mode.
376 void ComputeEvalScopeInfo(bool* outer_scope_calls_eval,
377 bool* outer_scope_calls_non_strict_eval);
378
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000379 // Code generation support.
380 static int SlotOffset(int index) {
381 return kHeaderSize + index * kPointerSize - kHeapObjectTag;
382 }
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000383
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000384 static const int kSize = kHeaderSize + GLOBAL_CONTEXT_SLOTS * kPointerSize;
385
386 // GC support.
387 typedef FixedBodyDescriptor<
388 kHeaderSize, kSize, kSize> ScavengeBodyDescriptor;
389
390 typedef FixedBodyDescriptor<
391 kHeaderSize,
392 kHeaderSize + FIRST_WEAK_SLOT * kPointerSize,
393 kSize> MarkCompactBodyDescriptor;
394
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000395 private:
396 // Unchecked access to the slots.
397 Object* unchecked_previous() { return get(PREVIOUS_INDEX); }
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000398
399#ifdef DEBUG
400 // Bootstrapping-aware type checks.
401 static bool IsBootstrappingOrContext(Object* object);
402 static bool IsBootstrappingOrGlobalObject(Object* object);
403#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000404};
405
406} } // namespace v8::internal
407
408#endif // V8_CONTEXTS_H_