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) \ 79e46be819fca9468a0cd4e74859ce0f778eb8ca60Leon Clarke V(GLOBAL_EVAL_FUN_INDEX, JSFunction, global_eval_fun) \ 80a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block V(INSTANTIATE_FUN_INDEX, JSFunction, instantiate_fun) \ 81a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block V(CONFIGURE_INSTANCE_FUN_INDEX, JSFunction, configure_instance_fun) \ 82a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block V(FUNCTION_MAP_INDEX, Map, function_map) \ 83a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block V(FUNCTION_INSTANCE_MAP_INDEX, Map, function_instance_map) \ 84a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block V(JS_ARRAY_MAP_INDEX, Map, js_array_map)\ 85a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block V(SPECIAL_FUNCTION_TABLE_INDEX, FixedArray, special_function_table) \ 86a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block V(ARGUMENTS_BOILERPLATE_INDEX, JSObject, arguments_boilerplate) \ 87a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block V(MESSAGE_LISTENERS_INDEX, JSObject, message_listeners) \ 88a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block V(MAKE_MESSAGE_FUN_INDEX, JSFunction, make_message_fun) \ 89a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block V(GET_STACK_TRACE_LINE_INDEX, JSFunction, get_stack_trace_line_fun) \ 90a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block V(CONFIGURE_GLOBAL_INDEX, JSFunction, configure_global_fun) \ 91a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block V(FUNCTION_CACHE_INDEX, JSObject, function_cache) \ 92a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block V(RUNTIME_CONTEXT_INDEX, Context, runtime_context) \ 93a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block V(CALL_AS_FUNCTION_DELEGATE_INDEX, JSFunction, call_as_function_delegate) \ 94a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block V(CALL_AS_CONSTRUCTOR_DELEGATE_INDEX, JSFunction, \ 95a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block call_as_constructor_delegate) \ 96a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block V(SCRIPT_FUNCTION_INDEX, JSFunction, script_function) \ 97a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block V(CONTEXT_EXTENSION_FUNCTION_INDEX, JSFunction, context_extension_function) \ 98a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block V(OUT_OF_MEMORY_INDEX, Object, out_of_memory) \ 99a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block V(MAP_CACHE_INDEX, Object, map_cache) \ 100a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block V(CONTEXT_DATA_INDEX, Object, data) 101a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 102a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// JSFunctions are pairs (context, function code), sometimes also called 103a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// closures. A Context object is used to represent function contexts and 104a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// dynamically pushed 'with' contexts (or 'scopes' in ECMA-262 speak). 105a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// 106a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// At runtime, the contexts build a stack in parallel to the execution 107a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// stack, with the top-most context being the current context. All contexts 108a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// have the following slots: 109a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// 110a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// [ closure ] This is the current function. It is the same for all 111a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// contexts inside a function. It provides access to the 112a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// incoming context (i.e., the outer context, which may 113a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// or may not become the current function's context), and 114a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// it provides access to the functions code and thus it's 115a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// scope information, which in turn contains the names of 116a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// statically allocated context slots. The names are needed 117a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// for dynamic lookups in the presence of 'with' or 'eval'. 118a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// 119a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// [ fcontext ] A pointer to the innermost enclosing function context. 120a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// It is the same for all contexts *allocated* inside a 121a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// function, and the function context's fcontext points 122a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// to itself. It is only needed for fast access of the 123a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// function context (used for declarations, and static 124a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// context slot access). 125a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// 126a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// [ previous ] A pointer to the previous context. It is NULL for 127a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// function contexts, and non-NULL for 'with' contexts. 128a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Used to implement the 'with' statement. 129a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// 130a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// [ extension ] A pointer to an extension JSObject, or NULL. Used to 131a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// implement 'with' statements and dynamic declarations 132a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// (through 'eval'). The object in a 'with' statement is 133a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// stored in the extension slot of a 'with' context. 134a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Dynamically declared variables/functions are also added 135a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// to lazily allocated extension object. Context::Lookup 136a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// searches the extension object for properties. 137a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// 138a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// [ global ] A pointer to the global object. Provided for quick 139a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// access to the global object from inside the code (since 140a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// we always have a context pointer). 141a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// 142a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// In addition, function contexts may have statically allocated context slots 143a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// to store local variables/functions that are accessed from inner functions 144a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// (via static context addresses) or through 'eval' (dynamic context lookups). 145a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Finally, the global context contains additional slots for fast access to 146a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// global properties. 147a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// 148a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// We may be able to simplify the implementation: 149a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// 150a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// - We may be able to get rid of 'fcontext': We can always use the fact that 151a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// previous == NULL for function contexts and so we can search for them. They 152a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// are only needed when doing dynamic declarations, and the context chains 153a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// tend to be very very short (depth of nesting of 'with' statements). At 154a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// the moment we also use it in generated code for context slot accesses - 155a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// and there we don't want a loop because of code bloat - but we may not 156a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// need it there after all (see comment in codegen_*.cc). 157a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// 158a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// - If we cannot get rid of fcontext, consider making 'previous' never NULL 159a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// except for the global context. This could simplify Context::Lookup. 160a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 161a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockclass Context: public FixedArray { 162a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block public: 163a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // Conversions. 164a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block static Context* cast(Object* context) { 165a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block ASSERT(context->IsContext()); 166a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block return reinterpret_cast<Context*>(context); 167a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block } 168a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 169a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // The default context slot layout; indices are FixedArray slot indices. 170a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block enum { 171a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // These slots are in all contexts. 172a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block CLOSURE_INDEX, 173a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block FCONTEXT_INDEX, 174a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block PREVIOUS_INDEX, 175a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block EXTENSION_INDEX, 176a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block GLOBAL_INDEX, 177a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block MIN_CONTEXT_SLOTS, 178a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 179a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // These slots are only in global contexts. 180a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block GLOBAL_PROXY_INDEX = MIN_CONTEXT_SLOTS, 181a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block SECURITY_TOKEN_INDEX, 182a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block ARGUMENTS_BOILERPLATE_INDEX, 183a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block JS_ARRAY_MAP_INDEX, 184a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block FUNCTION_MAP_INDEX, 185a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block FUNCTION_INSTANCE_MAP_INDEX, 186a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block INITIAL_OBJECT_PROTOTYPE_INDEX, 187a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block BOOLEAN_FUNCTION_INDEX, 188a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block NUMBER_FUNCTION_INDEX, 189a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block STRING_FUNCTION_INDEX, 190a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block OBJECT_FUNCTION_INDEX, 191a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block ARRAY_FUNCTION_INDEX, 192a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block DATE_FUNCTION_INDEX, 193a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block JSON_OBJECT_INDEX, 194a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block REGEXP_FUNCTION_INDEX, 195a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block CREATE_DATE_FUN_INDEX, 196a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block TO_NUMBER_FUN_INDEX, 197a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block TO_STRING_FUN_INDEX, 198a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block TO_DETAIL_STRING_FUN_INDEX, 199a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block TO_OBJECT_FUN_INDEX, 200a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block TO_INTEGER_FUN_INDEX, 201a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block TO_UINT32_FUN_INDEX, 202a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block TO_INT32_FUN_INDEX, 203a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block TO_BOOLEAN_FUN_INDEX, 204e46be819fca9468a0cd4e74859ce0f778eb8ca60Leon Clarke GLOBAL_EVAL_FUN_INDEX, 205a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block INSTANTIATE_FUN_INDEX, 206a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block CONFIGURE_INSTANCE_FUN_INDEX, 207a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block SPECIAL_FUNCTION_TABLE_INDEX, 208a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block MESSAGE_LISTENERS_INDEX, 209a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block MAKE_MESSAGE_FUN_INDEX, 210a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block GET_STACK_TRACE_LINE_INDEX, 211a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block CONFIGURE_GLOBAL_INDEX, 212a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block FUNCTION_CACHE_INDEX, 213a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block RUNTIME_CONTEXT_INDEX, 214a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block CALL_AS_FUNCTION_DELEGATE_INDEX, 215a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block CALL_AS_CONSTRUCTOR_DELEGATE_INDEX, 216a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block SCRIPT_FUNCTION_INDEX, 217a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block CONTEXT_EXTENSION_FUNCTION_INDEX, 218a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block OUT_OF_MEMORY_INDEX, 219a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block MAP_CACHE_INDEX, 220a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block CONTEXT_DATA_INDEX, 221a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block GLOBAL_CONTEXT_SLOTS 222a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block }; 223a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 224a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // Direct slot access. 225a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block JSFunction* closure() { return JSFunction::cast(get(CLOSURE_INDEX)); } 226a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block void set_closure(JSFunction* closure) { set(CLOSURE_INDEX, closure); } 227a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 228a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block Context* fcontext() { return Context::cast(get(FCONTEXT_INDEX)); } 229a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block void set_fcontext(Context* context) { set(FCONTEXT_INDEX, context); } 230a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 231a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block Context* previous() { 232a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block Object* result = unchecked_previous(); 233a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block ASSERT(IsBootstrappingOrContext(result)); 234a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block return reinterpret_cast<Context*>(result); 235a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block } 236a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block void set_previous(Context* context) { set(PREVIOUS_INDEX, context); } 237a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 238a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block bool has_extension() { return unchecked_extension() != NULL; } 239a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block JSObject* extension() { return JSObject::cast(unchecked_extension()); } 240a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block void set_extension(JSObject* object) { set(EXTENSION_INDEX, object); } 241a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 242a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block GlobalObject* global() { 243a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block Object* result = get(GLOBAL_INDEX); 244a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block ASSERT(IsBootstrappingOrGlobalObject(result)); 245a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block return reinterpret_cast<GlobalObject*>(result); 246a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block } 247a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block void set_global(GlobalObject* global) { set(GLOBAL_INDEX, global); } 248a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 249a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // Returns a JSGlobalProxy object or null. 250a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block JSObject* global_proxy(); 251a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block void set_global_proxy(JSObject* global); 252a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 253a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // The builtins object. 254a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block JSBuiltinsObject* builtins(); 255a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 256a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // Compute the global context by traversing the context chain. 257a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block Context* global_context(); 258a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 259a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // Tells if this is a function context (as opposed to a 'with' context). 260a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block bool is_function_context() { return unchecked_previous() == NULL; } 261a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 262a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // Tells whether the global context is marked with out of memory. 263a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block bool has_out_of_memory() { 264a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block return global_context()->out_of_memory() == Heap::true_value(); 265a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block } 266a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 267a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // Mark the global context with out of memory. 268a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block void mark_out_of_memory() { 269a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block global_context()->set_out_of_memory(Heap::true_value()); 270a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block } 271a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 272a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // The exception holder is the object used as a with object in 273a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // the implementation of a catch block. 274a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block bool is_exception_holder(Object* object) { 275a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block return IsCatchContext() && extension() == object; 276a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block } 277a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 278a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#define GLOBAL_CONTEXT_FIELD_ACCESSORS(index, type, name) \ 279a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block void set_##name(type* value) { \ 280a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block ASSERT(IsGlobalContext()); \ 281a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block set(index, value); \ 282a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block } \ 283a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block type* name() { \ 284a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block ASSERT(IsGlobalContext()); \ 285a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block return type::cast(get(index)); \ 286a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block } 287a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block GLOBAL_CONTEXT_FIELDS(GLOBAL_CONTEXT_FIELD_ACCESSORS) 288a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#undef GLOBAL_CONTEXT_FIELD_ACCESSORS 289a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 290a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // Lookup the the slot called name, starting with the current context. 291a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // There are 4 possible outcomes: 292a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // 293a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // 1) index_ >= 0 && result->IsContext(): 294a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // most common case, the result is a Context, and index is the 295a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // context slot index, and the slot exists. 296a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // attributes == READ_ONLY for the function name variable, NONE otherwise. 297a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // 298a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // 2) index_ >= 0 && result->IsJSObject(): 299a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // the result is the JSObject arguments object, the index is the parameter 300a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // index, i.e., key into the arguments object, and the property exists. 301a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // attributes != ABSENT. 302a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // 303a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // 3) index_ < 0 && result->IsJSObject(): 304a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // the result is the JSObject extension context or the global object, 305a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // and the name is the property name, and the property exists. 306a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // attributes != ABSENT. 307a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // 308a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // 4) index_ < 0 && result.is_null(): 309a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // there was no context found with the corresponding property. 310a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // attributes == ABSENT. 311a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block Handle<Object> Lookup(Handle<String> name, ContextLookupFlags flags, 312a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block int* index_, PropertyAttributes* attributes); 313a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 314a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // Determine if a local variable with the given name exists in a 315a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // context. Do not consider context extension objects. This is 316a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // used for compiling code using eval. If the context surrounding 317a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // the eval call does not have a local variable with this name and 318a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // does not contain a with statement the property is global unless 319a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // it is shadowed by a property in an extension object introduced by 320a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // eval. 321a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block bool GlobalIfNotShadowedByEval(Handle<String> name); 322a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 323a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // Code generation support. 324a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block static int SlotOffset(int index) { 325a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block return kHeaderSize + index * kPointerSize - kHeapObjectTag; 326a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block } 327a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 328a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block private: 329a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // Unchecked access to the slots. 330a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block Object* unchecked_previous() { return get(PREVIOUS_INDEX); } 331a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block Object* unchecked_extension() { return get(EXTENSION_INDEX); } 332a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 333a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#ifdef DEBUG 334a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // Bootstrapping-aware type checks. 335a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block static bool IsBootstrappingOrContext(Object* object); 336a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block static bool IsBootstrappingOrGlobalObject(Object* object); 337a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#endif 338a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}; 339a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 340a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block} } // namespace v8::internal 341a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 342a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#endif // V8_CONTEXTS_H_ 343