| 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 "config.h" | |
| 32 #include "bindings/core/v8/V8Debugger.h" | |
| 33 | |
| 34 #include "bindings/core/v8/ScriptValue.h" | |
| 35 #include "bindings/core/v8/V8Binding.h" | |
| 36 #include "bindings/core/v8/V8ScriptRunner.h" | |
| 37 #include "bindings/core/v8/inspector/V8JavaScriptCallFrame.h" | |
| 38 #include "core/inspector/JavaScriptCallFrame.h" | |
| 39 #include "core/inspector/ScriptDebugListener.h" | |
| 40 #include "platform/JSONValues.h" | |
| 41 #include "wtf/StdLibExtras.h" | |
| 42 #include "wtf/Vector.h" | |
| 43 #include "wtf/dtoa/utils.h" | |
| 44 #include "wtf/text/CString.h" | |
| 45 | |
| 46 namespace blink { | |
| 47 | |
| 48 namespace { | |
| 49 const char stepIntoV8MethodName[] = "stepIntoStatement"; | |
| 50 const char stepOutV8MethodName[] = "stepOutOfFunction"; | |
| 51 } | |
| 52 | |
| 53 v8::MaybeLocal<v8::Value> V8Debugger::callDebuggerMethod(const char* functionNam
e, int argc, v8::Local<v8::Value> argv[]) | |
| 54 { | |
| 55 v8::Local<v8::Object> debuggerScript = debuggerScriptLocal(); | |
| 56 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast(debuggerScr
ipt->Get(v8InternalizedString(functionName))); | |
| 57 ASSERT(m_isolate->InContext()); | |
| 58 return V8ScriptRunner::callInternalFunction(function, debuggerScript, argc,
argv, m_isolate); | |
| 59 } | |
| 60 | |
| 61 V8Debugger::V8Debugger(v8::Isolate* isolate, Client* client) | |
| 62 : m_isolate(isolate) | |
| 63 , m_client(client) | |
| 64 , m_breakpointsActivated(true) | |
| 65 , m_runningNestedMessageLoop(false) | |
| 66 { | |
| 67 } | |
| 68 | |
| 69 V8Debugger::~V8Debugger() | |
| 70 { | |
| 71 } | |
| 72 | |
| 73 DEFINE_TRACE(V8Debugger) | |
| 74 { | |
| 75 } | |
| 76 | |
| 77 void V8Debugger::enable() | |
| 78 { | |
| 79 ASSERT(!enabled()); | |
| 80 v8::HandleScope scope(m_isolate); | |
| 81 v8::Debug::SetDebugEventListener(&V8Debugger::v8DebugEventCallback, v8::Exte
rnal::New(m_isolate, this)); | |
| 82 m_debuggerContext.Reset(m_isolate, v8::Debug::GetDebugContext()); | |
| 83 m_callFrameWrapperTemplate.Reset(m_isolate, V8JavaScriptCallFrame::createWra
pperTemplate(m_isolate)); | |
| 84 compileDebuggerScript(); | |
| 85 } | |
| 86 | |
| 87 void V8Debugger::disable() | |
| 88 { | |
| 89 ASSERT(enabled()); | |
| 90 clearBreakpoints(); | |
| 91 m_debuggerScript.Reset(); | |
| 92 m_debuggerContext.Reset(); | |
| 93 m_callFrameWrapperTemplate.Reset(); | |
| 94 v8::Debug::SetDebugEventListener(nullptr); | |
| 95 } | |
| 96 | |
| 97 bool V8Debugger::enabled() const | |
| 98 { | |
| 99 return !m_debuggerScript.IsEmpty(); | |
| 100 } | |
| 101 | |
| 102 void V8Debugger::setContextDebugData(v8::Local<v8::Context> context, const Strin
g& contextDebugData) | |
| 103 { | |
| 104 v8::HandleScope scope(context->GetIsolate()); | |
| 105 v8::Context::Scope contextScope(context); | |
| 106 context->SetEmbedderData(static_cast<int>(gin::kDebugIdIndex), v8String(cont
ext->GetIsolate(), contextDebugData)); | |
| 107 } | |
| 108 | |
| 109 void V8Debugger::reportCompiledScripts(const String& contextDebugDataSubstring,
ScriptDebugListener* listener) | |
| 110 { | |
| 111 v8::HandleScope scope(m_isolate); | |
| 112 v8::Context::Scope contextScope(debuggerContext()); | |
| 113 | |
| 114 v8::Local<v8::Object> debuggerScript = debuggerScriptLocal(); | |
| 115 ASSERT(!debuggerScript->IsUndefined()); | |
| 116 v8::Local<v8::Function> getScriptsFunction = v8::Local<v8::Function>::Cast(d
ebuggerScript->Get(v8InternalizedString("getScripts"))); | |
| 117 v8::Local<v8::Value> argv[] = { v8String(m_isolate, contextDebugDataSubstrin
g) }; | |
| 118 v8::Local<v8::Value> value; | |
| 119 if (!V8ScriptRunner::callInternalFunction(getScriptsFunction, debuggerScript
, WTF_ARRAY_LENGTH(argv), argv, m_isolate).ToLocal(&value)) | |
| 120 return; | |
| 121 ASSERT(value->IsArray()); | |
| 122 v8::Local<v8::Array> scriptsArray = v8::Local<v8::Array>::Cast(value); | |
| 123 for (unsigned i = 0; i < scriptsArray->Length(); ++i) | |
| 124 dispatchDidParseSource(listener, v8::Local<v8::Object>::Cast(scriptsArra
y->Get(v8::Integer::New(m_isolate, i))), CompileSuccess); | |
| 125 } | |
| 126 | |
| 127 String V8Debugger::setBreakpoint(const String& sourceID, const ScriptBreakpoint&
scriptBreakpoint, int* actualLineNumber, int* actualColumnNumber, bool intersta
tementLocation) | |
| 128 { | |
| 129 v8::HandleScope scope(m_isolate); | |
| 130 v8::Context::Scope contextScope(debuggerContext()); | |
| 131 | |
| 132 v8::Local<v8::Object> info = v8::Object::New(m_isolate); | |
| 133 info->Set(v8InternalizedString("sourceID"), v8String(m_isolate, sourceID)); | |
| 134 info->Set(v8InternalizedString("lineNumber"), v8::Integer::New(m_isolate, sc
riptBreakpoint.lineNumber)); | |
| 135 info->Set(v8InternalizedString("columnNumber"), v8::Integer::New(m_isolate,
scriptBreakpoint.columnNumber)); | |
| 136 info->Set(v8InternalizedString("interstatementLocation"), v8Boolean(intersta
tementLocation, m_isolate)); | |
| 137 info->Set(v8InternalizedString("condition"), v8String(m_isolate, scriptBreak
point.condition)); | |
| 138 | |
| 139 v8::Local<v8::Function> setBreakpointFunction = v8::Local<v8::Function>::Cas
t(debuggerScriptLocal()->Get(v8InternalizedString("setBreakpoint"))); | |
| 140 v8::Local<v8::Value> breakpointId = v8::Debug::Call(setBreakpointFunction, i
nfo); | |
| 141 if (breakpointId.IsEmpty() || !breakpointId->IsString()) | |
| 142 return ""; | |
| 143 *actualLineNumber = info->Get(v8InternalizedString("lineNumber"))->Int32Valu
e(); | |
| 144 *actualColumnNumber = info->Get(v8InternalizedString("columnNumber"))->Int32
Value(); | |
| 145 return toCoreString(breakpointId.As<v8::String>()); | |
| 146 } | |
| 147 | |
| 148 void V8Debugger::removeBreakpoint(const String& breakpointId) | |
| 149 { | |
| 150 v8::HandleScope scope(m_isolate); | |
| 151 v8::Context::Scope contextScope(debuggerContext()); | |
| 152 | |
| 153 v8::Local<v8::Object> info = v8::Object::New(m_isolate); | |
| 154 info->Set(v8InternalizedString("breakpointId"), v8String(m_isolate, breakpoi
ntId)); | |
| 155 | |
| 156 v8::Local<v8::Function> removeBreakpointFunction = v8::Local<v8::Function>::
Cast(debuggerScriptLocal()->Get(v8InternalizedString("removeBreakpoint"))); | |
| 157 v8::Debug::Call(removeBreakpointFunction, info); | |
| 158 } | |
| 159 | |
| 160 void V8Debugger::clearBreakpoints() | |
| 161 { | |
| 162 v8::HandleScope scope(m_isolate); | |
| 163 v8::Context::Scope contextScope(debuggerContext()); | |
| 164 | |
| 165 v8::Local<v8::Function> clearBreakpoints = v8::Local<v8::Function>::Cast(deb
uggerScriptLocal()->Get(v8InternalizedString("clearBreakpoints"))); | |
| 166 v8::Debug::Call(clearBreakpoints); | |
| 167 } | |
| 168 | |
| 169 void V8Debugger::setBreakpointsActivated(bool activated) | |
| 170 { | |
| 171 if (!enabled()) { | |
| 172 ASSERT_NOT_REACHED(); | |
| 173 return; | |
| 174 } | |
| 175 v8::HandleScope scope(m_isolate); | |
| 176 v8::Context::Scope contextScope(debuggerContext()); | |
| 177 | |
| 178 v8::Local<v8::Object> info = v8::Object::New(m_isolate); | |
| 179 info->Set(v8InternalizedString("enabled"), v8::Boolean::New(m_isolate, activ
ated)); | |
| 180 v8::Local<v8::Function> setBreakpointsActivated = v8::Local<v8::Function>::C
ast(debuggerScriptLocal()->Get(v8InternalizedString("setBreakpointsActivated")))
; | |
| 181 v8::Debug::Call(setBreakpointsActivated, info); | |
| 182 | |
| 183 m_breakpointsActivated = activated; | |
| 184 } | |
| 185 | |
| 186 V8Debugger::PauseOnExceptionsState V8Debugger::pauseOnExceptionsState() | |
| 187 { | |
| 188 ASSERT(enabled()); | |
| 189 v8::HandleScope scope(m_isolate); | |
| 190 v8::Context::Scope contextScope(debuggerContext()); | |
| 191 | |
| 192 v8::Local<v8::Value> argv[] = { v8Undefined() }; | |
| 193 v8::Local<v8::Value> result = callDebuggerMethod("pauseOnExceptionsState", 0
, argv).ToLocalChecked(); | |
| 194 return static_cast<V8Debugger::PauseOnExceptionsState>(result->Int32Value())
; | |
| 195 } | |
| 196 | |
| 197 void V8Debugger::setPauseOnExceptionsState(PauseOnExceptionsState pauseOnExcepti
onsState) | |
| 198 { | |
| 199 ASSERT(enabled()); | |
| 200 v8::HandleScope scope(m_isolate); | |
| 201 v8::Context::Scope contextScope(debuggerContext()); | |
| 202 | |
| 203 v8::Local<v8::Value> argv[] = { v8::Int32::New(m_isolate, pauseOnExceptionsS
tate) }; | |
| 204 callDebuggerMethod("setPauseOnExceptionsState", 1, argv); | |
| 205 } | |
| 206 | |
| 207 void V8Debugger::setPauseOnNextStatement(bool pause) | |
| 208 { | |
| 209 ASSERT(!m_runningNestedMessageLoop); | |
| 210 if (pause) | |
| 211 v8::Debug::DebugBreak(m_isolate); | |
| 212 else | |
| 213 v8::Debug::CancelDebugBreak(m_isolate); | |
| 214 } | |
| 215 | |
| 216 bool V8Debugger::pausingOnNextStatement() | |
| 217 { | |
| 218 return v8::Debug::CheckDebugBreak(m_isolate); | |
| 219 } | |
| 220 | |
| 221 bool V8Debugger::canBreakProgram() | |
| 222 { | |
| 223 if (!m_breakpointsActivated) | |
| 224 return false; | |
| 225 return m_isolate->InContext(); | |
| 226 } | |
| 227 | |
| 228 void V8Debugger::breakProgram() | |
| 229 { | |
| 230 if (isPaused()) { | |
| 231 ASSERT(!m_runningNestedMessageLoop); | |
| 232 v8::Local<v8::Value> exception; | |
| 233 v8::Local<v8::Array> hitBreakpoints; | |
| 234 handleProgramBreak(m_pausedScriptState.get(), m_executionState, exceptio
n, hitBreakpoints); | |
| 235 return; | |
| 236 } | |
| 237 | |
| 238 if (!canBreakProgram()) | |
| 239 return; | |
| 240 | |
| 241 v8::HandleScope scope(m_isolate); | |
| 242 if (m_breakProgramCallbackTemplate.IsEmpty()) { | |
| 243 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(m_isol
ate); | |
| 244 templ->SetCallHandler(&V8Debugger::breakProgramCallback, v8::External::N
ew(m_isolate, this)); | |
| 245 m_breakProgramCallbackTemplate.Reset(m_isolate, templ); | |
| 246 } | |
| 247 | |
| 248 v8::Local<v8::Function> breakProgramFunction = v8::Local<v8::FunctionTemplat
e>::New(m_isolate, m_breakProgramCallbackTemplate)->GetFunction(); | |
| 249 v8::Debug::Call(breakProgramFunction); | |
| 250 } | |
| 251 | |
| 252 void V8Debugger::continueProgram() | |
| 253 { | |
| 254 if (isPaused()) | |
| 255 m_client->quitMessageLoopOnPause(); | |
| 256 m_pausedScriptState.clear(); | |
| 257 m_executionState.Clear(); | |
| 258 } | |
| 259 | |
| 260 void V8Debugger::stepIntoStatement() | |
| 261 { | |
| 262 ASSERT(isPaused()); | |
| 263 ASSERT(!m_executionState.IsEmpty()); | |
| 264 v8::HandleScope handleScope(m_isolate); | |
| 265 v8::Local<v8::Value> argv[] = { m_executionState }; | |
| 266 callDebuggerMethod(stepIntoV8MethodName, 1, argv); | |
| 267 continueProgram(); | |
| 268 } | |
| 269 | |
| 270 void V8Debugger::stepOverStatement() | |
| 271 { | |
| 272 ASSERT(isPaused()); | |
| 273 ASSERT(!m_executionState.IsEmpty()); | |
| 274 v8::HandleScope handleScope(m_isolate); | |
| 275 v8::Local<v8::Value> argv[] = { m_executionState }; | |
| 276 callDebuggerMethod("stepOverStatement", 1, argv); | |
| 277 continueProgram(); | |
| 278 } | |
| 279 | |
| 280 void V8Debugger::stepOutOfFunction() | |
| 281 { | |
| 282 ASSERT(isPaused()); | |
| 283 ASSERT(!m_executionState.IsEmpty()); | |
| 284 v8::HandleScope handleScope(m_isolate); | |
| 285 v8::Local<v8::Value> argv[] = { m_executionState }; | |
| 286 callDebuggerMethod(stepOutV8MethodName, 1, argv); | |
| 287 continueProgram(); | |
| 288 } | |
| 289 | |
| 290 void V8Debugger::clearStepping() | |
| 291 { | |
| 292 ASSERT(enabled()); | |
| 293 v8::HandleScope scope(m_isolate); | |
| 294 v8::Context::Scope contextScope(debuggerContext()); | |
| 295 | |
| 296 v8::Local<v8::Value> argv[] = { v8Undefined() }; | |
| 297 callDebuggerMethod("clearStepping", 0, argv); | |
| 298 } | |
| 299 | |
| 300 bool V8Debugger::setScriptSource(const String& sourceID, const String& newConten
t, bool preview, String* error, RefPtr<TypeBuilder::Debugger::SetScriptSourceErr
or>& errorData, ScriptValue* newCallFrames, RefPtr<JSONObject>* result) | |
| 301 { | |
| 302 class EnableLiveEditScope { | |
| 303 public: | |
| 304 explicit EnableLiveEditScope(v8::Isolate* isolate) : m_isolate(isolate)
{ v8::Debug::SetLiveEditEnabled(m_isolate, true); } | |
| 305 ~EnableLiveEditScope() { v8::Debug::SetLiveEditEnabled(m_isolate, false)
; } | |
| 306 private: | |
| 307 v8::Isolate* m_isolate; | |
| 308 }; | |
| 309 | |
| 310 ASSERT(enabled()); | |
| 311 v8::HandleScope scope(m_isolate); | |
| 312 | |
| 313 OwnPtr<v8::Context::Scope> contextScope; | |
| 314 if (!isPaused()) | |
| 315 contextScope = adoptPtr(new v8::Context::Scope(debuggerContext())); | |
| 316 | |
| 317 v8::Local<v8::Value> argv[] = { v8String(m_isolate, sourceID), v8String(m_is
olate, newContent), v8Boolean(preview, m_isolate) }; | |
| 318 | |
| 319 v8::Local<v8::Value> v8result; | |
| 320 { | |
| 321 EnableLiveEditScope enableLiveEditScope(m_isolate); | |
| 322 v8::TryCatch tryCatch; | |
| 323 tryCatch.SetVerbose(false); | |
| 324 v8::MaybeLocal<v8::Value> maybeResult = callDebuggerMethod("liveEditScri
ptSource", 3, argv); | |
| 325 if (tryCatch.HasCaught()) { | |
| 326 v8::Local<v8::Message> message = tryCatch.Message(); | |
| 327 if (!message.IsEmpty()) | |
| 328 *error = toCoreStringWithUndefinedOrNullCheck(message->Get()); | |
| 329 else | |
| 330 *error = "Unknown error."; | |
| 331 return false; | |
| 332 } | |
| 333 v8result = maybeResult.ToLocalChecked(); | |
| 334 } | |
| 335 ASSERT(!v8result.IsEmpty()); | |
| 336 v8::Local<v8::Object> resultTuple = v8result->ToObject(m_isolate); | |
| 337 int code = static_cast<int>(resultTuple->Get(0)->ToInteger(m_isolate)->Value
()); | |
| 338 switch (code) { | |
| 339 case 0: | |
| 340 { | |
| 341 v8::Local<v8::Value> normalResult = resultTuple->Get(1); | |
| 342 NonThrowableExceptionState exceptionState; | |
| 343 RefPtr<JSONValue> jsonResult = ScriptValue::to<JSONValuePtr>(m_isola
te, normalResult, exceptionState); | |
| 344 if (jsonResult) | |
| 345 *result = jsonResult->asObject(); | |
| 346 // Call stack may have changed after if the edited function was on t
he stack. | |
| 347 if (!preview && isPaused()) | |
| 348 *newCallFrames = currentCallFrames(); | |
| 349 return true; | |
| 350 } | |
| 351 // Compile error. | |
| 352 case 1: | |
| 353 { | |
| 354 RefPtr<TypeBuilder::Debugger::SetScriptSourceError::CompileError> co
mpileError = | |
| 355 TypeBuilder::Debugger::SetScriptSourceError::CompileError::creat
e() | |
| 356 .setMessage(toCoreStringWithUndefinedOrNullCheck(resultTuple
->Get(2))) | |
| 357 .setLineNumber(resultTuple->Get(3)->ToInteger(m_isolate)->Va
lue()) | |
| 358 .setColumnNumber(resultTuple->Get(4)->ToInteger(m_isolate)->
Value()); | |
| 359 | |
| 360 *error = toCoreStringWithUndefinedOrNullCheck(resultTuple->Get(1)); | |
| 361 errorData = TypeBuilder::Debugger::SetScriptSourceError::create(); | |
| 362 errorData->setCompileError(compileError); | |
| 363 return false; | |
| 364 } | |
| 365 } | |
| 366 *error = "Unknown error."; | |
| 367 return false; | |
| 368 } | |
| 369 | |
| 370 int V8Debugger::frameCount() | |
| 371 { | |
| 372 ASSERT(isPaused()); | |
| 373 ASSERT(!m_executionState.IsEmpty()); | |
| 374 v8::Local<v8::Value> argv[] = { m_executionState }; | |
| 375 v8::Local<v8::Value> result = callDebuggerMethod("frameCount", WTF_ARRAY_LEN
GTH(argv), argv).ToLocalChecked(); | |
| 376 if (result->IsInt32()) | |
| 377 return result->Int32Value(); | |
| 378 return 0; | |
| 379 } | |
| 380 | |
| 381 PassRefPtr<JavaScriptCallFrame> V8Debugger::toJavaScriptCallFrameUnsafe(const Sc
riptValue& value) | |
| 382 { | |
| 383 if (value.isEmpty()) | |
| 384 return nullptr; | |
| 385 ScriptState* scriptState = value.scriptState(); | |
| 386 if (!scriptState || !scriptState->contextIsValid()) | |
| 387 return nullptr; | |
| 388 ScriptState::Scope scope(scriptState); | |
| 389 ASSERT(value.isObject()); | |
| 390 return V8JavaScriptCallFrame::unwrap(v8::Local<v8::Object>::Cast(value.v8Val
ueUnsafe())); | |
| 391 } | |
| 392 | |
| 393 PassRefPtr<JavaScriptCallFrame> V8Debugger::wrapCallFrames(int maximumLimit, Sco
peInfoDetails scopeDetails) | |
| 394 { | |
| 395 const int scopeBits = 2; | |
| 396 static_assert(NoScopes < (1 << scopeBits), "there must be enough bits to enc
ode ScopeInfoDetails"); | |
| 397 | |
| 398 ASSERT(maximumLimit >= 0); | |
| 399 int data = (maximumLimit << scopeBits) | scopeDetails; | |
| 400 v8::Local<v8::Value> currentCallFrameV8; | |
| 401 if (m_executionState.IsEmpty()) { | |
| 402 v8::Local<v8::Function> currentCallFrameFunction = v8::Local<v8::Functio
n>::Cast(debuggerScriptLocal()->Get(v8InternalizedString("currentCallFrame"))); | |
| 403 currentCallFrameV8 = v8::Debug::Call(currentCallFrameFunction, v8::Integ
er::New(m_isolate, data)); | |
| 404 } else { | |
| 405 v8::Local<v8::Value> argv[] = { m_executionState, v8::Integer::New(m_iso
late, data) }; | |
| 406 currentCallFrameV8 = callDebuggerMethod("currentCallFrame", WTF_ARRAY_LE
NGTH(argv), argv).ToLocalChecked(); | |
| 407 } | |
| 408 ASSERT(!currentCallFrameV8.IsEmpty()); | |
| 409 if (!currentCallFrameV8->IsObject()) | |
| 410 return nullptr; | |
| 411 return JavaScriptCallFrame::create(debuggerContext(), v8::Local<v8::Object>:
:Cast(currentCallFrameV8)); | |
| 412 } | |
| 413 | |
| 414 ScriptValue V8Debugger::currentCallFramesInner(ScopeInfoDetails scopeDetails) | |
| 415 { | |
| 416 if (!m_isolate->InContext()) | |
| 417 return ScriptValue(); | |
| 418 v8::HandleScope handleScope(m_isolate); | |
| 419 | |
| 420 // Filter out stack traces entirely consisting of V8's internal scripts. | |
| 421 v8::Local<v8::StackTrace> stackTrace = v8::StackTrace::CurrentStackTrace(m_i
solate, 1); | |
| 422 if (!stackTrace->GetFrameCount()) | |
| 423 return ScriptValue(); | |
| 424 | |
| 425 RefPtr<JavaScriptCallFrame> currentCallFrame = wrapCallFrames(0, scopeDetail
s); | |
| 426 if (!currentCallFrame) | |
| 427 return ScriptValue(); | |
| 428 | |
| 429 v8::Local<v8::FunctionTemplate> wrapperTemplate = v8::Local<v8::FunctionTemp
late>::New(m_isolate, m_callFrameWrapperTemplate); | |
| 430 ScriptState* scriptState = m_pausedScriptState ? m_pausedScriptState.get() :
ScriptState::current(m_isolate); | |
| 431 ScriptState::Scope scope(scriptState); | |
| 432 v8::Local<v8::Object> wrapper = V8JavaScriptCallFrame::wrap(wrapperTemplate,
scriptState->context(), currentCallFrame.release()); | |
| 433 return ScriptValue(scriptState, wrapper); | |
| 434 } | |
| 435 | |
| 436 ScriptValue V8Debugger::currentCallFrames() | |
| 437 { | |
| 438 return currentCallFramesInner(AllScopes); | |
| 439 } | |
| 440 | |
| 441 ScriptValue V8Debugger::currentCallFramesForAsyncStack() | |
| 442 { | |
| 443 return currentCallFramesInner(FastAsyncScopes); | |
| 444 } | |
| 445 | |
| 446 PassRefPtr<JavaScriptCallFrame> V8Debugger::callFrameNoScopes(int index) | |
| 447 { | |
| 448 if (!m_isolate->InContext()) | |
| 449 return nullptr; | |
| 450 v8::HandleScope handleScope(m_isolate); | |
| 451 | |
| 452 v8::Local<v8::Value> currentCallFrameV8; | |
| 453 if (m_executionState.IsEmpty()) { | |
| 454 v8::Local<v8::Function> currentCallFrameFunction = v8::Local<v8::Functio
n>::Cast(debuggerScriptLocal()->Get(v8InternalizedString("currentCallFrameByInde
x"))); | |
| 455 currentCallFrameV8 = v8::Debug::Call(currentCallFrameFunction, v8::Integ
er::New(m_isolate, index)); | |
| 456 } else { | |
| 457 v8::Local<v8::Value> argv[] = { m_executionState, v8::Integer::New(m_iso
late, index) }; | |
| 458 currentCallFrameV8 = callDebuggerMethod("currentCallFrameByIndex", WTF_A
RRAY_LENGTH(argv), argv).ToLocalChecked(); | |
| 459 } | |
| 460 ASSERT(!currentCallFrameV8.IsEmpty()); | |
| 461 if (!currentCallFrameV8->IsObject()) | |
| 462 return nullptr; | |
| 463 return JavaScriptCallFrame::create(debuggerContext(), v8::Local<v8::Object>:
:Cast(currentCallFrameV8)); | |
| 464 } | |
| 465 | |
| 466 static V8Debugger* toV8Debugger(v8::Local<v8::Value> data) | |
| 467 { | |
| 468 void* p = v8::Local<v8::External>::Cast(data)->Value(); | |
| 469 return static_cast<V8Debugger*>(p); | |
| 470 } | |
| 471 | |
| 472 void V8Debugger::breakProgramCallback(const v8::FunctionCallbackInfo<v8::Value>&
info) | |
| 473 { | |
| 474 ASSERT(2 == info.Length()); | |
| 475 V8Debugger* thisPtr = toV8Debugger(info.Data()); | |
| 476 ScriptState* pausedScriptState = ScriptState::current(thisPtr->m_isolate); | |
| 477 v8::Local<v8::Value> exception; | |
| 478 v8::Local<v8::Array> hitBreakpoints; | |
| 479 thisPtr->handleProgramBreak(pausedScriptState, v8::Local<v8::Object>::Cast(i
nfo[0]), exception, hitBreakpoints); | |
| 480 } | |
| 481 | |
| 482 void V8Debugger::handleProgramBreak(ScriptState* pausedScriptState, v8::Local<v8
::Object> executionState, v8::Local<v8::Value> exception, v8::Local<v8::Array> h
itBreakpointNumbers, bool isPromiseRejection) | |
| 483 { | |
| 484 // Don't allow nested breaks. | |
| 485 if (m_runningNestedMessageLoop) | |
| 486 return; | |
| 487 | |
| 488 ScriptDebugListener* listener = m_client->getDebugListenerForContext(pausedS
criptState->context()); | |
| 489 if (!listener) | |
| 490 return; | |
| 491 | |
| 492 Vector<String> breakpointIds; | |
| 493 if (!hitBreakpointNumbers.IsEmpty()) { | |
| 494 breakpointIds.resize(hitBreakpointNumbers->Length()); | |
| 495 for (size_t i = 0; i < hitBreakpointNumbers->Length(); i++) { | |
| 496 v8::Local<v8::Value> hitBreakpointNumber = hitBreakpointNumbers->Get
(i); | |
| 497 ASSERT(!hitBreakpointNumber.IsEmpty() && hitBreakpointNumber->IsInt3
2()); | |
| 498 breakpointIds[i] = String::number(hitBreakpointNumber->Int32Value())
; | |
| 499 } | |
| 500 } | |
| 501 | |
| 502 m_pausedScriptState = pausedScriptState; | |
| 503 m_executionState = executionState; | |
| 504 ScriptDebugListener::SkipPauseRequest result = listener->didPause(pausedScri
ptState, currentCallFrames(), ScriptValue(pausedScriptState, exception), breakpo
intIds, isPromiseRejection); | |
| 505 if (result == ScriptDebugListener::NoSkip) { | |
| 506 m_runningNestedMessageLoop = true; | |
| 507 m_client->runMessageLoopOnPause(pausedScriptState->context()); | |
| 508 m_runningNestedMessageLoop = false; | |
| 509 } | |
| 510 m_pausedScriptState.clear(); | |
| 511 m_executionState.Clear(); | |
| 512 | |
| 513 if (result == ScriptDebugListener::StepFrame) { | |
| 514 v8::Local<v8::Value> argv[] = { executionState }; | |
| 515 callDebuggerMethod("stepFrameStatement", 1, argv); | |
| 516 } else if (result == ScriptDebugListener::StepInto) { | |
| 517 v8::Local<v8::Value> argv[] = { executionState }; | |
| 518 callDebuggerMethod(stepIntoV8MethodName, 1, argv); | |
| 519 } else if (result == ScriptDebugListener::StepOut) { | |
| 520 v8::Local<v8::Value> argv[] = { executionState }; | |
| 521 callDebuggerMethod(stepOutV8MethodName, 1, argv); | |
| 522 } | |
| 523 } | |
| 524 | |
| 525 void V8Debugger::v8DebugEventCallback(const v8::Debug::EventDetails& eventDetail
s) | |
| 526 { | |
| 527 V8Debugger* thisPtr = toV8Debugger(eventDetails.GetCallbackData()); | |
| 528 thisPtr->handleV8DebugEvent(eventDetails); | |
| 529 } | |
| 530 | |
| 531 v8::Local<v8::Value> V8Debugger::callInternalGetterFunction(v8::Local<v8::Object
> object, const char* functionName) | |
| 532 { | |
| 533 v8::Local<v8::Value> getterValue = object->Get(v8InternalizedString(function
Name)); | |
| 534 ASSERT(!getterValue.IsEmpty() && getterValue->IsFunction()); | |
| 535 return V8ScriptRunner::callInternalFunction(v8::Local<v8::Function>::Cast(ge
tterValue), object, 0, 0, m_isolate).ToLocalChecked(); | |
| 536 } | |
| 537 | |
| 538 void V8Debugger::handleV8DebugEvent(const v8::Debug::EventDetails& eventDetails) | |
| 539 { | |
| 540 if (!enabled()) | |
| 541 return; | |
| 542 v8::DebugEvent event = eventDetails.GetEvent(); | |
| 543 if (event != v8::AsyncTaskEvent && event != v8::Break && event != v8::Except
ion && event != v8::AfterCompile && event != v8::BeforeCompile && event != v8::C
ompileError && event != v8::PromiseEvent) | |
| 544 return; | |
| 545 | |
| 546 v8::Local<v8::Context> eventContext = eventDetails.GetEventContext(); | |
| 547 ASSERT(!eventContext.IsEmpty()); | |
| 548 | |
| 549 ScriptDebugListener* listener = m_client->getDebugListenerForContext(eventCo
ntext); | |
| 550 if (listener) { | |
| 551 v8::HandleScope scope(m_isolate); | |
| 552 if (event == v8::AfterCompile || event == v8::CompileError) { | |
| 553 v8::Context::Scope contextScope(debuggerContext()); | |
| 554 v8::Local<v8::Value> argv[] = { eventDetails.GetEventData() }; | |
| 555 v8::Local<v8::Value> value = callDebuggerMethod("getAfterCompileScri
pt", 1, argv).ToLocalChecked(); | |
| 556 ASSERT(value->IsObject()); | |
| 557 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(value); | |
| 558 dispatchDidParseSource(listener, object, event != v8::AfterCompile ?
CompileError : CompileSuccess); | |
| 559 } else if (event == v8::Exception) { | |
| 560 v8::Local<v8::Object> eventData = eventDetails.GetEventData(); | |
| 561 v8::Local<v8::Value> exception = callInternalGetterFunction(eventDat
a, "exception"); | |
| 562 v8::Local<v8::Value> promise = callInternalGetterFunction(eventData,
"promise"); | |
| 563 bool isPromiseRejection = !promise.IsEmpty() && promise->IsObject(); | |
| 564 handleProgramBreak(ScriptState::from(eventContext), eventDetails.Get
ExecutionState(), exception, v8::Local<v8::Array>(), isPromiseRejection); | |
| 565 } else if (event == v8::Break) { | |
| 566 v8::Local<v8::Value> argv[] = { eventDetails.GetEventData() }; | |
| 567 v8::Local<v8::Value> hitBreakpoints = callDebuggerMethod("getBreakpo
intNumbers", 1, argv).ToLocalChecked(); | |
| 568 ASSERT(hitBreakpoints->IsArray()); | |
| 569 handleProgramBreak(ScriptState::from(eventContext), eventDetails.Get
ExecutionState(), v8::Local<v8::Value>(), hitBreakpoints.As<v8::Array>()); | |
| 570 } else if (event == v8::AsyncTaskEvent) { | |
| 571 if (listener->v8AsyncTaskEventsEnabled()) | |
| 572 handleV8AsyncTaskEvent(listener, ScriptState::from(eventContext)
, eventDetails.GetExecutionState(), eventDetails.GetEventData()); | |
| 573 } else if (event == v8::PromiseEvent) { | |
| 574 if (listener->v8PromiseEventsEnabled()) | |
| 575 handleV8PromiseEvent(listener, ScriptState::from(eventContext),
eventDetails.GetExecutionState(), eventDetails.GetEventData()); | |
| 576 } | |
| 577 } | |
| 578 } | |
| 579 | |
| 580 void V8Debugger::handleV8AsyncTaskEvent(ScriptDebugListener* listener, ScriptSta
te* pausedScriptState, v8::Local<v8::Object> executionState, v8::Local<v8::Objec
t> eventData) | |
| 581 { | |
| 582 String type = toCoreStringWithUndefinedOrNullCheck(callInternalGetterFunctio
n(eventData, "type")); | |
| 583 String name = toCoreStringWithUndefinedOrNullCheck(callInternalGetterFunctio
n(eventData, "name")); | |
| 584 int id = callInternalGetterFunction(eventData, "id")->ToInteger(m_isolate)->
Value(); | |
| 585 | |
| 586 m_pausedScriptState = pausedScriptState; | |
| 587 m_executionState = executionState; | |
| 588 listener->didReceiveV8AsyncTaskEvent(pausedScriptState, type, name, id); | |
| 589 m_pausedScriptState.clear(); | |
| 590 m_executionState.Clear(); | |
| 591 } | |
| 592 | |
| 593 void V8Debugger::handleV8PromiseEvent(ScriptDebugListener* listener, ScriptState
* pausedScriptState, v8::Local<v8::Object> executionState, v8::Local<v8::Object>
eventData) | |
| 594 { | |
| 595 v8::Local<v8::Value> argv[] = { eventData }; | |
| 596 v8::Local<v8::Value> value = callDebuggerMethod("getPromiseDetails", 1, argv
).ToLocalChecked(); | |
| 597 ASSERT(value->IsObject()); | |
| 598 v8::Local<v8::Object> promiseDetails = v8::Local<v8::Object>::Cast(value); | |
| 599 v8::Local<v8::Object> promise = promiseDetails->Get(v8InternalizedString("pr
omise"))->ToObject(m_isolate); | |
| 600 int status = promiseDetails->Get(v8InternalizedString("status"))->ToInteger(
m_isolate)->Value(); | |
| 601 v8::Local<v8::Value> parentPromise = promiseDetails->Get(v8InternalizedStrin
g("parentPromise")); | |
| 602 | |
| 603 m_pausedScriptState = pausedScriptState; | |
| 604 m_executionState = executionState; | |
| 605 listener->didReceiveV8PromiseEvent(pausedScriptState, promise, parentPromise
, status); | |
| 606 m_pausedScriptState.clear(); | |
| 607 m_executionState.Clear(); | |
| 608 } | |
| 609 | |
| 610 void V8Debugger::dispatchDidParseSource(ScriptDebugListener* listener, v8::Local
<v8::Object> object, CompileResult compileResult) | |
| 611 { | |
| 612 v8::Local<v8::Value> id = object->Get(v8InternalizedString("id")); | |
| 613 ASSERT(!id.IsEmpty() && id->IsInt32()); | |
| 614 String sourceID = String::number(id->Int32Value()); | |
| 615 | |
| 616 ScriptDebugListener::Script script; | |
| 617 script.setURL(toCoreStringWithUndefinedOrNullCheck(object->Get(v8Internalize
dString("name")))) | |
| 618 .setSourceURL(toCoreStringWithUndefinedOrNullCheck(object->Get(v8Interna
lizedString("sourceURL")))) | |
| 619 .setSourceMappingURL(toCoreStringWithUndefinedOrNullCheck(object->Get(v8
InternalizedString("sourceMappingURL")))) | |
| 620 .setSource(toCoreStringWithUndefinedOrNullCheck(object->Get(v8Internaliz
edString("source")))) | |
| 621 .setStartLine(object->Get(v8InternalizedString("startLine"))->ToInteger(
m_isolate)->Value()) | |
| 622 .setStartColumn(object->Get(v8InternalizedString("startColumn"))->ToInte
ger(m_isolate)->Value()) | |
| 623 .setEndLine(object->Get(v8InternalizedString("endLine"))->ToInteger(m_is
olate)->Value()) | |
| 624 .setEndColumn(object->Get(v8InternalizedString("endColumn"))->ToInteger(
m_isolate)->Value()) | |
| 625 .setIsContentScript(object->Get(v8InternalizedString("isContentScript"))
->ToBoolean(m_isolate)->Value()) | |
| 626 .setIsInternalScript(object->Get(v8InternalizedString("isInternalScript"
))->ToBoolean(m_isolate)->Value()); | |
| 627 | |
| 628 listener->didParseSource(sourceID, script, compileResult); | |
| 629 } | |
| 630 | |
| 631 void V8Debugger::compileDebuggerScript() | |
| 632 { | |
| 633 if (!m_debuggerScript.IsEmpty()) { | |
| 634 ASSERT_NOT_REACHED(); | |
| 635 return; | |
| 636 } | |
| 637 | |
| 638 v8::HandleScope scope(m_isolate); | |
| 639 v8::Context::Scope contextScope(debuggerContext()); | |
| 640 v8::Local<v8::Object> value = m_client->compileDebuggerScript(); | |
| 641 if (value.IsEmpty()) | |
| 642 return; | |
| 643 m_debuggerScript.Reset(m_isolate, value); | |
| 644 } | |
| 645 | |
| 646 v8::Local<v8::Object> V8Debugger::debuggerScriptLocal() const | |
| 647 { | |
| 648 return v8::Local<v8::Object>::New(m_isolate, m_debuggerScript); | |
| 649 } | |
| 650 | |
| 651 v8::Local<v8::Context> V8Debugger::debuggerContext() const | |
| 652 { | |
| 653 ASSERT(!m_debuggerContext.IsEmpty()); | |
| 654 return v8::Local<v8::Context>::New(m_isolate, m_debuggerContext); | |
| 655 } | |
| 656 | |
| 657 v8::Local<v8::String> V8Debugger::v8InternalizedString(const char* str) const | |
| 658 { | |
| 659 return v8::String::NewFromUtf8(m_isolate, str, v8::NewStringType::kInternali
zed).ToLocalChecked(); | |
| 660 } | |
| 661 | |
| 662 v8::Local<v8::Value> V8Debugger::functionScopes(v8::Local<v8::Function> function
) | |
| 663 { | |
| 664 if (!enabled()) { | |
| 665 ASSERT_NOT_REACHED(); | |
| 666 return v8::Local<v8::Value>::New(m_isolate, v8::Undefined(m_isolate)); | |
| 667 } | |
| 668 v8::Local<v8::Value> argv[] = { function }; | |
| 669 return callDebuggerMethod("getFunctionScopes", 1, argv).ToLocalChecked(); | |
| 670 } | |
| 671 | |
| 672 v8::Local<v8::Value> V8Debugger::generatorObjectDetails(v8::Local<v8::Object>& o
bject) | |
| 673 { | |
| 674 if (!enabled()) { | |
| 675 ASSERT_NOT_REACHED(); | |
| 676 return v8::Local<v8::Value>::New(m_isolate, v8::Undefined(m_isolate)); | |
| 677 } | |
| 678 v8::Local<v8::Value> argv[] = { object }; | |
| 679 return callDebuggerMethod("getGeneratorObjectDetails", 1, argv).ToLocalCheck
ed(); | |
| 680 } | |
| 681 | |
| 682 v8::Local<v8::Value> V8Debugger::collectionEntries(v8::Local<v8::Object>& object
) | |
| 683 { | |
| 684 if (!enabled()) { | |
| 685 ASSERT_NOT_REACHED(); | |
| 686 return v8::Local<v8::Value>::New(m_isolate, v8::Undefined(m_isolate)); | |
| 687 } | |
| 688 v8::Local<v8::Value> argv[] = { object }; | |
| 689 return callDebuggerMethod("getCollectionEntries", 1, argv).ToLocalChecked(); | |
| 690 } | |
| 691 | |
| 692 v8::MaybeLocal<v8::Value> V8Debugger::setFunctionVariableValue(v8::Local<v8::Val
ue> functionValue, int scopeNumber, const String& variableName, v8::Local<v8::Va
lue> newValue) | |
| 693 { | |
| 694 if (m_debuggerScript.IsEmpty()) { | |
| 695 ASSERT_NOT_REACHED(); | |
| 696 return m_isolate->ThrowException(v8::String::NewFromUtf8(m_isolate, "Deb
ugging is not enabled.", v8::NewStringType::kNormal).ToLocalChecked()); | |
| 697 } | |
| 698 | |
| 699 v8::Local<v8::Value> argv[] = { | |
| 700 functionValue, | |
| 701 v8::Local<v8::Value>(v8::Integer::New(m_isolate, scopeNumber)), | |
| 702 v8String(m_isolate, variableName), | |
| 703 newValue | |
| 704 }; | |
| 705 return callDebuggerMethod("setFunctionVariableValue", 4, argv); | |
| 706 } | |
| 707 | |
| 708 | |
| 709 bool V8Debugger::isPaused() | |
| 710 { | |
| 711 return m_pausedScriptState; | |
| 712 } | |
| 713 | |
| 714 } // namespace blink | |
| OLD | NEW |