Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(41)

Side by Side Diff: Source/bindings/core/v8/V8ScriptRunner.cpp

Issue 368283002: Stream scripts to V8 as they load - Blink side. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: cleanup Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2009 Google Inc. All rights reserved. 2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution. 11 * documentation and/or other materials provided with the distribution.
12 * 12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE. 23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */ 24 */
25 25
26 #include "config.h" 26 #include "config.h"
27 #include "bindings/core/v8/V8ScriptRunner.h" 27 #include "bindings/core/v8/V8ScriptRunner.h"
28 28
29 #include "bindings/core/v8/ScriptSourceCode.h" 29 #include "bindings/core/v8/ScriptSourceCode.h"
30 #include "bindings/core/v8/ScriptStreamer.h"
30 #include "bindings/core/v8/V8Binding.h" 31 #include "bindings/core/v8/V8Binding.h"
31 #include "bindings/core/v8/V8GCController.h" 32 #include "bindings/core/v8/V8GCController.h"
32 #include "bindings/core/v8/V8RecursionScope.h" 33 #include "bindings/core/v8/V8RecursionScope.h"
33 #include "bindings/core/v8/V8ThrowException.h" 34 #include "bindings/core/v8/V8ThrowException.h"
34 #include "core/dom/ExecutionContext.h" 35 #include "core/dom/ExecutionContext.h"
35 #include "core/fetch/CachedMetadata.h" 36 #include "core/fetch/CachedMetadata.h"
36 #include "core/fetch/ScriptResource.h" 37 #include "core/fetch/ScriptResource.h"
37 #include "platform/TraceEvent.h" 38 #include "platform/TraceEvent.h"
38 39
39 namespace blink { 40 namespace blink {
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 // Consume existing cache data: 84 // Consume existing cache data:
84 CachedMetadata* cachedMetadata = resource->cachedMetadata(cacheTag); 85 CachedMetadata* cachedMetadata = resource->cachedMetadata(cacheTag);
85 v8::ScriptCompiler::CachedData* cachedData = new v8::ScriptCompiler::CachedD ata( 86 v8::ScriptCompiler::CachedData* cachedData = new v8::ScriptCompiler::CachedD ata(
86 reinterpret_cast<const uint8_t*>(cachedMetadata->data()), 87 reinterpret_cast<const uint8_t*>(cachedMetadata->data()),
87 cachedMetadata->size(), 88 cachedMetadata->size(),
88 v8::ScriptCompiler::CachedData::BufferNotOwned); 89 v8::ScriptCompiler::CachedData::BufferNotOwned);
89 v8::ScriptCompiler::Source source(code, origin, cachedData); 90 v8::ScriptCompiler::Source source(code, origin, cachedData);
90 return v8::ScriptCompiler::Compile(isolate, &source, options); 91 return v8::ScriptCompiler::Compile(isolate, &source, options);
91 } 92 }
92 93
93 unsigned tagForParserCache()
94 {
95 return StringHash::hash(v8::V8::GetVersion()) * 2;
96 }
97
98 unsigned tagForCodeCache()
99 {
100 return StringHash::hash(v8::V8::GetVersion()) * 2 + 1;
101 }
102
103 } // namespace 94 } // namespace
104 95
105 v8::Local<v8::Script> V8ScriptRunner::compileScript(const ScriptSourceCode& sour ce, v8::Isolate* isolate, AccessControlStatus corsStatus, V8CacheOptions cacheOp tions) 96 v8::Local<v8::Script> V8ScriptRunner::compileScript(const ScriptSourceCode& sour ce, v8::Isolate* isolate, AccessControlStatus corsStatus, V8CacheOptions cacheOp tions)
106 { 97 {
107 return compileScript(v8String(isolate, source.source()), source.url(), sourc e.startPosition(), source.resource(), isolate, corsStatus, cacheOptions); 98 return compileScript(v8String(isolate, source.source()), source.url(), sourc e.startPosition(), source.resource(), source.streamer(), isolate, corsStatus, ca cheOptions);
108 } 99 }
109 100
110 v8::Local<v8::Script> V8ScriptRunner::compileScript(v8::Handle<v8::String> code, const String& fileName, const TextPosition& scriptStartPosition, ScriptResource * resource, v8::Isolate* isolate, AccessControlStatus corsStatus, V8CacheOptions cacheOptions) 101 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)
111 { 102 {
112 TRACE_EVENT1("v8", "v8.compile", "fileName", fileName.utf8()); 103 TRACE_EVENT1("v8", "v8.compile", "fileName", fileName.utf8());
113 TRACE_EVENT_SCOPED_SAMPLING_STATE("v8", "V8Compile"); 104 TRACE_EVENT_SCOPED_SAMPLING_STATE("v8", "V8Compile");
114 105
115 // NOTE: For compatibility with WebCore, ScriptSourceCode's line starts at 106 // NOTE: For compatibility with WebCore, ScriptSourceCode's line starts at
116 // 1, whereas v8 starts at 0. 107 // 1, whereas v8 starts at 0.
117 v8::Handle<v8::String> name = v8String(isolate, fileName); 108 v8::Handle<v8::String> name = v8String(isolate, fileName);
118 v8::Handle<v8::Integer> line = v8::Integer::New(isolate, scriptStartPosition .m_line.zeroBasedInt()); 109 v8::Handle<v8::Integer> line = v8::Integer::New(isolate, scriptStartPosition .m_line.zeroBasedInt());
119 v8::Handle<v8::Integer> column = v8::Integer::New(isolate, scriptStartPositi on.m_column.zeroBasedInt()); 110 v8::Handle<v8::Integer> column = v8::Integer::New(isolate, scriptStartPositi on.m_column.zeroBasedInt());
120 v8::Handle<v8::Boolean> isSharedCrossOrigin = corsStatus == SharableCrossOri gin ? v8::True(isolate) : v8::False(isolate); 111 v8::Handle<v8::Boolean> isSharedCrossOrigin = corsStatus == SharableCrossOri gin ? v8::True(isolate) : v8::False(isolate);
121 v8::ScriptOrigin origin(name, line, column, isSharedCrossOrigin); 112 v8::ScriptOrigin origin(name, line, column, isSharedCrossOrigin);
122 113
123 v8::Local<v8::Script> script; 114 v8::Local<v8::Script> script;
124 unsigned cacheTag = 0; 115 unsigned cacheTag = 0;
125 if (!resource || !resource->url().protocolIsInHTTPFamily() || code->Length() < 1024) { 116 if (streamer) {
117 // We don't stream scripts which don't have a Resource.
118 ASSERT(resource);
119 // Failed resources should never get this far.
120 ASSERT(!resource->errorOccurred());
121 script = v8::ScriptCompiler::Compile(isolate, streamer->source(), code, origin);
122 // Whether to produce the cached data or not is decided when the
123 // streamer is started. Here we only need to get the data out.
124 const v8::ScriptCompiler::CachedData* newCachedData = streamer->source() ->GetCachedData();
125 if (newCachedData) {
126 resource->clearCachedMetadata();
127 resource->setCachedMetadata(streamer->cachedDataDataType(), reinterp ret_cast<const char*>(newCachedData->data), newCachedData->length);
128 }
129 } else if (!resource || !resource->url().protocolIsInHTTPFamily() || code->L ength() < 1024) {
126 v8::ScriptCompiler::Source source(code, origin); 130 v8::ScriptCompiler::Source source(code, origin);
127 script = v8::ScriptCompiler::Compile(isolate, &source, v8::ScriptCompile r::kNoCompileOptions); 131 script = v8::ScriptCompiler::Compile(isolate, &source, v8::ScriptCompile r::kNoCompileOptions);
128 } else { 132 } else {
129 switch (cacheOptions) { 133 switch (cacheOptions) {
130 case V8CacheOptionsParse: 134 case V8CacheOptionsParse:
131 cacheTag = tagForParserCache(); 135 cacheTag = tagForParserCache();
haraken 2014/09/10 05:57:51 Can you use this tagForParserCache() instead of in
marja 2014/09/11 09:15:37 Hmm? I'm not introducing a new tagForParserCache,
132 script = resource->cachedMetadata(cacheTag) 136 script = resource->cachedMetadata(cacheTag)
133 ? compileAndConsumeCache(isolate, code, origin, resource, v8::Sc riptCompiler::kConsumeParserCache, cacheTag) 137 ? compileAndConsumeCache(isolate, code, origin, resource, v8::Sc riptCompiler::kConsumeParserCache, cacheTag)
134 : compileAndProduceCache(isolate, code, origin, resource, v8::Sc riptCompiler::kProduceParserCache, cacheTag, Resource::SendToPlatform); 138 : compileAndProduceCache(isolate, code, origin, resource, v8::Sc riptCompiler::kProduceParserCache, cacheTag, Resource::SendToPlatform);
135 break; 139 break;
136 case V8CacheOptionsCode: 140 case V8CacheOptionsCode:
137 cacheTag = tagForCodeCache(); 141 cacheTag = tagForCodeCache();
138 script = resource->cachedMetadata(cacheTag) 142 script = resource->cachedMetadata(cacheTag)
139 ? compileAndConsumeCache(isolate, code, origin, resource, v8::Sc riptCompiler::kConsumeCodeCache, cacheTag) 143 ? compileAndConsumeCache(isolate, code, origin, resource, v8::Sc riptCompiler::kConsumeCodeCache, cacheTag)
140 : compileAndProduceCache(isolate, code, origin, resource, v8::Sc riptCompiler::kProduceCodeCache, cacheTag, Resource::SendToPlatform); 144 : compileAndProduceCache(isolate, code, origin, resource, v8::Sc riptCompiler::kProduceCodeCache, cacheTag, Resource::SendToPlatform);
141 break; 145 break;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 179
176 if (result.IsEmpty()) 180 if (result.IsEmpty())
177 return v8::Local<v8::Value>(); 181 return v8::Local<v8::Value>();
178 182
179 crashIfV8IsDead(); 183 crashIfV8IsDead();
180 return result; 184 return result;
181 } 185 }
182 186
183 v8::Local<v8::Value> V8ScriptRunner::compileAndRunInternalScript(v8::Handle<v8:: String> source, v8::Isolate* isolate, const String& fileName, const TextPosition & scriptStartPosition) 187 v8::Local<v8::Value> V8ScriptRunner::compileAndRunInternalScript(v8::Handle<v8:: String> source, v8::Isolate* isolate, const String& fileName, const TextPosition & scriptStartPosition)
184 { 188 {
185 v8::Handle<v8::Script> script = V8ScriptRunner::compileScript(source, fileNa me, scriptStartPosition, 0, isolate); 189 v8::Handle<v8::Script> script = V8ScriptRunner::compileScript(source, fileNa me, scriptStartPosition, 0, 0, isolate);
186 if (script.IsEmpty()) 190 if (script.IsEmpty())
187 return v8::Local<v8::Value>(); 191 return v8::Local<v8::Value>();
188 192
189 TRACE_EVENT0("v8", "v8.run"); 193 TRACE_EVENT0("v8", "v8.run");
190 TRACE_EVENT_SCOPED_SAMPLING_STATE("v8", "V8Execution"); 194 TRACE_EVENT_SCOPED_SAMPLING_STATE("v8", "V8Execution");
191 V8RecursionScope::MicrotaskSuppression recursionScope(isolate); 195 V8RecursionScope::MicrotaskSuppression recursionScope(isolate);
192 v8::Local<v8::Value> result = script->Run(); 196 v8::Local<v8::Value> result = script->Run();
193 crashIfV8IsDead(); 197 crashIfV8IsDead();
194 return result; 198 return result;
195 } 199 }
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
266 v8::Local<v8::Object> V8ScriptRunner::instantiateObjectInDocument(v8::Isolate* i solate, v8::Handle<v8::Function> function, ExecutionContext* context, int argc, v8::Handle<v8::Value> argv[]) 270 v8::Local<v8::Object> V8ScriptRunner::instantiateObjectInDocument(v8::Isolate* i solate, v8::Handle<v8::Function> function, ExecutionContext* context, int argc, v8::Handle<v8::Value> argv[])
267 { 271 {
268 TRACE_EVENT0("v8", "v8.newInstance"); 272 TRACE_EVENT0("v8", "v8.newInstance");
269 TRACE_EVENT_SCOPED_SAMPLING_STATE("v8", "V8Execution"); 273 TRACE_EVENT_SCOPED_SAMPLING_STATE("v8", "V8Execution");
270 V8RecursionScope scope(isolate, context); 274 V8RecursionScope scope(isolate, context);
271 v8::Local<v8::Object> result = function->NewInstance(argc, argv); 275 v8::Local<v8::Object> result = function->NewInstance(argc, argv);
272 crashIfV8IsDead(); 276 crashIfV8IsDead();
273 return result; 277 return result;
274 } 278 }
275 279
280 unsigned V8ScriptRunner::tagForParserCache()
281 {
282 return StringHash::hash(v8::V8::GetVersion()) * 2;
283 }
284
285 unsigned V8ScriptRunner::tagForCodeCache()
286 {
287 return StringHash::hash(v8::V8::GetVersion()) * 2 + 1;
288 }
289
276 } // namespace blink 290 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698