Chromium Code Reviews| Index: Source/bindings/core/v8/V8ScriptRunner.cpp |
| diff --git a/Source/bindings/core/v8/V8ScriptRunner.cpp b/Source/bindings/core/v8/V8ScriptRunner.cpp |
| index 12246ddd8eceb1c03b22a1681e3c07f277291e48..3ab6d4c9b56b9437848d9ad9d6b888df090812bd 100644 |
| --- a/Source/bindings/core/v8/V8ScriptRunner.cpp |
| +++ b/Source/bindings/core/v8/V8ScriptRunner.cpp |
| @@ -110,10 +110,10 @@ v8::Local<v8::Script> compileWithoutOptions(V8CompileHistogram::Cacheability cac |
| } |
| // Compile a script, and consume a V8 cache that was generated previously. |
| -v8::Local<v8::Script> compileAndConsumeCache(ScriptResource* resource, unsigned tag, v8::ScriptCompiler::CompileOptions compileOptions, bool compressed, v8::Isolate* isolate, v8::Handle<v8::String> code, v8::ScriptOrigin origin) |
| +v8::Local<v8::Script> compileAndConsumeCache(CachedMetadataHandler* cacheHandler, unsigned tag, v8::ScriptCompiler::CompileOptions compileOptions, bool compressed, v8::Isolate* isolate, v8::Handle<v8::String> code, v8::ScriptOrigin origin) |
| { |
| V8CompileHistogram histogramScope(V8CompileHistogram::Cacheable); |
| - CachedMetadata* cachedMetadata = resource->cachedMetadata(tag); |
| + CachedMetadata* cachedMetadata = cacheHandler->cachedMetadata(tag); |
| const char* data = cachedMetadata->data(); |
| int length = cachedMetadata->size(); |
| std::string uncompressedOutput; |
| @@ -138,12 +138,12 @@ v8::Local<v8::Script> compileAndConsumeCache(ScriptResource* resource, unsigned |
| invalidCache = cachedData->rejected; |
| } |
| if (invalidCache) |
| - resource->clearCachedMetadata(Resource::SendToPlatform); |
| + cacheHandler->clearCachedMetadata(CachedMetadataHandler::SendToPlatform); |
| return script; |
| } |
| // Compile a script, and produce a V8 cache for future use. |
| -v8::Local<v8::Script> compileAndProduceCache(ScriptResource* resource, unsigned tag, v8::ScriptCompiler::CompileOptions compileOptions, bool compressed, Resource::MetadataCacheType cacheType, v8::Isolate* isolate, v8::Handle<v8::String> code, v8::ScriptOrigin origin) |
| +v8::Local<v8::Script> compileAndProduceCache(CachedMetadataHandler* cacheHandler, unsigned tag, v8::ScriptCompiler::CompileOptions compileOptions, bool compressed, CachedMetadataHandler::CacheType cacheType, v8::Isolate* isolate, v8::Handle<v8::String> code, v8::ScriptOrigin origin) |
| { |
| V8CompileHistogram histogramScope(V8CompileHistogram::Cacheable); |
| v8::ScriptCompiler::Source source(code, origin); |
| @@ -163,19 +163,19 @@ v8::Local<v8::Script> compileAndProduceCache(ScriptResource* resource, unsigned |
| int cacheSizeRatio = static_cast<int>(100.0 * length / code->Length()); |
| blink::Platform::current()->histogramCustomCounts("V8.CodeCacheSizeRatio", cacheSizeRatio, 0, 10000, 50); |
| } |
| - resource->clearCachedMetadata(); |
| - resource->setCachedMetadata(tag, data, length, cacheType); |
| + cacheHandler->clearCachedMetadata(); |
| + cacheHandler->setCachedMetadata(tag, data, length, cacheType); |
| } |
| return script; |
| } |
| // Compile a script, and consume or produce a V8 Cache, depending on whether the |
| // given resource already has cached data available. |
| -v8::Local<v8::Script> compileAndConsumeOrProduce(ScriptResource* resource, unsigned tag, v8::ScriptCompiler::CompileOptions consumeOptions, v8::ScriptCompiler::CompileOptions produceOptions, bool compressed, Resource::MetadataCacheType cacheType, v8::Isolate* isolate, v8::Handle<v8::String> code, v8::ScriptOrigin origin) |
| +v8::Local<v8::Script> compileAndConsumeOrProduce(CachedMetadataHandler* cacheHandler, unsigned tag, v8::ScriptCompiler::CompileOptions consumeOptions, v8::ScriptCompiler::CompileOptions produceOptions, bool compressed, CachedMetadataHandler::CacheType cacheType, v8::Isolate* isolate, v8::Handle<v8::String> code, v8::ScriptOrigin origin) |
| { |
| - return resource->cachedMetadata(tag) |
| - ? compileAndConsumeCache(resource, tag, consumeOptions, compressed, isolate, code, origin) |
| - : compileAndProduceCache(resource, tag, produceOptions, compressed, cacheType, isolate, code, origin); |
| + return cacheHandler->cachedMetadata(tag) |
| + ? compileAndConsumeCache(cacheHandler, tag, consumeOptions, compressed, isolate, code, origin) |
| + : compileAndProduceCache(cacheHandler, tag, produceOptions, compressed, cacheType, isolate, code, origin); |
| } |
| enum CacheTagKind { |
| @@ -188,7 +188,7 @@ enum CacheTagKind { |
| static const int kCacheTagKindSize = 2; |
| -unsigned cacheTag(CacheTagKind kind, Resource* resource) |
| +unsigned cacheTag(CacheTagKind kind, CachedMetadataHandler* cacheHandler) |
| { |
| static_assert((1 << kCacheTagKindSize) >= CacheTagLast, "CacheTagLast must be large enough"); |
| @@ -199,23 +199,23 @@ unsigned cacheTag(CacheTagKind kind, Resource* resource) |
| // about encodings, but the cached data is specific to one encoding. If we |
| // later load the script from the cache and interpret it with a different |
| // encoding, the cached data is not valid for that encoding. |
| - return (v8CacheDataVersion | kind) + StringHash::hash(resource->encoding()); |
| + return (v8CacheDataVersion | kind) + StringHash::hash(cacheHandler->encoding()); |
| } |
| // Store a timestamp to the cache as hint. |
| -void setCacheTimeStamp(ScriptResource* resource) |
| +void setCacheTimeStamp(CachedMetadataHandler* cacheHandler) |
| { |
| double now = WTF::currentTime(); |
| - unsigned tag = cacheTag(CacheTagTimeStamp, resource); |
| - resource->setCachedMetadata(tag, reinterpret_cast<char*>(&now), sizeof(now), Resource::SendToPlatform); |
| + unsigned tag = cacheTag(CacheTagTimeStamp, cacheHandler); |
| + cacheHandler->setCachedMetadata(tag, reinterpret_cast<char*>(&now), sizeof(now), CachedMetadataHandler::SendToPlatform); |
| } |
| // Check previously stored timestamp. |
| -bool isResourceHotForCaching(ScriptResource* resource, int hotHours) |
| +bool isResourceHotForCaching(CachedMetadataHandler* cacheHandler, int hotHours) |
| { |
| const double kCacheWithinSeconds = hotHours * 60 * 60; |
| - unsigned tag = cacheTag(CacheTagTimeStamp, resource); |
| - CachedMetadata* cachedMetadata = resource->cachedMetadata(tag); |
| + unsigned tag = cacheTag(CacheTagTimeStamp, cacheHandler); |
| + CachedMetadata* cachedMetadata = cacheHandler->cachedMetadata(tag); |
| if (!cachedMetadata) |
| return false; |
| double timeStamp; |
| @@ -227,23 +227,26 @@ bool isResourceHotForCaching(ScriptResource* resource, int hotHours) |
| // Final compile call for a streamed compilation. Most decisions have already |
| // been made, but we need to write back data into the cache. |
| -v8::Local<v8::Script> postStreamCompile(ScriptResource* resource, ScriptStreamer* streamer, v8::Isolate* isolate, v8::Handle<v8::String> code, v8::ScriptOrigin origin) |
| +v8::Local<v8::Script> postStreamCompile(CachedMetadataHandler* cacheHandler, ScriptStreamer* streamer, v8::Isolate* isolate, v8::Handle<v8::String> code, v8::ScriptOrigin origin) |
| { |
| V8CompileHistogram histogramScope(V8CompileHistogram::Noncacheable); |
| v8::Local<v8::Script> script = v8::ScriptCompiler::Compile(isolate, streamer->source(), code, origin); |
| + if (!cacheHandler) |
| + return script; |
| + |
| // Whether to produce the cached data or not is decided when the |
| // streamer is started. Here we only need to get the data out. |
| const v8::ScriptCompiler::CachedData* newCachedData = streamer->source()->GetCachedData(); |
| if (newCachedData) { |
| - resource->clearCachedMetadata(); |
| + cacheHandler->clearCachedMetadata(); |
| v8::ScriptCompiler::CompileOptions options = streamer->compileOptions(); |
| switch (options) { |
| case v8::ScriptCompiler::kProduceParserCache: |
| - resource->setCachedMetadata(cacheTag(CacheTagParser, resource), reinterpret_cast<const char*>(newCachedData->data), newCachedData->length, Resource::CacheLocally); |
| + cacheHandler->setCachedMetadata(cacheTag(CacheTagParser, cacheHandler), reinterpret_cast<const char*>(newCachedData->data), newCachedData->length, CachedMetadataHandler::CacheLocally); |
| break; |
| case v8::ScriptCompiler::kProduceCodeCache: |
| - resource->setCachedMetadata(cacheTag(CacheTagCode, resource), reinterpret_cast<const char*>(newCachedData->data), newCachedData->length, Resource::SendToPlatform); |
| + cacheHandler->setCachedMetadata(cacheTag(CacheTagCode, cacheHandler), reinterpret_cast<const char*>(newCachedData->data), newCachedData->length, CachedMetadataHandler::SendToPlatform); |
| break; |
| default: |
| break; |
| @@ -269,11 +272,11 @@ PassOwnPtr<CompileFn> bind(const A&... args) |
| // Select a compile function from any of the above, mainly depending on |
| // cacheOptions. |
| -PassOwnPtr<CompileFn> selectCompileFunction(V8CacheOptions cacheOptions, ScriptResource* resource, v8::Handle<v8::String> code) |
| +PassOwnPtr<CompileFn> selectCompileFunction(V8CacheOptions cacheOptions, CachedMetadataHandler* cacheHandler, v8::Handle<v8::String> code) |
| { |
| static const int minimalCodeLength = 1024; |
| - if (!resource || !resource->url().protocolIsInHTTPFamily()) |
| + if (!cacheHandler) |
| // Caching is not available in this case. |
| return bind(compileWithoutOptions, V8CompileHistogram::Noncacheable); |
| @@ -290,24 +293,24 @@ PassOwnPtr<CompileFn> selectCompileFunction(V8CacheOptions cacheOptions, ScriptR |
| return bind(compileWithoutOptions, V8CompileHistogram::Cacheable); |
| } |
| // Use parser-cache; in-memory only. |
| - return bind(compileAndConsumeOrProduce, resource, cacheTag(CacheTagParser, resource), v8::ScriptCompiler::kConsumeParserCache, v8::ScriptCompiler::kProduceParserCache, false, Resource::CacheLocally); |
| + return bind(compileAndConsumeOrProduce, cacheHandler, cacheTag(CacheTagParser, cacheHandler), v8::ScriptCompiler::kConsumeParserCache, v8::ScriptCompiler::kProduceParserCache, false, CachedMetadataHandler::CacheLocally); |
| break; |
| case V8CacheOptionsParse: |
| // Use parser-cache. |
| - return bind(compileAndConsumeOrProduce, resource, cacheTag(CacheTagParser, resource), v8::ScriptCompiler::kConsumeParserCache, v8::ScriptCompiler::kProduceParserCache, false, Resource::SendToPlatform); |
| + return bind(compileAndConsumeOrProduce, cacheHandler, cacheTag(CacheTagParser, cacheHandler), v8::ScriptCompiler::kConsumeParserCache, v8::ScriptCompiler::kProduceParserCache, false, CachedMetadataHandler::SendToPlatform); |
| break; |
| case V8CacheOptionsHeuristicsDefault: |
| case V8CacheOptionsCode: |
| // Always use code caching. |
| - return bind(compileAndConsumeOrProduce, resource, cacheTag(CacheTagCode, resource), v8::ScriptCompiler::kConsumeCodeCache, v8::ScriptCompiler::kProduceCodeCache, false, Resource::SendToPlatform); |
| + return bind(compileAndConsumeOrProduce, cacheHandler, cacheTag(CacheTagCode, cacheHandler), v8::ScriptCompiler::kConsumeCodeCache, v8::ScriptCompiler::kProduceCodeCache, false, CachedMetadataHandler::SendToPlatform); |
| break; |
| case V8CacheOptionsHeuristicsDefaultMobile: |
| case V8CacheOptionsCodeCompressed: |
| // Always use code caching. Compress depending on cacheOptions. |
| - return bind(compileAndConsumeOrProduce, resource, cacheTag(CacheTagCodeCompressed, resource), v8::ScriptCompiler::kConsumeCodeCache, v8::ScriptCompiler::kProduceCodeCache, true, Resource::SendToPlatform); |
| + return bind(compileAndConsumeOrProduce, cacheHandler, cacheTag(CacheTagCodeCompressed, cacheHandler), v8::ScriptCompiler::kConsumeCodeCache, v8::ScriptCompiler::kProduceCodeCache, true, CachedMetadataHandler::SendToPlatform); |
| break; |
| case V8CacheOptionsHeuristics: |
| @@ -315,16 +318,16 @@ PassOwnPtr<CompileFn> selectCompileFunction(V8CacheOptions cacheOptions, ScriptR |
| case V8CacheOptionsRecent: |
| case V8CacheOptionsRecentSmall: { |
| bool compress = (cacheOptions == V8CacheOptionsRecentSmall || cacheOptions == V8CacheOptionsHeuristicsMobile); |
| - unsigned codeCacheTag = cacheTag(compress ? CacheTagCodeCompressed : CacheTagCode, resource); |
| - CachedMetadata* codeCache = resource->cachedMetadata(codeCacheTag); |
| + unsigned codeCacheTag = cacheTag(compress ? CacheTagCodeCompressed : CacheTagCode, cacheHandler); |
| + CachedMetadata* codeCache = cacheHandler->cachedMetadata(codeCacheTag); |
| if (codeCache) |
| - return bind(compileAndConsumeCache, resource, codeCacheTag, v8::ScriptCompiler::kConsumeCodeCache, compress); |
| + return bind(compileAndConsumeCache, cacheHandler, codeCacheTag, v8::ScriptCompiler::kConsumeCodeCache, compress); |
| int hotHours = (cacheOptions == V8CacheOptionsRecent || cacheOptions == V8CacheOptionsRecentSmall) ? 36 : 72; |
| - if (!isResourceHotForCaching(resource, hotHours)) { |
| - setCacheTimeStamp(resource); |
| + if (!isResourceHotForCaching(cacheHandler, hotHours)) { |
| + setCacheTimeStamp(cacheHandler); |
| return bind(compileWithoutOptions, V8CompileHistogram::Cacheable); |
| } |
| - return bind(compileAndProduceCache, resource, codeCacheTag, v8::ScriptCompiler::kProduceCodeCache, compress, Resource::SendToPlatform); |
| + return bind(compileAndProduceCache, cacheHandler, codeCacheTag, v8::ScriptCompiler::kProduceCodeCache, compress, CachedMetadataHandler::SendToPlatform); |
| break; |
| } |
| @@ -350,19 +353,21 @@ PassOwnPtr<CompileFn> selectCompileFunction(ScriptResource* resource, ScriptStre |
| ASSERT(!resource->errorOccurred()); |
| ASSERT(streamer->isFinished()); |
| ASSERT(!streamer->streamingSuppressed()); |
| - return WTF::bind<v8::Isolate*, v8::Handle<v8::String>, v8::ScriptOrigin>(postStreamCompile, resource, streamer); |
| + return WTF::bind<v8::Isolate*, v8::Handle<v8::String>, v8::ScriptOrigin>(postStreamCompile, resource->cacheHandler(), streamer); |
| } |
| } // namespace |
| v8::Local<v8::Script> V8ScriptRunner::compileScript(const ScriptSourceCode& source, v8::Isolate* isolate, AccessControlStatus corsStatus, V8CacheOptions cacheOptions) |
| { |
| - return compileScript(v8String(isolate, source.source()), source.url(), source.startPosition(), source.resource(), source.streamer(), isolate, corsStatus, cacheOptions); |
| + return compileScript(v8String(isolate, source.source()), source.url(), source.startPosition(), isolate, source.resource(), source.streamer(), source.resource() ? source.resource()->cacheHandler() : nullptr, corsStatus, cacheOptions); |
| } |
| -v8::Local<v8::Script> V8ScriptRunner::compileScript(v8::Handle<v8::String> code, const String& fileName, const TextPosition& scriptStartPosition, ScriptResource* resource, ScriptStreamer* streamer, v8::Isolate* isolate, AccessControlStatus corsStatus, V8CacheOptions cacheOptions, bool isInternalScript) |
| +v8::Local<v8::Script> V8ScriptRunner::compileScript(v8::Handle<v8::String> code, const String& fileName, const TextPosition& scriptStartPosition, v8::Isolate* isolate, ScriptResource* resource, ScriptStreamer* streamer, CachedMetadataHandler* cacheHandler, AccessControlStatus corsStatus, V8CacheOptions cacheOptions, bool isInternalScript) |
| { |
| TRACE_EVENT1("v8", "v8.compile", "fileName", fileName.utf8()); |
| TRACE_EVENT_SCOPED_SAMPLING_STATE("v8", "V8Compile"); |
| + // When streamer is set, resource must be set. |
|
marja
2015/02/17 12:45:59
This comment is unnecessary though :)
horo
2015/02/17 12:53:42
Done.
|
| + ASSERT(!streamer || resource); |
|
marja
2015/02/17 12:45:59
I'd add here an ASSERT which says the if there is
horo
2015/02/17 12:53:43
Done.
|
| // NOTE: For compatibility with WebCore, ScriptSourceCode's line starts at |
| // 1, whereas v8 starts at 0. |
| @@ -376,7 +381,7 @@ v8::Local<v8::Script> V8ScriptRunner::compileScript(v8::Handle<v8::String> code, |
| OwnPtr<CompileFn> compileFn = streamer |
| ? selectCompileFunction(resource, streamer) |
| - : selectCompileFunction(cacheOptions, resource, code); |
| + : selectCompileFunction(cacheOptions, cacheHandler, code); |
| return (*compileFn)(isolate, code, origin); |
| } |
| @@ -411,7 +416,7 @@ v8::Local<v8::Value> V8ScriptRunner::runCompiledScript(v8::Isolate* isolate, v8: |
| v8::Local<v8::Value> V8ScriptRunner::compileAndRunInternalScript(v8::Handle<v8::String> source, v8::Isolate* isolate, const String& fileName, const TextPosition& scriptStartPosition) |
| { |
| - v8::Handle<v8::Script> script = V8ScriptRunner::compileScript(source, fileName, scriptStartPosition, 0, 0, isolate, SharableCrossOrigin, V8CacheOptionsDefault, true); |
| + v8::Handle<v8::Script> script = V8ScriptRunner::compileScript(source, fileName, scriptStartPosition, isolate, nullptr, nullptr, nullptr, SharableCrossOrigin, V8CacheOptionsDefault, true); |
| if (script.IsEmpty()) |
| return v8::Local<v8::Value>(); |
| @@ -506,14 +511,14 @@ v8::Local<v8::Object> V8ScriptRunner::instantiateObjectInDocument(v8::Isolate* i |
| return result; |
| } |
| -unsigned V8ScriptRunner::tagForParserCache(Resource* resource) |
| +unsigned V8ScriptRunner::tagForParserCache(CachedMetadataHandler* cacheHandler) |
| { |
| - return cacheTag(CacheTagParser, resource); |
| + return cacheTag(CacheTagParser, cacheHandler); |
| } |
| -unsigned V8ScriptRunner::tagForCodeCache(Resource* resource) |
| +unsigned V8ScriptRunner::tagForCodeCache(CachedMetadataHandler* cacheHandler) |
| { |
| - return cacheTag(CacheTagCode, resource); |
| + return cacheTag(CacheTagCode, cacheHandler); |
| } |
| } // namespace blink |