| OLD | NEW |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/v8.h" | 5 #include "src/v8.h" |
| 6 | 6 |
| 7 #include "src/assembler.h" | 7 #include "src/assembler.h" |
| 8 #include "src/compilation-cache.h" | 8 #include "src/compilation-cache.h" |
| 9 #include "src/serialize.h" | 9 #include "src/serialize.h" |
| 10 | 10 |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 | 104 |
| 105 | 105 |
| 106 CompilationCacheScript::CompilationCacheScript(Isolate* isolate, | 106 CompilationCacheScript::CompilationCacheScript(Isolate* isolate, |
| 107 int generations) | 107 int generations) |
| 108 : CompilationSubCache(isolate, generations) {} | 108 : CompilationSubCache(isolate, generations) {} |
| 109 | 109 |
| 110 | 110 |
| 111 // We only re-use a cached function for some script source code if the | 111 // We only re-use a cached function for some script source code if the |
| 112 // script originates from the same place. This is to avoid issues | 112 // script originates from the same place. This is to avoid issues |
| 113 // when reporting errors, etc. | 113 // when reporting errors, etc. |
| 114 bool CompilationCacheScript::HasOrigin( | 114 bool CompilationCacheScript::HasOrigin(Handle<SharedFunctionInfo> function_info, |
| 115 Handle<SharedFunctionInfo> function_info, | 115 Handle<Object> name, int line_offset, |
| 116 Handle<Object> name, | 116 int column_offset, |
| 117 int line_offset, | 117 bool is_embedder_debug_script, |
| 118 int column_offset, | 118 bool is_shared_cross_origin) { |
| 119 bool is_shared_cross_origin) { | |
| 120 Handle<Script> script = | 119 Handle<Script> script = |
| 121 Handle<Script>(Script::cast(function_info->script()), isolate()); | 120 Handle<Script>(Script::cast(function_info->script()), isolate()); |
| 122 // If the script name isn't set, the boilerplate script should have | 121 // If the script name isn't set, the boilerplate script should have |
| 123 // an undefined name to have the same origin. | 122 // an undefined name to have the same origin. |
| 124 if (name.is_null()) { | 123 if (name.is_null()) { |
| 125 return script->name()->IsUndefined(); | 124 return script->name()->IsUndefined(); |
| 126 } | 125 } |
| 127 // Do the fast bailout checks first. | 126 // Do the fast bailout checks first. |
| 128 if (line_offset != script->line_offset()->value()) return false; | 127 if (line_offset != script->line_offset()->value()) return false; |
| 129 if (column_offset != script->column_offset()->value()) return false; | 128 if (column_offset != script->column_offset()->value()) return false; |
| 130 // Check that both names are strings. If not, no match. | 129 // Check that both names are strings. If not, no match. |
| 131 if (!name->IsString() || !script->name()->IsString()) return false; | 130 if (!name->IsString() || !script->name()->IsString()) return false; |
| 131 // Were both scripts tagged by the embedder as being internal script? |
| 132 if (is_embedder_debug_script != script->is_embedder_debug_script()) { |
| 133 return false; |
| 134 } |
| 132 // Were both scripts tagged by the embedder as being shared cross-origin? | 135 // Were both scripts tagged by the embedder as being shared cross-origin? |
| 133 if (is_shared_cross_origin != script->is_shared_cross_origin()) return false; | 136 if (is_shared_cross_origin != script->is_shared_cross_origin()) return false; |
| 134 // Compare the two name strings for equality. | 137 // Compare the two name strings for equality. |
| 135 return String::Equals(Handle<String>::cast(name), | 138 return String::Equals(Handle<String>::cast(name), |
| 136 Handle<String>(String::cast(script->name()))); | 139 Handle<String>(String::cast(script->name()))); |
| 137 } | 140 } |
| 138 | 141 |
| 139 | 142 |
| 140 // TODO(245): Need to allow identical code from different contexts to | 143 // TODO(245): Need to allow identical code from different contexts to |
| 141 // be cached in the same script generation. Currently the first use | 144 // be cached in the same script generation. Currently the first use |
| 142 // will be cached, but subsequent code from different source / line | 145 // will be cached, but subsequent code from different source / line |
| 143 // won't. | 146 // won't. |
| 144 Handle<SharedFunctionInfo> CompilationCacheScript::Lookup( | 147 Handle<SharedFunctionInfo> CompilationCacheScript::Lookup( |
| 145 Handle<String> source, | 148 Handle<String> source, Handle<Object> name, int line_offset, |
| 146 Handle<Object> name, | 149 int column_offset, bool is_embedder_debug_script, |
| 147 int line_offset, | 150 bool is_shared_cross_origin, Handle<Context> context) { |
| 148 int column_offset, | |
| 149 bool is_shared_cross_origin, | |
| 150 Handle<Context> context) { | |
| 151 Object* result = NULL; | 151 Object* result = NULL; |
| 152 int generation; | 152 int generation; |
| 153 | 153 |
| 154 // Probe the script generation tables. Make sure not to leak handles | 154 // Probe the script generation tables. Make sure not to leak handles |
| 155 // into the caller's handle scope. | 155 // into the caller's handle scope. |
| 156 { HandleScope scope(isolate()); | 156 { HandleScope scope(isolate()); |
| 157 for (generation = 0; generation < generations(); generation++) { | 157 for (generation = 0; generation < generations(); generation++) { |
| 158 Handle<CompilationCacheTable> table = GetTable(generation); | 158 Handle<CompilationCacheTable> table = GetTable(generation); |
| 159 Handle<Object> probe = table->Lookup(source, context); | 159 Handle<Object> probe = table->Lookup(source, context); |
| 160 if (probe->IsSharedFunctionInfo()) { | 160 if (probe->IsSharedFunctionInfo()) { |
| 161 Handle<SharedFunctionInfo> function_info = | 161 Handle<SharedFunctionInfo> function_info = |
| 162 Handle<SharedFunctionInfo>::cast(probe); | 162 Handle<SharedFunctionInfo>::cast(probe); |
| 163 // Break when we've found a suitable shared function info that | 163 // Break when we've found a suitable shared function info that |
| 164 // matches the origin. | 164 // matches the origin. |
| 165 if (HasOrigin(function_info, | 165 if (HasOrigin(function_info, name, line_offset, column_offset, |
| 166 name, | 166 is_embedder_debug_script, is_shared_cross_origin)) { |
| 167 line_offset, | |
| 168 column_offset, | |
| 169 is_shared_cross_origin)) { | |
| 170 result = *function_info; | 167 result = *function_info; |
| 171 break; | 168 break; |
| 172 } | 169 } |
| 173 } | 170 } |
| 174 } | 171 } |
| 175 } | 172 } |
| 176 | 173 |
| 177 // Once outside the manacles of the handle scope, we need to recheck | 174 // Once outside the manacles of the handle scope, we need to recheck |
| 178 // to see if we actually found a cached script. If so, we return a | 175 // to see if we actually found a cached script. If so, we return a |
| 179 // handle created in the caller's handle scope. | 176 // handle created in the caller's handle scope. |
| 180 if (result != NULL) { | 177 if (result != NULL) { |
| 181 Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result), | 178 Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result), |
| 182 isolate()); | 179 isolate()); |
| 183 DCHECK(HasOrigin(shared, | 180 DCHECK(HasOrigin(shared, name, line_offset, column_offset, |
| 184 name, | 181 is_embedder_debug_script, is_shared_cross_origin)); |
| 185 line_offset, | |
| 186 column_offset, | |
| 187 is_shared_cross_origin)); | |
| 188 // If the script was found in a later generation, we promote it to | 182 // If the script was found in a later generation, we promote it to |
| 189 // the first generation to let it survive longer in the cache. | 183 // the first generation to let it survive longer in the cache. |
| 190 if (generation != 0) Put(source, context, shared); | 184 if (generation != 0) Put(source, context, shared); |
| 191 isolate()->counters()->compilation_cache_hits()->Increment(); | 185 isolate()->counters()->compilation_cache_hits()->Increment(); |
| 192 return shared; | 186 return shared; |
| 193 } else { | 187 } else { |
| 194 isolate()->counters()->compilation_cache_misses()->Increment(); | 188 isolate()->counters()->compilation_cache_misses()->Increment(); |
| 195 return Handle<SharedFunctionInfo>::null(); | 189 return Handle<SharedFunctionInfo>::null(); |
| 196 } | 190 } |
| 197 } | 191 } |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 void CompilationCache::Remove(Handle<SharedFunctionInfo> function_info) { | 282 void CompilationCache::Remove(Handle<SharedFunctionInfo> function_info) { |
| 289 if (!IsEnabled()) return; | 283 if (!IsEnabled()) return; |
| 290 | 284 |
| 291 eval_global_.Remove(function_info); | 285 eval_global_.Remove(function_info); |
| 292 eval_contextual_.Remove(function_info); | 286 eval_contextual_.Remove(function_info); |
| 293 script_.Remove(function_info); | 287 script_.Remove(function_info); |
| 294 } | 288 } |
| 295 | 289 |
| 296 | 290 |
| 297 MaybeHandle<SharedFunctionInfo> CompilationCache::LookupScript( | 291 MaybeHandle<SharedFunctionInfo> CompilationCache::LookupScript( |
| 298 Handle<String> source, | 292 Handle<String> source, Handle<Object> name, int line_offset, |
| 299 Handle<Object> name, | 293 int column_offset, bool is_embedder_debug_script, |
| 300 int line_offset, | 294 bool is_shared_cross_origin, Handle<Context> context) { |
| 301 int column_offset, | |
| 302 bool is_shared_cross_origin, | |
| 303 Handle<Context> context) { | |
| 304 if (!IsEnabled()) return MaybeHandle<SharedFunctionInfo>(); | 295 if (!IsEnabled()) return MaybeHandle<SharedFunctionInfo>(); |
| 305 | 296 |
| 306 return script_.Lookup(source, name, line_offset, column_offset, | 297 return script_.Lookup(source, name, line_offset, column_offset, |
| 307 is_shared_cross_origin, context); | 298 is_embedder_debug_script, is_shared_cross_origin, |
| 299 context); |
| 308 } | 300 } |
| 309 | 301 |
| 310 | 302 |
| 311 MaybeHandle<SharedFunctionInfo> CompilationCache::LookupEval( | 303 MaybeHandle<SharedFunctionInfo> CompilationCache::LookupEval( |
| 312 Handle<String> source, Handle<SharedFunctionInfo> outer_info, | 304 Handle<String> source, Handle<SharedFunctionInfo> outer_info, |
| 313 Handle<Context> context, StrictMode strict_mode, int scope_position) { | 305 Handle<Context> context, StrictMode strict_mode, int scope_position) { |
| 314 if (!IsEnabled()) return MaybeHandle<SharedFunctionInfo>(); | 306 if (!IsEnabled()) return MaybeHandle<SharedFunctionInfo>(); |
| 315 | 307 |
| 316 MaybeHandle<SharedFunctionInfo> result; | 308 MaybeHandle<SharedFunctionInfo> result; |
| 317 if (context->IsNativeContext()) { | 309 if (context->IsNativeContext()) { |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 } | 397 } |
| 406 | 398 |
| 407 | 399 |
| 408 void CompilationCache::Disable() { | 400 void CompilationCache::Disable() { |
| 409 enabled_ = false; | 401 enabled_ = false; |
| 410 Clear(); | 402 Clear(); |
| 411 } | 403 } |
| 412 | 404 |
| 413 | 405 |
| 414 } } // namespace v8::internal | 406 } } // namespace v8::internal |
| OLD | NEW |