blob: 30aa5bb9f6beaa8fff87b85125550d849451aaed [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 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
31namespace v8 { namespace internal {
32
33
34enum ContextLookupFlags {
35 FOLLOW_CONTEXT_CHAIN = 1,
36 FOLLOW_PROTOTYPE_CHAIN = 2,
37
38 DONT_FOLLOW_CHAINS = 0,
39 FOLLOW_CHAINS = FOLLOW_CONTEXT_CHAIN | FOLLOW_PROTOTYPE_CHAIN
40};
41
42
43// Heap-allocated activation contexts.
44//
45// Contexts are implemented as FixedArray objects; the Context
46// class is a convenience interface casted on a FixedArray object.
47//
48// Note: Context must have no virtual functions and Context objects
49// must always be allocated via Heap::AllocateContext() or
50// Factory::NewContext.
51
52// Comment for special_function_table:
53// Table for providing optimized/specialized functions.
54// The array contains triplets [object, general_function, optimized_function].
55// Primarily added to support built-in optimized variants of
56// Array.prototype.{push,pop}.
57
58#define GLOBAL_CONTEXT_FIELDS(V) \
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +000059 V(GLOBAL_PROXY_INDEX, JSObject, global_proxy_object) \
60 V(SECURITY_TOKEN_INDEX, Object, security_token) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000061 V(BOOLEAN_FUNCTION_INDEX, JSFunction, boolean_function) \
62 V(NUMBER_FUNCTION_INDEX, JSFunction, number_function) \
63 V(STRING_FUNCTION_INDEX, JSFunction, string_function) \
64 V(OBJECT_FUNCTION_INDEX, JSFunction, object_function) \
65 V(ARRAY_FUNCTION_INDEX, JSFunction, array_function) \
66 V(DATE_FUNCTION_INDEX, JSFunction, date_function) \
67 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) \
77 V(TO_BOOLEAN_FUN_INDEX, JSFunction, to_boolean_fun) \
78 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) \
81 V(FUNCTION_INSTANCE_MAP_INDEX, Map, function_instance_map) \
82 V(JS_ARRAY_MAP_INDEX, Map, js_array_map)\
83 V(SPECIAL_FUNCTION_TABLE_INDEX, FixedArray, special_function_table) \
84 V(ARGUMENTS_BOILERPLATE_INDEX, JSObject, arguments_boilerplate) \
85 V(MESSAGE_LISTENERS_INDEX, JSObject, message_listeners) \
86 V(DEBUG_EVENT_LISTENERS_INDEX, JSObject, debug_event_listeners) \
87 V(MAKE_MESSAGE_FUN_INDEX, JSFunction, make_message_fun) \
88 V(GET_STACK_TRACE_LINE_INDEX, JSFunction, get_stack_trace_line_fun) \
89 V(CONFIGURE_GLOBAL_INDEX, JSFunction, configure_global_fun) \
90 V(FUNCTION_CACHE_INDEX, JSObject, function_cache) \
91 V(RUNTIME_CONTEXT_INDEX, Context, runtime_context) \
92 V(CALL_AS_FUNCTION_DELEGATE_INDEX, JSFunction, call_as_function_delegate) \
93 V(EMPTY_SCRIPT_INDEX, Script, empty_script) \
94 V(SCRIPT_FUNCTION_INDEX, JSFunction, script_function) \
95 V(CONTEXT_EXTENSION_FUNCTION_INDEX, JSFunction, context_extension_function) \
ager@chromium.org236ad962008-09-25 09:45:57 +000096 V(OUT_OF_MEMORY_INDEX, Object, out_of_memory) \
97 V(MAP_CACHE_INDEX, Object, map_cache)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000098
99// JSFunctions are pairs (context, function code), sometimes also called
100// closures. A Context object is used to represent function contexts and
101// dynamically pushed 'with' contexts (or 'scopes' in ECMA-262 speak).
102//
103// At runtime, the contexts build a stack in parallel to the execution
104// stack, with the top-most context being the current context. All contexts
105// have the following slots:
106//
107// [ closure ] This is the current function. It is the same for all
108// contexts inside a function. It provides access to the
109// incoming context (i.e., the outer context, which may
110// or may not become the current function's context), and
111// it provides access to the functions code and thus it's
112// scope information, which in turn contains the names of
113// statically allocated context slots. The names are needed
114// for dynamic lookups in the presence of 'with' or 'eval'.
115//
116// [ fcontext ] A pointer to the innermost enclosing function context.
117// It is the same for all contexts *allocated* inside a
118// function, and the function context's fcontext points
119// to itself. It is only needed for fast access of the
120// function context (used for declarations, and static
121// context slot access).
122//
123// [ previous ] A pointer to the previous context. It is NULL for
124// function contexts, and non-NULL for 'with' contexts.
125// Used to implement the 'with' statement.
126//
127// [ extension ] A pointer to an extension JSObject, or NULL. Used to
128// implement 'with' statements and dynamic declarations
129// (through 'eval'). The object in a 'with' statement is
130// stored in the extension slot of a 'with' context.
131// Dynamically declared variables/functions are also added
132// to lazily allocated extension object. Context::Lookup
133// searches the extension object for properties.
134//
135// [ global ] A pointer to the global object. Provided for quick
136// access to the global object from inside the code (since
137// we always have a context pointer).
138//
139// In addition, function contexts may have statically allocated context slots
140// to store local variables/functions that are accessed from inner functions
141// (via static context addresses) or through 'eval' (dynamic context lookups).
142// Finally, the global context contains additional slots for fast access to
143// global properties.
144//
145// We may be able to simplify the implementation:
146//
147// - We may be able to get rid of 'fcontext': We can always use the fact that
148// previous == NULL for function contexts and so we can search for them. They
149// are only needed when doing dynamic declarations, and the context chains
150// tend to be very very short (depth of nesting of 'with' statements). At
151// the moment we also use it in generated code for context slot accesses -
152// and there we don't want a loop because of code bloat - but we may not
153// need it there after all (see comment in codegen_*.cc).
154//
155// - If we cannot get rid of fcontext, consider making 'previous' never NULL
156// except for the global context. This could simplify Context::Lookup.
157
158class Context: public FixedArray {
159 public:
160 // Conversions.
161 static Context* cast(Object* context) {
162 ASSERT(context->IsContext());
163 return reinterpret_cast<Context*>(context);
164 }
165
166 // The default context slot layout; indices are FixedArray slot indices.
167 enum {
168 // These slots are in all contexts.
169 CLOSURE_INDEX,
170 FCONTEXT_INDEX,
171 PREVIOUS_INDEX,
172 EXTENSION_INDEX,
173 GLOBAL_INDEX,
174 MIN_CONTEXT_SLOTS,
175
176 // These slots are only in global contexts.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000177 GLOBAL_PROXY_INDEX = MIN_CONTEXT_SLOTS,
178 SECURITY_TOKEN_INDEX,
179 ARGUMENTS_BOILERPLATE_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000180 JS_ARRAY_MAP_INDEX,
181 FUNCTION_MAP_INDEX,
182 FUNCTION_INSTANCE_MAP_INDEX,
183 INITIAL_OBJECT_PROTOTYPE_INDEX,
184 BOOLEAN_FUNCTION_INDEX,
185 NUMBER_FUNCTION_INDEX,
186 STRING_FUNCTION_INDEX,
187 OBJECT_FUNCTION_INDEX,
188 ARRAY_FUNCTION_INDEX,
189 DATE_FUNCTION_INDEX,
190 REGEXP_FUNCTION_INDEX,
191 CREATE_DATE_FUN_INDEX,
192 TO_NUMBER_FUN_INDEX,
193 TO_STRING_FUN_INDEX,
194 TO_DETAIL_STRING_FUN_INDEX,
195 TO_OBJECT_FUN_INDEX,
196 TO_INTEGER_FUN_INDEX,
197 TO_UINT32_FUN_INDEX,
198 TO_INT32_FUN_INDEX,
199 TO_BOOLEAN_FUN_INDEX,
200 INSTANTIATE_FUN_INDEX,
201 CONFIGURE_INSTANCE_FUN_INDEX,
202 SPECIAL_FUNCTION_TABLE_INDEX,
203 MESSAGE_LISTENERS_INDEX,
204 DEBUG_EVENT_LISTENERS_INDEX,
205 MAKE_MESSAGE_FUN_INDEX,
206 GET_STACK_TRACE_LINE_INDEX,
207 CONFIGURE_GLOBAL_INDEX,
208 FUNCTION_CACHE_INDEX,
209 RUNTIME_CONTEXT_INDEX,
210 CALL_AS_FUNCTION_DELEGATE_INDEX,
211 EMPTY_SCRIPT_INDEX,
212 SCRIPT_FUNCTION_INDEX,
213 CONTEXT_EXTENSION_FUNCTION_INDEX,
214 OUT_OF_MEMORY_INDEX,
ager@chromium.org236ad962008-09-25 09:45:57 +0000215 MAP_CACHE_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000216 GLOBAL_CONTEXT_SLOTS
217 };
218
219 // Direct slot access.
220 JSFunction* closure() { return JSFunction::cast(get(CLOSURE_INDEX)); }
221 void set_closure(JSFunction* closure) { set(CLOSURE_INDEX, closure); }
222
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000223 Context* fcontext() { return Context::cast(get(FCONTEXT_INDEX)); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000224 void set_fcontext(Context* context) { set(FCONTEXT_INDEX, context); }
225
226 Context* previous() {
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000227 Object* result = unchecked_previous();
228 ASSERT(IsBootstrappingOrContext(result));
229 return reinterpret_cast<Context*>(result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000230 }
231 void set_previous(Context* context) { set(PREVIOUS_INDEX, context); }
232
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000233 bool has_extension() { return unchecked_extension() != NULL; }
234 JSObject* extension() { return JSObject::cast(unchecked_extension()); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000235 void set_extension(JSObject* object) { set(EXTENSION_INDEX, object); }
236
237 GlobalObject* global() {
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000238 Object* result = get(GLOBAL_INDEX);
239 ASSERT(IsBootstrappingOrGlobalObject(result));
240 return reinterpret_cast<GlobalObject*>(result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000241 }
242 void set_global(GlobalObject* global) { set(GLOBAL_INDEX, global); }
243
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000244 // Returns a JSGlobalProxy object or null.
245 JSObject* global_proxy();
246 void set_global_proxy(JSObject* global);
247
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000248 // The builtins object.
249 JSBuiltinsObject* builtins();
250
251 // Compute the global context by traversing the context chain.
252 Context* global_context();
253
254 // Tells if this is a function context (as opposed to a 'with' context).
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000255 bool is_function_context() { return unchecked_previous() == NULL; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000256
257 // Tells whether the global context is marked with out of memory.
258 bool has_out_of_memory() {
259 return global_context()->out_of_memory() == Heap::true_value();
260 }
261
262 // Mark the global context with out of memory.
263 void mark_out_of_memory() {
264 global_context()->set_out_of_memory(Heap::true_value());
265 }
266
267#define GLOBAL_CONTEXT_FIELD_ACCESSORS(index, type, name) \
268 void set_##name(type* value) { \
269 ASSERT(IsGlobalContext()); \
270 set(index, value); \
271 } \
272 type* name() { \
273 ASSERT(IsGlobalContext()); \
274 return type::cast(get(index)); \
275 }
276 GLOBAL_CONTEXT_FIELDS(GLOBAL_CONTEXT_FIELD_ACCESSORS)
277#undef GLOBAL_CONTEXT_FIELD_ACCESSORS
278
279 // Lookup the the slot called name, starting with the current context.
280 // There are 4 possible outcomes:
281 //
282 // 1) index_ >= 0 && result->IsContext():
283 // most common case, the result is a Context, and index is the
284 // context slot index, and the slot exists.
285 // attributes == READ_ONLY for the function name variable, NONE otherwise.
286 //
287 // 2) index_ >= 0 && result->IsJSObject():
288 // the result is the JSObject arguments object, the index is the parameter
289 // index, i.e., key into the arguments object, and the property exists.
290 // attributes != ABSENT.
291 //
292 // 3) index_ < 0 && result->IsJSObject():
293 // the result is the JSObject extension context or the global object,
294 // and the name is the property name, and the property exists.
295 // attributes != ABSENT.
296 //
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000297 // 4) index_ < 0 && result.is_null():
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000298 // there was no context found with the corresponding property.
299 // attributes == ABSENT.
300 Handle<Object> Lookup(Handle<String> name, ContextLookupFlags flags,
301 int* index_, PropertyAttributes* attributes);
302
303 // Code generation support.
304 static int SlotOffset(int index) {
305 return kHeaderSize + index * kPointerSize - kHeapObjectTag;
306 }
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000307
308 private:
309 // Unchecked access to the slots.
310 Object* unchecked_previous() { return get(PREVIOUS_INDEX); }
311 Object* unchecked_extension() { return get(EXTENSION_INDEX); }
312
313#ifdef DEBUG
314 // Bootstrapping-aware type checks.
315 static bool IsBootstrappingOrContext(Object* object);
316 static bool IsBootstrappingOrGlobalObject(Object* object);
317#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000318};
319
320} } // namespace v8::internal
321
322#endif // V8_CONTEXTS_H_