contexts.h revision e46be819fca9468a0cd4e74859ce0f778eb8ca60
1a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Copyright 2006-2008 the V8 project authors. All rights reserved.
2a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Redistribution and use in source and binary forms, with or without
3a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// modification, are permitted provided that the following conditions are
4a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// met:
5a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//
6a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//     * Redistributions of source code must retain the above copyright
7a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       notice, this list of conditions and the following disclaimer.
8a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//     * Redistributions in binary form must reproduce the above
9a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       copyright notice, this list of conditions and the following
10a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       disclaimer in the documentation and/or other materials provided
11a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       with the distribution.
12a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//     * Neither the name of Google Inc. nor the names of its
13a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       contributors may be used to endorse or promote products derived
14a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       from this software without specific prior written permission.
15a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//
16a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
28a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#ifndef V8_CONTEXTS_H_
29a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#define V8_CONTEXTS_H_
30a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
31a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocknamespace v8 {
32a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocknamespace internal {
33a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
34a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
35a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockenum ContextLookupFlags {
36a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  FOLLOW_CONTEXT_CHAIN = 1,
37a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  FOLLOW_PROTOTYPE_CHAIN = 2,
38a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
39a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  DONT_FOLLOW_CHAINS = 0,
40a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  FOLLOW_CHAINS = FOLLOW_CONTEXT_CHAIN | FOLLOW_PROTOTYPE_CHAIN
41a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block};
42a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
43a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
44a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Heap-allocated activation contexts.
45a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//
46a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Contexts are implemented as FixedArray objects; the Context
47a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// class is a convenience interface casted on a FixedArray object.
48a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//
49a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Note: Context must have no virtual functions and Context objects
50a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// must always be allocated via Heap::AllocateContext() or
51a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Factory::NewContext.
52a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
53a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Comment for special_function_table:
54a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Table for providing optimized/specialized functions.
55a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// The array contains triplets [object, general_function, optimized_function].
56a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Primarily added to support built-in optimized variants of
57a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Array.prototype.{push,pop}.
58a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
59a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#define GLOBAL_CONTEXT_FIELDS(V) \
60a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(GLOBAL_PROXY_INDEX, JSObject, global_proxy_object) \
61a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(SECURITY_TOKEN_INDEX, Object, security_token) \
62a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(BOOLEAN_FUNCTION_INDEX, JSFunction, boolean_function) \
63a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(NUMBER_FUNCTION_INDEX, JSFunction, number_function) \
64a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(STRING_FUNCTION_INDEX, JSFunction, string_function) \
65a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(OBJECT_FUNCTION_INDEX, JSFunction, object_function) \
66a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(ARRAY_FUNCTION_INDEX, JSFunction, array_function) \
67a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(DATE_FUNCTION_INDEX, JSFunction, date_function) \
68a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(JSON_OBJECT_INDEX, JSObject, json_object) \
69a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(REGEXP_FUNCTION_INDEX, JSFunction, regexp_function) \
70a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(INITIAL_OBJECT_PROTOTYPE_INDEX, JSObject, initial_object_prototype) \
71a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(CREATE_DATE_FUN_INDEX, JSFunction,  create_date_fun) \
72a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(TO_NUMBER_FUN_INDEX, JSFunction, to_number_fun) \
73a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(TO_STRING_FUN_INDEX, JSFunction, to_string_fun) \
74a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(TO_DETAIL_STRING_FUN_INDEX, JSFunction, to_detail_string_fun) \
75a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(TO_OBJECT_FUN_INDEX, JSFunction, to_object_fun) \
76a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(TO_INTEGER_FUN_INDEX, JSFunction, to_integer_fun) \
77a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(TO_UINT32_FUN_INDEX, JSFunction, to_uint32_fun) \
78a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(TO_INT32_FUN_INDEX, JSFunction, to_int32_fun) \
79a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(TO_BOOLEAN_FUN_INDEX, JSFunction, to_boolean_fun) \
80e46be819fca9468a0cd4e74859ce0f778eb8ca60Leon Clarke  V(GLOBAL_EVAL_FUN_INDEX, JSFunction, global_eval_fun) \
81a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(INSTANTIATE_FUN_INDEX, JSFunction, instantiate_fun) \
82a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(CONFIGURE_INSTANCE_FUN_INDEX, JSFunction, configure_instance_fun) \
83a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(FUNCTION_MAP_INDEX, Map, function_map) \
84a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(FUNCTION_INSTANCE_MAP_INDEX, Map, function_instance_map) \
85a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(JS_ARRAY_MAP_INDEX, Map, js_array_map)\
86a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(SPECIAL_FUNCTION_TABLE_INDEX, FixedArray, special_function_table) \
87a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(ARGUMENTS_BOILERPLATE_INDEX, JSObject, arguments_boilerplate) \
88a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(MESSAGE_LISTENERS_INDEX, JSObject, message_listeners) \
89a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(MAKE_MESSAGE_FUN_INDEX, JSFunction, make_message_fun) \
90a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(GET_STACK_TRACE_LINE_INDEX, JSFunction, get_stack_trace_line_fun) \
91a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(CONFIGURE_GLOBAL_INDEX, JSFunction, configure_global_fun) \
92a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(FUNCTION_CACHE_INDEX, JSObject, function_cache) \
93a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(RUNTIME_CONTEXT_INDEX, Context, runtime_context) \
94a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(CALL_AS_FUNCTION_DELEGATE_INDEX, JSFunction, call_as_function_delegate) \
95a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(CALL_AS_CONSTRUCTOR_DELEGATE_INDEX, JSFunction, \
96a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    call_as_constructor_delegate) \
97a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(EMPTY_SCRIPT_INDEX, Script, empty_script) \
98a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(SCRIPT_FUNCTION_INDEX, JSFunction, script_function) \
99a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(CONTEXT_EXTENSION_FUNCTION_INDEX, JSFunction, context_extension_function) \
100a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(OUT_OF_MEMORY_INDEX, Object, out_of_memory) \
101a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(MAP_CACHE_INDEX, Object, map_cache) \
102a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(CONTEXT_DATA_INDEX, Object, data)
103a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
104a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// JSFunctions are pairs (context, function code), sometimes also called
105a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// closures. A Context object is used to represent function contexts and
106a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// dynamically pushed 'with' contexts (or 'scopes' in ECMA-262 speak).
107a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//
108a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// At runtime, the contexts build a stack in parallel to the execution
109a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// stack, with the top-most context being the current context. All contexts
110a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// have the following slots:
111a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//
112a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// [ closure   ]  This is the current function. It is the same for all
113a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//                contexts inside a function. It provides access to the
114a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//                incoming context (i.e., the outer context, which may
115a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//                or may not become the current function's context), and
116a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//                it provides access to the functions code and thus it's
117a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//                scope information, which in turn contains the names of
118a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//                statically allocated context slots. The names are needed
119a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//                for dynamic lookups in the presence of 'with' or 'eval'.
120a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//
121a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// [ fcontext  ]  A pointer to the innermost enclosing function context.
122a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//                It is the same for all contexts *allocated* inside a
123a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//                function, and the function context's fcontext points
124a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//                to itself. It is only needed for fast access of the
125a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//                function context (used for declarations, and static
126a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//                context slot access).
127a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//
128a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// [ previous  ]  A pointer to the previous context. It is NULL for
129a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//                function contexts, and non-NULL for 'with' contexts.
130a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//                Used to implement the 'with' statement.
131a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//
132a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// [ extension ]  A pointer to an extension JSObject, or NULL. Used to
133a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//                implement 'with' statements and dynamic declarations
134a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//                (through 'eval'). The object in a 'with' statement is
135a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//                stored in the extension slot of a 'with' context.
136a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//                Dynamically declared variables/functions are also added
137a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//                to lazily allocated extension object. Context::Lookup
138a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//                searches the extension object for properties.
139a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//
140a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// [ global    ]  A pointer to the global object. Provided for quick
141a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//                access to the global object from inside the code (since
142a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//                we always have a context pointer).
143a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//
144a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// In addition, function contexts may have statically allocated context slots
145a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// to store local variables/functions that are accessed from inner functions
146a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// (via static context addresses) or through 'eval' (dynamic context lookups).
147a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Finally, the global context contains additional slots for fast access to
148a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// global properties.
149a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//
150a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// We may be able to simplify the implementation:
151a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//
152a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// - We may be able to get rid of 'fcontext': We can always use the fact that
153a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//   previous == NULL for function contexts and so we can search for them. They
154a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//   are only needed when doing dynamic declarations, and the context chains
155a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//   tend to be very very short (depth of nesting of 'with' statements). At
156a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//   the moment we also use it in generated code for context slot accesses -
157a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//   and there we don't want a loop because of code bloat - but we may not
158a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//   need it there after all (see comment in codegen_*.cc).
159a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//
160a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// - If we cannot get rid of fcontext, consider making 'previous' never NULL
161a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//   except for the global context. This could simplify Context::Lookup.
162a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
163a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockclass Context: public FixedArray {
164a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block public:
165a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Conversions.
166a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  static Context* cast(Object* context) {
167a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    ASSERT(context->IsContext());
168a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    return reinterpret_cast<Context*>(context);
169a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
170a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
171a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // The default context slot layout; indices are FixedArray slot indices.
172a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  enum {
173a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    // These slots are in all contexts.
174a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    CLOSURE_INDEX,
175a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    FCONTEXT_INDEX,
176a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    PREVIOUS_INDEX,
177a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    EXTENSION_INDEX,
178a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    GLOBAL_INDEX,
179a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    MIN_CONTEXT_SLOTS,
180a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
181a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    // These slots are only in global contexts.
182a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    GLOBAL_PROXY_INDEX = MIN_CONTEXT_SLOTS,
183a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    SECURITY_TOKEN_INDEX,
184a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    ARGUMENTS_BOILERPLATE_INDEX,
185a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    JS_ARRAY_MAP_INDEX,
186a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    FUNCTION_MAP_INDEX,
187a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    FUNCTION_INSTANCE_MAP_INDEX,
188a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    INITIAL_OBJECT_PROTOTYPE_INDEX,
189a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    BOOLEAN_FUNCTION_INDEX,
190a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    NUMBER_FUNCTION_INDEX,
191a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    STRING_FUNCTION_INDEX,
192a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    OBJECT_FUNCTION_INDEX,
193a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    ARRAY_FUNCTION_INDEX,
194a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    DATE_FUNCTION_INDEX,
195a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    JSON_OBJECT_INDEX,
196a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    REGEXP_FUNCTION_INDEX,
197a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    CREATE_DATE_FUN_INDEX,
198a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    TO_NUMBER_FUN_INDEX,
199a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    TO_STRING_FUN_INDEX,
200a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    TO_DETAIL_STRING_FUN_INDEX,
201a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    TO_OBJECT_FUN_INDEX,
202a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    TO_INTEGER_FUN_INDEX,
203a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    TO_UINT32_FUN_INDEX,
204a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    TO_INT32_FUN_INDEX,
205a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    TO_BOOLEAN_FUN_INDEX,
206e46be819fca9468a0cd4e74859ce0f778eb8ca60Leon Clarke    GLOBAL_EVAL_FUN_INDEX,
207a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    INSTANTIATE_FUN_INDEX,
208a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    CONFIGURE_INSTANCE_FUN_INDEX,
209a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    SPECIAL_FUNCTION_TABLE_INDEX,
210a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    MESSAGE_LISTENERS_INDEX,
211a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    MAKE_MESSAGE_FUN_INDEX,
212a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    GET_STACK_TRACE_LINE_INDEX,
213a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    CONFIGURE_GLOBAL_INDEX,
214a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    FUNCTION_CACHE_INDEX,
215a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    RUNTIME_CONTEXT_INDEX,
216a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    CALL_AS_FUNCTION_DELEGATE_INDEX,
217a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    CALL_AS_CONSTRUCTOR_DELEGATE_INDEX,
218a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    EMPTY_SCRIPT_INDEX,
219a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    SCRIPT_FUNCTION_INDEX,
220a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    CONTEXT_EXTENSION_FUNCTION_INDEX,
221a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    OUT_OF_MEMORY_INDEX,
222a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    MAP_CACHE_INDEX,
223a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    CONTEXT_DATA_INDEX,
224a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    GLOBAL_CONTEXT_SLOTS
225a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  };
226a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
227a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Direct slot access.
228a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  JSFunction* closure() { return JSFunction::cast(get(CLOSURE_INDEX)); }
229a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  void set_closure(JSFunction* closure) { set(CLOSURE_INDEX, closure); }
230a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
231a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  Context* fcontext() { return Context::cast(get(FCONTEXT_INDEX)); }
232a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  void set_fcontext(Context* context) { set(FCONTEXT_INDEX, context); }
233a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
234a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  Context* previous() {
235a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    Object* result = unchecked_previous();
236a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    ASSERT(IsBootstrappingOrContext(result));
237a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    return reinterpret_cast<Context*>(result);
238a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
239a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  void set_previous(Context* context) { set(PREVIOUS_INDEX, context); }
240a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
241a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  bool has_extension() { return unchecked_extension() != NULL; }
242a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  JSObject* extension() { return JSObject::cast(unchecked_extension()); }
243a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  void set_extension(JSObject* object) { set(EXTENSION_INDEX, object); }
244a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
245a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  GlobalObject* global() {
246a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    Object* result = get(GLOBAL_INDEX);
247a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    ASSERT(IsBootstrappingOrGlobalObject(result));
248a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    return reinterpret_cast<GlobalObject*>(result);
249a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
250a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  void set_global(GlobalObject* global) { set(GLOBAL_INDEX, global); }
251a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
252a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Returns a JSGlobalProxy object or null.
253a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  JSObject* global_proxy();
254a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  void set_global_proxy(JSObject* global);
255a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
256a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // The builtins object.
257a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  JSBuiltinsObject* builtins();
258a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
259a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Compute the global context by traversing the context chain.
260a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  Context* global_context();
261a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
262a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Tells if this is a function context (as opposed to a 'with' context).
263a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  bool is_function_context() { return unchecked_previous() == NULL; }
264a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
265a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Tells whether the global context is marked with out of memory.
266a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  bool has_out_of_memory() {
267a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    return global_context()->out_of_memory() == Heap::true_value();
268a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
269a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
270a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Mark the global context with out of memory.
271a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  void mark_out_of_memory() {
272a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    global_context()->set_out_of_memory(Heap::true_value());
273a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
274a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
275a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // The exception holder is the object used as a with object in
276a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // the implementation of a catch block.
277a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  bool is_exception_holder(Object* object) {
278a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    return IsCatchContext() && extension() == object;
279a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
280a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
281a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#define GLOBAL_CONTEXT_FIELD_ACCESSORS(index, type, name) \
282a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  void  set_##name(type* value) {                         \
283a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    ASSERT(IsGlobalContext());                            \
284a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    set(index, value);                                    \
285a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }                                                       \
286a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  type* name() {                                          \
287a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    ASSERT(IsGlobalContext());                            \
288a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    return type::cast(get(index));                        \
289a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
290a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  GLOBAL_CONTEXT_FIELDS(GLOBAL_CONTEXT_FIELD_ACCESSORS)
291a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#undef GLOBAL_CONTEXT_FIELD_ACCESSORS
292a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
293a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Lookup the the slot called name, starting with the current context.
294a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // There are 4 possible outcomes:
295a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  //
296a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // 1) index_ >= 0 && result->IsContext():
297a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  //    most common case, the result is a Context, and index is the
298a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  //    context slot index, and the slot exists.
299a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  //    attributes == READ_ONLY for the function name variable, NONE otherwise.
300a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  //
301a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // 2) index_ >= 0 && result->IsJSObject():
302a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  //    the result is the JSObject arguments object, the index is the parameter
303a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  //    index, i.e., key into the arguments object, and the property exists.
304a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  //    attributes != ABSENT.
305a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  //
306a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // 3) index_ < 0 && result->IsJSObject():
307a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  //    the result is the JSObject extension context or the global object,
308a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  //    and the name is the property name, and the property exists.
309a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  //    attributes != ABSENT.
310a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  //
311a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // 4) index_ < 0 && result.is_null():
312a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  //    there was no context found with the corresponding property.
313a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  //    attributes == ABSENT.
314a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  Handle<Object> Lookup(Handle<String> name, ContextLookupFlags flags,
315a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block                        int* index_, PropertyAttributes* attributes);
316a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
317a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Determine if a local variable with the given name exists in a
318a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // context.  Do not consider context extension objects.  This is
319a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // used for compiling code using eval.  If the context surrounding
320a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // the eval call does not have a local variable with this name and
321a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // does not contain a with statement the property is global unless
322a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // it is shadowed by a property in an extension object introduced by
323a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // eval.
324a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  bool GlobalIfNotShadowedByEval(Handle<String> name);
325a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
326a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Code generation support.
327a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  static int SlotOffset(int index) {
328a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    return kHeaderSize + index * kPointerSize - kHeapObjectTag;
329a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
330a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
331a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block private:
332a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Unchecked access to the slots.
333a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  Object* unchecked_previous() { return get(PREVIOUS_INDEX); }
334a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  Object* unchecked_extension() { return get(EXTENSION_INDEX); }
335a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
336a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#ifdef DEBUG
337a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Bootstrapping-aware type checks.
338a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  static bool IsBootstrappingOrContext(Object* object);
339a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  static bool IsBootstrappingOrGlobalObject(Object* object);
340a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#endif
341a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block};
342a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
343a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block} }  // namespace v8::internal
344a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
345a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#endif  // V8_CONTEXTS_H_
346