1 /*
2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
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,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
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
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
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27 #include "bindings/v8/V8ScriptRunner.h"
28
29 #include "bindings/v8/ScriptSourceCode.h"
30 #include "bindings/v8/V8Binding.h"
31 #include "bindings/v8/V8GCController.h"
32 #include "bindings/v8/V8RecursionScope.h"
33 #include "core/dom/ExecutionContext.h"
34 #include "core/fetch/CachedMetadata.h"
35 #include "core/fetch/ScriptResource.h"
36 #include "platform/TraceEvent.h"
37
38 namespace WebCore {
39
compileScript(const ScriptSourceCode & source,v8::Isolate * isolate,AccessControlStatus corsStatus)40 v8::Local<v8::Script> V8ScriptRunner::compileScript(const ScriptSourceCode& source, v8::Isolate* isolate, AccessControlStatus corsStatus)
41 {
42 return compileScript(v8String(isolate, source.source()), source.url(), source.startPosition(), source.resource(), isolate, corsStatus);
43 }
44
compileScript(v8::Handle<v8::String> code,const String & fileName,const TextPosition & scriptStartPosition,ScriptResource * resource,v8::Isolate * isolate,AccessControlStatus corsStatus)45 v8::Local<v8::Script> V8ScriptRunner::compileScript(v8::Handle<v8::String> code, const String& fileName, const TextPosition& scriptStartPosition, ScriptResource* resource, v8::Isolate* isolate, AccessControlStatus corsStatus)
46 {
47 // A pseudo-randomly chosen ID used to store and retrieve V8 ScriptData from
48 // the ScriptResource. If the format changes, this ID should be changed too.
49 static const unsigned dataTypeID = 0xECC13BD7;
50
51 // Very small scripts are not worth the effort to store cached data.
52 static const int minLengthForCachedData = 1024;
53
54 TRACE_EVENT1("v8", "v8.compile", "fileName", fileName.utf8());
55 TRACE_EVENT_SCOPED_SAMPLING_STATE("V8", "V8Compile");
56
57 // NOTE: For compatibility with WebCore, ScriptSourceCode's line starts at
58 // 1, whereas v8 starts at 0.
59 v8::Handle<v8::String> name = v8String(isolate, fileName);
60 v8::Handle<v8::Integer> line = v8::Integer::New(isolate, scriptStartPosition.m_line.zeroBasedInt());
61 v8::Handle<v8::Integer> column = v8::Integer::New(isolate, scriptStartPosition.m_column.zeroBasedInt());
62 v8::Handle<v8::Boolean> isSharedCrossOrigin = corsStatus == SharableCrossOrigin ? v8::True(isolate) : v8::False(isolate);
63 v8::ScriptOrigin origin(name, line, column, isSharedCrossOrigin);
64
65 v8::ScriptCompiler::CompileOptions options = v8::ScriptCompiler::kNoCompileOptions;
66 OwnPtr<v8::ScriptCompiler::CachedData> cachedData;
67 if (resource) {
68 CachedMetadata* cachedMetadata = resource->cachedMetadata(dataTypeID);
69 if (cachedMetadata) {
70 // Ownership of the buffer is not transferred to CachedData.
71 cachedData = adoptPtr(new v8::ScriptCompiler::CachedData(reinterpret_cast<const uint8_t*>(cachedMetadata->data()), cachedMetadata->size()));
72 } else if (code->Length() >= minLengthForCachedData) {
73 options = v8::ScriptCompiler::kProduceDataToCache;
74 }
75 }
76 // source takes ownership of cachedData.
77 v8::ScriptCompiler::Source source(code, origin, cachedData.leakPtr());
78 v8::Local<v8::Script> script = v8::ScriptCompiler::Compile(isolate, &source, options);
79 if (options == v8::ScriptCompiler::kProduceDataToCache) {
80 const v8::ScriptCompiler::CachedData* newCachedData = source.GetCachedData();
81 if (newCachedData) {
82 // Ownership of the buffer is not transferred; source's cachedData continues to own it.
83 resource->setCachedMetadata(dataTypeID, reinterpret_cast<const char*>(newCachedData->data), newCachedData->length);
84 }
85 }
86 return script;
87 }
88
runCompiledScript(v8::Handle<v8::Script> script,ExecutionContext * context,v8::Isolate * isolate)89 v8::Local<v8::Value> V8ScriptRunner::runCompiledScript(v8::Handle<v8::Script> script, ExecutionContext* context, v8::Isolate* isolate)
90 {
91 if (script.IsEmpty())
92 return v8::Local<v8::Value>();
93 TRACE_EVENT_SCOPED_SAMPLING_STATE("V8", "V8Execution");
94 TRACE_EVENT1("v8", "v8.run", "fileName", TRACE_STR_COPY(*v8::String::Utf8Value(script->GetUnboundScript()->GetScriptName())));
95
96 if (V8RecursionScope::recursionLevel(isolate) >= kMaxRecursionDepth)
97 return handleMaxRecursionDepthExceeded(isolate);
98
99 RELEASE_ASSERT(!context->isIteratingOverObservers());
100
101 // Run the script and keep track of the current recursion depth.
102 v8::Local<v8::Value> result;
103 {
104 V8RecursionScope recursionScope(isolate, context);
105 result = script->Run();
106 }
107
108 if (result.IsEmpty())
109 return v8::Local<v8::Value>();
110
111 crashIfV8IsDead();
112 return result;
113 }
114
compileAndRunInternalScript(v8::Handle<v8::String> source,v8::Isolate * isolate,const String & fileName,const TextPosition & scriptStartPosition)115 v8::Local<v8::Value> V8ScriptRunner::compileAndRunInternalScript(v8::Handle<v8::String> source, v8::Isolate* isolate, const String& fileName, const TextPosition& scriptStartPosition)
116 {
117 TRACE_EVENT0("v8", "v8.run");
118 TRACE_EVENT_SCOPED_SAMPLING_STATE("V8", "V8Execution");
119 v8::Handle<v8::Script> script = V8ScriptRunner::compileScript(source, fileName, scriptStartPosition, 0, isolate);
120 if (script.IsEmpty())
121 return v8::Local<v8::Value>();
122
123 V8RecursionScope::MicrotaskSuppression recursionScope(isolate);
124 v8::Local<v8::Value> result = script->Run();
125 crashIfV8IsDead();
126 return result;
127 }
128
callFunction(v8::Handle<v8::Function> function,ExecutionContext * context,v8::Handle<v8::Value> receiver,int argc,v8::Handle<v8::Value> args[],v8::Isolate * isolate)129 v8::Local<v8::Value> V8ScriptRunner::callFunction(v8::Handle<v8::Function> function, ExecutionContext* context, v8::Handle<v8::Value> receiver, int argc, v8::Handle<v8::Value> args[], v8::Isolate* isolate)
130 {
131 TRACE_EVENT0("v8", "v8.callFunction");
132 TRACE_EVENT_SCOPED_SAMPLING_STATE("V8", "V8Execution");
133
134 if (V8RecursionScope::recursionLevel(isolate) >= kMaxRecursionDepth)
135 return handleMaxRecursionDepthExceeded(isolate);
136
137 RELEASE_ASSERT(!context->isIteratingOverObservers());
138
139 V8RecursionScope recursionScope(isolate, context);
140 v8::Local<v8::Value> result = function->Call(receiver, argc, args);
141 crashIfV8IsDead();
142 return result;
143 }
144
callInternalFunction(v8::Handle<v8::Function> function,v8::Handle<v8::Value> receiver,int argc,v8::Handle<v8::Value> args[],v8::Isolate * isolate)145 v8::Local<v8::Value> V8ScriptRunner::callInternalFunction(v8::Handle<v8::Function> function, v8::Handle<v8::Value> receiver, int argc, v8::Handle<v8::Value> args[], v8::Isolate* isolate)
146 {
147 TRACE_EVENT0("v8", "v8.callFunction");
148 TRACE_EVENT_SCOPED_SAMPLING_STATE("V8", "V8Execution");
149 V8RecursionScope::MicrotaskSuppression recursionScope(isolate);
150 v8::Local<v8::Value> result = function->Call(receiver, argc, args);
151 crashIfV8IsDead();
152 return result;
153 }
154
callAsFunction(v8::Isolate * isolate,v8::Handle<v8::Object> object,v8::Handle<v8::Value> receiver,int argc,v8::Handle<v8::Value> args[])155 v8::Local<v8::Value> V8ScriptRunner::callAsFunction(v8::Isolate* isolate, v8::Handle<v8::Object> object, v8::Handle<v8::Value> receiver, int argc, v8::Handle<v8::Value> args[])
156 {
157 TRACE_EVENT0("v8", "v8.callFunction");
158 TRACE_EVENT_SCOPED_SAMPLING_STATE("V8", "V8Execution");
159
160 V8RecursionScope::MicrotaskSuppression recursionScope(isolate);
161 v8::Local<v8::Value> result = object->CallAsFunction(receiver, argc, args);
162 crashIfV8IsDead();
163 return result;
164 }
165
instantiateObject(v8::Isolate * isolate,v8::Handle<v8::ObjectTemplate> objectTemplate)166 v8::Local<v8::Object> V8ScriptRunner::instantiateObject(v8::Isolate* isolate, v8::Handle<v8::ObjectTemplate> objectTemplate)
167 {
168 TRACE_EVENT0("v8", "v8.newInstance");
169 TRACE_EVENT_SCOPED_SAMPLING_STATE("V8", "V8Execution");
170
171 V8RecursionScope::MicrotaskSuppression scope(isolate);
172 v8::Local<v8::Object> result = objectTemplate->NewInstance();
173 crashIfV8IsDead();
174 return result;
175 }
176
instantiateObject(v8::Isolate * isolate,v8::Handle<v8::Function> function,int argc,v8::Handle<v8::Value> argv[])177 v8::Local<v8::Object> V8ScriptRunner::instantiateObject(v8::Isolate* isolate, v8::Handle<v8::Function> function, int argc, v8::Handle<v8::Value> argv[])
178 {
179 TRACE_EVENT0("v8", "v8.newInstance");
180 TRACE_EVENT_SCOPED_SAMPLING_STATE("V8", "V8Execution");
181
182 V8RecursionScope::MicrotaskSuppression scope(isolate);
183 v8::Local<v8::Object> result = function->NewInstance(argc, argv);
184 crashIfV8IsDead();
185 return result;
186 }
187
instantiateObjectInDocument(v8::Isolate * isolate,v8::Handle<v8::Function> function,ExecutionContext * context,int argc,v8::Handle<v8::Value> argv[])188 v8::Local<v8::Object> V8ScriptRunner::instantiateObjectInDocument(v8::Isolate* isolate, v8::Handle<v8::Function> function, ExecutionContext* context, int argc, v8::Handle<v8::Value> argv[])
189 {
190 TRACE_EVENT0("v8", "v8.newInstance");
191 TRACE_EVENT_SCOPED_SAMPLING_STATE("V8", "V8Execution");
192 V8RecursionScope scope(isolate, context);
193 v8::Local<v8::Object> result = function->NewInstance(argc, argv);
194 crashIfV8IsDead();
195 return result;
196 }
197
198 } // namespace WebCore
199