karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +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 | #include "v8.h" |
| 29 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 30 | #include "bootstrapper.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 31 | #include "debug.h" |
| 32 | #include "scopeinfo.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 | |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 37 | Context* Context::declaration_context() { |
| 38 | Context* current = this; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 39 | while (!current->IsFunctionContext() && !current->IsNativeContext()) { |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 40 | current = current->previous(); |
| 41 | ASSERT(current->closure() == closure()); |
| 42 | } |
| 43 | return current; |
| 44 | } |
| 45 | |
| 46 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 47 | JSBuiltinsObject* Context::builtins() { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 48 | GlobalObject* object = global_object(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 49 | if (object->IsJSGlobalObject()) { |
| 50 | return JSGlobalObject::cast(object)->builtins(); |
| 51 | } else { |
| 52 | ASSERT(object->IsJSBuiltinsObject()); |
| 53 | return JSBuiltinsObject::cast(object); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 58 | Context* Context::global_context() { |
| 59 | Context* current = this; |
| 60 | while (!current->IsGlobalContext()) { |
| 61 | current = current->previous(); |
| 62 | } |
| 63 | return current; |
| 64 | } |
| 65 | |
| 66 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 67 | Context* Context::native_context() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 68 | // Fast case: the global object for this context has been set. In |
| 69 | // that case, the global object has a direct pointer to the global |
| 70 | // context. |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 71 | if (global_object()->IsGlobalObject()) { |
| 72 | return global_object()->native_context(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 73 | } |
kasperl@chromium.org | 7b9eafd | 2009-12-21 15:20:30 +0000 | [diff] [blame] | 74 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 75 | // During bootstrapping, the global object might not be set and we |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 76 | // have to search the context chain to find the native context. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 77 | ASSERT(Isolate::Current()->bootstrapper()->IsActive()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 78 | Context* current = this; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 79 | while (!current->IsNativeContext()) { |
kasperl@chromium.org | 7b9eafd | 2009-12-21 15:20:30 +0000 | [diff] [blame] | 80 | JSFunction* closure = JSFunction::cast(current->closure()); |
| 81 | current = Context::cast(closure->context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 82 | } |
| 83 | return current; |
| 84 | } |
| 85 | |
| 86 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 87 | JSObject* Context::global_proxy() { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 88 | return native_context()->global_proxy_object(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void Context::set_global_proxy(JSObject* object) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 92 | native_context()->set_global_proxy_object(object); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 96 | Handle<Object> Context::Lookup(Handle<String> name, |
| 97 | ContextLookupFlags flags, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 98 | int* index, |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 99 | PropertyAttributes* attributes, |
| 100 | BindingFlags* binding_flags) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 101 | Isolate* isolate = GetIsolate(); |
| 102 | Handle<Context> context(this, isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 103 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 104 | bool follow_context_chain = (flags & FOLLOW_CONTEXT_CHAIN) != 0; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 105 | *index = -1; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 106 | *attributes = ABSENT; |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 107 | *binding_flags = MISSING_BINDING; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 108 | |
| 109 | if (FLAG_trace_contexts) { |
| 110 | PrintF("Context::Lookup("); |
| 111 | name->ShortPrint(); |
| 112 | PrintF(")\n"); |
| 113 | } |
| 114 | |
| 115 | do { |
| 116 | if (FLAG_trace_contexts) { |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 117 | PrintF(" - looking in context %p", reinterpret_cast<void*>(*context)); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 118 | if (context->IsNativeContext()) PrintF(" (native context)"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 119 | PrintF("\n"); |
| 120 | } |
| 121 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 122 | // 1. Check global objects, subjects of with, and extension objects. |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 123 | if (context->IsNativeContext() || |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 124 | context->IsWithContext() || |
| 125 | (context->IsFunctionContext() && context->has_extension())) { |
| 126 | Handle<JSObject> object(JSObject::cast(context->extension()), isolate); |
| 127 | // Context extension objects needs to behave as if they have no |
| 128 | // prototype. So even if we want to follow prototype chains, we need |
| 129 | // to only do a local lookup for context extension objects. |
| 130 | if ((flags & FOLLOW_PROTOTYPE_CHAIN) == 0 || |
| 131 | object->IsJSContextExtensionObject()) { |
| 132 | *attributes = object->GetLocalPropertyAttribute(*name); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 133 | } else { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 134 | *attributes = object->GetPropertyAttribute(*name); |
| 135 | } |
| 136 | if (*attributes != ABSENT) { |
| 137 | if (FLAG_trace_contexts) { |
| 138 | PrintF("=> found property in context object %p\n", |
| 139 | reinterpret_cast<void*>(*object)); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 140 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 141 | return object; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 145 | // 2. Check the context proper if it has slots. |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 146 | if (context->IsFunctionContext() || context->IsBlockContext()) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 147 | // Use serialized scope information of functions and blocks to search |
| 148 | // for the context index. |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 149 | Handle<ScopeInfo> scope_info; |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 150 | if (context->IsFunctionContext()) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 151 | scope_info = Handle<ScopeInfo>( |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 152 | context->closure()->shared()->scope_info(), isolate); |
| 153 | } else { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 154 | scope_info = Handle<ScopeInfo>( |
| 155 | ScopeInfo::cast(context->extension()), isolate); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 156 | } |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 157 | VariableMode mode; |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 158 | InitializationFlag init_flag; |
| 159 | int slot_index = scope_info->ContextSlotIndex(*name, &mode, &init_flag); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 160 | ASSERT(slot_index < 0 || slot_index >= MIN_CONTEXT_SLOTS); |
| 161 | if (slot_index >= 0) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 162 | if (FLAG_trace_contexts) { |
| 163 | PrintF("=> found local in context slot %d (mode = %d)\n", |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 164 | slot_index, mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 165 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 166 | *index = slot_index; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 167 | // Note: Fixed context slots are statically allocated by the compiler. |
| 168 | // Statically allocated variables always have a statically known mode, |
| 169 | // which is the mode with which they were declared when added to the |
| 170 | // scope. Thus, the DYNAMIC mode (which corresponds to dynamically |
| 171 | // declared variables that were introduced through declaration nodes) |
| 172 | // must not appear here. |
| 173 | switch (mode) { |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 174 | case INTERNAL: // Fall through. |
| 175 | case VAR: |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 176 | *attributes = NONE; |
| 177 | *binding_flags = MUTABLE_IS_INITIALIZED; |
| 178 | break; |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 179 | case LET: |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 180 | *attributes = NONE; |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 181 | *binding_flags = (init_flag == kNeedsInitialization) |
| 182 | ? MUTABLE_CHECK_INITIALIZED : MUTABLE_IS_INITIALIZED; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 183 | break; |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 184 | case CONST: |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 185 | *attributes = READ_ONLY; |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 186 | *binding_flags = (init_flag == kNeedsInitialization) |
| 187 | ? IMMUTABLE_CHECK_INITIALIZED : IMMUTABLE_IS_INITIALIZED; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 188 | break; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 189 | case CONST_HARMONY: |
| 190 | *attributes = READ_ONLY; |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 191 | *binding_flags = (init_flag == kNeedsInitialization) |
| 192 | ? IMMUTABLE_CHECK_INITIALIZED_HARMONY : |
| 193 | IMMUTABLE_IS_INITIALIZED_HARMONY; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 194 | break; |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 195 | case MODULE: |
| 196 | *attributes = READ_ONLY; |
| 197 | *binding_flags = IMMUTABLE_IS_INITIALIZED_HARMONY; |
| 198 | break; |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 199 | case DYNAMIC: |
| 200 | case DYNAMIC_GLOBAL: |
| 201 | case DYNAMIC_LOCAL: |
| 202 | case TEMPORARY: |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 203 | UNREACHABLE(); |
| 204 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 205 | } |
| 206 | return context; |
| 207 | } |
| 208 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 209 | // Check the slot corresponding to the intermediate context holding |
| 210 | // only the function name variable. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 211 | if (follow_context_chain && context->IsFunctionContext()) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 212 | VariableMode mode; |
| 213 | int function_index = scope_info->FunctionContextSlotIndex(*name, &mode); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 214 | if (function_index >= 0) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 215 | if (FLAG_trace_contexts) { |
| 216 | PrintF("=> found intermediate function in context slot %d\n", |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 217 | function_index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 218 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 219 | *index = function_index; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 220 | *attributes = READ_ONLY; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 221 | ASSERT(mode == CONST || mode == CONST_HARMONY); |
| 222 | *binding_flags = (mode == CONST) |
| 223 | ? IMMUTABLE_IS_INITIALIZED : IMMUTABLE_IS_INITIALIZED_HARMONY; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 224 | return context; |
| 225 | } |
| 226 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 227 | |
| 228 | } else if (context->IsCatchContext()) { |
| 229 | // Catch contexts have the variable name in the extension slot. |
| 230 | if (name->Equals(String::cast(context->extension()))) { |
| 231 | if (FLAG_trace_contexts) { |
| 232 | PrintF("=> found in catch context\n"); |
| 233 | } |
| 234 | *index = Context::THROWN_OBJECT_INDEX; |
| 235 | *attributes = NONE; |
| 236 | *binding_flags = MUTABLE_IS_INITIALIZED; |
| 237 | return context; |
| 238 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 239 | } |
| 240 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 241 | // 3. Prepare to continue with the previous (next outermost) context. |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 242 | if (context->IsNativeContext()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 243 | follow_context_chain = false; |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 244 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 245 | context = Handle<Context>(context->previous(), isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 246 | } |
| 247 | } while (follow_context_chain); |
| 248 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 249 | if (FLAG_trace_contexts) { |
| 250 | PrintF("=> no property/slot found\n"); |
| 251 | } |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 252 | return Handle<Object>::null(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 256 | void Context::AddOptimizedFunction(JSFunction* function) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 257 | ASSERT(IsNativeContext()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 258 | #ifdef DEBUG |
svenpanne@chromium.org | 619781a | 2012-07-05 08:22:44 +0000 | [diff] [blame] | 259 | if (FLAG_enable_slow_asserts) { |
| 260 | Object* element = get(OPTIMIZED_FUNCTIONS_LIST); |
| 261 | while (!element->IsUndefined()) { |
| 262 | CHECK(element != function); |
| 263 | element = JSFunction::cast(element)->next_function_link(); |
| 264 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 265 | } |
| 266 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 267 | // Check that the context belongs to the weak native contexts list. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 268 | bool found = false; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 269 | Object* context = GetHeap()->native_contexts_list(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 270 | while (!context->IsUndefined()) { |
| 271 | if (context == this) { |
| 272 | found = true; |
| 273 | break; |
| 274 | } |
| 275 | context = Context::cast(context)->get(Context::NEXT_CONTEXT_LINK); |
| 276 | } |
| 277 | CHECK(found); |
| 278 | #endif |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 279 | |
| 280 | // If the function link field is already used then the function was |
| 281 | // enqueued as a code flushing candidate and we remove it now. |
| 282 | if (!function->next_function_link()->IsUndefined()) { |
| 283 | CodeFlusher* flusher = GetHeap()->mark_compact_collector()->code_flusher(); |
| 284 | flusher->EvictCandidate(function); |
| 285 | } |
| 286 | |
| 287 | ASSERT(function->next_function_link()->IsUndefined()); |
| 288 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 289 | function->set_next_function_link(get(OPTIMIZED_FUNCTIONS_LIST)); |
| 290 | set(OPTIMIZED_FUNCTIONS_LIST, function); |
| 291 | } |
| 292 | |
| 293 | |
| 294 | void Context::RemoveOptimizedFunction(JSFunction* function) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 295 | ASSERT(IsNativeContext()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 296 | Object* element = get(OPTIMIZED_FUNCTIONS_LIST); |
| 297 | JSFunction* prev = NULL; |
| 298 | while (!element->IsUndefined()) { |
| 299 | JSFunction* element_function = JSFunction::cast(element); |
| 300 | ASSERT(element_function->next_function_link()->IsUndefined() || |
| 301 | element_function->next_function_link()->IsJSFunction()); |
| 302 | if (element_function == function) { |
| 303 | if (prev == NULL) { |
| 304 | set(OPTIMIZED_FUNCTIONS_LIST, element_function->next_function_link()); |
| 305 | } else { |
| 306 | prev->set_next_function_link(element_function->next_function_link()); |
| 307 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 308 | element_function->set_next_function_link(GetHeap()->undefined_value()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 309 | return; |
| 310 | } |
| 311 | prev = element_function; |
| 312 | element = element_function->next_function_link(); |
| 313 | } |
| 314 | UNREACHABLE(); |
| 315 | } |
| 316 | |
| 317 | |
| 318 | Object* Context::OptimizedFunctionsListHead() { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 319 | ASSERT(IsNativeContext()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 320 | return get(OPTIMIZED_FUNCTIONS_LIST); |
| 321 | } |
| 322 | |
| 323 | |
| 324 | void Context::ClearOptimizedFunctions() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 325 | set(OPTIMIZED_FUNCTIONS_LIST, GetHeap()->undefined_value()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | |
ulan@chromium.org | 56c14af | 2012-09-20 12:51:09 +0000 | [diff] [blame] | 329 | Handle<Object> Context::ErrorMessageForCodeGenerationFromStrings() { |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 330 | Handle<Object> result(error_message_for_code_gen_from_strings(), |
| 331 | GetIsolate()); |
| 332 | if (!result->IsUndefined()) return result; |
| 333 | return GetIsolate()->factory()->NewStringFromAscii(i::CStrVector( |
| 334 | "Code generation from strings disallowed for this context")); |
ulan@chromium.org | 56c14af | 2012-09-20 12:51:09 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 338 | #ifdef DEBUG |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 339 | bool Context::IsBootstrappingOrValidParentContext( |
| 340 | Object* object, Context* child) { |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 341 | // During bootstrapping we allow all objects to pass as |
| 342 | // contexts. This is necessary to fix circular dependencies. |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 343 | if (child->GetIsolate()->bootstrapper()->IsActive()) return true; |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 344 | if (!object->IsContext()) return false; |
| 345 | Context* context = Context::cast(object); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 346 | return context->IsNativeContext() || context->IsGlobalContext() || |
| 347 | context->IsModuleContext() || !child->IsModuleContext(); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | |
| 351 | bool Context::IsBootstrappingOrGlobalObject(Object* object) { |
| 352 | // During bootstrapping we allow all objects to pass as global |
| 353 | // objects. This is necessary to fix circular dependencies. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 354 | Isolate* isolate = Isolate::Current(); |
| 355 | return isolate->heap()->gc_state() != Heap::NOT_IN_GC || |
| 356 | isolate->bootstrapper()->IsActive() || |
| 357 | object->IsGlobalObject(); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 358 | } |
| 359 | #endif |
| 360 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 361 | } } // namespace v8::internal |