blob: 181efc9bf7c22f00749e29bddafceba345dc2d1d [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
31namespace v8 {
32namespace internal {
33
34
35enum ContextLookupFlags {
36 FOLLOW_CONTEXT_CHAIN = 1,
37 FOLLOW_PROTOTYPE_CHAIN = 2,
38
39 DONT_FOLLOW_CHAINS = 0,
40 FOLLOW_CHAINS = FOLLOW_CONTEXT_CHAIN | FOLLOW_PROTOTYPE_CHAIN
41};
42
43
44// Heap-allocated activation contexts.
45//
46// Contexts are implemented as FixedArray objects; the Context
47// class is a convenience interface casted on a FixedArray object.
48//
49// Note: Context must have no virtual functions and Context objects
50// must always be allocated via Heap::AllocateContext() or
51// Factory::NewContext.
52
Steve Blocka7e24c12009-10-30 11:49:00 +000053#define GLOBAL_CONTEXT_FIELDS(V) \
54 V(GLOBAL_PROXY_INDEX, JSObject, global_proxy_object) \
55 V(SECURITY_TOKEN_INDEX, Object, security_token) \
56 V(BOOLEAN_FUNCTION_INDEX, JSFunction, boolean_function) \
57 V(NUMBER_FUNCTION_INDEX, JSFunction, number_function) \
58 V(STRING_FUNCTION_INDEX, JSFunction, string_function) \
Iain Merrick75681382010-08-19 15:07:18 +010059 V(STRING_FUNCTION_PROTOTYPE_MAP_INDEX, Map, string_function_prototype_map) \
Steve Blocka7e24c12009-10-30 11:49:00 +000060 V(OBJECT_FUNCTION_INDEX, JSFunction, object_function) \
61 V(ARRAY_FUNCTION_INDEX, JSFunction, array_function) \
62 V(DATE_FUNCTION_INDEX, JSFunction, date_function) \
63 V(JSON_OBJECT_INDEX, JSObject, json_object) \
64 V(REGEXP_FUNCTION_INDEX, JSFunction, regexp_function) \
65 V(INITIAL_OBJECT_PROTOTYPE_INDEX, JSObject, initial_object_prototype) \
66 V(CREATE_DATE_FUN_INDEX, JSFunction, create_date_fun) \
67 V(TO_NUMBER_FUN_INDEX, JSFunction, to_number_fun) \
68 V(TO_STRING_FUN_INDEX, JSFunction, to_string_fun) \
69 V(TO_DETAIL_STRING_FUN_INDEX, JSFunction, to_detail_string_fun) \
70 V(TO_OBJECT_FUN_INDEX, JSFunction, to_object_fun) \
71 V(TO_INTEGER_FUN_INDEX, JSFunction, to_integer_fun) \
72 V(TO_UINT32_FUN_INDEX, JSFunction, to_uint32_fun) \
73 V(TO_INT32_FUN_INDEX, JSFunction, to_int32_fun) \
Leon Clarkee46be812010-01-19 14:06:41 +000074 V(GLOBAL_EVAL_FUN_INDEX, JSFunction, global_eval_fun) \
Steve Blocka7e24c12009-10-30 11:49:00 +000075 V(INSTANTIATE_FUN_INDEX, JSFunction, instantiate_fun) \
76 V(CONFIGURE_INSTANCE_FUN_INDEX, JSFunction, configure_instance_fun) \
77 V(FUNCTION_MAP_INDEX, Map, function_map) \
Steve Block6ded16b2010-05-10 14:33:55 +010078 V(FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map, function_without_prototype_map) \
Steve Blocka7e24c12009-10-30 11:49:00 +000079 V(FUNCTION_INSTANCE_MAP_INDEX, Map, function_instance_map) \
80 V(JS_ARRAY_MAP_INDEX, Map, js_array_map)\
Steve Block6ded16b2010-05-10 14:33:55 +010081 V(REGEXP_RESULT_MAP_INDEX, Map, regexp_result_map)\
Steve Blocka7e24c12009-10-30 11:49:00 +000082 V(ARGUMENTS_BOILERPLATE_INDEX, JSObject, arguments_boilerplate) \
83 V(MESSAGE_LISTENERS_INDEX, JSObject, message_listeners) \
84 V(MAKE_MESSAGE_FUN_INDEX, JSFunction, make_message_fun) \
85 V(GET_STACK_TRACE_LINE_INDEX, JSFunction, get_stack_trace_line_fun) \
86 V(CONFIGURE_GLOBAL_INDEX, JSFunction, configure_global_fun) \
87 V(FUNCTION_CACHE_INDEX, JSObject, function_cache) \
Steve Block6ded16b2010-05-10 14:33:55 +010088 V(JSFUNCTION_RESULT_CACHES_INDEX, FixedArray, jsfunction_result_caches) \
Steve Blocka7e24c12009-10-30 11:49:00 +000089 V(RUNTIME_CONTEXT_INDEX, Context, runtime_context) \
90 V(CALL_AS_FUNCTION_DELEGATE_INDEX, JSFunction, call_as_function_delegate) \
91 V(CALL_AS_CONSTRUCTOR_DELEGATE_INDEX, JSFunction, \
92 call_as_constructor_delegate) \
Steve Blocka7e24c12009-10-30 11:49:00 +000093 V(SCRIPT_FUNCTION_INDEX, JSFunction, script_function) \
Steve Block6ded16b2010-05-10 14:33:55 +010094 V(OPAQUE_REFERENCE_FUNCTION_INDEX, JSFunction, opaque_reference_function) \
Steve Blocka7e24c12009-10-30 11:49:00 +000095 V(CONTEXT_EXTENSION_FUNCTION_INDEX, JSFunction, context_extension_function) \
96 V(OUT_OF_MEMORY_INDEX, Object, out_of_memory) \
97 V(MAP_CACHE_INDEX, Object, map_cache) \
98 V(CONTEXT_DATA_INDEX, Object, data)
99
100// JSFunctions are pairs (context, function code), sometimes also called
101// closures. A Context object is used to represent function contexts and
102// dynamically pushed 'with' contexts (or 'scopes' in ECMA-262 speak).
103//
104// At runtime, the contexts build a stack in parallel to the execution
105// stack, with the top-most context being the current context. All contexts
106// have the following slots:
107//
108// [ closure ] This is the current function. It is the same for all
109// contexts inside a function. It provides access to the
110// incoming context (i.e., the outer context, which may
111// or may not become the current function's context), and
112// it provides access to the functions code and thus it's
113// scope information, which in turn contains the names of
114// statically allocated context slots. The names are needed
115// for dynamic lookups in the presence of 'with' or 'eval'.
116//
117// [ fcontext ] A pointer to the innermost enclosing function context.
118// It is the same for all contexts *allocated* inside a
119// function, and the function context's fcontext points
120// to itself. It is only needed for fast access of the
121// function context (used for declarations, and static
122// context slot access).
123//
124// [ previous ] A pointer to the previous context. It is NULL for
125// function contexts, and non-NULL for 'with' contexts.
126// Used to implement the 'with' statement.
127//
128// [ extension ] A pointer to an extension JSObject, or NULL. Used to
129// implement 'with' statements and dynamic declarations
130// (through 'eval'). The object in a 'with' statement is
131// stored in the extension slot of a 'with' context.
132// Dynamically declared variables/functions are also added
133// to lazily allocated extension object. Context::Lookup
134// searches the extension object for properties.
135//
136// [ global ] A pointer to the global object. Provided for quick
137// access to the global object from inside the code (since
138// we always have a context pointer).
139//
140// In addition, function contexts may have statically allocated context slots
141// to store local variables/functions that are accessed from inner functions
142// (via static context addresses) or through 'eval' (dynamic context lookups).
143// Finally, the global context contains additional slots for fast access to
144// global properties.
145//
146// We may be able to simplify the implementation:
147//
148// - We may be able to get rid of 'fcontext': We can always use the fact that
149// previous == NULL for function contexts and so we can search for them. They
150// are only needed when doing dynamic declarations, and the context chains
151// tend to be very very short (depth of nesting of 'with' statements). At
152// the moment we also use it in generated code for context slot accesses -
153// and there we don't want a loop because of code bloat - but we may not
154// need it there after all (see comment in codegen_*.cc).
155//
156// - If we cannot get rid of fcontext, consider making 'previous' never NULL
157// except for the global context. This could simplify Context::Lookup.
158
159class Context: public FixedArray {
160 public:
161 // Conversions.
162 static Context* cast(Object* context) {
163 ASSERT(context->IsContext());
164 return reinterpret_cast<Context*>(context);
165 }
166
167 // The default context slot layout; indices are FixedArray slot indices.
168 enum {
169 // These slots are in all contexts.
170 CLOSURE_INDEX,
171 FCONTEXT_INDEX,
172 PREVIOUS_INDEX,
173 EXTENSION_INDEX,
174 GLOBAL_INDEX,
175 MIN_CONTEXT_SLOTS,
176
177 // These slots are only in global contexts.
178 GLOBAL_PROXY_INDEX = MIN_CONTEXT_SLOTS,
179 SECURITY_TOKEN_INDEX,
180 ARGUMENTS_BOILERPLATE_INDEX,
181 JS_ARRAY_MAP_INDEX,
Steve Block6ded16b2010-05-10 14:33:55 +0100182 REGEXP_RESULT_MAP_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000183 FUNCTION_MAP_INDEX,
Steve Block6ded16b2010-05-10 14:33:55 +0100184 FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000185 FUNCTION_INSTANCE_MAP_INDEX,
186 INITIAL_OBJECT_PROTOTYPE_INDEX,
187 BOOLEAN_FUNCTION_INDEX,
188 NUMBER_FUNCTION_INDEX,
189 STRING_FUNCTION_INDEX,
Iain Merrick75681382010-08-19 15:07:18 +0100190 STRING_FUNCTION_PROTOTYPE_MAP_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000191 OBJECT_FUNCTION_INDEX,
192 ARRAY_FUNCTION_INDEX,
193 DATE_FUNCTION_INDEX,
194 JSON_OBJECT_INDEX,
195 REGEXP_FUNCTION_INDEX,
196 CREATE_DATE_FUN_INDEX,
197 TO_NUMBER_FUN_INDEX,
198 TO_STRING_FUN_INDEX,
199 TO_DETAIL_STRING_FUN_INDEX,
200 TO_OBJECT_FUN_INDEX,
201 TO_INTEGER_FUN_INDEX,
202 TO_UINT32_FUN_INDEX,
203 TO_INT32_FUN_INDEX,
204 TO_BOOLEAN_FUN_INDEX,
Leon Clarkee46be812010-01-19 14:06:41 +0000205 GLOBAL_EVAL_FUN_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000206 INSTANTIATE_FUN_INDEX,
207 CONFIGURE_INSTANCE_FUN_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000208 MESSAGE_LISTENERS_INDEX,
209 MAKE_MESSAGE_FUN_INDEX,
210 GET_STACK_TRACE_LINE_INDEX,
211 CONFIGURE_GLOBAL_INDEX,
212 FUNCTION_CACHE_INDEX,
Steve Block6ded16b2010-05-10 14:33:55 +0100213 JSFUNCTION_RESULT_CACHES_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000214 RUNTIME_CONTEXT_INDEX,
215 CALL_AS_FUNCTION_DELEGATE_INDEX,
216 CALL_AS_CONSTRUCTOR_DELEGATE_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000217 SCRIPT_FUNCTION_INDEX,
Steve Block6ded16b2010-05-10 14:33:55 +0100218 OPAQUE_REFERENCE_FUNCTION_INDEX,
Steve Blocka7e24c12009-10-30 11:49:00 +0000219 CONTEXT_EXTENSION_FUNCTION_INDEX,
220 OUT_OF_MEMORY_INDEX,
221 MAP_CACHE_INDEX,
222 CONTEXT_DATA_INDEX,
223 GLOBAL_CONTEXT_SLOTS
224 };
225
226 // Direct slot access.
227 JSFunction* closure() { return JSFunction::cast(get(CLOSURE_INDEX)); }
228 void set_closure(JSFunction* closure) { set(CLOSURE_INDEX, closure); }
229
230 Context* fcontext() { return Context::cast(get(FCONTEXT_INDEX)); }
231 void set_fcontext(Context* context) { set(FCONTEXT_INDEX, context); }
232
233 Context* previous() {
234 Object* result = unchecked_previous();
235 ASSERT(IsBootstrappingOrContext(result));
236 return reinterpret_cast<Context*>(result);
237 }
238 void set_previous(Context* context) { set(PREVIOUS_INDEX, context); }
239
240 bool has_extension() { return unchecked_extension() != NULL; }
241 JSObject* extension() { return JSObject::cast(unchecked_extension()); }
242 void set_extension(JSObject* object) { set(EXTENSION_INDEX, object); }
243
244 GlobalObject* global() {
245 Object* result = get(GLOBAL_INDEX);
Iain Merrick75681382010-08-19 15:07:18 +0100246 ASSERT(Heap::gc_state() != Heap::NOT_IN_GC ||
247 IsBootstrappingOrGlobalObject(result));
Steve Blocka7e24c12009-10-30 11:49:00 +0000248 return reinterpret_cast<GlobalObject*>(result);
249 }
250 void set_global(GlobalObject* global) { set(GLOBAL_INDEX, global); }
251
252 // Returns a JSGlobalProxy object or null.
253 JSObject* global_proxy();
254 void set_global_proxy(JSObject* global);
255
256 // The builtins object.
257 JSBuiltinsObject* builtins();
258
259 // Compute the global context by traversing the context chain.
260 Context* global_context();
261
262 // Tells if this is a function context (as opposed to a 'with' context).
263 bool is_function_context() { return unchecked_previous() == NULL; }
264
265 // Tells whether the global context is marked with out of memory.
266 bool has_out_of_memory() {
267 return global_context()->out_of_memory() == Heap::true_value();
268 }
269
270 // Mark the global context with out of memory.
271 void mark_out_of_memory() {
272 global_context()->set_out_of_memory(Heap::true_value());
273 }
274
275 // The exception holder is the object used as a with object in
276 // the implementation of a catch block.
277 bool is_exception_holder(Object* object) {
278 return IsCatchContext() && extension() == object;
279 }
280
281#define GLOBAL_CONTEXT_FIELD_ACCESSORS(index, type, name) \
282 void set_##name(type* value) { \
283 ASSERT(IsGlobalContext()); \
284 set(index, value); \
285 } \
286 type* name() { \
287 ASSERT(IsGlobalContext()); \
288 return type::cast(get(index)); \
289 }
290 GLOBAL_CONTEXT_FIELDS(GLOBAL_CONTEXT_FIELD_ACCESSORS)
291#undef GLOBAL_CONTEXT_FIELD_ACCESSORS
292
293 // Lookup the the slot called name, starting with the current context.
294 // There are 4 possible outcomes:
295 //
296 // 1) index_ >= 0 && result->IsContext():
297 // most common case, the result is a Context, and index is the
298 // context slot index, and the slot exists.
299 // attributes == READ_ONLY for the function name variable, NONE otherwise.
300 //
301 // 2) index_ >= 0 && result->IsJSObject():
302 // the result is the JSObject arguments object, the index is the parameter
303 // index, i.e., key into the arguments object, and the property exists.
304 // attributes != ABSENT.
305 //
306 // 3) index_ < 0 && result->IsJSObject():
307 // the result is the JSObject extension context or the global object,
308 // and the name is the property name, and the property exists.
309 // attributes != ABSENT.
310 //
311 // 4) index_ < 0 && result.is_null():
312 // there was no context found with the corresponding property.
313 // attributes == ABSENT.
314 Handle<Object> Lookup(Handle<String> name, ContextLookupFlags flags,
315 int* index_, PropertyAttributes* attributes);
316
317 // Determine if a local variable with the given name exists in a
318 // context. Do not consider context extension objects. This is
319 // used for compiling code using eval. If the context surrounding
320 // the eval call does not have a local variable with this name and
321 // does not contain a with statement the property is global unless
322 // it is shadowed by a property in an extension object introduced by
323 // eval.
324 bool GlobalIfNotShadowedByEval(Handle<String> name);
325
326 // Code generation support.
327 static int SlotOffset(int index) {
328 return kHeaderSize + index * kPointerSize - kHeapObjectTag;
329 }
330
331 private:
332 // Unchecked access to the slots.
333 Object* unchecked_previous() { return get(PREVIOUS_INDEX); }
334 Object* unchecked_extension() { return get(EXTENSION_INDEX); }
335
336#ifdef DEBUG
337 // Bootstrapping-aware type checks.
338 static bool IsBootstrappingOrContext(Object* object);
339 static bool IsBootstrappingOrGlobalObject(Object* object);
340#endif
341};
342
343} } // namespace v8::internal
344
345#endif // V8_CONTEXTS_H_