| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (c) 2010-2011 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 are | |
| 6 * met: | |
| 7 * | |
| 8 * * Redistributions of source code must retain the above copyright | |
| 9 * notice, this list of conditions and the following disclaimer. | |
| 10 * * Redistributions in binary form must reproduce the above | |
| 11 * copyright notice, this list of conditions and the following disclaimer | |
| 12 * in the documentation and/or other materials provided with the | |
| 13 * distribution. | |
| 14 * * Neither the name of Google Inc. nor the names of its | |
| 15 * contributors may be used to endorse or promote products derived from | |
| 16 * this software without specific prior written permission. | |
| 17 * | |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 */ | |
| 30 | |
| 31 #include "sky/engine/config.h" | |
| 32 #include "sky/engine/bindings/core/v8/ScriptDebugServer.h" | |
| 33 | |
| 34 #include "bindings/core/v8/V8JavaScriptCallFrame.h" | |
| 35 #include "sky/engine/bindings/core/v8/ScopedPersistent.h" | |
| 36 #include "sky/engine/bindings/core/v8/ScriptCallStackFactory.h" | |
| 37 #include "sky/engine/bindings/core/v8/ScriptController.h" | |
| 38 #include "sky/engine/bindings/core/v8/ScriptSourceCode.h" | |
| 39 #include "sky/engine/bindings/core/v8/ScriptValue.h" | |
| 40 #include "sky/engine/bindings/core/v8/V8Binding.h" | |
| 41 #include "sky/engine/bindings/core/v8/V8ScriptRunner.h" | |
| 42 #include "sky/engine/core/inspector/JavaScriptCallFrame.h" | |
| 43 #include "sky/engine/core/inspector/ScriptDebugListener.h" | |
| 44 #include "sky/engine/platform/JSONValues.h" | |
| 45 #include "sky/engine/public/platform/Platform.h" | |
| 46 #include "sky/engine/public/platform/WebData.h" | |
| 47 #include "sky/engine/wtf/StdLibExtras.h" | |
| 48 #include "sky/engine/wtf/Vector.h" | |
| 49 #include "sky/engine/wtf/dtoa/utils.h" | |
| 50 #include "sky/engine/wtf/text/CString.h" | |
| 51 | |
| 52 namespace blink { | |
| 53 | |
| 54 namespace { | |
| 55 | |
| 56 class ClientDataImpl : public v8::Debug::ClientData { | |
| 57 public: | |
| 58 ClientDataImpl(PassOwnPtr<ScriptDebugServer::Task> task) : m_task(task) { } | |
| 59 virtual ~ClientDataImpl() { } | |
| 60 ScriptDebugServer::Task* task() const { return m_task.get(); } | |
| 61 private: | |
| 62 OwnPtr<ScriptDebugServer::Task> m_task; | |
| 63 }; | |
| 64 | |
| 65 const char stepIntoV8MethodName[] = "stepIntoStatement"; | |
| 66 const char stepOutV8MethodName[] = "stepOutOfFunction"; | |
| 67 } | |
| 68 | |
| 69 v8::Local<v8::Value> ScriptDebugServer::callDebuggerMethod(const char* functionN
ame, int argc, v8::Handle<v8::Value> argv[]) | |
| 70 { | |
| 71 v8::Handle<v8::Object> debuggerScript = m_debuggerScript.newLocal(m_isolate)
; | |
| 72 v8::Handle<v8::Function> function = v8::Local<v8::Function>::Cast(debuggerSc
ript->Get(v8AtomicString(m_isolate, functionName))); | |
| 73 ASSERT(m_isolate->InContext()); | |
| 74 return V8ScriptRunner::callInternalFunction(function, debuggerScript, argc,
argv, m_isolate); | |
| 75 } | |
| 76 | |
| 77 ScriptDebugServer::ScriptDebugServer(v8::Isolate* isolate) | |
| 78 : m_pauseOnExceptionsState(DontPauseOnExceptions) | |
| 79 , m_breakpointsActivated(true) | |
| 80 , m_isolate(isolate) | |
| 81 , m_runningNestedMessageLoop(false) | |
| 82 { | |
| 83 } | |
| 84 | |
| 85 ScriptDebugServer::~ScriptDebugServer() | |
| 86 { | |
| 87 } | |
| 88 | |
| 89 String ScriptDebugServer::setBreakpoint(const String& sourceID, const ScriptBrea
kpoint& scriptBreakpoint, int* actualLineNumber, int* actualColumnNumber, bool i
nterstatementLocation) | |
| 90 { | |
| 91 v8::HandleScope scope(m_isolate); | |
| 92 v8::Local<v8::Context> debuggerContext = v8::Debug::GetDebugContext(); | |
| 93 v8::Context::Scope contextScope(debuggerContext); | |
| 94 | |
| 95 v8::Local<v8::Object> info = v8::Object::New(m_isolate); | |
| 96 info->Set(v8AtomicString(m_isolate, "sourceID"), v8String(debuggerContext->G
etIsolate(), sourceID)); | |
| 97 info->Set(v8AtomicString(m_isolate, "lineNumber"), v8::Integer::New(debugger
Context->GetIsolate(), scriptBreakpoint.lineNumber)); | |
| 98 info->Set(v8AtomicString(m_isolate, "columnNumber"), v8::Integer::New(debugg
erContext->GetIsolate(), scriptBreakpoint.columnNumber)); | |
| 99 info->Set(v8AtomicString(m_isolate, "interstatementLocation"), v8Boolean(int
erstatementLocation, debuggerContext->GetIsolate())); | |
| 100 info->Set(v8AtomicString(m_isolate, "condition"), v8String(debuggerContext->
GetIsolate(), scriptBreakpoint.condition)); | |
| 101 | |
| 102 v8::Handle<v8::Function> setBreakpointFunction = v8::Local<v8::Function>::Ca
st(m_debuggerScript.newLocal(m_isolate)->Get(v8AtomicString(m_isolate, "setBreak
point"))); | |
| 103 v8::Handle<v8::Value> breakpointId = v8::Debug::Call(setBreakpointFunction,
info); | |
| 104 if (breakpointId.IsEmpty() || !breakpointId->IsString()) | |
| 105 return ""; | |
| 106 *actualLineNumber = info->Get(v8AtomicString(m_isolate, "lineNumber"))->Int3
2Value(); | |
| 107 *actualColumnNumber = info->Get(v8AtomicString(m_isolate, "columnNumber"))->
Int32Value(); | |
| 108 return toCoreString(breakpointId.As<v8::String>()); | |
| 109 } | |
| 110 | |
| 111 void ScriptDebugServer::removeBreakpoint(const String& breakpointId) | |
| 112 { | |
| 113 v8::HandleScope scope(m_isolate); | |
| 114 v8::Local<v8::Context> debuggerContext = v8::Debug::GetDebugContext(); | |
| 115 v8::Context::Scope contextScope(debuggerContext); | |
| 116 | |
| 117 v8::Local<v8::Object> info = v8::Object::New(m_isolate); | |
| 118 info->Set(v8AtomicString(m_isolate, "breakpointId"), v8String(debuggerContex
t->GetIsolate(), breakpointId)); | |
| 119 | |
| 120 v8::Handle<v8::Function> removeBreakpointFunction = v8::Local<v8::Function>:
:Cast(m_debuggerScript.newLocal(m_isolate)->Get(v8AtomicString(m_isolate, "remov
eBreakpoint"))); | |
| 121 v8::Debug::Call(removeBreakpointFunction, info); | |
| 122 } | |
| 123 | |
| 124 void ScriptDebugServer::clearBreakpoints() | |
| 125 { | |
| 126 ensureDebuggerScriptCompiled(); | |
| 127 v8::HandleScope scope(m_isolate); | |
| 128 v8::Local<v8::Context> debuggerContext = v8::Debug::GetDebugContext(); | |
| 129 v8::Context::Scope contextScope(debuggerContext); | |
| 130 | |
| 131 v8::Handle<v8::Function> clearBreakpoints = v8::Local<v8::Function>::Cast(m_
debuggerScript.newLocal(m_isolate)->Get(v8AtomicString(m_isolate, "clearBreakpoi
nts"))); | |
| 132 v8::Debug::Call(clearBreakpoints); | |
| 133 } | |
| 134 | |
| 135 void ScriptDebugServer::setBreakpointsActivated(bool activated) | |
| 136 { | |
| 137 ensureDebuggerScriptCompiled(); | |
| 138 v8::HandleScope scope(m_isolate); | |
| 139 v8::Local<v8::Context> debuggerContext = v8::Debug::GetDebugContext(); | |
| 140 v8::Context::Scope contextScope(debuggerContext); | |
| 141 | |
| 142 v8::Local<v8::Object> info = v8::Object::New(m_isolate); | |
| 143 info->Set(v8AtomicString(m_isolate, "enabled"), v8::Boolean::New(m_isolate,
activated)); | |
| 144 v8::Handle<v8::Function> setBreakpointsActivated = v8::Local<v8::Function>::
Cast(m_debuggerScript.newLocal(m_isolate)->Get(v8AtomicString(m_isolate, "setBre
akpointsActivated"))); | |
| 145 v8::Debug::Call(setBreakpointsActivated, info); | |
| 146 | |
| 147 m_breakpointsActivated = activated; | |
| 148 } | |
| 149 | |
| 150 ScriptDebugServer::PauseOnExceptionsState ScriptDebugServer::pauseOnExceptionsSt
ate() | |
| 151 { | |
| 152 ensureDebuggerScriptCompiled(); | |
| 153 v8::HandleScope scope(m_isolate); | |
| 154 v8::Context::Scope contextScope(v8::Debug::GetDebugContext()); | |
| 155 | |
| 156 v8::Handle<v8::Value> argv[] = { v8Undefined() }; | |
| 157 v8::Handle<v8::Value> result = callDebuggerMethod("pauseOnExceptionsState",
0, argv); | |
| 158 return static_cast<ScriptDebugServer::PauseOnExceptionsState>(result->Int32V
alue()); | |
| 159 } | |
| 160 | |
| 161 void ScriptDebugServer::setPauseOnExceptionsState(PauseOnExceptionsState pauseOn
ExceptionsState) | |
| 162 { | |
| 163 ensureDebuggerScriptCompiled(); | |
| 164 v8::HandleScope scope(m_isolate); | |
| 165 v8::Context::Scope contextScope(v8::Debug::GetDebugContext()); | |
| 166 | |
| 167 v8::Handle<v8::Value> argv[] = { v8::Int32::New(m_isolate, pauseOnExceptions
State) }; | |
| 168 callDebuggerMethod("setPauseOnExceptionsState", 1, argv); | |
| 169 } | |
| 170 | |
| 171 void ScriptDebugServer::setPauseOnNextStatement(bool pause) | |
| 172 { | |
| 173 ASSERT(!isPaused()); | |
| 174 if (pause) | |
| 175 v8::Debug::DebugBreak(m_isolate); | |
| 176 else | |
| 177 v8::Debug::CancelDebugBreak(m_isolate); | |
| 178 } | |
| 179 | |
| 180 bool ScriptDebugServer::pausingOnNextStatement() | |
| 181 { | |
| 182 return v8::Debug::CheckDebugBreak(m_isolate); | |
| 183 } | |
| 184 | |
| 185 bool ScriptDebugServer::canBreakProgram() | |
| 186 { | |
| 187 if (!m_breakpointsActivated) | |
| 188 return false; | |
| 189 return m_isolate->InContext(); | |
| 190 } | |
| 191 | |
| 192 void ScriptDebugServer::breakProgram() | |
| 193 { | |
| 194 if (!canBreakProgram()) | |
| 195 return; | |
| 196 | |
| 197 v8::HandleScope scope(m_isolate); | |
| 198 if (m_breakProgramCallbackTemplate.isEmpty()) { | |
| 199 v8::Handle<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(m_iso
late); | |
| 200 templ->SetCallHandler(&ScriptDebugServer::breakProgramCallback, v8::Exte
rnal::New(m_isolate, this)); | |
| 201 m_breakProgramCallbackTemplate.set(m_isolate, templ); | |
| 202 } | |
| 203 | |
| 204 v8::Handle<v8::Function> breakProgramFunction = m_breakProgramCallbackTempla
te.newLocal(m_isolate)->GetFunction(); | |
| 205 v8::Debug::Call(breakProgramFunction); | |
| 206 } | |
| 207 | |
| 208 void ScriptDebugServer::continueProgram() | |
| 209 { | |
| 210 if (isPaused()) | |
| 211 quitMessageLoopOnPause(); | |
| 212 m_pausedScriptState.clear(); | |
| 213 m_executionState.Clear(); | |
| 214 } | |
| 215 | |
| 216 void ScriptDebugServer::stepIntoStatement() | |
| 217 { | |
| 218 ASSERT(isPaused()); | |
| 219 ASSERT(!m_executionState.IsEmpty()); | |
| 220 v8::HandleScope handleScope(m_isolate); | |
| 221 v8::Handle<v8::Value> argv[] = { m_executionState }; | |
| 222 callDebuggerMethod(stepIntoV8MethodName, 1, argv); | |
| 223 continueProgram(); | |
| 224 } | |
| 225 | |
| 226 void ScriptDebugServer::stepOverStatement() | |
| 227 { | |
| 228 ASSERT(isPaused()); | |
| 229 ASSERT(!m_executionState.IsEmpty()); | |
| 230 v8::HandleScope handleScope(m_isolate); | |
| 231 v8::Handle<v8::Value> argv[] = { m_executionState }; | |
| 232 callDebuggerMethod("stepOverStatement", 1, argv); | |
| 233 continueProgram(); | |
| 234 } | |
| 235 | |
| 236 void ScriptDebugServer::stepOutOfFunction() | |
| 237 { | |
| 238 ASSERT(isPaused()); | |
| 239 ASSERT(!m_executionState.IsEmpty()); | |
| 240 v8::HandleScope handleScope(m_isolate); | |
| 241 v8::Handle<v8::Value> argv[] = { m_executionState }; | |
| 242 callDebuggerMethod(stepOutV8MethodName, 1, argv); | |
| 243 continueProgram(); | |
| 244 } | |
| 245 | |
| 246 bool ScriptDebugServer::setScriptSource(const String& sourceID, const String& ne
wContent, bool preview, String* error, RefPtr<TypeBuilder::Debugger::SetScriptSo
urceError>& errorData, ScriptValue* newCallFrames, RefPtr<JSONObject>* result) | |
| 247 { | |
| 248 class EnableLiveEditScope { | |
| 249 public: | |
| 250 explicit EnableLiveEditScope(v8::Isolate* isolate) : m_isolate(isolate)
{ v8::Debug::SetLiveEditEnabled(m_isolate, true); } | |
| 251 ~EnableLiveEditScope() { v8::Debug::SetLiveEditEnabled(m_isolate, false)
; } | |
| 252 private: | |
| 253 v8::Isolate* m_isolate; | |
| 254 }; | |
| 255 | |
| 256 ensureDebuggerScriptCompiled(); | |
| 257 v8::HandleScope scope(m_isolate); | |
| 258 | |
| 259 OwnPtr<v8::Context::Scope> contextScope; | |
| 260 v8::Handle<v8::Context> debuggerContext = v8::Debug::GetDebugContext(); | |
| 261 if (!isPaused()) | |
| 262 contextScope = adoptPtr(new v8::Context::Scope(debuggerContext)); | |
| 263 | |
| 264 v8::Handle<v8::Value> argv[] = { v8String(m_isolate, sourceID), v8String(m_i
solate, newContent), v8Boolean(preview, m_isolate) }; | |
| 265 | |
| 266 v8::Local<v8::Value> v8result; | |
| 267 { | |
| 268 EnableLiveEditScope enableLiveEditScope(m_isolate); | |
| 269 v8::TryCatch tryCatch; | |
| 270 tryCatch.SetVerbose(false); | |
| 271 v8result = callDebuggerMethod("liveEditScriptSource", 3, argv); | |
| 272 if (tryCatch.HasCaught()) { | |
| 273 v8::Local<v8::Message> message = tryCatch.Message(); | |
| 274 if (!message.IsEmpty()) | |
| 275 *error = toCoreStringWithUndefinedOrNullCheck(message->Get()); | |
| 276 else | |
| 277 *error = "Unknown error."; | |
| 278 return false; | |
| 279 } | |
| 280 } | |
| 281 ASSERT(!v8result.IsEmpty()); | |
| 282 v8::Local<v8::Object> resultTuple = v8result->ToObject(); | |
| 283 int code = static_cast<int>(resultTuple->Get(0)->ToInteger()->Value()); | |
| 284 switch (code) { | |
| 285 case 0: | |
| 286 { | |
| 287 v8::Local<v8::Value> normalResult = resultTuple->Get(1); | |
| 288 RefPtr<JSONValue> jsonResult = v8ToJSONValue(m_isolate, normalResult
, JSONValue::maxDepth); | |
| 289 if (jsonResult) | |
| 290 *result = jsonResult->asObject(); | |
| 291 // Call stack may have changed after if the edited function was on t
he stack. | |
| 292 if (!preview && isPaused()) | |
| 293 *newCallFrames = currentCallFrames(); | |
| 294 return true; | |
| 295 } | |
| 296 // Compile error. | |
| 297 case 1: | |
| 298 { | |
| 299 RefPtr<TypeBuilder::Debugger::SetScriptSourceError::CompileError> co
mpileError = | |
| 300 TypeBuilder::Debugger::SetScriptSourceError::CompileError::creat
e() | |
| 301 .setMessage(toCoreStringWithUndefinedOrNullCheck(resultTuple
->Get(2))) | |
| 302 .setLineNumber(resultTuple->Get(3)->ToInteger()->Value()) | |
| 303 .setColumnNumber(resultTuple->Get(4)->ToInteger()->Value()); | |
| 304 | |
| 305 *error = toCoreStringWithUndefinedOrNullCheck(resultTuple->Get(1)); | |
| 306 errorData = TypeBuilder::Debugger::SetScriptSourceError::create(); | |
| 307 errorData->setCompileError(compileError); | |
| 308 return false; | |
| 309 } | |
| 310 } | |
| 311 *error = "Unknown error."; | |
| 312 return false; | |
| 313 } | |
| 314 | |
| 315 int ScriptDebugServer::frameCount() | |
| 316 { | |
| 317 ASSERT(isPaused()); | |
| 318 ASSERT(!m_executionState.IsEmpty()); | |
| 319 v8::Handle<v8::Value> argv[] = { m_executionState }; | |
| 320 v8::Handle<v8::Value> result = callDebuggerMethod("frameCount", WTF_ARRAY_LE
NGTH(argv), argv); | |
| 321 if (result->IsInt32()) | |
| 322 return result->Int32Value(); | |
| 323 return 0; | |
| 324 } | |
| 325 | |
| 326 PassRefPtr<JavaScriptCallFrame> ScriptDebugServer::toJavaScriptCallFrameUnsafe(c
onst ScriptValue& value) | |
| 327 { | |
| 328 if (value.isEmpty()) | |
| 329 return nullptr; | |
| 330 ASSERT(value.isObject()); | |
| 331 return V8JavaScriptCallFrame::toNative(v8::Handle<v8::Object>::Cast(value.v8
ValueUnsafe())); | |
| 332 } | |
| 333 | |
| 334 PassRefPtr<JavaScriptCallFrame> ScriptDebugServer::wrapCallFrames(int maximumLim
it, ScopeInfoDetails scopeDetails) | |
| 335 { | |
| 336 const int scopeBits = 2; | |
| 337 COMPILE_ASSERT(NoScopes < (1 << scopeBits), not_enough_bits_to_encode_ScopeI
nfoDetails); | |
| 338 | |
| 339 ASSERT(maximumLimit >= 0); | |
| 340 int data = (maximumLimit << scopeBits) | scopeDetails; | |
| 341 v8::Handle<v8::Value> currentCallFrameV8; | |
| 342 if (m_executionState.IsEmpty()) { | |
| 343 v8::Handle<v8::Function> currentCallFrameFunction = v8::Local<v8::Functi
on>::Cast(m_debuggerScript.newLocal(m_isolate)->Get(v8AtomicString(m_isolate, "c
urrentCallFrame"))); | |
| 344 currentCallFrameV8 = v8::Debug::Call(currentCallFrameFunction, v8::Integ
er::New(m_isolate, data)); | |
| 345 } else { | |
| 346 v8::Handle<v8::Value> argv[] = { m_executionState, v8::Integer::New(m_is
olate, data) }; | |
| 347 currentCallFrameV8 = callDebuggerMethod("currentCallFrame", WTF_ARRAY_LE
NGTH(argv), argv); | |
| 348 } | |
| 349 ASSERT(!currentCallFrameV8.IsEmpty()); | |
| 350 if (!currentCallFrameV8->IsObject()) | |
| 351 return nullptr; | |
| 352 return JavaScriptCallFrame::create(v8::Debug::GetDebugContext(), v8::Handle<
v8::Object>::Cast(currentCallFrameV8)); | |
| 353 } | |
| 354 | |
| 355 ScriptValue ScriptDebugServer::currentCallFramesInner(ScopeInfoDetails scopeDeta
ils) | |
| 356 { | |
| 357 if (!m_isolate->InContext()) | |
| 358 return ScriptValue(); | |
| 359 v8::HandleScope handleScope(m_isolate); | |
| 360 | |
| 361 // Filter out stack traces entirely consisting of V8's internal scripts. | |
| 362 v8::Local<v8::StackTrace> stackTrace = v8::StackTrace::CurrentStackTrace(m_i
solate, 1); | |
| 363 if (!stackTrace->GetFrameCount()) | |
| 364 return ScriptValue(); | |
| 365 | |
| 366 RefPtr<JavaScriptCallFrame> currentCallFrame = wrapCallFrames(0, scopeDetail
s); | |
| 367 if (!currentCallFrame) | |
| 368 return ScriptValue(); | |
| 369 | |
| 370 ScriptState* scriptState = m_pausedScriptState ? m_pausedScriptState.get() :
ScriptState::current(m_isolate); | |
| 371 ScriptState::Scope scope(scriptState); | |
| 372 return ScriptValue(scriptState, toV8(currentCallFrame.release(), scriptState
->context()->Global(), m_isolate)); | |
| 373 } | |
| 374 | |
| 375 ScriptValue ScriptDebugServer::currentCallFrames() | |
| 376 { | |
| 377 return currentCallFramesInner(AllScopes); | |
| 378 } | |
| 379 | |
| 380 ScriptValue ScriptDebugServer::currentCallFramesForAsyncStack() | |
| 381 { | |
| 382 return currentCallFramesInner(FastAsyncScopes); | |
| 383 } | |
| 384 | |
| 385 PassRefPtr<JavaScriptCallFrame> ScriptDebugServer::callFrameNoScopes(int index) | |
| 386 { | |
| 387 v8::Handle<v8::Value> currentCallFrameV8; | |
| 388 if (m_executionState.IsEmpty()) { | |
| 389 v8::Handle<v8::Function> currentCallFrameFunction = v8::Local<v8::Functi
on>::Cast(m_debuggerScript.newLocal(m_isolate)->Get(v8AtomicString(m_isolate, "c
urrentCallFrameByIndex"))); | |
| 390 currentCallFrameV8 = v8::Debug::Call(currentCallFrameFunction, v8::Integ
er::New(m_isolate, index)); | |
| 391 } else { | |
| 392 v8::Handle<v8::Value> argv[] = { m_executionState, v8::Integer::New(m_is
olate, index) }; | |
| 393 currentCallFrameV8 = callDebuggerMethod("currentCallFrameByIndex", WTF_A
RRAY_LENGTH(argv), argv); | |
| 394 } | |
| 395 ASSERT(!currentCallFrameV8.IsEmpty()); | |
| 396 if (!currentCallFrameV8->IsObject()) | |
| 397 return nullptr; | |
| 398 return JavaScriptCallFrame::create(v8::Debug::GetDebugContext(), v8::Handle<
v8::Object>::Cast(currentCallFrameV8)); | |
| 399 } | |
| 400 | |
| 401 void ScriptDebugServer::interruptAndRun(PassOwnPtr<Task> task, v8::Isolate* isol
ate) | |
| 402 { | |
| 403 v8::Debug::DebugBreakForCommand(isolate, new ClientDataImpl(task)); | |
| 404 } | |
| 405 | |
| 406 void ScriptDebugServer::runPendingTasks() | |
| 407 { | |
| 408 v8::Debug::ProcessDebugMessages(); | |
| 409 } | |
| 410 | |
| 411 static ScriptDebugServer* toScriptDebugServer(v8::Handle<v8::Value> data) | |
| 412 { | |
| 413 void* p = v8::Handle<v8::External>::Cast(data)->Value(); | |
| 414 return static_cast<ScriptDebugServer*>(p); | |
| 415 } | |
| 416 | |
| 417 void ScriptDebugServer::breakProgramCallback(const v8::FunctionCallbackInfo<v8::
Value>& info) | |
| 418 { | |
| 419 ASSERT(2 == info.Length()); | |
| 420 ScriptDebugServer* thisPtr = toScriptDebugServer(info.Data()); | |
| 421 ScriptState* pausedScriptState = ScriptState::current(thisPtr->m_isolate); | |
| 422 v8::Handle<v8::Value> exception; | |
| 423 v8::Handle<v8::Array> hitBreakpoints; | |
| 424 thisPtr->handleProgramBreak(pausedScriptState, v8::Handle<v8::Object>::Cast(
info[0]), exception, hitBreakpoints); | |
| 425 } | |
| 426 | |
| 427 void ScriptDebugServer::handleProgramBreak(ScriptState* pausedScriptState, v8::H
andle<v8::Object> executionState, v8::Handle<v8::Value> exception, v8::Handle<v8
::Array> hitBreakpointNumbers) | |
| 428 { | |
| 429 // Don't allow nested breaks. | |
| 430 if (isPaused()) | |
| 431 return; | |
| 432 | |
| 433 ScriptDebugListener* listener = getDebugListenerForContext(pausedScriptState
->context()); | |
| 434 if (!listener) | |
| 435 return; | |
| 436 | |
| 437 Vector<String> breakpointIds; | |
| 438 if (!hitBreakpointNumbers.IsEmpty()) { | |
| 439 breakpointIds.resize(hitBreakpointNumbers->Length()); | |
| 440 for (size_t i = 0; i < hitBreakpointNumbers->Length(); i++) { | |
| 441 v8::Handle<v8::Value> hitBreakpointNumber = hitBreakpointNumbers->Ge
t(i); | |
| 442 ASSERT(!hitBreakpointNumber.IsEmpty() && hitBreakpointNumber->IsInt3
2()); | |
| 443 breakpointIds[i] = String::number(hitBreakpointNumber->Int32Value())
; | |
| 444 } | |
| 445 } | |
| 446 | |
| 447 m_pausedScriptState = pausedScriptState; | |
| 448 m_executionState = executionState; | |
| 449 ScriptDebugListener::SkipPauseRequest result = listener->didPause(pausedScri
ptState, currentCallFrames(), ScriptValue(pausedScriptState, exception), breakpo
intIds); | |
| 450 if (result == ScriptDebugListener::NoSkip) { | |
| 451 m_runningNestedMessageLoop = true; | |
| 452 runMessageLoopOnPause(pausedScriptState->context()); | |
| 453 m_runningNestedMessageLoop = false; | |
| 454 } | |
| 455 m_pausedScriptState.clear(); | |
| 456 m_executionState.Clear(); | |
| 457 | |
| 458 if (result == ScriptDebugListener::StepInto) { | |
| 459 v8::Handle<v8::Value> argv[] = { executionState }; | |
| 460 callDebuggerMethod(stepIntoV8MethodName, 1, argv); | |
| 461 } else if (result == ScriptDebugListener::StepOut) { | |
| 462 v8::Handle<v8::Value> argv[] = { executionState }; | |
| 463 callDebuggerMethod(stepOutV8MethodName, 1, argv); | |
| 464 } | |
| 465 } | |
| 466 | |
| 467 void ScriptDebugServer::v8DebugEventCallback(const v8::Debug::EventDetails& even
tDetails) | |
| 468 { | |
| 469 ScriptDebugServer* thisPtr = toScriptDebugServer(eventDetails.GetCallbackDat
a()); | |
| 470 thisPtr->handleV8DebugEvent(eventDetails); | |
| 471 } | |
| 472 | |
| 473 static v8::Handle<v8::Value> callInternalGetterFunction(v8::Handle<v8::Object> o
bject, const char* functionName, v8::Isolate* isolate) | |
| 474 { | |
| 475 v8::Handle<v8::Value> getterValue = object->Get(v8AtomicString(isolate, func
tionName)); | |
| 476 ASSERT(!getterValue.IsEmpty() && getterValue->IsFunction()); | |
| 477 return V8ScriptRunner::callInternalFunction(v8::Handle<v8::Function>::Cast(g
etterValue), object, 0, 0, isolate); | |
| 478 } | |
| 479 | |
| 480 void ScriptDebugServer::handleV8DebugEvent(const v8::Debug::EventDetails& eventD
etails) | |
| 481 { | |
| 482 v8::DebugEvent event = eventDetails.GetEvent(); | |
| 483 | |
| 484 if (event == v8::BreakForCommand) { | |
| 485 ClientDataImpl* data = static_cast<ClientDataImpl*>(eventDetails.GetClie
ntData()); | |
| 486 data->task()->run(); | |
| 487 return; | |
| 488 } | |
| 489 | |
| 490 if (event != v8::AsyncTaskEvent && event != v8::Break && event != v8::Except
ion && event != v8::AfterCompile && event != v8::BeforeCompile && event != v8::C
ompileError && event != v8::PromiseEvent) | |
| 491 return; | |
| 492 | |
| 493 v8::Handle<v8::Context> eventContext = eventDetails.GetEventContext(); | |
| 494 ASSERT(!eventContext.IsEmpty()); | |
| 495 | |
| 496 ScriptDebugListener* listener = getDebugListenerForContext(eventContext); | |
| 497 if (listener) { | |
| 498 v8::HandleScope scope(m_isolate); | |
| 499 v8::Handle<v8::Object> debuggerScript = m_debuggerScript.newLocal(m_isol
ate); | |
| 500 if (event == v8::BeforeCompile) { | |
| 501 preprocessBeforeCompile(eventDetails); | |
| 502 } else if (event == v8::AfterCompile || event == v8::CompileError) { | |
| 503 v8::Context::Scope contextScope(v8::Debug::GetDebugContext()); | |
| 504 v8::Handle<v8::Function> getAfterCompileScript = v8::Local<v8::Funct
ion>::Cast(debuggerScript->Get(v8AtomicString(m_isolate, "getAfterCompileScript"
))); | |
| 505 v8::Handle<v8::Value> argv[] = { eventDetails.GetEventData() }; | |
| 506 v8::Handle<v8::Value> value = V8ScriptRunner::callInternalFunction(g
etAfterCompileScript, debuggerScript, WTF_ARRAY_LENGTH(argv), argv, m_isolate); | |
| 507 ASSERT(value->IsObject()); | |
| 508 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(value); | |
| 509 dispatchDidParseSource(listener, object, event != v8::AfterCompile ?
CompileError : CompileSuccess); | |
| 510 } else if (event == v8::Exception) { | |
| 511 v8::Handle<v8::Object> eventData = eventDetails.GetEventData(); | |
| 512 v8::Handle<v8::Value> exception = callInternalGetterFunction(eventDa
ta, "exception", m_isolate); | |
| 513 handleProgramBreak(ScriptState::from(eventContext), eventDetails.Get
ExecutionState(), exception, v8::Handle<v8::Array>()); | |
| 514 } else if (event == v8::Break) { | |
| 515 v8::Handle<v8::Function> getBreakpointNumbersFunction = v8::Local<v8
::Function>::Cast(debuggerScript->Get(v8AtomicString(m_isolate, "getBreakpointNu
mbers"))); | |
| 516 v8::Handle<v8::Value> argv[] = { eventDetails.GetEventData() }; | |
| 517 v8::Handle<v8::Value> hitBreakpoints = V8ScriptRunner::callInternalF
unction(getBreakpointNumbersFunction, debuggerScript, WTF_ARRAY_LENGTH(argv), ar
gv, m_isolate); | |
| 518 ASSERT(hitBreakpoints->IsArray()); | |
| 519 handleProgramBreak(ScriptState::from(eventContext), eventDetails.Get
ExecutionState(), v8::Handle<v8::Value>(), hitBreakpoints.As<v8::Array>()); | |
| 520 } else if (event == v8::AsyncTaskEvent) { | |
| 521 handleV8AsyncTaskEvent(listener, ScriptState::from(eventContext), ev
entDetails.GetExecutionState(), eventDetails.GetEventData()); | |
| 522 } else if (event == v8::PromiseEvent) { | |
| 523 handleV8PromiseEvent(listener, ScriptState::from(eventContext), even
tDetails.GetExecutionState(), eventDetails.GetEventData()); | |
| 524 } | |
| 525 } | |
| 526 } | |
| 527 | |
| 528 void ScriptDebugServer::handleV8AsyncTaskEvent(ScriptDebugListener* listener, Sc
riptState* pausedScriptState, v8::Handle<v8::Object> executionState, v8::Handle<
v8::Object> eventData) | |
| 529 { | |
| 530 String type = toCoreStringWithUndefinedOrNullCheck(callInternalGetterFunctio
n(eventData, "type", m_isolate)); | |
| 531 String name = toCoreStringWithUndefinedOrNullCheck(callInternalGetterFunctio
n(eventData, "name", m_isolate)); | |
| 532 int id = callInternalGetterFunction(eventData, "id", m_isolate)->ToInteger()
->Value(); | |
| 533 | |
| 534 m_pausedScriptState = pausedScriptState; | |
| 535 m_executionState = executionState; | |
| 536 listener->didReceiveV8AsyncTaskEvent(pausedScriptState->executionContext(),
type, name, id); | |
| 537 m_pausedScriptState.clear(); | |
| 538 m_executionState.Clear(); | |
| 539 } | |
| 540 | |
| 541 void ScriptDebugServer::handleV8PromiseEvent(ScriptDebugListener* listener, Scri
ptState* pausedScriptState, v8::Handle<v8::Object> executionState, v8::Handle<v8
::Object> eventData) | |
| 542 { | |
| 543 v8::Handle<v8::Value> argv[] = { eventData }; | |
| 544 v8::Local<v8::Object> promiseDetails = callDebuggerMethod("getPromiseDetails
", 1, argv)->ToObject(); | |
| 545 v8::Handle<v8::Object> promise = promiseDetails->Get(v8AtomicString(m_isolat
e, "promise"))->ToObject(); | |
| 546 int status = promiseDetails->Get(v8AtomicString(m_isolate, "status"))->ToInt
eger()->Value(); | |
| 547 v8::Handle<v8::Value> parentPromise = promiseDetails->Get(v8AtomicString(m_i
solate, "parentPromise")); | |
| 548 | |
| 549 m_pausedScriptState = pausedScriptState; | |
| 550 m_executionState = executionState; | |
| 551 listener->didReceiveV8PromiseEvent(pausedScriptState, promise, parentPromise
, status); | |
| 552 m_pausedScriptState.clear(); | |
| 553 m_executionState.Clear(); | |
| 554 } | |
| 555 | |
| 556 void ScriptDebugServer::dispatchDidParseSource(ScriptDebugListener* listener, v8
::Handle<v8::Object> object, CompileResult compileResult) | |
| 557 { | |
| 558 v8::Handle<v8::Value> id = object->Get(v8AtomicString(m_isolate, "id")); | |
| 559 ASSERT(!id.IsEmpty() && id->IsInt32()); | |
| 560 String sourceID = String::number(id->Int32Value()); | |
| 561 | |
| 562 ScriptDebugListener::Script script; | |
| 563 script.url = toCoreStringWithUndefinedOrNullCheck(object->Get(v8AtomicString
(m_isolate, "name"))); | |
| 564 script.sourceURL = toCoreStringWithUndefinedOrNullCheck(object->Get(v8Atomic
String(m_isolate, "sourceURL"))); | |
| 565 script.sourceMappingURL = toCoreStringWithUndefinedOrNullCheck(object->Get(v
8AtomicString(m_isolate, "sourceMappingURL"))); | |
| 566 script.source = toCoreStringWithUndefinedOrNullCheck(object->Get(v8AtomicStr
ing(m_isolate, "source"))); | |
| 567 script.startLine = object->Get(v8AtomicString(m_isolate, "startLine"))->ToIn
teger()->Value(); | |
| 568 script.startColumn = object->Get(v8AtomicString(m_isolate, "startColumn"))->
ToInteger()->Value(); | |
| 569 script.endLine = object->Get(v8AtomicString(m_isolate, "endLine"))->ToIntege
r()->Value(); | |
| 570 script.endColumn = object->Get(v8AtomicString(m_isolate, "endColumn"))->ToIn
teger()->Value(); | |
| 571 script.isContentScript = object->Get(v8AtomicString(m_isolate, "isContentScr
ipt"))->ToBoolean()->Value(); | |
| 572 | |
| 573 listener->didParseSource(sourceID, script, compileResult); | |
| 574 } | |
| 575 | |
| 576 void ScriptDebugServer::ensureDebuggerScriptCompiled() | |
| 577 { | |
| 578 if (!m_debuggerScript.isEmpty()) | |
| 579 return; | |
| 580 | |
| 581 v8::HandleScope scope(m_isolate); | |
| 582 v8::Context::Scope contextScope(v8::Debug::GetDebugContext()); | |
| 583 const blink::WebData& debuggerScriptSourceResource = blink::Platform::curren
t()->loadResource("DebuggerScript.js"); | |
| 584 v8::Handle<v8::String> source = v8String(m_isolate, String(debuggerScriptSou
rceResource.data(), debuggerScriptSourceResource.size())); | |
| 585 v8::Local<v8::Value> value = V8ScriptRunner::compileAndRunInternalScript(sou
rce, m_isolate); | |
| 586 ASSERT(!value.IsEmpty()); | |
| 587 ASSERT(value->IsObject()); | |
| 588 m_debuggerScript.set(m_isolate, v8::Handle<v8::Object>::Cast(value)); | |
| 589 } | |
| 590 | |
| 591 void ScriptDebugServer::discardDebuggerScript() | |
| 592 { | |
| 593 ASSERT(!m_debuggerScript.isEmpty()); | |
| 594 m_debuggerScript.clear(); | |
| 595 } | |
| 596 | |
| 597 v8::Local<v8::Value> ScriptDebugServer::functionScopes(v8::Handle<v8::Function>
function) | |
| 598 { | |
| 599 ensureDebuggerScriptCompiled(); | |
| 600 | |
| 601 v8::Handle<v8::Value> argv[] = { function }; | |
| 602 return callDebuggerMethod("getFunctionScopes", 1, argv); | |
| 603 } | |
| 604 | |
| 605 v8::Local<v8::Value> ScriptDebugServer::collectionEntries(v8::Handle<v8::Object>
& object) | |
| 606 { | |
| 607 ensureDebuggerScriptCompiled(); | |
| 608 | |
| 609 v8::Handle<v8::Value> argv[] = { object }; | |
| 610 return callDebuggerMethod("getCollectionEntries", 1, argv); | |
| 611 } | |
| 612 | |
| 613 v8::Local<v8::Value> ScriptDebugServer::getInternalProperties(v8::Handle<v8::Obj
ect>& object) | |
| 614 { | |
| 615 if (m_debuggerScript.isEmpty()) | |
| 616 return v8::Local<v8::Value>::New(m_isolate, v8::Undefined(m_isolate)); | |
| 617 | |
| 618 v8::Handle<v8::Value> argv[] = { object }; | |
| 619 return callDebuggerMethod("getInternalProperties", 1, argv); | |
| 620 } | |
| 621 | |
| 622 v8::Handle<v8::Value> ScriptDebugServer::setFunctionVariableValue(v8::Handle<v8:
:Value> functionValue, int scopeNumber, const String& variableName, v8::Handle<v
8::Value> newValue) | |
| 623 { | |
| 624 v8::Local<v8::Context> debuggerContext = v8::Debug::GetDebugContext(); | |
| 625 if (m_debuggerScript.isEmpty()) | |
| 626 return m_isolate->ThrowException(v8::String::NewFromUtf8(m_isolate, "Deb
ugging is not enabled.")); | |
| 627 | |
| 628 v8::Handle<v8::Value> argv[] = { | |
| 629 functionValue, | |
| 630 v8::Handle<v8::Value>(v8::Integer::New(debuggerContext->GetIsolate(), sc
opeNumber)), | |
| 631 v8String(debuggerContext->GetIsolate(), variableName), | |
| 632 newValue | |
| 633 }; | |
| 634 return callDebuggerMethod("setFunctionVariableValue", 4, argv); | |
| 635 } | |
| 636 | |
| 637 bool ScriptDebugServer::isPaused() | |
| 638 { | |
| 639 return m_pausedScriptState; | |
| 640 } | |
| 641 | |
| 642 void ScriptDebugServer::compileScript(ScriptState* scriptState, const String& ex
pression, const String& sourceURL, String* scriptId, String* exceptionDetailsTex
t, int* lineNumber, int* columnNumber, RefPtr<ScriptCallStack>* stackTrace) | |
| 643 { | |
| 644 if (scriptState->contextIsEmpty()) | |
| 645 return; | |
| 646 ScriptState::Scope scope(scriptState); | |
| 647 | |
| 648 v8::Handle<v8::String> source = v8String(m_isolate, expression); | |
| 649 v8::TryCatch tryCatch; | |
| 650 v8::Local<v8::Script> script = V8ScriptRunner::compileScript(source, sourceU
RL, TextPosition(), m_isolate); | |
| 651 if (tryCatch.HasCaught()) { | |
| 652 v8::Local<v8::Message> message = tryCatch.Message(); | |
| 653 if (!message.IsEmpty()) { | |
| 654 *exceptionDetailsText = toCoreStringWithUndefinedOrNullCheck(message
->Get()); | |
| 655 *lineNumber = message->GetLineNumber(); | |
| 656 *columnNumber = message->GetStartColumn(); | |
| 657 *stackTrace = createScriptCallStack(message->GetStackTrace(), messag
e->GetStackTrace()->GetFrameCount(), m_isolate); | |
| 658 } | |
| 659 return; | |
| 660 } | |
| 661 if (script.IsEmpty()) | |
| 662 return; | |
| 663 | |
| 664 *scriptId = String::number(script->GetUnboundScript()->GetId()); | |
| 665 m_compiledScripts.set(*scriptId, adoptPtr(new ScopedPersistent<v8::Script>(m
_isolate, script))); | |
| 666 } | |
| 667 | |
| 668 void ScriptDebugServer::clearCompiledScripts() | |
| 669 { | |
| 670 m_compiledScripts.clear(); | |
| 671 } | |
| 672 | |
| 673 void ScriptDebugServer::runScript(ScriptState* scriptState, const String& script
Id, ScriptValue* result, bool* wasThrown, String* exceptionDetailsText, int* lin
eNumber, int* columnNumber, RefPtr<ScriptCallStack>* stackTrace) | |
| 674 { | |
| 675 if (!m_compiledScripts.contains(scriptId)) | |
| 676 return; | |
| 677 v8::HandleScope handleScope(m_isolate); | |
| 678 ScopedPersistent<v8::Script>* scriptHandle = m_compiledScripts.get(scriptId)
; | |
| 679 v8::Local<v8::Script> script = scriptHandle->newLocal(m_isolate); | |
| 680 m_compiledScripts.remove(scriptId); | |
| 681 if (script.IsEmpty()) | |
| 682 return; | |
| 683 | |
| 684 if (scriptState->contextIsEmpty()) | |
| 685 return; | |
| 686 ScriptState::Scope scope(scriptState); | |
| 687 v8::TryCatch tryCatch; | |
| 688 v8::Local<v8::Value> value = V8ScriptRunner::runCompiledScript(script, scrip
tState->executionContext(), m_isolate); | |
| 689 *wasThrown = false; | |
| 690 if (tryCatch.HasCaught()) { | |
| 691 *wasThrown = true; | |
| 692 *result = ScriptValue(scriptState, tryCatch.Exception()); | |
| 693 v8::Local<v8::Message> message = tryCatch.Message(); | |
| 694 if (!message.IsEmpty()) { | |
| 695 *exceptionDetailsText = toCoreStringWithUndefinedOrNullCheck(message
->Get()); | |
| 696 *lineNumber = message->GetLineNumber(); | |
| 697 *columnNumber = message->GetStartColumn(); | |
| 698 *stackTrace = createScriptCallStack(message->GetStackTrace(), messag
e->GetStackTrace()->GetFrameCount(), m_isolate); | |
| 699 } | |
| 700 } else { | |
| 701 *result = ScriptValue(scriptState, value); | |
| 702 } | |
| 703 } | |
| 704 | |
| 705 PassOwnPtr<ScriptSourceCode> ScriptDebugServer::preprocess(LocalFrame*, const Sc
riptSourceCode&) | |
| 706 { | |
| 707 return PassOwnPtr<ScriptSourceCode>(); | |
| 708 } | |
| 709 | |
| 710 String ScriptDebugServer::preprocessEventListener(LocalFrame*, const String& sou
rce, const String& url, const String& functionName) | |
| 711 { | |
| 712 return source; | |
| 713 } | |
| 714 | |
| 715 } // namespace blink | |
| OLD | NEW |