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