blob: d0d54d1bd2ab2a3c0a641e5426f031baf0f99710 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
2// 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
Kristian Monsen80d68ea2010-09-08 11:05:35 +010031#include "heap.h"
32#include "objects.h"
33
Steve Blocka7e24c12009-10-30 11:49:00 +000034namespace v8 {
35namespace internal {
36
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
Steve Blocka7e24c12009-10-30 11:49:00 +000056#define GLOBAL_CONTEXT_FIELDS(V) \
57 V(GLOBAL_PROXY_INDEX, JSObject, global_proxy_object) \
58 V(SECURITY_TOKEN_INDEX, Object, security_token) \
59 V(BOOLEAN_FUNCTION_INDEX, JSFunction, boolean_function) \
60 V(NUMBER_FUNCTION_INDEX, JSFunction, number_function) \
61 V(STRING_FUNCTION_INDEX, JSFunction, string_function) \
Iain Merrick75681382010-08-19 15:07:18 +010062 V(STRING_FUNCTION_PROTOTYPE_MAP_INDEX, Map, string_function_prototype_map) \
Steve Blocka7e24c12009-10-30 11:49:00 +000063 V(OBJECT_FUNCTION_INDEX, JSFunction, object_function) \
64 V(ARRAY_FUNCTION_INDEX, JSFunction, array_function) \
65 V(DATE_FUNCTION_INDEX, JSFunction, date_function) \
66 V(JSON_OBJECT_INDEX, JSObject, json_object) \
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) \
Leon Clarkee46be812010-01-19 14:06:41 +000077 V(GLOBAL_EVAL_FUN_INDEX, JSFunction, global_eval_fun) \
Steve Blocka7e24c12009-10-30 11:49:00 +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) \
Steve Block6ded16b2010-05-10 14:33:55 +010081 V(FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map, function_without_prototype_map) \
Steve Blocka7e24c12009-10-30 11:49:00 +000082 V(FUNCTION_INSTANCE_MAP_INDEX, Map, function_instance_map) \
83 V(JS_ARRAY_MAP_INDEX, Map, js_array_map)\
Steve Block6ded16b2010-05-10 14:33:55 +010084 V(REGEXP_RESULT_MAP_INDEX, Map, regexp_result_map)\
Steve Blocka7e24c12009-10-30 11:49:00 +000085 V(ARGUMENTS_BOILERPLATE_INDEX, JSObject, arguments_boilerplate) \
86 V(MESSAGE_LISTENERS_INDEX, JSObject, message_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) \
Steve Block6ded16b2010-05-10 14:33:55 +010091 V(JSFUNCTION_RESULT_CACHES_INDEX, FixedArray, jsfunction_result_caches) \
Kristian Monsen80d68ea2010-09-08 11:05:35 +010092 V(NORMALIZED_MAP_CACHE_INDEX, NormalizedMapCache, normalized_map_cache) \
Steve Blocka7e24c12009-10-30 11:49:00 +000093 V(RUNTIME_CONTEXT_INDEX, Context, runtime_context) \
94 V(CALL_AS_FUNCTION_DELEGATE_INDEX, JSFunction, call_as_function_delegate) \
95 V(CALL_AS_CONSTRUCTOR_DELEGATE_INDEX, JSFunction, \
96 call_as_constructor_delegate) \
Steve Blocka7e24c12009-10-30 11:49:00 +000097 V(SCRIPT_FUNCTION_INDEX, JSFunction, script_function) \
Steve Block6ded16b2010-05-10 14:33:55 +010098 V(OPAQUE_REFERENCE_FUNCTION_INDEX, JSFunction, opaque_reference_function) \
Steve Blocka7e24c12009-10-30 11:49:00 +000099 V(CONTEXT_EXTENSION_FUNCTION_INDEX, JSFunction, context_extension_function) \
100 V(OUT_OF_MEMORY_INDEX, Object, out_of_memory) \
101 V(MAP_CACHE_INDEX, Object, map_cache) \
102 V(CONTEXT_DATA_INDEX, Object, data)
103
104// JSFunctions are pairs (context, function code), sometimes also called
105// closures. A Context object is used to represent function contexts and
106// dynamically pushed 'with' contexts (or 'scopes' in ECMA-262 speak).
107//
108// At runtime, the contexts build a stack in parallel to the execution
109// stack, with the top-most context being the current context. All contexts
110// have the following slots:
111//
112// [ closure ] This is the current function. It is the same for all
113// contexts inside a function. It provides access to the
114// incoming context (i.e., the outer context, which may
115// or may not become the current function's context), and
116// it provides access to the functions code and thus it's
117// scope information, which in turn contains the names of
118// statically allocated context slots. The names are needed
119// for dynamic lookups in the presence of 'with' or 'eval'.
120//
121// [ fcontext ] A pointer to the innermost enclosing function context.
122// It is the same for all contexts *allocated* inside a
123// function, and the function context's fcontext points
124// to itself. It is only needed for fast access of the
125// function context (used for declarations, and static
126// context slot access).
127//
128// [ previous ] A pointer to the previous context. It is NULL for
129// function contexts, and non-NULL for 'with' contexts.
130// Used to implement the 'with' statement.
131//
132// [ extension ] A pointer to an extension JSObject, or NULL. Used to
133// implement 'with' statements and dynamic declarations
134// (through 'eval'). The object in a 'with' statement is
135// stored in the extension slot of a 'with' context.
136// Dynamically declared variables/functions are also added
137// to lazily allocated extension object. Context::Lookup
138// searches the extension object for properties.
139//
140// [ global ] A pointer to the global object. Provided for quick
141// access to the global object from inside the code (since
142// we always have a context pointer).
143//
144// In addition, function contexts may have statically allocated context slots
145// to store local variables/functions that are accessed from inner functions
146// (via static context addresses) or through 'eval' (dynamic context lookups).
147// Finally, the global context contains additional slots for fast access to
148// global properties.
149//
150// We may be able to simplify the implementation:
151//
152// - We may be able to get rid of 'fcontext': We can always use the fact that
153// previous == NULL for function contexts and so we can search for them. They
154// are only needed when doing dynamic declarations, and the context chains
155// tend to be very very short (depth of nesting of 'with' statements). At
156// the moment we also use it in generated code for context slot accesses -
157// and there we don't want a loop because of code bloat - but we may not
158// need it there after all (see comment in codegen_*.cc).
159//
160// - If we cannot get rid of fcontext, consider making 'previous' never NULL
161// except for the global context. This could simplify Context::Lookup.
162
163class Context: public FixedArray {
164 public:
165 // Conversions.
166 static Context* cast(Object* context) {
167 ASSERT(context->IsContext());
168 return reinterpret_cast<Context*>(context);
169 }
170
171 // The default context slot layout; indices are FixedArray slot indices.
172 enum {
173 // These slots are in all contexts.
174 CLOSURE_INDEX,
175 FCONTEXT_INDEX,
176 PREVIOUS_INDEX,
177 EXTENSION_INDEX,
178 GLOBAL_INDEX,
179 MIN_CONTEXT_SLOTS,
180
181 // These slots are only in global contexts.
182 GLOBAL_PROXY_INDEX = MIN_CONTEXT_SLOTS,
183 SECURITY_TOKEN_INDEX,
184 ARGUMENTS_BOILERPLATE_INDEX,
185 JS_ARRAY_MAP_INDEX,
Steve Block6ded16b2010-05-10 14:33:55 +0100186 REGEXP_RESULT_MAP_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000187 FUNCTION_MAP_INDEX,
Steve Block6ded16b2010-05-10 14:33:55 +0100188 FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000189 FUNCTION_INSTANCE_MAP_INDEX,
190 INITIAL_OBJECT_PROTOTYPE_INDEX,
191 BOOLEAN_FUNCTION_INDEX,
192 NUMBER_FUNCTION_INDEX,
193 STRING_FUNCTION_INDEX,
Iain Merrick75681382010-08-19 15:07:18 +0100194 STRING_FUNCTION_PROTOTYPE_MAP_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000195 OBJECT_FUNCTION_INDEX,
196 ARRAY_FUNCTION_INDEX,
197 DATE_FUNCTION_INDEX,
198 JSON_OBJECT_INDEX,
199 REGEXP_FUNCTION_INDEX,
200 CREATE_DATE_FUN_INDEX,
201 TO_NUMBER_FUN_INDEX,
202 TO_STRING_FUN_INDEX,
203 TO_DETAIL_STRING_FUN_INDEX,
204 TO_OBJECT_FUN_INDEX,
205 TO_INTEGER_FUN_INDEX,
206 TO_UINT32_FUN_INDEX,
207 TO_INT32_FUN_INDEX,
208 TO_BOOLEAN_FUN_INDEX,
Leon Clarkee46be812010-01-19 14:06:41 +0000209 GLOBAL_EVAL_FUN_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000210 INSTANTIATE_FUN_INDEX,
211 CONFIGURE_INSTANCE_FUN_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000212 MESSAGE_LISTENERS_INDEX,
213 MAKE_MESSAGE_FUN_INDEX,
214 GET_STACK_TRACE_LINE_INDEX,
215 CONFIGURE_GLOBAL_INDEX,
216 FUNCTION_CACHE_INDEX,
Steve Block6ded16b2010-05-10 14:33:55 +0100217 JSFUNCTION_RESULT_CACHES_INDEX,
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100218 NORMALIZED_MAP_CACHE_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000219 RUNTIME_CONTEXT_INDEX,
220 CALL_AS_FUNCTION_DELEGATE_INDEX,
221 CALL_AS_CONSTRUCTOR_DELEGATE_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000222 SCRIPT_FUNCTION_INDEX,
Steve Block6ded16b2010-05-10 14:33:55 +0100223 OPAQUE_REFERENCE_FUNCTION_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000224 CONTEXT_EXTENSION_FUNCTION_INDEX,
225 OUT_OF_MEMORY_INDEX,
226 MAP_CACHE_INDEX,
227 CONTEXT_DATA_INDEX,
Ben Murdochf87a2032010-10-22 12:50:53 +0100228
229 // Properties from here are treated as weak references by the full GC.
230 // Scavenge treats them as strong references.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100231 OPTIMIZED_FUNCTIONS_LIST, // Weak.
232 NEXT_CONTEXT_LINK, // Weak.
Ben Murdochf87a2032010-10-22 12:50:53 +0100233
234 // Total number of slots.
235 GLOBAL_CONTEXT_SLOTS,
236
Ben Murdochb0fe1622011-05-05 13:52:32 +0100237 FIRST_WEAK_SLOT = OPTIMIZED_FUNCTIONS_LIST
Steve Blocka7e24c12009-10-30 11:49:00 +0000238 };
239
240 // Direct slot access.
241 JSFunction* closure() { return JSFunction::cast(get(CLOSURE_INDEX)); }
242 void set_closure(JSFunction* closure) { set(CLOSURE_INDEX, closure); }
243
244 Context* fcontext() { return Context::cast(get(FCONTEXT_INDEX)); }
245 void set_fcontext(Context* context) { set(FCONTEXT_INDEX, context); }
246
247 Context* previous() {
248 Object* result = unchecked_previous();
249 ASSERT(IsBootstrappingOrContext(result));
250 return reinterpret_cast<Context*>(result);
251 }
252 void set_previous(Context* context) { set(PREVIOUS_INDEX, context); }
253
254 bool has_extension() { return unchecked_extension() != NULL; }
255 JSObject* extension() { return JSObject::cast(unchecked_extension()); }
256 void set_extension(JSObject* object) { set(EXTENSION_INDEX, object); }
257
258 GlobalObject* global() {
259 Object* result = get(GLOBAL_INDEX);
Iain Merrick75681382010-08-19 15:07:18 +0100260 ASSERT(Heap::gc_state() != Heap::NOT_IN_GC ||
261 IsBootstrappingOrGlobalObject(result));
Steve Blocka7e24c12009-10-30 11:49:00 +0000262 return reinterpret_cast<GlobalObject*>(result);
263 }
264 void set_global(GlobalObject* global) { set(GLOBAL_INDEX, global); }
265
266 // Returns a JSGlobalProxy object or null.
267 JSObject* global_proxy();
268 void set_global_proxy(JSObject* global);
269
270 // The builtins object.
271 JSBuiltinsObject* builtins();
272
273 // Compute the global context by traversing the context chain.
274 Context* global_context();
275
276 // Tells if this is a function context (as opposed to a 'with' context).
277 bool is_function_context() { return unchecked_previous() == NULL; }
278
279 // Tells whether the global context is marked with out of memory.
280 bool has_out_of_memory() {
281 return global_context()->out_of_memory() == Heap::true_value();
282 }
283
284 // Mark the global context with out of memory.
285 void mark_out_of_memory() {
286 global_context()->set_out_of_memory(Heap::true_value());
287 }
288
289 // The exception holder is the object used as a with object in
290 // the implementation of a catch block.
291 bool is_exception_holder(Object* object) {
292 return IsCatchContext() && extension() == object;
293 }
294
Ben Murdochb0fe1622011-05-05 13:52:32 +0100295 // A global context hold a list of all functions which have been optimized.
296 void AddOptimizedFunction(JSFunction* function);
297 void RemoveOptimizedFunction(JSFunction* function);
298 Object* OptimizedFunctionsListHead();
299 void ClearOptimizedFunctions();
300
Steve Blocka7e24c12009-10-30 11:49:00 +0000301#define GLOBAL_CONTEXT_FIELD_ACCESSORS(index, type, name) \
302 void set_##name(type* value) { \
303 ASSERT(IsGlobalContext()); \
304 set(index, value); \
305 } \
306 type* name() { \
307 ASSERT(IsGlobalContext()); \
308 return type::cast(get(index)); \
309 }
310 GLOBAL_CONTEXT_FIELDS(GLOBAL_CONTEXT_FIELD_ACCESSORS)
311#undef GLOBAL_CONTEXT_FIELD_ACCESSORS
312
313 // Lookup the the slot called name, starting with the current context.
314 // There are 4 possible outcomes:
315 //
316 // 1) index_ >= 0 && result->IsContext():
317 // most common case, the result is a Context, and index is the
318 // context slot index, and the slot exists.
319 // attributes == READ_ONLY for the function name variable, NONE otherwise.
320 //
321 // 2) index_ >= 0 && result->IsJSObject():
322 // the result is the JSObject arguments object, the index is the parameter
323 // index, i.e., key into the arguments object, and the property exists.
324 // attributes != ABSENT.
325 //
326 // 3) index_ < 0 && result->IsJSObject():
327 // the result is the JSObject extension context or the global object,
328 // and the name is the property name, and the property exists.
329 // attributes != ABSENT.
330 //
331 // 4) index_ < 0 && result.is_null():
332 // there was no context found with the corresponding property.
333 // attributes == ABSENT.
334 Handle<Object> Lookup(Handle<String> name, ContextLookupFlags flags,
335 int* index_, PropertyAttributes* attributes);
336
337 // Determine if a local variable with the given name exists in a
338 // context. Do not consider context extension objects. This is
339 // used for compiling code using eval. If the context surrounding
340 // the eval call does not have a local variable with this name and
341 // does not contain a with statement the property is global unless
342 // it is shadowed by a property in an extension object introduced by
343 // eval.
344 bool GlobalIfNotShadowedByEval(Handle<String> name);
345
346 // Code generation support.
347 static int SlotOffset(int index) {
348 return kHeaderSize + index * kPointerSize - kHeapObjectTag;
349 }
350
Ben Murdochf87a2032010-10-22 12:50:53 +0100351 static const int kSize = kHeaderSize + GLOBAL_CONTEXT_SLOTS * kPointerSize;
352
353 // GC support.
354 typedef FixedBodyDescriptor<
355 kHeaderSize, kSize, kSize> ScavengeBodyDescriptor;
356
357 typedef FixedBodyDescriptor<
358 kHeaderSize,
359 kHeaderSize + FIRST_WEAK_SLOT * kPointerSize,
360 kSize> MarkCompactBodyDescriptor;
361
Steve Blocka7e24c12009-10-30 11:49:00 +0000362 private:
363 // Unchecked access to the slots.
364 Object* unchecked_previous() { return get(PREVIOUS_INDEX); }
365 Object* unchecked_extension() { return get(EXTENSION_INDEX); }
366
367#ifdef DEBUG
368 // Bootstrapping-aware type checks.
369 static bool IsBootstrappingOrContext(Object* object);
370 static bool IsBootstrappingOrGlobalObject(Object* object);
371#endif
372};
373
374} } // namespace v8::internal
375
376#endif // V8_CONTEXTS_H_