2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "bindings/v8/V8ScriptRunner.h"
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"
40 v8::Local<v8::Script> V8ScriptRunner::compileScript(const ScriptSourceCode& source, v8::Isolate* isolate, AccessControlStatus corsStatus)
42 return compileScript(v8String(isolate, source.source()), source.url(), source.startPosition(), source.resource(), isolate, 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)
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;
51 // Very small scripts are not worth the effort to store cached data.
52 static const int minLengthForCachedData = 1024;
54 TRACE_EVENT0("v8", "v8.compile");
55 TRACE_EVENT_SCOPED_SAMPLING_STATE("V8", "V8Compile");
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);
65 v8::ScriptCompiler::CompileOptions options = v8::ScriptCompiler::kNoCompileOptions;
66 OwnPtr<v8::ScriptCompiler::CachedData> cachedData;
68 CachedMetadata* cachedMetadata = resource->cachedMetadata(dataTypeID);
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;
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();
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);
89 v8::Local<v8::Value> V8ScriptRunner::runCompiledScript(v8::Handle<v8::Script> script, ExecutionContext* context, v8::Isolate* isolate)
91 TRACE_EVENT0("v8", "v8.run");
92 TRACE_EVENT_SCOPED_SAMPLING_STATE("V8", "V8Execution");
94 return v8::Local<v8::Value>();
96 if (V8RecursionScope::recursionLevel() >= kMaxRecursionDepth)
97 return handleMaxRecursionDepthExceeded(isolate);
99 RELEASE_ASSERT(!context->isIteratingOverObservers());
101 // Run the script and keep track of the current recursion depth.
102 v8::Local<v8::Value> result;
104 V8RecursionScope recursionScope(context);
105 result = script->Run();
108 if (result.IsEmpty())
109 return v8::Local<v8::Value>();
115 v8::Local<v8::Value> V8ScriptRunner::compileAndRunInternalScript(v8::Handle<v8::String> source, v8::Isolate* isolate, const String& fileName, const TextPosition& scriptStartPosition)
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>();
123 V8RecursionScope::MicrotaskSuppression recursionScope;
124 v8::Local<v8::Value> result = script->Run();
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)
131 TRACE_EVENT0("v8", "v8.callFunction");
132 TRACE_EVENT_SCOPED_SAMPLING_STATE("V8", "V8Execution");
134 if (V8RecursionScope::recursionLevel() >= kMaxRecursionDepth)
135 return handleMaxRecursionDepthExceeded(isolate);
137 RELEASE_ASSERT(!context->isIteratingOverObservers());
139 V8RecursionScope recursionScope(context);
140 v8::Local<v8::Value> result = function->Call(receiver, argc, args);
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)
147 TRACE_EVENT0("v8", "v8.callFunction");
148 TRACE_EVENT_SCOPED_SAMPLING_STATE("V8", "V8Execution");
149 V8RecursionScope::MicrotaskSuppression recursionScope;
150 v8::Local<v8::Value> result = function->Call(receiver, argc, args);
155 v8::Local<v8::Value> V8ScriptRunner::callAsFunction(v8::Handle<v8::Object> object, v8::Handle<v8::Value> receiver, int argc, v8::Handle<v8::Value> args[])
157 TRACE_EVENT0("v8", "v8.callFunction");
158 TRACE_EVENT_SCOPED_SAMPLING_STATE("V8", "V8Execution");
160 V8RecursionScope::MicrotaskSuppression recursionScope;
161 v8::Local<v8::Value> result = object->CallAsFunction(receiver, argc, args);
166 v8::Local<v8::Value> V8ScriptRunner::callAsConstructor(v8::Handle<v8::Object> object, int argc, v8::Handle<v8::Value> args[])
168 TRACE_EVENT0("v8", "v8.callFunction");
169 TRACE_EVENT_SCOPED_SAMPLING_STATE("V8", "V8Execution");
171 V8RecursionScope::MicrotaskSuppression recursionScope;
172 v8::Local<v8::Value> result = object->CallAsConstructor(argc, args);
177 v8::Local<v8::Object> V8ScriptRunner::instantiateObject(v8::Handle<v8::ObjectTemplate> objectTemplate)
179 TRACE_EVENT0("v8", "v8.newInstance");
180 TRACE_EVENT_SCOPED_SAMPLING_STATE("V8", "V8Execution");
182 V8RecursionScope::MicrotaskSuppression scope;
183 v8::Local<v8::Object> result = objectTemplate->NewInstance();
188 v8::Local<v8::Object> V8ScriptRunner::instantiateObject(v8::Handle<v8::Function> function, int argc, v8::Handle<v8::Value> argv[])
190 TRACE_EVENT0("v8", "v8.newInstance");
191 TRACE_EVENT_SCOPED_SAMPLING_STATE("V8", "V8Execution");
193 V8RecursionScope::MicrotaskSuppression scope;
194 v8::Local<v8::Object> result = function->NewInstance(argc, argv);
199 v8::Local<v8::Object> V8ScriptRunner::instantiateObjectInDocument(v8::Handle<v8::Function> function, ExecutionContext* context, int argc, v8::Handle<v8::Value> argv[])
201 TRACE_EVENT0("v8", "v8.newInstance");
202 TRACE_EVENT_SCOPED_SAMPLING_STATE("V8", "V8Execution");
203 V8RecursionScope scope(context);
204 v8::Local<v8::Object> result = function->NewInstance(argc, argv);
209 } // namespace WebCore