contexts.h revision 257744e915dfc84d6d07a6b2accf8402d9ffc708
1257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch// Copyright 2011 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) \ 8144f0eee88ff00398ff7f715fab053374d808c90dSteve Block V(STRICT_MODE_FUNCTION_MAP_INDEX, Map, strict_mode_function_map) \ 826ded16be15dd865a9b21ea304d5273c8be299c87Steve Block V(FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map, function_without_prototype_map) \ 8344f0eee88ff00398ff7f715fab053374d808c90dSteve Block V(STRICT_MODE_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map, \ 8444f0eee88ff00398ff7f715fab053374d808c90dSteve Block strict_mode_function_without_prototype_map) \ 85a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block V(FUNCTION_INSTANCE_MAP_INDEX, Map, function_instance_map) \ 8644f0eee88ff00398ff7f715fab053374d808c90dSteve Block V(STRICT_MODE_FUNCTION_INSTANCE_MAP_INDEX, Map, \ 8744f0eee88ff00398ff7f715fab053374d808c90dSteve Block strict_mode_function_instance_map) \ 88a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block V(JS_ARRAY_MAP_INDEX, Map, js_array_map)\ 896ded16be15dd865a9b21ea304d5273c8be299c87Steve Block V(REGEXP_RESULT_MAP_INDEX, Map, regexp_result_map)\ 90a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block V(ARGUMENTS_BOILERPLATE_INDEX, JSObject, arguments_boilerplate) \ 9144f0eee88ff00398ff7f715fab053374d808c90dSteve Block V(STRICT_MODE_ARGUMENTS_BOILERPLATE_INDEX, JSObject, \ 9244f0eee88ff00398ff7f715fab053374d808c90dSteve Block strict_mode_arguments_boilerplate) \ 93a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block V(MESSAGE_LISTENERS_INDEX, JSObject, message_listeners) \ 94a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block V(MAKE_MESSAGE_FUN_INDEX, JSFunction, make_message_fun) \ 95a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block V(GET_STACK_TRACE_LINE_INDEX, JSFunction, get_stack_trace_line_fun) \ 96a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block V(CONFIGURE_GLOBAL_INDEX, JSFunction, configure_global_fun) \ 97a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block V(FUNCTION_CACHE_INDEX, JSObject, function_cache) \ 986ded16be15dd865a9b21ea304d5273c8be299c87Steve Block V(JSFUNCTION_RESULT_CACHES_INDEX, FixedArray, jsfunction_result_caches) \ 9980d68eab642096c1a48b6474d6ec33064b0ad1f5Kristian Monsen V(NORMALIZED_MAP_CACHE_INDEX, NormalizedMapCache, normalized_map_cache) \ 100a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block V(RUNTIME_CONTEXT_INDEX, Context, runtime_context) \ 101a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block V(CALL_AS_FUNCTION_DELEGATE_INDEX, JSFunction, call_as_function_delegate) \ 102a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block V(CALL_AS_CONSTRUCTOR_DELEGATE_INDEX, JSFunction, \ 103a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block call_as_constructor_delegate) \ 104a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block V(SCRIPT_FUNCTION_INDEX, JSFunction, script_function) \ 1056ded16be15dd865a9b21ea304d5273c8be299c87Steve Block V(OPAQUE_REFERENCE_FUNCTION_INDEX, JSFunction, opaque_reference_function) \ 106a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block V(CONTEXT_EXTENSION_FUNCTION_INDEX, JSFunction, context_extension_function) \ 107a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block V(OUT_OF_MEMORY_INDEX, Object, out_of_memory) \ 108a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block V(MAP_CACHE_INDEX, Object, map_cache) \ 109257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch V(CONTEXT_DATA_INDEX, Object, data) \ 110257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch V(ALLOW_CODE_GEN_FROM_STRINGS_INDEX, Object, allow_code_gen_from_strings) \ 111257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch V(DERIVED_GET_TRAP_INDEX, JSFunction, derived_get_trap) 112a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 113a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// JSFunctions are pairs (context, function code), sometimes also called 114a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// closures. A Context object is used to represent function contexts and 115a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// dynamically pushed 'with' contexts (or 'scopes' in ECMA-262 speak). 116a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// 117a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// At runtime, the contexts build a stack in parallel to the execution 118a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// stack, with the top-most context being the current context. All contexts 119a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// have the following slots: 120a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// 121a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// [ closure ] This is the current function. It is the same for all 122a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// contexts inside a function. It provides access to the 123a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// incoming context (i.e., the outer context, which may 124a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// or may not become the current function's context), and 125a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// it provides access to the functions code and thus it's 126a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// scope information, which in turn contains the names of 127a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// statically allocated context slots. The names are needed 128a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// for dynamic lookups in the presence of 'with' or 'eval'. 129a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// 130a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// [ fcontext ] A pointer to the innermost enclosing function context. 131a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// It is the same for all contexts *allocated* inside a 132a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// function, and the function context's fcontext points 133a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// to itself. It is only needed for fast access of the 134a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// function context (used for declarations, and static 135a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// context slot access). 136a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// 137a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// [ previous ] A pointer to the previous context. It is NULL for 138a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// function contexts, and non-NULL for 'with' contexts. 139a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Used to implement the 'with' statement. 140a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// 141a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// [ extension ] A pointer to an extension JSObject, or NULL. Used to 142a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// implement 'with' statements and dynamic declarations 143a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// (through 'eval'). The object in a 'with' statement is 144a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// stored in the extension slot of a 'with' context. 145a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Dynamically declared variables/functions are also added 146a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// to lazily allocated extension object. Context::Lookup 147a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// searches the extension object for properties. 148a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// 149a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// [ global ] A pointer to the global object. Provided for quick 150a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// access to the global object from inside the code (since 151a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// we always have a context pointer). 152a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// 153a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// In addition, function contexts may have statically allocated context slots 154a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// to store local variables/functions that are accessed from inner functions 155a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// (via static context addresses) or through 'eval' (dynamic context lookups). 156a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Finally, the global context contains additional slots for fast access to 157a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// global properties. 158a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// 159a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// We may be able to simplify the implementation: 160a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// 161a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// - We may be able to get rid of 'fcontext': We can always use the fact that 162a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// previous == NULL for function contexts and so we can search for them. They 163a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// are only needed when doing dynamic declarations, and the context chains 164a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// tend to be very very short (depth of nesting of 'with' statements). At 165a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// the moment we also use it in generated code for context slot accesses - 166a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// and there we don't want a loop because of code bloat - but we may not 167a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// need it there after all (see comment in codegen_*.cc). 168a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// 169a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// - If we cannot get rid of fcontext, consider making 'previous' never NULL 170a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// except for the global context. This could simplify Context::Lookup. 171a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 172a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockclass Context: public FixedArray { 173a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block public: 174a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // Conversions. 175a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block static Context* cast(Object* context) { 176a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block ASSERT(context->IsContext()); 177a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block return reinterpret_cast<Context*>(context); 178a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block } 179a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 180a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // The default context slot layout; indices are FixedArray slot indices. 181a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block enum { 182a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // These slots are in all contexts. 183a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block CLOSURE_INDEX, 184a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block FCONTEXT_INDEX, 185a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block PREVIOUS_INDEX, 186a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block EXTENSION_INDEX, 187a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block GLOBAL_INDEX, 188a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block MIN_CONTEXT_SLOTS, 189a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 190a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // These slots are only in global contexts. 191a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block GLOBAL_PROXY_INDEX = MIN_CONTEXT_SLOTS, 192a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block SECURITY_TOKEN_INDEX, 193a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block ARGUMENTS_BOILERPLATE_INDEX, 19444f0eee88ff00398ff7f715fab053374d808c90dSteve Block STRICT_MODE_ARGUMENTS_BOILERPLATE_INDEX, 195a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block JS_ARRAY_MAP_INDEX, 1966ded16be15dd865a9b21ea304d5273c8be299c87Steve Block REGEXP_RESULT_MAP_INDEX, 197a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block FUNCTION_MAP_INDEX, 19844f0eee88ff00398ff7f715fab053374d808c90dSteve Block STRICT_MODE_FUNCTION_MAP_INDEX, 1996ded16be15dd865a9b21ea304d5273c8be299c87Steve Block FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, 20044f0eee88ff00398ff7f715fab053374d808c90dSteve Block STRICT_MODE_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, 201a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block FUNCTION_INSTANCE_MAP_INDEX, 20244f0eee88ff00398ff7f715fab053374d808c90dSteve Block STRICT_MODE_FUNCTION_INSTANCE_MAP_INDEX, 203a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block INITIAL_OBJECT_PROTOTYPE_INDEX, 204a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block BOOLEAN_FUNCTION_INDEX, 205a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block NUMBER_FUNCTION_INDEX, 206a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block STRING_FUNCTION_INDEX, 207756813857a4c2a4d8ad2e805969d5768d3cf43a0Iain Merrick STRING_FUNCTION_PROTOTYPE_MAP_INDEX, 208a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block OBJECT_FUNCTION_INDEX, 209a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block ARRAY_FUNCTION_INDEX, 210a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block DATE_FUNCTION_INDEX, 211a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block JSON_OBJECT_INDEX, 212a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block REGEXP_FUNCTION_INDEX, 213a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block CREATE_DATE_FUN_INDEX, 214a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block TO_NUMBER_FUN_INDEX, 215a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block TO_STRING_FUN_INDEX, 216a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block TO_DETAIL_STRING_FUN_INDEX, 217a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block TO_OBJECT_FUN_INDEX, 218a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block TO_INTEGER_FUN_INDEX, 219a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block TO_UINT32_FUN_INDEX, 220a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block TO_INT32_FUN_INDEX, 221a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block TO_BOOLEAN_FUN_INDEX, 222e46be819fca9468a0cd4e74859ce0f778eb8ca60Leon Clarke GLOBAL_EVAL_FUN_INDEX, 223a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block INSTANTIATE_FUN_INDEX, 224a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block CONFIGURE_INSTANCE_FUN_INDEX, 225a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block MESSAGE_LISTENERS_INDEX, 226a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block MAKE_MESSAGE_FUN_INDEX, 227a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block GET_STACK_TRACE_LINE_INDEX, 228a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block CONFIGURE_GLOBAL_INDEX, 229a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block FUNCTION_CACHE_INDEX, 2306ded16be15dd865a9b21ea304d5273c8be299c87Steve Block JSFUNCTION_RESULT_CACHES_INDEX, 23180d68eab642096c1a48b6474d6ec33064b0ad1f5Kristian Monsen NORMALIZED_MAP_CACHE_INDEX, 232a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block RUNTIME_CONTEXT_INDEX, 233a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block CALL_AS_FUNCTION_DELEGATE_INDEX, 234a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block CALL_AS_CONSTRUCTOR_DELEGATE_INDEX, 235a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block SCRIPT_FUNCTION_INDEX, 2366ded16be15dd865a9b21ea304d5273c8be299c87Steve Block OPAQUE_REFERENCE_FUNCTION_INDEX, 237a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block CONTEXT_EXTENSION_FUNCTION_INDEX, 238a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block OUT_OF_MEMORY_INDEX, 239a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block MAP_CACHE_INDEX, 240a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block CONTEXT_DATA_INDEX, 241257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch ALLOW_CODE_GEN_FROM_STRINGS_INDEX, 242257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch DERIVED_GET_TRAP_INDEX, 243f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch 244f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch // Properties from here are treated as weak references by the full GC. 245f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch // Scavenge treats them as strong references. 246b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch OPTIMIZED_FUNCTIONS_LIST, // Weak. 247b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch NEXT_CONTEXT_LINK, // Weak. 248f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch 249f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch // Total number of slots. 250f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch GLOBAL_CONTEXT_SLOTS, 251f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch 252b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch FIRST_WEAK_SLOT = OPTIMIZED_FUNCTIONS_LIST 253a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block }; 254a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 255a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // Direct slot access. 256a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block JSFunction* closure() { return JSFunction::cast(get(CLOSURE_INDEX)); } 257a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block void set_closure(JSFunction* closure) { set(CLOSURE_INDEX, closure); } 258a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 259a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block Context* fcontext() { return Context::cast(get(FCONTEXT_INDEX)); } 260a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block void set_fcontext(Context* context) { set(FCONTEXT_INDEX, context); } 261a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 262a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block Context* previous() { 263a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block Object* result = unchecked_previous(); 264a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block ASSERT(IsBootstrappingOrContext(result)); 265a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block return reinterpret_cast<Context*>(result); 266a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block } 267a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block void set_previous(Context* context) { set(PREVIOUS_INDEX, context); } 268a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 269a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block bool has_extension() { return unchecked_extension() != NULL; } 270a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block JSObject* extension() { return JSObject::cast(unchecked_extension()); } 271a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block void set_extension(JSObject* object) { set(EXTENSION_INDEX, object); } 272a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 273a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block GlobalObject* global() { 274a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block Object* result = get(GLOBAL_INDEX); 27544f0eee88ff00398ff7f715fab053374d808c90dSteve Block ASSERT(IsBootstrappingOrGlobalObject(result)); 276a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block return reinterpret_cast<GlobalObject*>(result); 277a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block } 278a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block void set_global(GlobalObject* global) { set(GLOBAL_INDEX, global); } 279a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 280a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // Returns a JSGlobalProxy object or null. 281a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block JSObject* global_proxy(); 282a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block void set_global_proxy(JSObject* global); 283a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 284a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // The builtins object. 285a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block JSBuiltinsObject* builtins(); 286a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 287a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // Compute the global context by traversing the context chain. 288a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block Context* global_context(); 289a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 290a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // Tells if this is a function context (as opposed to a 'with' context). 291a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block bool is_function_context() { return unchecked_previous() == NULL; } 292a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 293a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // Tells whether the global context is marked with out of memory. 29444f0eee88ff00398ff7f715fab053374d808c90dSteve Block inline bool has_out_of_memory(); 295a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 296a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // Mark the global context with out of memory. 29744f0eee88ff00398ff7f715fab053374d808c90dSteve Block inline void mark_out_of_memory(); 298a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 299a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // The exception holder is the object used as a with object in 300a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // the implementation of a catch block. 301a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block bool is_exception_holder(Object* object) { 302a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block return IsCatchContext() && extension() == object; 303a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block } 304a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 305b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch // A global context hold a list of all functions which have been optimized. 306b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch void AddOptimizedFunction(JSFunction* function); 307b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch void RemoveOptimizedFunction(JSFunction* function); 308b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch Object* OptimizedFunctionsListHead(); 309b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch void ClearOptimizedFunctions(); 310b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch 311a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#define GLOBAL_CONTEXT_FIELD_ACCESSORS(index, type, name) \ 312a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block void set_##name(type* value) { \ 313a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block ASSERT(IsGlobalContext()); \ 314a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block set(index, value); \ 315a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block } \ 316a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block type* name() { \ 317a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block ASSERT(IsGlobalContext()); \ 318a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block return type::cast(get(index)); \ 319a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block } 320a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block GLOBAL_CONTEXT_FIELDS(GLOBAL_CONTEXT_FIELD_ACCESSORS) 321a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#undef GLOBAL_CONTEXT_FIELD_ACCESSORS 322a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 323a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // Lookup the the slot called name, starting with the current context. 324a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // There are 4 possible outcomes: 325a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // 326a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // 1) index_ >= 0 && result->IsContext(): 327a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // most common case, the result is a Context, and index is the 328a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // context slot index, and the slot exists. 329a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // attributes == READ_ONLY for the function name variable, NONE otherwise. 330a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // 331a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // 2) index_ >= 0 && result->IsJSObject(): 332a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // the result is the JSObject arguments object, the index is the parameter 333a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // index, i.e., key into the arguments object, and the property exists. 334a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // attributes != ABSENT. 335a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // 336a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // 3) index_ < 0 && result->IsJSObject(): 337a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // the result is the JSObject extension context or the global object, 338a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // and the name is the property name, and the property exists. 339a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // attributes != ABSENT. 340a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // 341a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // 4) index_ < 0 && result.is_null(): 342a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // there was no context found with the corresponding property. 343a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // attributes == ABSENT. 344a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block Handle<Object> Lookup(Handle<String> name, ContextLookupFlags flags, 345a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block int* index_, PropertyAttributes* attributes); 346a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 347a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // Determine if a local variable with the given name exists in a 348a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // context. Do not consider context extension objects. This is 349a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // used for compiling code using eval. If the context surrounding 350a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // the eval call does not have a local variable with this name and 351a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // does not contain a with statement the property is global unless 352a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // it is shadowed by a property in an extension object introduced by 353a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // eval. 354a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block bool GlobalIfNotShadowedByEval(Handle<String> name); 355a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 356257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch // Determine if any function scope in the context call eval and if 357257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch // any of those calls are in non-strict mode. 358257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch void ComputeEvalScopeInfo(bool* outer_scope_calls_eval, 359257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch bool* outer_scope_calls_non_strict_eval); 360257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch 361a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // Code generation support. 362a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block static int SlotOffset(int index) { 363a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block return kHeaderSize + index * kPointerSize - kHeapObjectTag; 364a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block } 365a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 366f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch static const int kSize = kHeaderSize + GLOBAL_CONTEXT_SLOTS * kPointerSize; 367f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch 368f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch // GC support. 369f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch typedef FixedBodyDescriptor< 370f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch kHeaderSize, kSize, kSize> ScavengeBodyDescriptor; 371f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch 372f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch typedef FixedBodyDescriptor< 373f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch kHeaderSize, 374f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch kHeaderSize + FIRST_WEAK_SLOT * kPointerSize, 375f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch kSize> MarkCompactBodyDescriptor; 376f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch 377a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block private: 378a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // Unchecked access to the slots. 379a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block Object* unchecked_previous() { return get(PREVIOUS_INDEX); } 380a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block Object* unchecked_extension() { return get(EXTENSION_INDEX); } 381a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 382a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#ifdef DEBUG 383a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block // Bootstrapping-aware type checks. 384a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block static bool IsBootstrappingOrContext(Object* object); 385a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block static bool IsBootstrappingOrGlobalObject(Object* object); 386a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#endif 387a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}; 388a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 389a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block} } // namespace v8::internal 390a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 391a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#endif // V8_CONTEXTS_H_ 392