OLD | NEW |
---|---|
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 Loading... | |
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) { |
haraken
2014/09/11 16:12:30
Can we add:
ASSERT(!m_streamer->streamingSuppre
marja
2014/09/15 17:45:27
Done.
| |
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->cachedDataType(), reinterpret_ 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(); |
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; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 Loading... | |
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 |
OLD | NEW |