fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 31 | #include "heap.h" |
| 32 | #include "objects.h" |
| 33 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 34 | namespace v8 { |
| 35 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 36 | |
| 37 | |
| 38 | enum 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 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 56 | #define GLOBAL_CONTEXT_FIELDS(V) \ |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 57 | V(GLOBAL_PROXY_INDEX, JSObject, global_proxy_object) \ |
| 58 | V(SECURITY_TOKEN_INDEX, Object, security_token) \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 59 | V(BOOLEAN_FUNCTION_INDEX, JSFunction, boolean_function) \ |
| 60 | V(NUMBER_FUNCTION_INDEX, JSFunction, number_function) \ |
| 61 | V(STRING_FUNCTION_INDEX, JSFunction, string_function) \ |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 62 | V(STRING_FUNCTION_PROTOTYPE_MAP_INDEX, Map, string_function_prototype_map) \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 63 | V(OBJECT_FUNCTION_INDEX, JSFunction, object_function) \ |
| 64 | V(ARRAY_FUNCTION_INDEX, JSFunction, array_function) \ |
| 65 | V(DATE_FUNCTION_INDEX, JSFunction, date_function) \ |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 66 | V(JSON_OBJECT_INDEX, JSObject, json_object) \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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) \ |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 77 | V(GLOBAL_EVAL_FUN_INDEX, JSFunction, global_eval_fun) \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 78 | V(INSTANTIATE_FUN_INDEX, JSFunction, instantiate_fun) \ |
| 79 | V(CONFIGURE_INSTANCE_FUN_INDEX, JSFunction, configure_instance_fun) \ |
| 80 | V(FUNCTION_MAP_INDEX, Map, function_map) \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 81 | V(STRICT_MODE_FUNCTION_MAP_INDEX, Map, strict_mode_function_map) \ |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 82 | V(FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map, function_without_prototype_map) \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 83 | V(STRICT_MODE_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map, \ |
| 84 | strict_mode_function_without_prototype_map) \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 85 | V(FUNCTION_INSTANCE_MAP_INDEX, Map, function_instance_map) \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 86 | V(STRICT_MODE_FUNCTION_INSTANCE_MAP_INDEX, Map, \ |
| 87 | strict_mode_function_instance_map) \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 88 | V(JS_ARRAY_MAP_INDEX, Map, js_array_map)\ |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 89 | V(REGEXP_RESULT_MAP_INDEX, Map, regexp_result_map)\ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 90 | V(ARGUMENTS_BOILERPLATE_INDEX, JSObject, arguments_boilerplate) \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 91 | V(STRICT_MODE_ARGUMENTS_BOILERPLATE_INDEX, JSObject, \ |
| 92 | strict_mode_arguments_boilerplate) \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 93 | V(MESSAGE_LISTENERS_INDEX, JSObject, message_listeners) \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 94 | V(MAKE_MESSAGE_FUN_INDEX, JSFunction, make_message_fun) \ |
| 95 | V(GET_STACK_TRACE_LINE_INDEX, JSFunction, get_stack_trace_line_fun) \ |
| 96 | V(CONFIGURE_GLOBAL_INDEX, JSFunction, configure_global_fun) \ |
| 97 | V(FUNCTION_CACHE_INDEX, JSObject, function_cache) \ |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 98 | V(JSFUNCTION_RESULT_CACHES_INDEX, FixedArray, jsfunction_result_caches) \ |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 99 | V(NORMALIZED_MAP_CACHE_INDEX, NormalizedMapCache, normalized_map_cache) \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 100 | V(RUNTIME_CONTEXT_INDEX, Context, runtime_context) \ |
| 101 | V(CALL_AS_FUNCTION_DELEGATE_INDEX, JSFunction, call_as_function_delegate) \ |
sgjesse@chromium.org | 05521fc | 2009-05-21 07:37:44 +0000 | [diff] [blame] | 102 | V(CALL_AS_CONSTRUCTOR_DELEGATE_INDEX, JSFunction, \ |
| 103 | call_as_constructor_delegate) \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 104 | V(SCRIPT_FUNCTION_INDEX, JSFunction, script_function) \ |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 105 | V(OPAQUE_REFERENCE_FUNCTION_INDEX, JSFunction, opaque_reference_function) \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 106 | V(CONTEXT_EXTENSION_FUNCTION_INDEX, JSFunction, context_extension_function) \ |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 107 | V(OUT_OF_MEMORY_INDEX, Object, out_of_memory) \ |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 108 | V(MAP_CACHE_INDEX, Object, map_cache) \ |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 109 | V(CONTEXT_DATA_INDEX, Object, data) \ |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame^] | 110 | V(ALLOW_CODE_GEN_FROM_STRINGS_INDEX, Object, allow_code_gen_from_strings) \ |
| 111 | V(DERIVED_GET_TRAP_INDEX, JSFunction, derived_get_trap) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 112 | |
| 113 | // JSFunctions are pairs (context, function code), sometimes also called |
| 114 | // closures. A Context object is used to represent function contexts and |
| 115 | // dynamically pushed 'with' contexts (or 'scopes' in ECMA-262 speak). |
| 116 | // |
| 117 | // At runtime, the contexts build a stack in parallel to the execution |
| 118 | // stack, with the top-most context being the current context. All contexts |
| 119 | // have the following slots: |
| 120 | // |
| 121 | // [ closure ] This is the current function. It is the same for all |
| 122 | // contexts inside a function. It provides access to the |
| 123 | // incoming context (i.e., the outer context, which may |
| 124 | // or may not become the current function's context), and |
| 125 | // it provides access to the functions code and thus it's |
| 126 | // scope information, which in turn contains the names of |
| 127 | // statically allocated context slots. The names are needed |
| 128 | // for dynamic lookups in the presence of 'with' or 'eval'. |
| 129 | // |
| 130 | // [ fcontext ] A pointer to the innermost enclosing function context. |
| 131 | // It is the same for all contexts *allocated* inside a |
| 132 | // function, and the function context's fcontext points |
| 133 | // to itself. It is only needed for fast access of the |
| 134 | // function context (used for declarations, and static |
| 135 | // context slot access). |
| 136 | // |
| 137 | // [ previous ] A pointer to the previous context. It is NULL for |
| 138 | // function contexts, and non-NULL for 'with' contexts. |
| 139 | // Used to implement the 'with' statement. |
| 140 | // |
| 141 | // [ extension ] A pointer to an extension JSObject, or NULL. Used to |
| 142 | // implement 'with' statements and dynamic declarations |
| 143 | // (through 'eval'). The object in a 'with' statement is |
| 144 | // stored in the extension slot of a 'with' context. |
| 145 | // Dynamically declared variables/functions are also added |
| 146 | // to lazily allocated extension object. Context::Lookup |
| 147 | // searches the extension object for properties. |
| 148 | // |
| 149 | // [ global ] A pointer to the global object. Provided for quick |
| 150 | // access to the global object from inside the code (since |
| 151 | // we always have a context pointer). |
| 152 | // |
| 153 | // In addition, function contexts may have statically allocated context slots |
| 154 | // to store local variables/functions that are accessed from inner functions |
| 155 | // (via static context addresses) or through 'eval' (dynamic context lookups). |
| 156 | // Finally, the global context contains additional slots for fast access to |
| 157 | // global properties. |
| 158 | // |
| 159 | // We may be able to simplify the implementation: |
| 160 | // |
| 161 | // - We may be able to get rid of 'fcontext': We can always use the fact that |
| 162 | // previous == NULL for function contexts and so we can search for them. They |
| 163 | // are only needed when doing dynamic declarations, and the context chains |
| 164 | // tend to be very very short (depth of nesting of 'with' statements). At |
| 165 | // the moment we also use it in generated code for context slot accesses - |
| 166 | // and there we don't want a loop because of code bloat - but we may not |
| 167 | // need it there after all (see comment in codegen_*.cc). |
| 168 | // |
| 169 | // - If we cannot get rid of fcontext, consider making 'previous' never NULL |
| 170 | // except for the global context. This could simplify Context::Lookup. |
| 171 | |
| 172 | class Context: public FixedArray { |
| 173 | public: |
| 174 | // Conversions. |
| 175 | static Context* cast(Object* context) { |
| 176 | ASSERT(context->IsContext()); |
| 177 | return reinterpret_cast<Context*>(context); |
| 178 | } |
| 179 | |
| 180 | // The default context slot layout; indices are FixedArray slot indices. |
| 181 | enum { |
| 182 | // These slots are in all contexts. |
| 183 | CLOSURE_INDEX, |
| 184 | FCONTEXT_INDEX, |
| 185 | PREVIOUS_INDEX, |
| 186 | EXTENSION_INDEX, |
| 187 | GLOBAL_INDEX, |
| 188 | MIN_CONTEXT_SLOTS, |
| 189 | |
| 190 | // These slots are only in global contexts. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 191 | GLOBAL_PROXY_INDEX = MIN_CONTEXT_SLOTS, |
| 192 | SECURITY_TOKEN_INDEX, |
| 193 | ARGUMENTS_BOILERPLATE_INDEX, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 194 | STRICT_MODE_ARGUMENTS_BOILERPLATE_INDEX, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 195 | JS_ARRAY_MAP_INDEX, |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 196 | REGEXP_RESULT_MAP_INDEX, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 197 | FUNCTION_MAP_INDEX, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 198 | STRICT_MODE_FUNCTION_MAP_INDEX, |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 199 | FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 200 | STRICT_MODE_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 201 | FUNCTION_INSTANCE_MAP_INDEX, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 202 | STRICT_MODE_FUNCTION_INSTANCE_MAP_INDEX, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 203 | INITIAL_OBJECT_PROTOTYPE_INDEX, |
| 204 | BOOLEAN_FUNCTION_INDEX, |
| 205 | NUMBER_FUNCTION_INDEX, |
| 206 | STRING_FUNCTION_INDEX, |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 207 | STRING_FUNCTION_PROTOTYPE_MAP_INDEX, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 208 | OBJECT_FUNCTION_INDEX, |
| 209 | ARRAY_FUNCTION_INDEX, |
| 210 | DATE_FUNCTION_INDEX, |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 211 | JSON_OBJECT_INDEX, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 212 | REGEXP_FUNCTION_INDEX, |
| 213 | CREATE_DATE_FUN_INDEX, |
| 214 | TO_NUMBER_FUN_INDEX, |
| 215 | TO_STRING_FUN_INDEX, |
| 216 | TO_DETAIL_STRING_FUN_INDEX, |
| 217 | TO_OBJECT_FUN_INDEX, |
| 218 | TO_INTEGER_FUN_INDEX, |
| 219 | TO_UINT32_FUN_INDEX, |
| 220 | TO_INT32_FUN_INDEX, |
| 221 | TO_BOOLEAN_FUN_INDEX, |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 222 | GLOBAL_EVAL_FUN_INDEX, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 223 | INSTANTIATE_FUN_INDEX, |
| 224 | CONFIGURE_INSTANCE_FUN_INDEX, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 225 | MESSAGE_LISTENERS_INDEX, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 226 | MAKE_MESSAGE_FUN_INDEX, |
| 227 | GET_STACK_TRACE_LINE_INDEX, |
| 228 | CONFIGURE_GLOBAL_INDEX, |
| 229 | FUNCTION_CACHE_INDEX, |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 230 | JSFUNCTION_RESULT_CACHES_INDEX, |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 231 | NORMALIZED_MAP_CACHE_INDEX, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 232 | RUNTIME_CONTEXT_INDEX, |
| 233 | CALL_AS_FUNCTION_DELEGATE_INDEX, |
sgjesse@chromium.org | 05521fc | 2009-05-21 07:37:44 +0000 | [diff] [blame] | 234 | CALL_AS_CONSTRUCTOR_DELEGATE_INDEX, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 235 | SCRIPT_FUNCTION_INDEX, |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 236 | OPAQUE_REFERENCE_FUNCTION_INDEX, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 237 | CONTEXT_EXTENSION_FUNCTION_INDEX, |
| 238 | OUT_OF_MEMORY_INDEX, |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 239 | MAP_CACHE_INDEX, |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 240 | CONTEXT_DATA_INDEX, |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 241 | ALLOW_CODE_GEN_FROM_STRINGS_INDEX, |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame^] | 242 | DERIVED_GET_TRAP_INDEX, |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 243 | |
| 244 | // Properties from here are treated as weak references by the full GC. |
| 245 | // Scavenge treats them as strong references. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 246 | OPTIMIZED_FUNCTIONS_LIST, // Weak. |
| 247 | NEXT_CONTEXT_LINK, // Weak. |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 248 | |
| 249 | // Total number of slots. |
| 250 | GLOBAL_CONTEXT_SLOTS, |
| 251 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 252 | FIRST_WEAK_SLOT = OPTIMIZED_FUNCTIONS_LIST |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 253 | }; |
| 254 | |
| 255 | // Direct slot access. |
| 256 | JSFunction* closure() { return JSFunction::cast(get(CLOSURE_INDEX)); } |
| 257 | void set_closure(JSFunction* closure) { set(CLOSURE_INDEX, closure); } |
| 258 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 259 | Context* fcontext() { return Context::cast(get(FCONTEXT_INDEX)); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 260 | void set_fcontext(Context* context) { set(FCONTEXT_INDEX, context); } |
| 261 | |
| 262 | Context* previous() { |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 263 | Object* result = unchecked_previous(); |
| 264 | ASSERT(IsBootstrappingOrContext(result)); |
| 265 | return reinterpret_cast<Context*>(result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 266 | } |
| 267 | void set_previous(Context* context) { set(PREVIOUS_INDEX, context); } |
| 268 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 269 | bool has_extension() { return unchecked_extension() != NULL; } |
| 270 | JSObject* extension() { return JSObject::cast(unchecked_extension()); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 271 | void set_extension(JSObject* object) { set(EXTENSION_INDEX, object); } |
| 272 | |
| 273 | GlobalObject* global() { |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 274 | Object* result = get(GLOBAL_INDEX); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 275 | ASSERT(IsBootstrappingOrGlobalObject(result)); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 276 | return reinterpret_cast<GlobalObject*>(result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 277 | } |
| 278 | void set_global(GlobalObject* global) { set(GLOBAL_INDEX, global); } |
| 279 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 280 | // Returns a JSGlobalProxy object or null. |
| 281 | JSObject* global_proxy(); |
| 282 | void set_global_proxy(JSObject* global); |
| 283 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 284 | // The builtins object. |
| 285 | JSBuiltinsObject* builtins(); |
| 286 | |
| 287 | // Compute the global context by traversing the context chain. |
| 288 | Context* global_context(); |
| 289 | |
| 290 | // Tells if this is a function context (as opposed to a 'with' context). |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 291 | bool is_function_context() { return unchecked_previous() == NULL; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 292 | |
| 293 | // Tells whether the global context is marked with out of memory. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 294 | inline bool has_out_of_memory(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 295 | |
| 296 | // Mark the global context with out of memory. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 297 | inline void mark_out_of_memory(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 298 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 299 | // The exception holder is the object used as a with object in |
| 300 | // the implementation of a catch block. |
| 301 | bool is_exception_holder(Object* object) { |
| 302 | return IsCatchContext() && extension() == object; |
| 303 | } |
| 304 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 305 | // A global context hold a list of all functions which have been optimized. |
| 306 | void AddOptimizedFunction(JSFunction* function); |
| 307 | void RemoveOptimizedFunction(JSFunction* function); |
| 308 | Object* OptimizedFunctionsListHead(); |
| 309 | void ClearOptimizedFunctions(); |
| 310 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 311 | #define GLOBAL_CONTEXT_FIELD_ACCESSORS(index, type, name) \ |
| 312 | void set_##name(type* value) { \ |
| 313 | ASSERT(IsGlobalContext()); \ |
| 314 | set(index, value); \ |
| 315 | } \ |
| 316 | type* name() { \ |
| 317 | ASSERT(IsGlobalContext()); \ |
| 318 | return type::cast(get(index)); \ |
| 319 | } |
| 320 | GLOBAL_CONTEXT_FIELDS(GLOBAL_CONTEXT_FIELD_ACCESSORS) |
| 321 | #undef GLOBAL_CONTEXT_FIELD_ACCESSORS |
| 322 | |
| 323 | // Lookup the the slot called name, starting with the current context. |
| 324 | // There are 4 possible outcomes: |
| 325 | // |
| 326 | // 1) index_ >= 0 && result->IsContext(): |
| 327 | // most common case, the result is a Context, and index is the |
| 328 | // context slot index, and the slot exists. |
| 329 | // attributes == READ_ONLY for the function name variable, NONE otherwise. |
| 330 | // |
| 331 | // 2) index_ >= 0 && result->IsJSObject(): |
| 332 | // the result is the JSObject arguments object, the index is the parameter |
| 333 | // index, i.e., key into the arguments object, and the property exists. |
| 334 | // attributes != ABSENT. |
| 335 | // |
| 336 | // 3) index_ < 0 && result->IsJSObject(): |
| 337 | // the result is the JSObject extension context or the global object, |
| 338 | // and the name is the property name, and the property exists. |
| 339 | // attributes != ABSENT. |
| 340 | // |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 341 | // 4) index_ < 0 && result.is_null(): |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 342 | // there was no context found with the corresponding property. |
| 343 | // attributes == ABSENT. |
| 344 | Handle<Object> Lookup(Handle<String> name, ContextLookupFlags flags, |
| 345 | int* index_, PropertyAttributes* attributes); |
| 346 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 347 | // Determine if a local variable with the given name exists in a |
| 348 | // context. Do not consider context extension objects. This is |
| 349 | // used for compiling code using eval. If the context surrounding |
| 350 | // the eval call does not have a local variable with this name and |
| 351 | // does not contain a with statement the property is global unless |
| 352 | // it is shadowed by a property in an extension object introduced by |
| 353 | // eval. |
| 354 | bool GlobalIfNotShadowedByEval(Handle<String> name); |
| 355 | |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 356 | // Determine if any function scope in the context call eval and if |
| 357 | // any of those calls are in non-strict mode. |
| 358 | void ComputeEvalScopeInfo(bool* outer_scope_calls_eval, |
| 359 | bool* outer_scope_calls_non_strict_eval); |
| 360 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 361 | // Code generation support. |
| 362 | static int SlotOffset(int index) { |
| 363 | return kHeaderSize + index * kPointerSize - kHeapObjectTag; |
| 364 | } |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 365 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 366 | static const int kSize = kHeaderSize + GLOBAL_CONTEXT_SLOTS * kPointerSize; |
| 367 | |
| 368 | // GC support. |
| 369 | typedef FixedBodyDescriptor< |
| 370 | kHeaderSize, kSize, kSize> ScavengeBodyDescriptor; |
| 371 | |
| 372 | typedef FixedBodyDescriptor< |
| 373 | kHeaderSize, |
| 374 | kHeaderSize + FIRST_WEAK_SLOT * kPointerSize, |
| 375 | kSize> MarkCompactBodyDescriptor; |
| 376 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 377 | private: |
| 378 | // Unchecked access to the slots. |
| 379 | Object* unchecked_previous() { return get(PREVIOUS_INDEX); } |
| 380 | Object* unchecked_extension() { return get(EXTENSION_INDEX); } |
| 381 | |
| 382 | #ifdef DEBUG |
| 383 | // Bootstrapping-aware type checks. |
| 384 | static bool IsBootstrappingOrContext(Object* object); |
| 385 | static bool IsBootstrappingOrGlobalObject(Object* object); |
| 386 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 387 | }; |
| 388 | |
| 389 | } } // namespace v8::internal |
| 390 | |
| 391 | #endif // V8_CONTEXTS_H_ |