This source file includes following definitions.
- compileScript
- compileScript
- runCompiledScript
- compileAndRunInternalScript
- callFunction
- callInternalFunction
- callAsFunction
- instantiateObject
- instantiateObject
- instantiateObjectInDocument
#include "config.h"
#include "bindings/v8/V8ScriptRunner.h"
#include "bindings/v8/ScriptSourceCode.h"
#include "bindings/v8/V8Binding.h"
#include "bindings/v8/V8GCController.h"
#include "bindings/v8/V8RecursionScope.h"
#include "core/dom/ExecutionContext.h"
#include "core/fetch/CachedMetadata.h"
#include "core/fetch/ScriptResource.h"
#include "platform/TraceEvent.h"
namespace WebCore {
v8::Local<v8::Script> V8ScriptRunner::compileScript(const ScriptSourceCode& source, v8::Isolate* isolate, AccessControlStatus corsStatus)
{
return compileScript(v8String(isolate, source.source()), source.url(), source.startPosition(), source.resource(), isolate, corsStatus);
}
v8::Local<v8::Script> V8ScriptRunner::compileScript(v8::Handle<v8::String> code, const String& fileName, const TextPosition& scriptStartPosition, ScriptResource* resource, v8::Isolate* isolate, AccessControlStatus corsStatus)
{
static const unsigned dataTypeID = 0xECC13BD7;
static const int minLengthForCachedData = 1024;
TRACE_EVENT0("v8", "v8.compile");
TRACE_EVENT_SCOPED_SAMPLING_STATE("V8", "V8Compile");
v8::Handle<v8::String> name = v8String(isolate, fileName);
v8::Handle<v8::Integer> line = v8::Integer::New(isolate, scriptStartPosition.m_line.zeroBasedInt());
v8::Handle<v8::Integer> column = v8::Integer::New(isolate, scriptStartPosition.m_column.zeroBasedInt());
v8::Handle<v8::Boolean> isSharedCrossOrigin = corsStatus == SharableCrossOrigin ? v8::True(isolate) : v8::False(isolate);
v8::ScriptOrigin origin(name, line, column, isSharedCrossOrigin);
v8::ScriptCompiler::CompileOptions options = v8::ScriptCompiler::kNoCompileOptions;
OwnPtr<v8::ScriptCompiler::CachedData> cachedData;
if (resource) {
CachedMetadata* cachedMetadata = resource->cachedMetadata(dataTypeID);
if (cachedMetadata) {
cachedData = adoptPtr(new v8::ScriptCompiler::CachedData(reinterpret_cast<const uint8_t*>(cachedMetadata->data()), cachedMetadata->size()));
} else if (code->Length() >= minLengthForCachedData) {
options = v8::ScriptCompiler::kProduceDataToCache;
}
}
v8::ScriptCompiler::Source source(code, origin, cachedData.leakPtr());
v8::Local<v8::Script> script = v8::ScriptCompiler::Compile(isolate, &source, options);
if (options == v8::ScriptCompiler::kProduceDataToCache) {
const v8::ScriptCompiler::CachedData* newCachedData = source.GetCachedData();
if (newCachedData) {
resource->setCachedMetadata(dataTypeID, reinterpret_cast<const char*>(newCachedData->data), newCachedData->length);
}
}
return script;
}
v8::Local<v8::Value> V8ScriptRunner::runCompiledScript(v8::Handle<v8::Script> script, ExecutionContext* context, v8::Isolate* isolate)
{
TRACE_EVENT0("v8", "v8.run");
TRACE_EVENT_SCOPED_SAMPLING_STATE("V8", "V8Execution");
if (script.IsEmpty())
return v8::Local<v8::Value>();
if (V8RecursionScope::recursionLevel(isolate) >= kMaxRecursionDepth)
return handleMaxRecursionDepthExceeded(isolate);
RELEASE_ASSERT(!context->isIteratingOverObservers());
v8::Local<v8::Value> result;
{
V8RecursionScope recursionScope(isolate, context);
result = script->Run();
}
if (result.IsEmpty())
return v8::Local<v8::Value>();
crashIfV8IsDead();
return result;
}
v8::Local<v8::Value> V8ScriptRunner::compileAndRunInternalScript(v8::Handle<v8::String> source, v8::Isolate* isolate, const String& fileName, const TextPosition& scriptStartPosition)
{
TRACE_EVENT0("v8", "v8.run");
TRACE_EVENT_SCOPED_SAMPLING_STATE("V8", "V8Execution");
v8::Handle<v8::Script> script = V8ScriptRunner::compileScript(source, fileName, scriptStartPosition, 0, isolate);
if (script.IsEmpty())
return v8::Local<v8::Value>();
V8RecursionScope::MicrotaskSuppression recursionScope(isolate);
v8::Local<v8::Value> result = script->Run();
crashIfV8IsDead();
return result;
}
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)
{
TRACE_EVENT0("v8", "v8.callFunction");
TRACE_EVENT_SCOPED_SAMPLING_STATE("V8", "V8Execution");
if (V8RecursionScope::recursionLevel(isolate) >= kMaxRecursionDepth)
return handleMaxRecursionDepthExceeded(isolate);
RELEASE_ASSERT(!context->isIteratingOverObservers());
V8RecursionScope recursionScope(isolate, context);
v8::Local<v8::Value> result = function->Call(receiver, argc, args);
crashIfV8IsDead();
return result;
}
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)
{
TRACE_EVENT0("v8", "v8.callFunction");
TRACE_EVENT_SCOPED_SAMPLING_STATE("V8", "V8Execution");
V8RecursionScope::MicrotaskSuppression recursionScope(isolate);
v8::Local<v8::Value> result = function->Call(receiver, argc, args);
crashIfV8IsDead();
return result;
}
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[])
{
TRACE_EVENT0("v8", "v8.callFunction");
TRACE_EVENT_SCOPED_SAMPLING_STATE("V8", "V8Execution");
V8RecursionScope::MicrotaskSuppression recursionScope(isolate);
v8::Local<v8::Value> result = object->CallAsFunction(receiver, argc, args);
crashIfV8IsDead();
return result;
}
v8::Local<v8::Object> V8ScriptRunner::instantiateObject(v8::Isolate* isolate, v8::Handle<v8::ObjectTemplate> objectTemplate)
{
TRACE_EVENT0("v8", "v8.newInstance");
TRACE_EVENT_SCOPED_SAMPLING_STATE("V8", "V8Execution");
V8RecursionScope::MicrotaskSuppression scope(isolate);
v8::Local<v8::Object> result = objectTemplate->NewInstance();
crashIfV8IsDead();
return result;
}
v8::Local<v8::Object> V8ScriptRunner::instantiateObject(v8::Isolate* isolate, v8::Handle<v8::Function> function, int argc, v8::Handle<v8::Value> argv[])
{
TRACE_EVENT0("v8", "v8.newInstance");
TRACE_EVENT_SCOPED_SAMPLING_STATE("V8", "V8Execution");
V8RecursionScope::MicrotaskSuppression scope(isolate);
v8::Local<v8::Object> result = function->NewInstance(argc, argv);
crashIfV8IsDead();
return result;
}
v8::Local<v8::Object> V8ScriptRunner::instantiateObjectInDocument(v8::Isolate* isolate, v8::Handle<v8::Function> function, ExecutionContext* context, int argc, v8::Handle<v8::Value> argv[])
{
TRACE_EVENT0("v8", "v8.newInstance");
TRACE_EVENT_SCOPED_SAMPLING_STATE("V8", "V8Execution");
V8RecursionScope scope(isolate, context);
v8::Local<v8::Object> result = function->NewInstance(argc, argv);
crashIfV8IsDead();
return result;
}
}