| OLD | NEW |
| (Empty) |
| 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 "sky/engine/config.h" | |
| 27 #include "sky/engine/bindings/core/v8/V8ScriptRunner.h" | |
| 28 | |
| 29 #include "sky/engine/bindings/core/v8/ScriptSourceCode.h" | |
| 30 #include "sky/engine/bindings/core/v8/V8Binding.h" | |
| 31 #include "sky/engine/bindings/core/v8/V8GCController.h" | |
| 32 #include "sky/engine/bindings/core/v8/V8RecursionScope.h" | |
| 33 #include "sky/engine/bindings/core/v8/V8ThrowException.h" | |
| 34 #include "sky/engine/core/dom/ExecutionContext.h" | |
| 35 #include "sky/engine/platform/TraceEvent.h" | |
| 36 | |
| 37 namespace blink { | |
| 38 | |
| 39 namespace { | |
| 40 | |
| 41 // In order to make sure all pending messages to be processed in | |
| 42 // v8::Function::Call, we don't call handleMaxRecursionDepthExceeded | |
| 43 // directly. Instead, we create a v8::Function of | |
| 44 // throwStackOverflowException and call it. | |
| 45 void throwStackOverflowException(const v8::FunctionCallbackInfo<v8::Value>& info
) | |
| 46 { | |
| 47 V8ThrowException::throwRangeError("Maximum call stack size exceeded.", info.
GetIsolate()); | |
| 48 } | |
| 49 | |
| 50 v8::Local<v8::Value> throwStackOverflowExceptionIfNeeded(v8::Isolate* isolate) | |
| 51 { | |
| 52 if (V8PerIsolateData::from(isolate)->isHandlingRecursionLevelError()) { | |
| 53 // If we are already handling a recursion level error, we should | |
| 54 // not invoke v8::Function::Call. | |
| 55 return v8::Undefined(isolate); | |
| 56 } | |
| 57 V8PerIsolateData::from(isolate)->setIsHandlingRecursionLevelError(true); | |
| 58 v8::Local<v8::Value> result = v8::Function::New(isolate, throwStackOverflowE
xception)->Call(v8::Undefined(isolate), 0, 0); | |
| 59 V8PerIsolateData::from(isolate)->setIsHandlingRecursionLevelError(false); | |
| 60 return result; | |
| 61 } | |
| 62 | |
| 63 } // namespace | |
| 64 | |
| 65 v8::Local<v8::Script> V8ScriptRunner::compileScript(const ScriptSourceCode& sour
ce, v8::Isolate* isolate, V8CacheOptions cacheOptions) | |
| 66 { | |
| 67 return compileScript(v8String(isolate, source.source()), source.url(), sourc
e.startPosition(), isolate, cacheOptions); | |
| 68 } | |
| 69 | |
| 70 v8::Local<v8::Script> V8ScriptRunner::compileScript(v8::Handle<v8::String> code,
const String& fileName, const TextPosition& scriptStartPosition, v8::Isolate* i
solate, V8CacheOptions cacheOptions) | |
| 71 { | |
| 72 TRACE_EVENT1("v8", "v8.compile", "fileName", fileName.utf8().data()); | |
| 73 TRACE_EVENT_SCOPED_SAMPLING_STATE("v8", "V8Compile"); | |
| 74 | |
| 75 // NOTE: For compatibility with WebCore, ScriptSourceCode's line starts at | |
| 76 // 1, whereas v8 starts at 0. | |
| 77 v8::Handle<v8::String> name = v8String(isolate, fileName); | |
| 78 v8::Handle<v8::Integer> line = v8::Integer::New(isolate, scriptStartPosition
.m_line.zeroBasedInt()); | |
| 79 v8::Handle<v8::Integer> column = v8::Integer::New(isolate, scriptStartPositi
on.m_column.zeroBasedInt()); | |
| 80 v8::ScriptOrigin origin(name, line, column, v8::True(isolate)); | |
| 81 | |
| 82 v8::ScriptCompiler::Source source(code, origin); | |
| 83 return v8::ScriptCompiler::Compile(isolate, &source, v8::ScriptCompiler::kNo
CompileOptions); | |
| 84 } | |
| 85 | |
| 86 v8::Local<v8::Value> V8ScriptRunner::runCompiledScript(v8::Handle<v8::Script> sc
ript, ExecutionContext* context, v8::Isolate* isolate) | |
| 87 { | |
| 88 if (script.IsEmpty()) | |
| 89 return v8::Local<v8::Value>(); | |
| 90 | |
| 91 TRACE_EVENT_SCOPED_SAMPLING_STATE("v8", "V8Execution"); | |
| 92 TRACE_EVENT1("v8", "v8.run", "fileName", TRACE_STR_COPY(*v8::String::Utf8Val
ue(script->GetUnboundScript()->GetScriptName()))); | |
| 93 | |
| 94 if (V8RecursionScope::recursionLevel(isolate) >= kMaxRecursionDepth) | |
| 95 return throwStackOverflowExceptionIfNeeded(isolate); | |
| 96 | |
| 97 RELEASE_ASSERT(!context->isIteratingOverObservers()); | |
| 98 | |
| 99 V8RecursionScope recursionScope(isolate, context); | |
| 100 v8::Local<v8::Value> result = script->Run(); | |
| 101 crashIfV8IsDead(); | |
| 102 return result; | |
| 103 } | |
| 104 | |
| 105 v8::Local<v8::Value> V8ScriptRunner::compileAndRunInternalScript(v8::Handle<v8::
String> source, v8::Isolate* isolate, const String& fileName, const TextPosition
& scriptStartPosition) | |
| 106 { | |
| 107 v8::Handle<v8::Script> script = V8ScriptRunner::compileScript(source, fileNa
me, scriptStartPosition, isolate); | |
| 108 if (script.IsEmpty()) | |
| 109 return v8::Local<v8::Value>(); | |
| 110 | |
| 111 TRACE_EVENT0("v8", "v8.run"); | |
| 112 TRACE_EVENT_SCOPED_SAMPLING_STATE("v8", "V8Execution"); | |
| 113 V8RecursionScope::MicrotaskSuppression recursionScope(isolate); | |
| 114 v8::Local<v8::Value> result = script->Run(); | |
| 115 crashIfV8IsDead(); | |
| 116 return result; | |
| 117 } | |
| 118 | |
| 119 v8::Local<v8::Value> V8ScriptRunner::runCompiledInternalScript(v8::Handle<v8::Sc
ript> script, v8::Isolate* isolate) | |
| 120 { | |
| 121 TRACE_EVENT0("v8", "v8.run"); | |
| 122 TRACE_EVENT_SCOPED_SAMPLING_STATE("v8", "V8Execution"); | |
| 123 V8RecursionScope::MicrotaskSuppression recursionScope(isolate); | |
| 124 v8::Local<v8::Value> result = script->Run(); | |
| 125 crashIfV8IsDead(); | |
| 126 return result; | |
| 127 } | |
| 128 | |
| 129 v8::Local<v8::Value> V8ScriptRunner::callFunction(v8::Handle<v8::Function> funct
ion, ExecutionContext* context, v8::Handle<v8::Value> receiver, int argc, v8::Ha
ndle<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 throwStackOverflowExceptionIfNeeded(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 | |
| 145 v8::Local<v8::Value> V8ScriptRunner::callInternalFunction(v8::Handle<v8::Functio
n> function, v8::Handle<v8::Value> receiver, int argc, v8::Handle<v8::Value> arg
s[], 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 | |
| 155 v8::Local<v8::Object> V8ScriptRunner::instantiateObject(v8::Isolate* isolate, v8
::Handle<v8::ObjectTemplate> objectTemplate) | |
| 156 { | |
| 157 TRACE_EVENT0("v8", "v8.newInstance"); | |
| 158 TRACE_EVENT_SCOPED_SAMPLING_STATE("v8", "V8Execution"); | |
| 159 | |
| 160 V8RecursionScope::MicrotaskSuppression scope(isolate); | |
| 161 v8::Local<v8::Object> result = objectTemplate->NewInstance(); | |
| 162 crashIfV8IsDead(); | |
| 163 return result; | |
| 164 } | |
| 165 | |
| 166 v8::Local<v8::Object> V8ScriptRunner::instantiateObject(v8::Isolate* isolate, v8
::Handle<v8::Function> function, int argc, v8::Handle<v8::Value> argv[]) | |
| 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 = function->NewInstance(argc, argv); | |
| 173 crashIfV8IsDead(); | |
| 174 return result; | |
| 175 } | |
| 176 | |
| 177 v8::Local<v8::Object> V8ScriptRunner::instantiateObjectInDocument(v8::Isolate* i
solate, v8::Handle<v8::Function> function, ExecutionContext* context, int argc,
v8::Handle<v8::Value> argv[]) | |
| 178 { | |
| 179 TRACE_EVENT0("v8", "v8.newInstance"); | |
| 180 TRACE_EVENT_SCOPED_SAMPLING_STATE("v8", "V8Execution"); | |
| 181 V8RecursionScope scope(isolate, context); | |
| 182 v8::Local<v8::Object> result = function->NewInstance(argc, argv); | |
| 183 crashIfV8IsDead(); | |
| 184 return result; | |
| 185 } | |
| 186 | |
| 187 void V8ScriptRunner::runModule(v8::Isolate* isolate, ExecutionContext* context,
V8ScriptModule& module) | |
| 188 { | |
| 189 TRACE_EVENT0("v8", "v8.runModule"); | |
| 190 TRACE_EVENT_SCOPED_SAMPLING_STATE("v8", "V8Execution"); | |
| 191 V8RecursionScope scope(isolate, context); | |
| 192 | |
| 193 StringBuilder hackedSource; | |
| 194 hackedSource.append("(function("); | |
| 195 for (String& formal : module.formalDependencies) { | |
| 196 hackedSource.append(formal); | |
| 197 hackedSource.append(", "); | |
| 198 } | |
| 199 hackedSource.append("module) {"); | |
| 200 hackedSource.append(module.source); | |
| 201 hackedSource.append("\n/**/})"); | |
| 202 | |
| 203 v8::Handle<v8::Script> script = compileScript( | |
| 204 v8String(isolate, hackedSource.toString()), | |
| 205 module.resourceName, | |
| 206 module.textPosition, | |
| 207 isolate, | |
| 208 V8CacheOptionsOff); | |
| 209 | |
| 210 if (script.IsEmpty()) | |
| 211 return; | |
| 212 | |
| 213 v8::Handle<v8::Value> scriptResult = script->Run(); | |
| 214 if (scriptResult.IsEmpty()) | |
| 215 return; | |
| 216 | |
| 217 auto arguments = module.resolvedDependencies; | |
| 218 arguments.append(module.moduleObject); | |
| 219 | |
| 220 RELEASE_ASSERT(scriptResult->IsObject()); | |
| 221 scriptResult.As<v8::Object>()->CallAsFunction( | |
| 222 v8::Null(isolate), arguments.size(), arguments.data()); | |
| 223 crashIfV8IsDead(); | |
| 224 } | |
| 225 | |
| 226 } // namespace blink | |
| OLD | NEW |