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 71a86becbd9539d4911793591189016ed0bade6c..33556a66d9da0bcb13366de81398da8dc637cbaf 100644 |
| --- a/Source/bindings/core/v8/V8ScriptRunner.cpp |
| +++ b/Source/bindings/core/v8/V8ScriptRunner.cpp |
| @@ -38,6 +38,8 @@ |
| #include "platform/ScriptForbiddenScope.h" |
| #include "platform/TraceEvent.h" |
| +#include <snappy.h> |
| + |
| namespace blink { |
| namespace { |
| @@ -64,29 +66,45 @@ v8::Local<v8::Value> throwStackOverflowExceptionIfNeeded(v8::Isolate* isolate) |
| return result; |
| } |
| -v8::Local<v8::Script> compileAndProduceCache(v8::Isolate* isolate, v8::Handle<v8::String> code, v8::ScriptOrigin origin, ScriptResource* resource, v8::ScriptCompiler::CompileOptions options, unsigned cacheTag, Resource::MetadataCacheType cacheType) |
| +v8::Local<v8::Script> compileAndProduceCache(v8::Isolate* isolate, v8::Handle<v8::String> code, v8::ScriptOrigin origin, ScriptResource* resource, v8::ScriptCompiler::CompileOptions options, unsigned cacheTag, Resource::MetadataCacheType cacheType, bool compressed) |
| { |
| v8::ScriptCompiler::Source source(code, origin); |
| v8::Local<v8::Script> script = v8::ScriptCompiler::Compile(isolate, &source, options); |
| const v8::ScriptCompiler::CachedData* cachedData = source.GetCachedData(); |
| if (resource && cachedData) { |
| + const char* data = reinterpret_cast<const char*>(cachedData->data); |
| + int length = cachedData->length; |
| + std::string compressedOutput; |
| + if (compressed) { |
| + snappy::Compress(data, length, &compressedOutput); |
| + data = compressedOutput.data(); |
| + length = compressedOutput.length(); |
| + } |
| resource->clearCachedMetadata(); |
| resource->setCachedMetadata( |
| cacheTag, |
| - reinterpret_cast<const char*>(cachedData->data), |
| - cachedData->length, |
| + data, |
| + length, |
| cacheType); |
| } |
| return script; |
| } |
| -v8::Local<v8::Script> compileAndConsumeCache(v8::Isolate* isolate, v8::Handle<v8::String> code, v8::ScriptOrigin origin, ScriptResource* resource, v8::ScriptCompiler::CompileOptions options, unsigned cacheTag) |
| +v8::Local<v8::Script> compileAndConsumeCache(v8::Isolate* isolate, v8::Handle<v8::String> code, v8::ScriptOrigin origin, ScriptResource* resource, v8::ScriptCompiler::CompileOptions options, unsigned cacheTag, bool compressed) |
| { |
| // Consume existing cache data: |
| CachedMetadata* cachedMetadata = resource->cachedMetadata(cacheTag); |
|
marja
2014/11/12 14:11:43
Actually... doesn't this break... so if the user f
vogelheim
2014/11/12 18:53:52
This indeed kinda breaks this. As I'm about to cha
|
| + const char* data = cachedMetadata->data(); |
| + int length = cachedMetadata->size(); |
| + std::string uncompressedOutput; |
| + if (compressed) { |
| + snappy::Uncompress(data, length, &uncompressedOutput); |
| + data = uncompressedOutput.data(); |
| + length = uncompressedOutput.length(); |
| + } |
| v8::ScriptCompiler::CachedData* cachedData = new v8::ScriptCompiler::CachedData( |
| - reinterpret_cast<const uint8_t*>(cachedMetadata->data()), |
| - cachedMetadata->size(), |
| + reinterpret_cast<const uint8_t*>(data), |
| + length, |
| v8::ScriptCompiler::CachedData::BufferNotOwned); |
| v8::ScriptCompiler::Source source(code, origin, cachedData); |
| return v8::ScriptCompiler::Compile(isolate, &source, options); |
| @@ -133,30 +151,33 @@ v8::Local<v8::Script> V8ScriptRunner::compileScript(v8::Handle<v8::String> code, |
| v8::ScriptCompiler::Source source(code, origin); |
| script = v8::ScriptCompiler::Compile(isolate, &source, v8::ScriptCompiler::kNoCompileOptions); |
| } else { |
| + bool compressed = false; |
| + Resource::MetadataCacheType cacheType = Resource::CacheLocally; |
| + v8::ScriptCompiler::CompileOptions consumeOption = v8::ScriptCompiler::kConsumeParserCache; |
| + v8::ScriptCompiler::CompileOptions produceOption = v8::ScriptCompiler::kProduceParserCache; |
| switch (cacheOptions) { |
| + case V8CacheOptionsOff: |
| + // Use default. |
| + break; |
| case V8CacheOptionsParse: |
| cacheTag = tagForParserCache(); |
| - script = resource->cachedMetadata(cacheTag) |
| - ? compileAndConsumeCache(isolate, code, origin, resource, v8::ScriptCompiler::kConsumeParserCache, cacheTag) |
| - : compileAndProduceCache(isolate, code, origin, resource, v8::ScriptCompiler::kProduceParserCache, cacheTag, Resource::SendToPlatform); |
| + cacheType = Resource::SendToPlatform; |
| + consumeOption = v8::ScriptCompiler::kConsumeParserCache; |
| + produceOption = v8::ScriptCompiler::kProduceParserCache; |
| break; |
| + case V8CacheOptionsCodeCompressed: |
| + compressed = true; |
| + // Fall through. |
| case V8CacheOptionsCode: |
| cacheTag = tagForCodeCache(); |
| - script = resource->cachedMetadata(cacheTag) |
| - ? compileAndConsumeCache(isolate, code, origin, resource, v8::ScriptCompiler::kConsumeCodeCache, cacheTag) |
| - : compileAndProduceCache(isolate, code, origin, resource, v8::ScriptCompiler::kProduceCodeCache, cacheTag, Resource::SendToPlatform); |
| - break; |
| - case V8CacheOptionsOff: |
| - // Previous behaviour was to always generate an in-memory parser |
| - // cache. We emulate this here. |
| - // FIXME: Determine whether this should get its own setting, so we |
| - // can also have a true 'off'. |
| - cacheTag = tagForParserCache(); |
| - script = resource->cachedMetadata(cacheTag) |
| - ? compileAndConsumeCache(isolate, code, origin, resource, v8::ScriptCompiler::kConsumeParserCache, cacheTag) |
| - : compileAndProduceCache(isolate, code, origin, resource, v8::ScriptCompiler::kProduceParserCache, cacheTag, Resource::CacheLocally); |
| + cacheType = Resource::SendToPlatform; |
| + consumeOption = v8::ScriptCompiler::kConsumeCodeCache; |
| + produceOption = v8::ScriptCompiler::kProduceCodeCache; |
| break; |
| } |
| + script = resource->cachedMetadata(cacheTag) |
| + ? compileAndConsumeCache(isolate, code, origin, resource, consumeOption, cacheTag, compressed) |
| + : compileAndProduceCache(isolate, code, origin, resource, produceOption, cacheTag, cacheType, compressed); |
| } |
| return script; |
| } |