contexts.h revision 80d68eab642096c1a48b6474d6ec33064b0ad1f5
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
3180d68eab642096c1a48b6474d6ec33064b0ad1f5Kristian Monsen#include "heap.h"
3280d68eab642096c1a48b6474d6ec33064b0ad1f5Kristian Monsen#include "objects.h"
3380d68eab642096c1a48b6474d6ec33064b0ad1f5Kristian Monsen
34a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocknamespace v8 {
35a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocknamespace internal {
36a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
37a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
38a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockenum ContextLookupFlags {
39a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  FOLLOW_CONTEXT_CHAIN = 1,
40a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  FOLLOW_PROTOTYPE_CHAIN = 2,
41a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
42a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  DONT_FOLLOW_CHAINS = 0,
43a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  FOLLOW_CHAINS = FOLLOW_CONTEXT_CHAIN | FOLLOW_PROTOTYPE_CHAIN
44a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block};
45a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
46a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
47a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Heap-allocated activation contexts.
48a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//
49a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Contexts are implemented as FixedArray objects; the Context
50a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// class is a convenience interface casted on a FixedArray object.
51a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//
52a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Note: Context must have no virtual functions and Context objects
53a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// must always be allocated via Heap::AllocateContext() or
54a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Factory::NewContext.
55a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
56a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#define GLOBAL_CONTEXT_FIELDS(V) \
57a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(GLOBAL_PROXY_INDEX, JSObject, global_proxy_object) \
58a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(SECURITY_TOKEN_INDEX, Object, security_token) \
59a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(BOOLEAN_FUNCTION_INDEX, JSFunction, boolean_function) \
60a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(NUMBER_FUNCTION_INDEX, JSFunction, number_function) \
61a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(STRING_FUNCTION_INDEX, JSFunction, string_function) \
62756813857a4c2a4d8ad2e805969d5768d3cf43a0Iain Merrick  V(STRING_FUNCTION_PROTOTYPE_MAP_INDEX, Map, string_function_prototype_map) \
63a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(OBJECT_FUNCTION_INDEX, JSFunction, object_function) \
64a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(ARRAY_FUNCTION_INDEX, JSFunction, array_function) \
65a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(DATE_FUNCTION_INDEX, JSFunction, date_function) \
66a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(JSON_OBJECT_INDEX, JSObject, json_object) \
67a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(REGEXP_FUNCTION_INDEX, JSFunction, regexp_function) \
68a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(INITIAL_OBJECT_PROTOTYPE_INDEX, JSObject, initial_object_prototype) \
69a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(CREATE_DATE_FUN_INDEX, JSFunction,  create_date_fun) \
70a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(TO_NUMBER_FUN_INDEX, JSFunction, to_number_fun) \
71a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(TO_STRING_FUN_INDEX, JSFunction, to_string_fun) \
72a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(TO_DETAIL_STRING_FUN_INDEX, JSFunction, to_detail_string_fun) \
73a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(TO_OBJECT_FUN_INDEX, JSFunction, to_object_fun) \
74a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(TO_INTEGER_FUN_INDEX, JSFunction, to_integer_fun) \
75a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(TO_UINT32_FUN_INDEX, JSFunction, to_uint32_fun) \
76a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(TO_INT32_FUN_INDEX, JSFunction, to_int32_fun) \
77e46be819fca9468a0cd4e74859ce0f778eb8ca60Leon Clarke  V(GLOBAL_EVAL_FUN_INDEX, JSFunction, global_eval_fun) \
78a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(INSTANTIATE_FUN_INDEX, JSFunction, instantiate_fun) \
79a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(CONFIGURE_INSTANCE_FUN_INDEX, JSFunction, configure_instance_fun) \
80a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(FUNCTION_MAP_INDEX, Map, function_map) \
816ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  V(FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map, function_without_prototype_map) \
82a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(FUNCTION_INSTANCE_MAP_INDEX, Map, function_instance_map) \
83a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(JS_ARRAY_MAP_INDEX, Map, js_array_map)\
846ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  V(REGEXP_RESULT_MAP_INDEX, Map, regexp_result_map)\
85a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(ARGUMENTS_BOILERPLATE_INDEX, JSObject, arguments_boilerplate) \
86a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(MESSAGE_LISTENERS_INDEX, JSObject, message_listeners) \
87a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(MAKE_MESSAGE_FUN_INDEX, JSFunction, make_message_fun) \
88a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(GET_STACK_TRACE_LINE_INDEX, JSFunction, get_stack_trace_line_fun) \
89a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(CONFIGURE_GLOBAL_INDEX, JSFunction, configure_global_fun) \
90a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  V(FUNCTION_CACHE_INDEX, JSObject, function_cache) \
916ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  V(JSFUNCTION_RESULT_CACHES_INDEX, FixedArray, jsfunction_result_caches) \
9280d68eab642096c1a48b6474d6ec33064b0ad1f5Kristian Monsen  V(NORMALIZED_MAP_CACHE_INDEX, NormalizedMapCache, normalized_map_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(SCRIPT_FUNCTION_INDEX, JSFunction, script_function) \
986ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  V(OPAQUE_REFERENCE_FUNCTION_INDEX, JSFunction, opaque_reference_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,
1866ded16be15dd865a9b21ea304d5273c8be299c87Steve Block    REGEXP_RESULT_MAP_INDEX,
187a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    FUNCTION_MAP_INDEX,
1886ded16be15dd865a9b21ea304d5273c8be299c87Steve Block    FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX,
189a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    FUNCTION_INSTANCE_MAP_INDEX,
190a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    INITIAL_OBJECT_PROTOTYPE_INDEX,
191a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    BOOLEAN_FUNCTION_INDEX,
192a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    NUMBER_FUNCTION_INDEX,
193a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    STRING_FUNCTION_INDEX,
194756813857a4c2a4d8ad2e805969d5768d3cf43a0Iain Merrick    STRING_FUNCTION_PROTOTYPE_MAP_INDEX,
195a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    OBJECT_FUNCTION_INDEX,
196a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    ARRAY_FUNCTION_INDEX,
197a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    DATE_FUNCTION_INDEX,
198a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    JSON_OBJECT_INDEX,
199a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    REGEXP_FUNCTION_INDEX,
200a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    CREATE_DATE_FUN_INDEX,
201a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    TO_NUMBER_FUN_INDEX,
202a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    TO_STRING_FUN_INDEX,
203a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    TO_DETAIL_STRING_FUN_INDEX,
204a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    TO_OBJECT_FUN_INDEX,
205a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    TO_INTEGER_FUN_INDEX,
206a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    TO_UINT32_FUN_INDEX,
207a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    TO_INT32_FUN_INDEX,
208a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    TO_BOOLEAN_FUN_INDEX,
209e46be819fca9468a0cd4e74859ce0f778eb8ca60Leon Clarke    GLOBAL_EVAL_FUN_INDEX,
210a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    INSTANTIATE_FUN_INDEX,
211a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    CONFIGURE_INSTANCE_FUN_INDEX,
212a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    MESSAGE_LISTENERS_INDEX,
213a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    MAKE_MESSAGE_FUN_INDEX,
214a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    GET_STACK_TRACE_LINE_INDEX,
215a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    CONFIGURE_GLOBAL_INDEX,
216a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    FUNCTION_CACHE_INDEX,
2176ded16be15dd865a9b21ea304d5273c8be299c87Steve Block    JSFUNCTION_RESULT_CACHES_INDEX,
21880d68eab642096c1a48b6474d6ec33064b0ad1f5Kristian Monsen    NORMALIZED_MAP_CACHE_INDEX,
219a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    RUNTIME_CONTEXT_INDEX,
220a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    CALL_AS_FUNCTION_DELEGATE_INDEX,
221a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    CALL_AS_CONSTRUCTOR_DELEGATE_INDEX,
222a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    SCRIPT_FUNCTION_INDEX,
2236ded16be15dd865a9b21ea304d5273c8be299c87Steve Block    OPAQUE_REFERENCE_FUNCTION_INDEX,
224a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    CONTEXT_EXTENSION_FUNCTION_INDEX,
225a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    OUT_OF_MEMORY_INDEX,
226a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    MAP_CACHE_INDEX,
227a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    CONTEXT_DATA_INDEX,
228a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    GLOBAL_CONTEXT_SLOTS
229a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  };
230a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
231a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Direct slot access.
232a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  JSFunction* closure() { return JSFunction::cast(get(CLOSURE_INDEX)); }
233a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  void set_closure(JSFunction* closure) { set(CLOSURE_INDEX, closure); }
234a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
235a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  Context* fcontext() { return Context::cast(get(FCONTEXT_INDEX)); }
236a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  void set_fcontext(Context* context) { set(FCONTEXT_INDEX, context); }
237a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
238a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  Context* previous() {
239a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    Object* result = unchecked_previous();
240a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    ASSERT(IsBootstrappingOrContext(result));
241a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    return reinterpret_cast<Context*>(result);
242a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
243a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  void set_previous(Context* context) { set(PREVIOUS_INDEX, context); }
244a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
245a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  bool has_extension() { return unchecked_extension() != NULL; }
246a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  JSObject* extension() { return JSObject::cast(unchecked_extension()); }
247a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  void set_extension(JSObject* object) { set(EXTENSION_INDEX, object); }
248a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
249a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  GlobalObject* global() {
250a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    Object* result = get(GLOBAL_INDEX);
251756813857a4c2a4d8ad2e805969d5768d3cf43a0Iain Merrick    ASSERT(Heap::gc_state() != Heap::NOT_IN_GC ||
252756813857a4c2a4d8ad2e805969d5768d3cf43a0Iain Merrick           IsBootstrappingOrGlobalObject(result));
253a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    return reinterpret_cast<GlobalObject*>(result);
254a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
255a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  void set_global(GlobalObject* global) { set(GLOBAL_INDEX, global); }
256a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
257a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Returns a JSGlobalProxy object or null.
258a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  JSObject* global_proxy();
259a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  void set_global_proxy(JSObject* global);
260a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
261a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // The builtins object.
262a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  JSBuiltinsObject* builtins();
263a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
264a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Compute the global context by traversing the context chain.
265a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  Context* global_context();
266a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
267a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Tells if this is a function context (as opposed to a 'with' context).
268a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  bool is_function_context() { return unchecked_previous() == NULL; }
269a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
270a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Tells whether the global context is marked with out of memory.
271a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  bool has_out_of_memory() {
272a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    return global_context()->out_of_memory() == Heap::true_value();
273a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
274a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
275a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Mark the global context with out of memory.
276a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  void mark_out_of_memory() {
277a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    global_context()->set_out_of_memory(Heap::true_value());
278a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
279a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
280a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // The exception holder is the object used as a with object in
281a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // the implementation of a catch block.
282a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  bool is_exception_holder(Object* object) {
283a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    return IsCatchContext() && extension() == object;
284a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
285a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
286a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#define GLOBAL_CONTEXT_FIELD_ACCESSORS(index, type, name) \
287a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  void  set_##name(type* value) {                         \
288a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    ASSERT(IsGlobalContext());                            \
289a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    set(index, value);                                    \
290a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }                                                       \
291a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  type* name() {                                          \
292a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    ASSERT(IsGlobalContext());                            \
293a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    return type::cast(get(index));                        \
294a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
295a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  GLOBAL_CONTEXT_FIELDS(GLOBAL_CONTEXT_FIELD_ACCESSORS)
296a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#undef GLOBAL_CONTEXT_FIELD_ACCESSORS
297a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
298a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Lookup the the slot called name, starting with the current context.
299a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // There are 4 possible outcomes:
300a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  //
301a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // 1) index_ >= 0 && result->IsContext():
302a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  //    most common case, the result is a Context, and index is the
303a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  //    context slot index, and the slot exists.
304a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  //    attributes == READ_ONLY for the function name variable, NONE otherwise.
305a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  //
306a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // 2) index_ >= 0 && result->IsJSObject():
307a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  //    the result is the JSObject arguments object, the index is the parameter
308a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  //    index, i.e., key into the arguments object, and the property exists.
309a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  //    attributes != ABSENT.
310a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  //
311a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // 3) index_ < 0 && result->IsJSObject():
312a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  //    the result is the JSObject extension context or the global object,
313a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  //    and the name is the property name, and the property exists.
314a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  //    attributes != ABSENT.
315a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  //
316a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // 4) index_ < 0 && result.is_null():
317a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  //    there was no context found with the corresponding property.
318a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  //    attributes == ABSENT.
319a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  Handle<Object> Lookup(Handle<String> name, ContextLookupFlags flags,
320a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block                        int* index_, PropertyAttributes* attributes);
321a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
322a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Determine if a local variable with the given name exists in a
323a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // context.  Do not consider context extension objects.  This is
324a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // used for compiling code using eval.  If the context surrounding
325a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // the eval call does not have a local variable with this name and
326a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // does not contain a with statement the property is global unless
327a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // it is shadowed by a property in an extension object introduced by
328a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // eval.
329a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  bool GlobalIfNotShadowedByEval(Handle<String> name);
330a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
331a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Code generation support.
332a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  static int SlotOffset(int index) {
333a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    return kHeaderSize + index * kPointerSize - kHeapObjectTag;
334a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
335a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
336a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block private:
337a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Unchecked access to the slots.
338a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  Object* unchecked_previous() { return get(PREVIOUS_INDEX); }
339a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  Object* unchecked_extension() { return get(EXTENSION_INDEX); }
340a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
341a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#ifdef DEBUG
342a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Bootstrapping-aware type checks.
343a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  static bool IsBootstrappingOrContext(Object* object);
344a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  static bool IsBootstrappingOrGlobalObject(Object* object);
345a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#endif
346a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block};
347a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
348a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block} }  // namespace v8::internal
349a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
350a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#endif  // V8_CONTEXTS_H_
351