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 | |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 47 | // ES5 10.2 defines lexical environments with mutable and immutable bindings. |
| 48 | // Immutable bindings have two states, initialized and uninitialized, and |
| 49 | // their state is changed by the InitializeImmutableBinding method. |
| 50 | // |
| 51 | // The harmony proposal for block scoped bindings also introduces the |
| 52 | // uninitialized state for mutable bindings. A 'let' declared variable |
| 53 | // is a mutable binding that is created uninitalized upon activation of its |
| 54 | // lexical environment and it is initialized when evaluating its declaration |
| 55 | // statement. Var declared variables are mutable bindings that are |
| 56 | // immediately initialized upon creation. The BindingFlags enum represents |
| 57 | // information if a binding has definitely been initialized. 'const' declared |
| 58 | // variables are created as uninitialized immutable bindings. |
| 59 | |
| 60 | // In harmony mode accessing an uninitialized binding produces a reference |
| 61 | // error. |
| 62 | enum BindingFlags { |
| 63 | MUTABLE_IS_INITIALIZED, |
| 64 | MUTABLE_CHECK_INITIALIZED, |
| 65 | IMMUTABLE_IS_INITIALIZED, |
| 66 | IMMUTABLE_CHECK_INITIALIZED, |
| 67 | MISSING_BINDING |
| 68 | }; |
| 69 | |
| 70 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 71 | // Heap-allocated activation contexts. |
| 72 | // |
| 73 | // Contexts are implemented as FixedArray objects; the Context |
| 74 | // class is a convenience interface casted on a FixedArray object. |
| 75 | // |
| 76 | // Note: Context must have no virtual functions and Context objects |
| 77 | // must always be allocated via Heap::AllocateContext() or |
| 78 | // Factory::NewContext. |
| 79 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 80 | #define GLOBAL_CONTEXT_FIELDS(V) \ |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 81 | V(GLOBAL_PROXY_INDEX, JSObject, global_proxy_object) \ |
| 82 | V(SECURITY_TOKEN_INDEX, Object, security_token) \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 83 | V(BOOLEAN_FUNCTION_INDEX, JSFunction, boolean_function) \ |
| 84 | V(NUMBER_FUNCTION_INDEX, JSFunction, number_function) \ |
| 85 | V(STRING_FUNCTION_INDEX, JSFunction, string_function) \ |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 86 | V(STRING_FUNCTION_PROTOTYPE_MAP_INDEX, Map, string_function_prototype_map) \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 87 | V(OBJECT_FUNCTION_INDEX, JSFunction, object_function) \ |
| 88 | V(ARRAY_FUNCTION_INDEX, JSFunction, array_function) \ |
| 89 | V(DATE_FUNCTION_INDEX, JSFunction, date_function) \ |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 90 | V(JSON_OBJECT_INDEX, JSObject, json_object) \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 91 | V(REGEXP_FUNCTION_INDEX, JSFunction, regexp_function) \ |
| 92 | V(INITIAL_OBJECT_PROTOTYPE_INDEX, JSObject, initial_object_prototype) \ |
| 93 | V(CREATE_DATE_FUN_INDEX, JSFunction, create_date_fun) \ |
| 94 | V(TO_NUMBER_FUN_INDEX, JSFunction, to_number_fun) \ |
| 95 | V(TO_STRING_FUN_INDEX, JSFunction, to_string_fun) \ |
| 96 | V(TO_DETAIL_STRING_FUN_INDEX, JSFunction, to_detail_string_fun) \ |
| 97 | V(TO_OBJECT_FUN_INDEX, JSFunction, to_object_fun) \ |
| 98 | V(TO_INTEGER_FUN_INDEX, JSFunction, to_integer_fun) \ |
| 99 | V(TO_UINT32_FUN_INDEX, JSFunction, to_uint32_fun) \ |
| 100 | V(TO_INT32_FUN_INDEX, JSFunction, to_int32_fun) \ |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 101 | V(GLOBAL_EVAL_FUN_INDEX, JSFunction, global_eval_fun) \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 102 | V(INSTANTIATE_FUN_INDEX, JSFunction, instantiate_fun) \ |
| 103 | V(CONFIGURE_INSTANCE_FUN_INDEX, JSFunction, configure_instance_fun) \ |
| 104 | V(FUNCTION_MAP_INDEX, Map, function_map) \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 105 | V(STRICT_MODE_FUNCTION_MAP_INDEX, Map, strict_mode_function_map) \ |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 106 | V(FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map, function_without_prototype_map) \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 107 | V(STRICT_MODE_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map, \ |
| 108 | strict_mode_function_without_prototype_map) \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 109 | V(FUNCTION_INSTANCE_MAP_INDEX, Map, function_instance_map) \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 110 | V(STRICT_MODE_FUNCTION_INSTANCE_MAP_INDEX, Map, \ |
| 111 | strict_mode_function_instance_map) \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 112 | V(JS_ARRAY_MAP_INDEX, Map, js_array_map)\ |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 113 | V(REGEXP_RESULT_MAP_INDEX, Map, regexp_result_map)\ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 114 | V(ARGUMENTS_BOILERPLATE_INDEX, JSObject, arguments_boilerplate) \ |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 115 | V(ALIASED_ARGUMENTS_BOILERPLATE_INDEX, JSObject, \ |
| 116 | aliased_arguments_boilerplate) \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 117 | V(STRICT_MODE_ARGUMENTS_BOILERPLATE_INDEX, JSObject, \ |
| 118 | strict_mode_arguments_boilerplate) \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 119 | V(MESSAGE_LISTENERS_INDEX, JSObject, message_listeners) \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 120 | V(MAKE_MESSAGE_FUN_INDEX, JSFunction, make_message_fun) \ |
| 121 | V(GET_STACK_TRACE_LINE_INDEX, JSFunction, get_stack_trace_line_fun) \ |
| 122 | V(CONFIGURE_GLOBAL_INDEX, JSFunction, configure_global_fun) \ |
| 123 | V(FUNCTION_CACHE_INDEX, JSObject, function_cache) \ |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 124 | V(JSFUNCTION_RESULT_CACHES_INDEX, FixedArray, jsfunction_result_caches) \ |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 125 | V(NORMALIZED_MAP_CACHE_INDEX, NormalizedMapCache, normalized_map_cache) \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 126 | V(RUNTIME_CONTEXT_INDEX, Context, runtime_context) \ |
| 127 | V(CALL_AS_FUNCTION_DELEGATE_INDEX, JSFunction, call_as_function_delegate) \ |
sgjesse@chromium.org | 05521fc | 2009-05-21 07:37:44 +0000 | [diff] [blame] | 128 | V(CALL_AS_CONSTRUCTOR_DELEGATE_INDEX, JSFunction, \ |
| 129 | call_as_constructor_delegate) \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 130 | V(SCRIPT_FUNCTION_INDEX, JSFunction, script_function) \ |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 131 | V(OPAQUE_REFERENCE_FUNCTION_INDEX, JSFunction, opaque_reference_function) \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 132 | V(CONTEXT_EXTENSION_FUNCTION_INDEX, JSFunction, context_extension_function) \ |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 133 | V(OUT_OF_MEMORY_INDEX, Object, out_of_memory) \ |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 134 | V(MAP_CACHE_INDEX, Object, map_cache) \ |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 135 | V(CONTEXT_DATA_INDEX, Object, data) \ |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 136 | V(ALLOW_CODE_GEN_FROM_STRINGS_INDEX, Object, allow_code_gen_from_strings) \ |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 137 | V(DERIVED_HAS_TRAP_INDEX, JSFunction, derived_has_trap) \ |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 138 | V(DERIVED_GET_TRAP_INDEX, JSFunction, derived_get_trap) \ |
| 139 | V(DERIVED_SET_TRAP_INDEX, JSFunction, derived_set_trap) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 140 | |
| 141 | // JSFunctions are pairs (context, function code), sometimes also called |
| 142 | // closures. A Context object is used to represent function contexts and |
| 143 | // dynamically pushed 'with' contexts (or 'scopes' in ECMA-262 speak). |
| 144 | // |
| 145 | // At runtime, the contexts build a stack in parallel to the execution |
| 146 | // stack, with the top-most context being the current context. All contexts |
| 147 | // have the following slots: |
| 148 | // |
| 149 | // [ closure ] This is the current function. It is the same for all |
| 150 | // contexts inside a function. It provides access to the |
| 151 | // incoming context (i.e., the outer context, which may |
| 152 | // or may not become the current function's context), and |
| 153 | // it provides access to the functions code and thus it's |
| 154 | // scope information, which in turn contains the names of |
| 155 | // statically allocated context slots. The names are needed |
| 156 | // for dynamic lookups in the presence of 'with' or 'eval'. |
| 157 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 158 | // [ previous ] A pointer to the previous context. It is NULL for |
| 159 | // function contexts, and non-NULL for 'with' contexts. |
| 160 | // Used to implement the 'with' statement. |
| 161 | // |
| 162 | // [ extension ] A pointer to an extension JSObject, or NULL. Used to |
| 163 | // implement 'with' statements and dynamic declarations |
| 164 | // (through 'eval'). The object in a 'with' statement is |
| 165 | // stored in the extension slot of a 'with' context. |
| 166 | // Dynamically declared variables/functions are also added |
| 167 | // to lazily allocated extension object. Context::Lookup |
| 168 | // searches the extension object for properties. |
| 169 | // |
| 170 | // [ global ] A pointer to the global object. Provided for quick |
| 171 | // access to the global object from inside the code (since |
| 172 | // we always have a context pointer). |
| 173 | // |
| 174 | // In addition, function contexts may have statically allocated context slots |
| 175 | // to store local variables/functions that are accessed from inner functions |
| 176 | // (via static context addresses) or through 'eval' (dynamic context lookups). |
| 177 | // Finally, the global context contains additional slots for fast access to |
| 178 | // global properties. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 179 | |
| 180 | class Context: public FixedArray { |
| 181 | public: |
| 182 | // Conversions. |
| 183 | static Context* cast(Object* context) { |
| 184 | ASSERT(context->IsContext()); |
| 185 | return reinterpret_cast<Context*>(context); |
| 186 | } |
| 187 | |
| 188 | // The default context slot layout; indices are FixedArray slot indices. |
| 189 | enum { |
| 190 | // These slots are in all contexts. |
| 191 | CLOSURE_INDEX, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 192 | PREVIOUS_INDEX, |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 193 | // The extension slot is used for either the global object (in global |
| 194 | // contexts), eval extension object (function contexts), subject of with |
| 195 | // (with contexts), or the variable name (catch contexts). |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 196 | EXTENSION_INDEX, |
| 197 | GLOBAL_INDEX, |
| 198 | MIN_CONTEXT_SLOTS, |
| 199 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 200 | // This slot holds the thrown value in catch contexts. |
| 201 | THROWN_OBJECT_INDEX = MIN_CONTEXT_SLOTS, |
| 202 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 203 | // These slots are only in global contexts. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 204 | GLOBAL_PROXY_INDEX = MIN_CONTEXT_SLOTS, |
| 205 | SECURITY_TOKEN_INDEX, |
| 206 | ARGUMENTS_BOILERPLATE_INDEX, |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 207 | ALIASED_ARGUMENTS_BOILERPLATE_INDEX, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 208 | STRICT_MODE_ARGUMENTS_BOILERPLATE_INDEX, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 209 | JS_ARRAY_MAP_INDEX, |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 210 | REGEXP_RESULT_MAP_INDEX, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 211 | FUNCTION_MAP_INDEX, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 212 | STRICT_MODE_FUNCTION_MAP_INDEX, |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 213 | FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 214 | STRICT_MODE_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 215 | FUNCTION_INSTANCE_MAP_INDEX, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 216 | STRICT_MODE_FUNCTION_INSTANCE_MAP_INDEX, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 217 | INITIAL_OBJECT_PROTOTYPE_INDEX, |
| 218 | BOOLEAN_FUNCTION_INDEX, |
| 219 | NUMBER_FUNCTION_INDEX, |
| 220 | STRING_FUNCTION_INDEX, |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 221 | STRING_FUNCTION_PROTOTYPE_MAP_INDEX, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 222 | OBJECT_FUNCTION_INDEX, |
| 223 | ARRAY_FUNCTION_INDEX, |
| 224 | DATE_FUNCTION_INDEX, |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 225 | JSON_OBJECT_INDEX, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 226 | REGEXP_FUNCTION_INDEX, |
| 227 | CREATE_DATE_FUN_INDEX, |
| 228 | TO_NUMBER_FUN_INDEX, |
| 229 | TO_STRING_FUN_INDEX, |
| 230 | TO_DETAIL_STRING_FUN_INDEX, |
| 231 | TO_OBJECT_FUN_INDEX, |
| 232 | TO_INTEGER_FUN_INDEX, |
| 233 | TO_UINT32_FUN_INDEX, |
| 234 | TO_INT32_FUN_INDEX, |
| 235 | TO_BOOLEAN_FUN_INDEX, |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 236 | GLOBAL_EVAL_FUN_INDEX, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 237 | INSTANTIATE_FUN_INDEX, |
| 238 | CONFIGURE_INSTANCE_FUN_INDEX, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 239 | MESSAGE_LISTENERS_INDEX, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 240 | MAKE_MESSAGE_FUN_INDEX, |
| 241 | GET_STACK_TRACE_LINE_INDEX, |
| 242 | CONFIGURE_GLOBAL_INDEX, |
| 243 | FUNCTION_CACHE_INDEX, |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 244 | JSFUNCTION_RESULT_CACHES_INDEX, |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 245 | NORMALIZED_MAP_CACHE_INDEX, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 246 | RUNTIME_CONTEXT_INDEX, |
| 247 | CALL_AS_FUNCTION_DELEGATE_INDEX, |
sgjesse@chromium.org | 05521fc | 2009-05-21 07:37:44 +0000 | [diff] [blame] | 248 | CALL_AS_CONSTRUCTOR_DELEGATE_INDEX, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 249 | SCRIPT_FUNCTION_INDEX, |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 250 | OPAQUE_REFERENCE_FUNCTION_INDEX, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 251 | CONTEXT_EXTENSION_FUNCTION_INDEX, |
| 252 | OUT_OF_MEMORY_INDEX, |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 253 | CONTEXT_DATA_INDEX, |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 254 | ALLOW_CODE_GEN_FROM_STRINGS_INDEX, |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 255 | DERIVED_HAS_TRAP_INDEX, |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 256 | DERIVED_GET_TRAP_INDEX, |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 257 | DERIVED_SET_TRAP_INDEX, |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 258 | |
| 259 | // Properties from here are treated as weak references by the full GC. |
| 260 | // Scavenge treats them as strong references. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 261 | OPTIMIZED_FUNCTIONS_LIST, // Weak. |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 262 | MAP_CACHE_INDEX, // Weak. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 263 | NEXT_CONTEXT_LINK, // Weak. |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 264 | |
| 265 | // Total number of slots. |
| 266 | GLOBAL_CONTEXT_SLOTS, |
| 267 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 268 | FIRST_WEAK_SLOT = OPTIMIZED_FUNCTIONS_LIST |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 269 | }; |
| 270 | |
| 271 | // Direct slot access. |
| 272 | JSFunction* closure() { return JSFunction::cast(get(CLOSURE_INDEX)); } |
| 273 | void set_closure(JSFunction* closure) { set(CLOSURE_INDEX, closure); } |
| 274 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 275 | Context* previous() { |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 276 | Object* result = unchecked_previous(); |
| 277 | ASSERT(IsBootstrappingOrContext(result)); |
| 278 | return reinterpret_cast<Context*>(result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 279 | } |
| 280 | void set_previous(Context* context) { set(PREVIOUS_INDEX, context); } |
| 281 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 282 | bool has_extension() { return extension() != NULL; } |
| 283 | Object* extension() { return get(EXTENSION_INDEX); } |
| 284 | void set_extension(Object* object) { set(EXTENSION_INDEX, object); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 285 | |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 286 | // Get the context where var declarations will be hoisted to, which |
| 287 | // may be the context itself. |
| 288 | Context* declaration_context(); |
| 289 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 290 | GlobalObject* global() { |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 291 | Object* result = get(GLOBAL_INDEX); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 292 | ASSERT(IsBootstrappingOrGlobalObject(result)); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 293 | return reinterpret_cast<GlobalObject*>(result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 294 | } |
| 295 | void set_global(GlobalObject* global) { set(GLOBAL_INDEX, global); } |
| 296 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 297 | // Returns a JSGlobalProxy object or null. |
| 298 | JSObject* global_proxy(); |
| 299 | void set_global_proxy(JSObject* global); |
| 300 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 301 | // The builtins object. |
| 302 | JSBuiltinsObject* builtins(); |
| 303 | |
| 304 | // Compute the global context by traversing the context chain. |
| 305 | Context* global_context(); |
| 306 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 307 | // Predicates for context types. IsGlobalContext is defined on Object |
| 308 | // because we frequently have to know if arbitrary objects are global |
| 309 | // contexts. |
| 310 | bool IsFunctionContext() { |
| 311 | Map* map = this->map(); |
| 312 | return map == map->GetHeap()->function_context_map(); |
| 313 | } |
| 314 | bool IsCatchContext() { |
| 315 | Map* map = this->map(); |
| 316 | return map == map->GetHeap()->catch_context_map(); |
| 317 | } |
| 318 | bool IsWithContext() { |
| 319 | Map* map = this->map(); |
| 320 | return map == map->GetHeap()->with_context_map(); |
| 321 | } |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 322 | bool IsBlockContext() { |
| 323 | Map* map = this->map(); |
| 324 | return map == map->GetHeap()->block_context_map(); |
| 325 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 326 | |
| 327 | // Tells whether the global context is marked with out of memory. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 328 | inline bool has_out_of_memory(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 329 | |
| 330 | // Mark the global context with out of memory. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 331 | inline void mark_out_of_memory(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 332 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 333 | // The exception holder is the object used as a with object in |
| 334 | // the implementation of a catch block. |
| 335 | bool is_exception_holder(Object* object) { |
| 336 | return IsCatchContext() && extension() == object; |
| 337 | } |
| 338 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 339 | // A global context hold a list of all functions which have been optimized. |
| 340 | void AddOptimizedFunction(JSFunction* function); |
| 341 | void RemoveOptimizedFunction(JSFunction* function); |
| 342 | Object* OptimizedFunctionsListHead(); |
| 343 | void ClearOptimizedFunctions(); |
| 344 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 345 | #define GLOBAL_CONTEXT_FIELD_ACCESSORS(index, type, name) \ |
| 346 | void set_##name(type* value) { \ |
| 347 | ASSERT(IsGlobalContext()); \ |
| 348 | set(index, value); \ |
| 349 | } \ |
| 350 | type* name() { \ |
| 351 | ASSERT(IsGlobalContext()); \ |
| 352 | return type::cast(get(index)); \ |
| 353 | } |
| 354 | GLOBAL_CONTEXT_FIELDS(GLOBAL_CONTEXT_FIELD_ACCESSORS) |
| 355 | #undef GLOBAL_CONTEXT_FIELD_ACCESSORS |
| 356 | |
| 357 | // Lookup the the slot called name, starting with the current context. |
| 358 | // There are 4 possible outcomes: |
| 359 | // |
| 360 | // 1) index_ >= 0 && result->IsContext(): |
| 361 | // most common case, the result is a Context, and index is the |
| 362 | // context slot index, and the slot exists. |
| 363 | // attributes == READ_ONLY for the function name variable, NONE otherwise. |
| 364 | // |
| 365 | // 2) index_ >= 0 && result->IsJSObject(): |
| 366 | // the result is the JSObject arguments object, the index is the parameter |
| 367 | // index, i.e., key into the arguments object, and the property exists. |
| 368 | // attributes != ABSENT. |
| 369 | // |
| 370 | // 3) index_ < 0 && result->IsJSObject(): |
| 371 | // the result is the JSObject extension context or the global object, |
| 372 | // and the name is the property name, and the property exists. |
| 373 | // attributes != ABSENT. |
| 374 | // |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 375 | // 4) index_ < 0 && result.is_null(): |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 376 | // there was no context found with the corresponding property. |
| 377 | // attributes == ABSENT. |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 378 | Handle<Object> Lookup(Handle<String> name, |
| 379 | ContextLookupFlags flags, |
| 380 | int* index_, |
| 381 | PropertyAttributes* attributes, |
| 382 | BindingFlags* binding_flags); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 383 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 384 | // Determine if a local variable with the given name exists in a |
| 385 | // context. Do not consider context extension objects. This is |
| 386 | // used for compiling code using eval. If the context surrounding |
| 387 | // the eval call does not have a local variable with this name and |
| 388 | // does not contain a with statement the property is global unless |
| 389 | // it is shadowed by a property in an extension object introduced by |
| 390 | // eval. |
| 391 | bool GlobalIfNotShadowedByEval(Handle<String> name); |
| 392 | |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 393 | // Determine if any function scope in the context call eval and if |
| 394 | // any of those calls are in non-strict mode. |
| 395 | void ComputeEvalScopeInfo(bool* outer_scope_calls_eval, |
| 396 | bool* outer_scope_calls_non_strict_eval); |
| 397 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 398 | // Code generation support. |
| 399 | static int SlotOffset(int index) { |
| 400 | return kHeaderSize + index * kPointerSize - kHeapObjectTag; |
| 401 | } |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 402 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 403 | static const int kSize = kHeaderSize + GLOBAL_CONTEXT_SLOTS * kPointerSize; |
| 404 | |
| 405 | // GC support. |
| 406 | typedef FixedBodyDescriptor< |
| 407 | kHeaderSize, kSize, kSize> ScavengeBodyDescriptor; |
| 408 | |
| 409 | typedef FixedBodyDescriptor< |
| 410 | kHeaderSize, |
| 411 | kHeaderSize + FIRST_WEAK_SLOT * kPointerSize, |
| 412 | kSize> MarkCompactBodyDescriptor; |
| 413 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 414 | private: |
| 415 | // Unchecked access to the slots. |
| 416 | Object* unchecked_previous() { return get(PREVIOUS_INDEX); } |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 417 | |
| 418 | #ifdef DEBUG |
| 419 | // Bootstrapping-aware type checks. |
| 420 | static bool IsBootstrappingOrContext(Object* object); |
| 421 | static bool IsBootstrappingOrGlobalObject(Object* object); |
| 422 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 423 | }; |
| 424 | |
| 425 | } } // namespace v8::internal |
| 426 | |
| 427 | #endif // V8_CONTEXTS_H_ |