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) { |
| 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 ASSERT(!streamer->streamingInProgress()); |
| 122 ASSERT(!streamer->streamingSuppressed()); |
| 123 script = v8::ScriptCompiler::Compile(isolate, streamer->source(), code,
origin); |
| 124 // Whether to produce the cached data or not is decided when the |
| 125 // streamer is started. Here we only need to get the data out. |
| 126 const v8::ScriptCompiler::CachedData* newCachedData = streamer->source()
->GetCachedData(); |
| 127 if (newCachedData) { |
| 128 resource->clearCachedMetadata(); |
| 129 resource->setCachedMetadata(streamer->cachedDataType(), reinterpret_
cast<const char*>(newCachedData->data), newCachedData->length); |
| 130 } |
| 131 } else if (!resource || !resource->url().protocolIsInHTTPFamily() || code->L
ength() < 1024) { |
126 v8::ScriptCompiler::Source source(code, origin); | 132 v8::ScriptCompiler::Source source(code, origin); |
127 script = v8::ScriptCompiler::Compile(isolate, &source, v8::ScriptCompile
r::kNoCompileOptions); | 133 script = v8::ScriptCompiler::Compile(isolate, &source, v8::ScriptCompile
r::kNoCompileOptions); |
128 } else { | 134 } else { |
129 switch (cacheOptions) { | 135 switch (cacheOptions) { |
130 case V8CacheOptionsParse: | 136 case V8CacheOptionsParse: |
131 cacheTag = tagForParserCache(); | 137 cacheTag = tagForParserCache(); |
132 script = resource->cachedMetadata(cacheTag) | 138 script = resource->cachedMetadata(cacheTag) |
133 ? compileAndConsumeCache(isolate, code, origin, resource, v8::Sc
riptCompiler::kConsumeParserCache, cacheTag) | 139 ? compileAndConsumeCache(isolate, code, origin, resource, v8::Sc
riptCompiler::kConsumeParserCache, cacheTag) |
134 : compileAndProduceCache(isolate, code, origin, resource, v8::Sc
riptCompiler::kProduceParserCache, cacheTag, Resource::SendToPlatform); | 140 : compileAndProduceCache(isolate, code, origin, resource, v8::Sc
riptCompiler::kProduceParserCache, cacheTag, Resource::SendToPlatform); |
135 break; | 141 break; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 | 181 |
176 if (result.IsEmpty()) | 182 if (result.IsEmpty()) |
177 return v8::Local<v8::Value>(); | 183 return v8::Local<v8::Value>(); |
178 | 184 |
179 crashIfV8IsDead(); | 185 crashIfV8IsDead(); |
180 return result; | 186 return result; |
181 } | 187 } |
182 | 188 |
183 v8::Local<v8::Value> V8ScriptRunner::compileAndRunInternalScript(v8::Handle<v8::
String> source, v8::Isolate* isolate, const String& fileName, const TextPosition
& scriptStartPosition) | 189 v8::Local<v8::Value> V8ScriptRunner::compileAndRunInternalScript(v8::Handle<v8::
String> source, v8::Isolate* isolate, const String& fileName, const TextPosition
& scriptStartPosition) |
184 { | 190 { |
185 v8::Handle<v8::Script> script = V8ScriptRunner::compileScript(source, fileNa
me, scriptStartPosition, 0, isolate); | 191 v8::Handle<v8::Script> script = V8ScriptRunner::compileScript(source, fileNa
me, scriptStartPosition, 0, 0, isolate); |
186 if (script.IsEmpty()) | 192 if (script.IsEmpty()) |
187 return v8::Local<v8::Value>(); | 193 return v8::Local<v8::Value>(); |
188 | 194 |
189 TRACE_EVENT0("v8", "v8.run"); | 195 TRACE_EVENT0("v8", "v8.run"); |
190 TRACE_EVENT_SCOPED_SAMPLING_STATE("v8", "V8Execution"); | 196 TRACE_EVENT_SCOPED_SAMPLING_STATE("v8", "V8Execution"); |
191 V8RecursionScope::MicrotaskSuppression recursionScope(isolate); | 197 V8RecursionScope::MicrotaskSuppression recursionScope(isolate); |
192 v8::Local<v8::Value> result = script->Run(); | 198 v8::Local<v8::Value> result = script->Run(); |
193 crashIfV8IsDead(); | 199 crashIfV8IsDead(); |
194 return result; | 200 return result; |
195 } | 201 } |
(...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[]) | 272 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 { | 273 { |
268 TRACE_EVENT0("v8", "v8.newInstance"); | 274 TRACE_EVENT0("v8", "v8.newInstance"); |
269 TRACE_EVENT_SCOPED_SAMPLING_STATE("v8", "V8Execution"); | 275 TRACE_EVENT_SCOPED_SAMPLING_STATE("v8", "V8Execution"); |
270 V8RecursionScope scope(isolate); | 276 V8RecursionScope scope(isolate); |
271 v8::Local<v8::Object> result = function->NewInstance(argc, argv); | 277 v8::Local<v8::Object> result = function->NewInstance(argc, argv); |
272 crashIfV8IsDead(); | 278 crashIfV8IsDead(); |
273 return result; | 279 return result; |
274 } | 280 } |
275 | 281 |
| 282 unsigned V8ScriptRunner::tagForParserCache() |
| 283 { |
| 284 return StringHash::hash(v8::V8::GetVersion()) * 2; |
| 285 } |
| 286 |
| 287 unsigned V8ScriptRunner::tagForCodeCache() |
| 288 { |
| 289 return StringHash::hash(v8::V8::GetVersion()) * 2 + 1; |
| 290 } |
| 291 |
276 } // namespace blink | 292 } // namespace blink |
OLD | NEW |