blob: 28e833a4933e1457a5f08ac8132338aef82c7938 [file] [log] [blame]
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001// Copyright 2008 the V8 project authors. All rights reserved.
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
30#include "compilation-cache.h"
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +000031#include "serialize.h"
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000032
kasperl@chromium.org71affb52009-05-26 05:44:31 +000033namespace v8 {
34namespace internal {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000035
ager@chromium.org5aa501c2009-06-23 07:57:28 +000036
37// The number of generations for each sub cache.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +000038// The number of ScriptGenerations is carefully chosen based on histograms.
39// See issue 458: http://code.google.com/p/v8/issues/detail?id=458
ager@chromium.org5aa501c2009-06-23 07:57:28 +000040static const int kScriptGenerations = 5;
41static const int kEvalGlobalGenerations = 2;
42static const int kEvalContextualGenerations = 2;
43static const int kRegExpGenerations = 2;
44
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +000045// Initial size of each compilation cache table allocated.
ager@chromium.org5aa501c2009-06-23 07:57:28 +000046static const int kInitialCacheSize = 64;
47
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000048
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000049CompilationCache::CompilationCache(Isolate* isolate)
50 : isolate_(isolate),
51 script_(isolate, kScriptGenerations),
52 eval_global_(isolate, kEvalGlobalGenerations),
53 eval_contextual_(isolate, kEvalContextualGenerations),
54 reg_exp_(isolate, kRegExpGenerations),
ricow@chromium.org4f693d62011-07-04 14:01:31 +000055 enabled_(true) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000056 CompilationSubCache* subcaches[kSubCacheCount] =
57 {&script_, &eval_global_, &eval_contextual_, &reg_exp_};
58 for (int i = 0; i < kSubCacheCount; ++i) {
59 subcaches_[i] = subcaches[i];
ager@chromium.org5aa501c2009-06-23 07:57:28 +000060 }
kasperl@chromium.org71affb52009-05-26 05:44:31 +000061}
62
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000063
ricow@chromium.org4f693d62011-07-04 14:01:31 +000064CompilationCache::~CompilationCache() {}
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000065
66
67static Handle<CompilationCacheTable> AllocateTable(Isolate* isolate, int size) {
68 CALL_HEAP_FUNCTION(isolate,
69 CompilationCacheTable::Allocate(size),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000070 CompilationCacheTable);
71}
72
73
ager@chromium.org5aa501c2009-06-23 07:57:28 +000074Handle<CompilationCacheTable> CompilationSubCache::GetTable(int generation) {
75 ASSERT(generation < generations_);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000076 Handle<CompilationCacheTable> result;
ager@chromium.org5aa501c2009-06-23 07:57:28 +000077 if (tables_[generation]->IsUndefined()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000078 result = AllocateTable(isolate(), kInitialCacheSize);
ager@chromium.org5aa501c2009-06-23 07:57:28 +000079 tables_[generation] = *result;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000080 } else {
ager@chromium.org5aa501c2009-06-23 07:57:28 +000081 CompilationCacheTable* table =
82 CompilationCacheTable::cast(tables_[generation]);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000083 result = Handle<CompilationCacheTable>(table, isolate());
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000084 }
85 return result;
86}
87
ager@chromium.org5aa501c2009-06-23 07:57:28 +000088void CompilationSubCache::Age() {
89 // Age the generations implicitly killing off the oldest.
90 for (int i = generations_ - 1; i > 0; i--) {
91 tables_[i] = tables_[i - 1];
ager@chromium.org381abbb2009-02-25 13:23:22 +000092 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +000093
94 // Set the first generation as unborn.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000095 tables_[0] = isolate()->heap()->undefined_value();
ager@chromium.org381abbb2009-02-25 13:23:22 +000096}
97
98
ricow@chromium.org0b9f8502010-08-18 07:45:01 +000099void CompilationSubCache::IterateFunctions(ObjectVisitor* v) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000100 Object* undefined = isolate()->heap()->raw_unchecked_undefined_value();
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000101 for (int i = 0; i < generations_; i++) {
102 if (tables_[i] != undefined) {
103 reinterpret_cast<CompilationCacheTable*>(tables_[i])->IterateElements(v);
104 }
105 }
106}
107
108
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000109void CompilationSubCache::Iterate(ObjectVisitor* v) {
110 v->VisitPointers(&tables_[0], &tables_[generations_]);
111}
112
113
114void CompilationSubCache::Clear() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000115 MemsetPointer(tables_, isolate()->heap()->undefined_value(), generations_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000116}
117
118
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000119void CompilationSubCache::Remove(Handle<SharedFunctionInfo> function_info) {
120 // Probe the script generation tables. Make sure not to leak handles
121 // into the caller's handle scope.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000122 { HandleScope scope(isolate());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000123 for (int generation = 0; generation < generations(); generation++) {
124 Handle<CompilationCacheTable> table = GetTable(generation);
125 table->Remove(*function_info);
126 }
127 }
128}
129
130
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000131CompilationCacheScript::CompilationCacheScript(Isolate* isolate,
132 int generations)
133 : CompilationSubCache(isolate, generations),
134 script_histogram_(NULL),
135 script_histogram_initialized_(false) { }
136
137
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +0000138// We only re-use a cached function for some script source code if the
139// script originates from the same place. This is to avoid issues
140// when reporting errors, etc.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000141bool CompilationCacheScript::HasOrigin(
142 Handle<SharedFunctionInfo> function_info,
143 Handle<Object> name,
144 int line_offset,
145 int column_offset) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +0000146 Handle<Script> script =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000147 Handle<Script>(Script::cast(function_info->script()), isolate());
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +0000148 // If the script name isn't set, the boilerplate script should have
149 // an undefined name to have the same origin.
150 if (name.is_null()) {
151 return script->name()->IsUndefined();
152 }
153 // Do the fast bailout checks first.
154 if (line_offset != script->line_offset()->value()) return false;
155 if (column_offset != script->column_offset()->value()) return false;
156 // Check that both names are strings. If not, no match.
157 if (!name->IsString() || !script->name()->IsString()) return false;
158 // Compare the two name strings for equality.
159 return String::cast(*name)->Equals(String::cast(script->name()));
160}
161
162
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000163// TODO(245): Need to allow identical code from different contexts to
164// be cached in the same script generation. Currently the first use
165// will be cached, but subsequent code from different source / line
166// won't.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000167Handle<SharedFunctionInfo> CompilationCacheScript::Lookup(Handle<String> source,
168 Handle<Object> name,
169 int line_offset,
170 int column_offset) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000171 Object* result = NULL;
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000172 int generation;
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000173
174 // Probe the script generation tables. Make sure not to leak handles
175 // into the caller's handle scope.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000176 { HandleScope scope(isolate());
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000177 for (generation = 0; generation < generations(); generation++) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000178 Handle<CompilationCacheTable> table = GetTable(generation);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000179 Handle<Object> probe(table->Lookup(*source), isolate());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000180 if (probe->IsSharedFunctionInfo()) {
181 Handle<SharedFunctionInfo> function_info =
182 Handle<SharedFunctionInfo>::cast(probe);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000183 // Break when we've found a suitable shared function info that
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000184 // matches the origin.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000185 if (HasOrigin(function_info, name, line_offset, column_offset)) {
186 result = *function_info;
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000187 break;
188 }
189 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000190 }
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +0000191 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000192
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000193 if (!script_histogram_initialized_) {
194 script_histogram_ = isolate()->stats_table()->CreateHistogram(
195 "V8.ScriptCache",
196 0,
197 kScriptGenerations,
198 kScriptGenerations + 1);
199 script_histogram_initialized_ = true;
200 }
kasperl@chromium.org31ca8882009-05-26 09:36:02 +0000201
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000202 if (script_histogram_ != NULL) {
kasperl@chromium.org31ca8882009-05-26 09:36:02 +0000203 // The level NUMBER_OF_SCRIPT_GENERATIONS is equivalent to a cache miss.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000204 isolate()->stats_table()->AddHistogramSample(script_histogram_, generation);
kasperl@chromium.org31ca8882009-05-26 09:36:02 +0000205 }
206
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000207 // Once outside the manacles of the handle scope, we need to recheck
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000208 // to see if we actually found a cached script. If so, we return a
209 // handle created in the caller's handle scope.
210 if (result != NULL) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000211 Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result),
212 isolate());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000213 ASSERT(HasOrigin(shared, name, line_offset, column_offset));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000214 // If the script was found in a later generation, we promote it to
215 // the first generation to let it survive longer in the cache.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000216 if (generation != 0) Put(source, shared);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000217 isolate()->counters()->compilation_cache_hits()->Increment();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000218 return shared;
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000219 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000220 isolate()->counters()->compilation_cache_misses()->Increment();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000221 return Handle<SharedFunctionInfo>::null();
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000222 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000223}
224
225
lrn@chromium.org303ada72010-10-27 09:33:13 +0000226MaybeObject* CompilationCacheScript::TryTablePut(
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000227 Handle<String> source,
228 Handle<SharedFunctionInfo> function_info) {
229 Handle<CompilationCacheTable> table = GetFirstTable();
230 return table->Put(*source, *function_info);
231}
232
233
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000234Handle<CompilationCacheTable> CompilationCacheScript::TablePut(
235 Handle<String> source,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000236 Handle<SharedFunctionInfo> function_info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000237 CALL_HEAP_FUNCTION(isolate(),
238 TryTablePut(source, function_info),
239 CompilationCacheTable);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000240}
241
242
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000243void CompilationCacheScript::Put(Handle<String> source,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000244 Handle<SharedFunctionInfo> function_info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000245 HandleScope scope(isolate());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000246 SetFirstTable(TablePut(source, function_info));
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000247}
248
249
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000250Handle<SharedFunctionInfo> CompilationCacheEval::Lookup(
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000251 Handle<String> source,
252 Handle<Context> context,
253 StrictModeFlag strict_mode) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000254 // Make sure not to leak the table into the surrounding handle
255 // scope. Otherwise, we risk keeping old tables around even after
256 // having cleared the cache.
257 Object* result = NULL;
258 int generation;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000259 { HandleScope scope(isolate());
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000260 for (generation = 0; generation < generations(); generation++) {
261 Handle<CompilationCacheTable> table = GetTable(generation);
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000262 result = table->LookupEval(*source, *context, strict_mode);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000263 if (result->IsSharedFunctionInfo()) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000264 break;
265 }
266 }
267 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000268 if (result->IsSharedFunctionInfo()) {
269 Handle<SharedFunctionInfo>
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000270 function_info(SharedFunctionInfo::cast(result), isolate());
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000271 if (generation != 0) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000272 Put(source, context, function_info);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000273 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000274 isolate()->counters()->compilation_cache_hits()->Increment();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000275 return function_info;
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000276 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000277 isolate()->counters()->compilation_cache_misses()->Increment();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000278 return Handle<SharedFunctionInfo>::null();
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000279 }
280}
281
282
lrn@chromium.org303ada72010-10-27 09:33:13 +0000283MaybeObject* CompilationCacheEval::TryTablePut(
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000284 Handle<String> source,
285 Handle<Context> context,
286 Handle<SharedFunctionInfo> function_info) {
287 Handle<CompilationCacheTable> table = GetFirstTable();
288 return table->PutEval(*source, *context, *function_info);
289}
290
291
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000292Handle<CompilationCacheTable> CompilationCacheEval::TablePut(
293 Handle<String> source,
294 Handle<Context> context,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000295 Handle<SharedFunctionInfo> function_info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000296 CALL_HEAP_FUNCTION(isolate(),
297 TryTablePut(source, context, function_info),
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000298 CompilationCacheTable);
299}
300
301
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000302void CompilationCacheEval::Put(Handle<String> source,
303 Handle<Context> context,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000304 Handle<SharedFunctionInfo> function_info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000305 HandleScope scope(isolate());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000306 SetFirstTable(TablePut(source, context, function_info));
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000307}
308
309
310Handle<FixedArray> CompilationCacheRegExp::Lookup(Handle<String> source,
311 JSRegExp::Flags flags) {
312 // Make sure not to leak the table into the surrounding handle
313 // scope. Otherwise, we risk keeping old tables around even after
314 // having cleared the cache.
315 Object* result = NULL;
316 int generation;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000317 { HandleScope scope(isolate());
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000318 for (generation = 0; generation < generations(); generation++) {
319 Handle<CompilationCacheTable> table = GetTable(generation);
320 result = table->LookupRegExp(*source, flags);
321 if (result->IsFixedArray()) {
322 break;
323 }
324 }
325 }
326 if (result->IsFixedArray()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000327 Handle<FixedArray> data(FixedArray::cast(result), isolate());
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000328 if (generation != 0) {
329 Put(source, flags, data);
330 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000331 isolate()->counters()->compilation_cache_hits()->Increment();
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000332 return data;
333 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000334 isolate()->counters()->compilation_cache_misses()->Increment();
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000335 return Handle<FixedArray>::null();
336 }
337}
338
339
lrn@chromium.org303ada72010-10-27 09:33:13 +0000340MaybeObject* CompilationCacheRegExp::TryTablePut(
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000341 Handle<String> source,
342 JSRegExp::Flags flags,
343 Handle<FixedArray> data) {
344 Handle<CompilationCacheTable> table = GetFirstTable();
345 return table->PutRegExp(*source, flags, *data);
346}
347
348
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000349Handle<CompilationCacheTable> CompilationCacheRegExp::TablePut(
350 Handle<String> source,
351 JSRegExp::Flags flags,
352 Handle<FixedArray> data) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000353 CALL_HEAP_FUNCTION(isolate(),
354 TryTablePut(source, flags, data),
355 CompilationCacheTable);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000356}
357
358
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000359void CompilationCacheRegExp::Put(Handle<String> source,
360 JSRegExp::Flags flags,
361 Handle<FixedArray> data) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000362 HandleScope scope(isolate());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000363 SetFirstTable(TablePut(source, flags, data));
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000364}
365
366
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000367void CompilationCache::Remove(Handle<SharedFunctionInfo> function_info) {
368 if (!IsEnabled()) return;
369
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000370 eval_global_.Remove(function_info);
371 eval_contextual_.Remove(function_info);
372 script_.Remove(function_info);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000373}
374
375
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000376Handle<SharedFunctionInfo> CompilationCache::LookupScript(Handle<String> source,
377 Handle<Object> name,
378 int line_offset,
379 int column_offset) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000380 if (!IsEnabled()) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000381 return Handle<SharedFunctionInfo>::null();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000382 }
383
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000384 return script_.Lookup(source, name, line_offset, column_offset);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000385}
386
387
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000388Handle<SharedFunctionInfo> CompilationCache::LookupEval(
389 Handle<String> source,
390 Handle<Context> context,
391 bool is_global,
392 StrictModeFlag strict_mode) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000393 if (!IsEnabled()) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000394 return Handle<SharedFunctionInfo>::null();
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000395 }
396
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000397 Handle<SharedFunctionInfo> result;
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000398 if (is_global) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000399 result = eval_global_.Lookup(source, context, strict_mode);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000400 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000401 result = eval_contextual_.Lookup(source, context, strict_mode);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000402 }
403 return result;
404}
405
406
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000407Handle<FixedArray> CompilationCache::LookupRegExp(Handle<String> source,
408 JSRegExp::Flags flags) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000409 if (!IsEnabled()) {
410 return Handle<FixedArray>::null();
411 }
412
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000413 return reg_exp_.Lookup(source, flags);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000414}
415
416
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000417void CompilationCache::PutScript(Handle<String> source,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000418 Handle<SharedFunctionInfo> function_info) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000419 if (!IsEnabled()) {
420 return;
421 }
422
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000423 script_.Put(source, function_info);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000424}
425
426
427void CompilationCache::PutEval(Handle<String> source,
428 Handle<Context> context,
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000429 bool is_global,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000430 Handle<SharedFunctionInfo> function_info) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000431 if (!IsEnabled()) {
432 return;
433 }
434
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000435 HandleScope scope(isolate());
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000436 if (is_global) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000437 eval_global_.Put(source, context, function_info);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000438 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000439 eval_contextual_.Put(source, context, function_info);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000440 }
ager@chromium.org381abbb2009-02-25 13:23:22 +0000441}
442
443
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000444
445void CompilationCache::PutRegExp(Handle<String> source,
446 JSRegExp::Flags flags,
447 Handle<FixedArray> data) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000448 if (!IsEnabled()) {
449 return;
450 }
451
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000452 reg_exp_.Put(source, flags, data);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000453}
454
455
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000456void CompilationCache::Clear() {
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000457 for (int i = 0; i < kSubCacheCount; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000458 subcaches_[i]->Clear();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000459 }
460}
461
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000462
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000463void CompilationCache::Iterate(ObjectVisitor* v) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000464 for (int i = 0; i < kSubCacheCount; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000465 subcaches_[i]->Iterate(v);
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000466 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000467}
468
469
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000470void CompilationCache::IterateFunctions(ObjectVisitor* v) {
471 for (int i = 0; i < kSubCacheCount; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000472 subcaches_[i]->IterateFunctions(v);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000473 }
474}
475
476
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000477void CompilationCache::MarkCompactPrologue() {
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000478 for (int i = 0; i < kSubCacheCount; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000479 subcaches_[i]->Age();
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000480 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000481}
482
483
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000484void CompilationCache::Enable() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000485 enabled_ = true;
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000486}
487
488
489void CompilationCache::Disable() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000490 enabled_ = false;
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000491 Clear();
492}
493
494
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000495} } // namespace v8::internal