2 * Copyright (c) 2010-2011 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 are
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
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.
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.
32 #include "bindings/v8/ScriptDebugServer.h"
34 #include "DebuggerScriptSource.h"
35 #include "V8JavaScriptCallFrame.h"
36 #include "bindings/v8/ScopedPersistent.h"
37 #include "bindings/v8/ScriptController.h"
38 #include "bindings/v8/ScriptObject.h"
39 #include "bindings/v8/ScriptSourceCode.h"
40 #include "bindings/v8/V8Binding.h"
41 #include "bindings/v8/V8ScriptRunner.h"
42 #include "core/inspector/JavaScriptCallFrame.h"
43 #include "core/inspector/ScriptDebugListener.h"
44 #include "platform/JSONValues.h"
45 #include "wtf/StdLibExtras.h"
46 #include "wtf/Vector.h"
47 #include "wtf/dtoa/utils.h"
48 #include "wtf/text/CString.h"
54 class ClientDataImpl : public v8::Debug::ClientData {
56 ClientDataImpl(PassOwnPtr<ScriptDebugServer::Task> task) : m_task(task) { }
57 virtual ~ClientDataImpl() { }
58 ScriptDebugServer::Task* task() const { return m_task.get(); }
60 OwnPtr<ScriptDebugServer::Task> m_task;
63 const char stepIntoV8MethodName[] = "stepIntoStatement";
64 const char stepOutV8MethodName[] = "stepOutOfFunction";
67 v8::Local<v8::Value> ScriptDebugServer::callDebuggerMethod(const char* functionName, int argc, v8::Handle<v8::Value> argv[])
69 v8::Handle<v8::Object> debuggerScript = m_debuggerScript.newLocal(m_isolate);
70 v8::Handle<v8::Function> function = v8::Local<v8::Function>::Cast(debuggerScript->Get(v8AtomicString(m_isolate, functionName)));
71 ASSERT(m_isolate->InContext());
72 return V8ScriptRunner::callInternalFunction(function, debuggerScript, argc, argv, m_isolate);
75 ScriptDebugServer::ScriptDebugServer(v8::Isolate* isolate)
76 : m_pauseOnExceptionsState(DontPauseOnExceptions)
77 , m_breakpointsActivated(true)
79 , m_runningNestedMessageLoop(false)
83 ScriptDebugServer::~ScriptDebugServer()
87 String ScriptDebugServer::setBreakpoint(const String& sourceID, const ScriptBreakpoint& scriptBreakpoint, int* actualLineNumber, int* actualColumnNumber, bool interstatementLocation)
89 v8::HandleScope scope(m_isolate);
90 v8::Local<v8::Context> debuggerContext = v8::Debug::GetDebugContext();
91 v8::Context::Scope contextScope(debuggerContext);
93 v8::Local<v8::Object> info = v8::Object::New(m_isolate);
94 info->Set(v8AtomicString(m_isolate, "sourceID"), v8String(debuggerContext->GetIsolate(), sourceID));
95 info->Set(v8AtomicString(m_isolate, "lineNumber"), v8::Integer::New(debuggerContext->GetIsolate(), scriptBreakpoint.lineNumber));
96 info->Set(v8AtomicString(m_isolate, "columnNumber"), v8::Integer::New(debuggerContext->GetIsolate(), scriptBreakpoint.columnNumber));
97 info->Set(v8AtomicString(m_isolate, "interstatementLocation"), v8Boolean(interstatementLocation, debuggerContext->GetIsolate()));
98 info->Set(v8AtomicString(m_isolate, "condition"), v8String(debuggerContext->GetIsolate(), scriptBreakpoint.condition));
100 v8::Handle<v8::Function> setBreakpointFunction = v8::Local<v8::Function>::Cast(m_debuggerScript.newLocal(m_isolate)->Get(v8AtomicString(m_isolate, "setBreakpoint")));
101 v8::Handle<v8::Value> breakpointId = v8::Debug::Call(setBreakpointFunction, info);
102 if (!breakpointId->IsString())
104 *actualLineNumber = info->Get(v8AtomicString(m_isolate, "lineNumber"))->Int32Value();
105 *actualColumnNumber = info->Get(v8AtomicString(m_isolate, "columnNumber"))->Int32Value();
106 return toCoreString(breakpointId.As<v8::String>());
109 void ScriptDebugServer::removeBreakpoint(const String& breakpointId)
111 v8::HandleScope scope(m_isolate);
112 v8::Local<v8::Context> debuggerContext = v8::Debug::GetDebugContext();
113 v8::Context::Scope contextScope(debuggerContext);
115 v8::Local<v8::Object> info = v8::Object::New(m_isolate);
116 info->Set(v8AtomicString(m_isolate, "breakpointId"), v8String(debuggerContext->GetIsolate(), breakpointId));
118 v8::Handle<v8::Function> removeBreakpointFunction = v8::Local<v8::Function>::Cast(m_debuggerScript.newLocal(m_isolate)->Get(v8AtomicString(m_isolate, "removeBreakpoint")));
119 v8::Debug::Call(removeBreakpointFunction, info);
122 void ScriptDebugServer::clearBreakpoints()
124 ensureDebuggerScriptCompiled();
125 v8::HandleScope scope(m_isolate);
126 v8::Local<v8::Context> debuggerContext = v8::Debug::GetDebugContext();
127 v8::Context::Scope contextScope(debuggerContext);
129 v8::Handle<v8::Function> clearBreakpoints = v8::Local<v8::Function>::Cast(m_debuggerScript.newLocal(m_isolate)->Get(v8AtomicString(m_isolate, "clearBreakpoints")));
130 v8::Debug::Call(clearBreakpoints);
133 void ScriptDebugServer::setBreakpointsActivated(bool activated)
135 ensureDebuggerScriptCompiled();
136 v8::HandleScope scope(m_isolate);
137 v8::Local<v8::Context> debuggerContext = v8::Debug::GetDebugContext();
138 v8::Context::Scope contextScope(debuggerContext);
140 v8::Local<v8::Object> info = v8::Object::New(m_isolate);
141 info->Set(v8AtomicString(m_isolate, "enabled"), v8::Boolean::New(m_isolate, activated));
142 v8::Handle<v8::Function> setBreakpointsActivated = v8::Local<v8::Function>::Cast(m_debuggerScript.newLocal(m_isolate)->Get(v8AtomicString(m_isolate, "setBreakpointsActivated")));
143 v8::Debug::Call(setBreakpointsActivated, info);
145 m_breakpointsActivated = activated;
148 ScriptDebugServer::PauseOnExceptionsState ScriptDebugServer::pauseOnExceptionsState()
150 ensureDebuggerScriptCompiled();
151 v8::HandleScope scope(m_isolate);
152 v8::Context::Scope contextScope(v8::Debug::GetDebugContext());
154 v8::Handle<v8::Value> argv[] = { v8Undefined() };
155 v8::Handle<v8::Value> result = callDebuggerMethod("pauseOnExceptionsState", 0, argv);
156 return static_cast<ScriptDebugServer::PauseOnExceptionsState>(result->Int32Value());
159 void ScriptDebugServer::setPauseOnExceptionsState(PauseOnExceptionsState pauseOnExceptionsState)
161 ensureDebuggerScriptCompiled();
162 v8::HandleScope scope(m_isolate);
163 v8::Context::Scope contextScope(v8::Debug::GetDebugContext());
165 v8::Handle<v8::Value> argv[] = { v8::Int32::New(m_isolate, pauseOnExceptionsState) };
166 callDebuggerMethod("setPauseOnExceptionsState", 1, argv);
169 void ScriptDebugServer::setPauseOnNextStatement(bool pause)
174 v8::Debug::DebugBreak(m_isolate);
176 v8::Debug::CancelDebugBreak(m_isolate);
179 bool ScriptDebugServer::canBreakProgram()
181 if (!m_breakpointsActivated)
183 v8::HandleScope scope(m_isolate);
184 return !m_isolate->GetCurrentContext().IsEmpty();
187 void ScriptDebugServer::breakProgram()
189 if (!canBreakProgram())
192 v8::HandleScope scope(m_isolate);
193 if (m_breakProgramCallbackTemplate.isEmpty()) {
194 v8::Handle<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(m_isolate);
195 templ->SetCallHandler(&ScriptDebugServer::breakProgramCallback, v8::External::New(m_isolate, this));
196 m_breakProgramCallbackTemplate.set(m_isolate, templ);
199 m_pausedContext = m_isolate->GetCurrentContext();
200 v8::Handle<v8::Function> breakProgramFunction = m_breakProgramCallbackTemplate.newLocal(m_isolate)->GetFunction();
201 v8::Debug::Call(breakProgramFunction);
202 m_pausedContext.Clear();
205 void ScriptDebugServer::continueProgram()
208 quitMessageLoopOnPause();
209 m_executionState.clear();
212 void ScriptDebugServer::stepIntoStatement()
215 v8::HandleScope handleScope(m_isolate);
216 v8::Handle<v8::Value> argv[] = { m_executionState.newLocal(m_isolate) };
217 callDebuggerMethod(stepIntoV8MethodName, 1, argv);
221 void ScriptDebugServer::stepCommandWithFrame(const char* functionName, const ScriptValue& frame)
224 v8::HandleScope handleScope(m_isolate);
225 v8::Handle<v8::Value> callFrame;
226 if (frame.isEmpty()) {
227 callFrame = v8::Undefined(m_isolate);
229 JavaScriptCallFrame* impl = V8JavaScriptCallFrame::toNative(v8::Handle<v8::Object>::Cast(frame.v8Value()));
230 callFrame = impl->innerCallFrame();
233 v8::Handle<v8::Value> argv[] = {
234 m_executionState.newLocal(m_isolate),
238 callDebuggerMethod(functionName, 2, argv);
242 void ScriptDebugServer::stepOverStatement(const ScriptValue& frame)
244 stepCommandWithFrame("stepOverStatement", frame);
247 void ScriptDebugServer::stepOutOfFunction(const ScriptValue& frame)
249 stepCommandWithFrame(stepOutV8MethodName, frame);
252 bool ScriptDebugServer::setScriptSource(const String& sourceID, const String& newContent, bool preview, String* error, RefPtr<TypeBuilder::Debugger::SetScriptSourceError>& errorData, ScriptValue* newCallFrames, RefPtr<JSONObject>* result)
254 class EnableLiveEditScope {
256 explicit EnableLiveEditScope(v8::Isolate* isolate) : m_isolate(isolate) { v8::Debug::SetLiveEditEnabled(m_isolate, true); }
257 ~EnableLiveEditScope() { v8::Debug::SetLiveEditEnabled(m_isolate, false); }
259 v8::Isolate* m_isolate;
262 ensureDebuggerScriptCompiled();
263 v8::HandleScope scope(m_isolate);
265 OwnPtr<v8::Context::Scope> contextScope;
266 v8::Handle<v8::Context> debuggerContext = v8::Debug::GetDebugContext();
268 contextScope = adoptPtr(new v8::Context::Scope(debuggerContext));
270 v8::Handle<v8::Value> argv[] = { v8String(m_isolate, sourceID), v8String(m_isolate, newContent), v8Boolean(preview, m_isolate) };
272 v8::Local<v8::Value> v8result;
274 EnableLiveEditScope enableLiveEditScope(m_isolate);
275 v8::TryCatch tryCatch;
276 tryCatch.SetVerbose(false);
277 v8result = callDebuggerMethod("liveEditScriptSource", 3, argv);
278 if (tryCatch.HasCaught()) {
279 v8::Local<v8::Message> message = tryCatch.Message();
280 if (!message.IsEmpty())
281 *error = toCoreStringWithUndefinedOrNullCheck(message->Get());
283 *error = "Unknown error.";
287 ASSERT(!v8result.IsEmpty());
288 v8::Local<v8::Object> resultTuple = v8result->ToObject();
289 int code = static_cast<int>(resultTuple->Get(0)->ToInteger()->Value());
293 v8::Local<v8::Value> normalResult = resultTuple->Get(1);
294 RefPtr<JSONValue> jsonResult = v8ToJSONValue(m_isolate, normalResult, JSONValue::maxDepth);
296 *result = jsonResult->asObject();
297 // Call stack may have changed after if the edited function was on the stack.
298 if (!preview && isPaused())
299 *newCallFrames = currentCallFrames();
305 RefPtr<TypeBuilder::Debugger::SetScriptSourceError::CompileError> compileError =
306 TypeBuilder::Debugger::SetScriptSourceError::CompileError::create()
307 .setMessage(toCoreStringWithUndefinedOrNullCheck(resultTuple->Get(2)))
308 .setLineNumber(resultTuple->Get(3)->ToInteger()->Value())
309 .setColumnNumber(resultTuple->Get(4)->ToInteger()->Value());
311 *error = toCoreStringWithUndefinedOrNullCheck(resultTuple->Get(1));
312 errorData = TypeBuilder::Debugger::SetScriptSourceError::create();
313 errorData->setCompileError(compileError);
317 *error = "Unknown error.";
321 PassRefPtr<JavaScriptCallFrame> ScriptDebugServer::wrapCallFrames(v8::Handle<v8::Object> executionState, int maximumLimit, ScopeInfoDetails scopeDetails)
323 const int scopeBits = 2;
324 COMPILE_ASSERT(NoScopes < (1 << scopeBits), not_enough_bits_to_encode_ScopeInfoDetails);
326 int data = (maximumLimit << scopeBits) | scopeDetails;
327 v8::Handle<v8::Value> currentCallFrameV8;
328 if (executionState.IsEmpty()) {
329 v8::Handle<v8::Function> currentCallFrameFunction = v8::Local<v8::Function>::Cast(m_debuggerScript.newLocal(m_isolate)->Get(v8AtomicString(m_isolate, "currentCallFrame")));
330 currentCallFrameV8 = v8::Debug::Call(currentCallFrameFunction, v8::Integer::New(m_isolate, data));
332 v8::Handle<v8::Value> argv[] = { executionState, v8::Integer::New(m_isolate, data) };
333 currentCallFrameV8 = callDebuggerMethod("currentCallFrame", WTF_ARRAY_LENGTH(argv), argv);
335 ASSERT(!currentCallFrameV8.IsEmpty());
336 if (!currentCallFrameV8->IsObject())
337 return PassRefPtr<JavaScriptCallFrame>();
338 return JavaScriptCallFrame::create(v8::Debug::GetDebugContext(), v8::Handle<v8::Object>::Cast(currentCallFrameV8));
341 ScriptValue ScriptDebugServer::currentCallFramesInner(ScopeInfoDetails scopeDetails)
343 v8::HandleScope scope(m_isolate);
344 v8::Handle<v8::Context> pausedContext = m_pausedContext.IsEmpty() ? m_isolate->GetCurrentContext() : m_pausedContext;
345 if (pausedContext.IsEmpty())
346 return ScriptValue();
348 RefPtr<JavaScriptCallFrame> currentCallFrame = wrapCallFrames(m_executionState.newLocal(m_isolate), 0, scopeDetails);
349 if (!currentCallFrame)
350 return ScriptValue();
352 v8::Context::Scope contextScope(pausedContext);
353 return ScriptValue(ScriptState::from(pausedContext), toV8(currentCallFrame.release(), v8::Handle<v8::Object>(), pausedContext->GetIsolate()));
356 ScriptValue ScriptDebugServer::currentCallFrames()
358 return currentCallFramesInner(AllScopes);
361 ScriptValue ScriptDebugServer::currentCallFramesForAsyncStack()
363 return currentCallFramesInner(FastAsyncScopes);
366 void ScriptDebugServer::interruptAndRun(PassOwnPtr<Task> task, v8::Isolate* isolate)
368 v8::Debug::DebugBreakForCommand(isolate, new ClientDataImpl(task));
371 void ScriptDebugServer::runPendingTasks()
373 v8::Debug::ProcessDebugMessages();
376 static ScriptDebugServer* toScriptDebugServer(v8::Handle<v8::Value> data)
378 void* p = v8::Handle<v8::External>::Cast(data)->Value();
379 return static_cast<ScriptDebugServer*>(p);
382 void ScriptDebugServer::breakProgramCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
384 ASSERT(2 == info.Length());
385 ScriptDebugServer* thisPtr = toScriptDebugServer(info.Data());
386 v8::Handle<v8::Value> exception;
387 v8::Handle<v8::Array> hitBreakpoints;
388 thisPtr->handleProgramBreak(v8::Handle<v8::Object>::Cast(info[0]), exception, hitBreakpoints);
391 void ScriptDebugServer::handleProgramBreak(v8::Handle<v8::Object> executionState, v8::Handle<v8::Value> exception, v8::Handle<v8::Array> hitBreakpointNumbers)
393 // Don't allow nested breaks.
397 ScriptDebugListener* listener = getDebugListenerForContext(m_pausedContext);
401 Vector<String> breakpointIds;
402 if (!hitBreakpointNumbers.IsEmpty()) {
403 breakpointIds.resize(hitBreakpointNumbers->Length());
404 for (size_t i = 0; i < hitBreakpointNumbers->Length(); i++) {
405 v8::Handle<v8::Value> hitBreakpointNumber = hitBreakpointNumbers->Get(i);
406 ASSERT(!hitBreakpointNumber.IsEmpty() && hitBreakpointNumber->IsInt32());
407 breakpointIds[i] = String::number(hitBreakpointNumber->Int32Value());
411 m_executionState.set(m_isolate, executionState);
412 listener->didPause(ScriptState::from(m_pausedContext), currentCallFrames(), ScriptValue(ScriptState::from(m_pausedContext), exception), breakpointIds);
414 m_runningNestedMessageLoop = true;
415 runMessageLoopOnPause(m_pausedContext);
416 m_runningNestedMessageLoop = false;
419 void ScriptDebugServer::handleProgramBreak(const v8::Debug::EventDetails& eventDetails, v8::Handle<v8::Value> exception, v8::Handle<v8::Array> hitBreakpointNumbers)
421 m_pausedContext = eventDetails.GetEventContext();
422 handleProgramBreak(eventDetails.GetExecutionState(), exception, hitBreakpointNumbers);
423 m_pausedContext.Clear();
426 void ScriptDebugServer::v8DebugEventCallback(const v8::Debug::EventDetails& eventDetails)
428 ScriptDebugServer* thisPtr = toScriptDebugServer(eventDetails.GetCallbackData());
429 thisPtr->handleV8DebugEvent(eventDetails);
432 bool ScriptDebugServer::executeSkipPauseRequest(ScriptDebugListener::SkipPauseRequest request, v8::Handle<v8::Object> executionState)
434 if (request == ScriptDebugListener::NoSkip)
436 if (request == ScriptDebugListener::Continue)
438 ASSERT(request == ScriptDebugListener::StepInto || request == ScriptDebugListener::StepOut);
439 v8::Handle<v8::Value> argv[] = { executionState };
440 callDebuggerMethod(request == ScriptDebugListener::StepInto ? stepIntoV8MethodName : stepOutV8MethodName, 1, argv);
444 void ScriptDebugServer::handleV8DebugEvent(const v8::Debug::EventDetails& eventDetails)
446 v8::DebugEvent event = eventDetails.GetEvent();
448 if (event == v8::BreakForCommand) {
449 ClientDataImpl* data = static_cast<ClientDataImpl*>(eventDetails.GetClientData());
454 if (event != v8::Break && event != v8::Exception && event != v8::AfterCompile && event != v8::BeforeCompile)
457 v8::Handle<v8::Context> eventContext = eventDetails.GetEventContext();
458 ASSERT(!eventContext.IsEmpty());
460 ScriptDebugListener* listener = getDebugListenerForContext(eventContext);
462 v8::HandleScope scope(m_isolate);
463 v8::Handle<v8::Object> debuggerScript = m_debuggerScript.newLocal(m_isolate);
464 if (event == v8::BeforeCompile) {
465 preprocessBeforeCompile(eventDetails);
466 } else if (event == v8::AfterCompile) {
467 v8::Context::Scope contextScope(v8::Debug::GetDebugContext());
468 v8::Handle<v8::Function> getAfterCompileScript = v8::Local<v8::Function>::Cast(debuggerScript->Get(v8AtomicString(m_isolate, "getAfterCompileScript")));
469 v8::Handle<v8::Value> argv[] = { eventDetails.GetEventData() };
470 v8::Handle<v8::Value> value = V8ScriptRunner::callInternalFunction(getAfterCompileScript, debuggerScript, WTF_ARRAY_LENGTH(argv), argv, m_isolate);
471 ASSERT(value->IsObject());
472 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(value);
473 dispatchDidParseSource(listener, object);
474 } else if (event == v8::Exception) {
475 v8::Local<v8::StackTrace> stackTrace = v8::StackTrace::CurrentStackTrace(m_isolate, 1);
476 // Stack trace is empty in case of syntax error. Silently continue execution in such cases.
477 if (!stackTrace->GetFrameCount())
479 RefPtr<JavaScriptCallFrame> topFrame = wrapCallFrames(eventDetails.GetExecutionState(), 1, NoScopes);
480 if (executeSkipPauseRequest(listener->shouldSkipExceptionPause(topFrame), eventDetails.GetExecutionState()))
482 v8::Handle<v8::Object> eventData = eventDetails.GetEventData();
483 v8::Handle<v8::Value> exceptionGetterValue = eventData->Get(v8AtomicString(m_isolate, "exception"));
484 ASSERT(!exceptionGetterValue.IsEmpty() && exceptionGetterValue->IsFunction());
485 v8::Handle<v8::Value> exception = V8ScriptRunner::callInternalFunction(v8::Handle<v8::Function>::Cast(exceptionGetterValue), eventData, 0, 0, m_isolate);
486 handleProgramBreak(eventDetails, exception, v8::Handle<v8::Array>());
487 } else if (event == v8::Break) {
488 v8::Handle<v8::Function> getBreakpointNumbersFunction = v8::Local<v8::Function>::Cast(debuggerScript->Get(v8AtomicString(m_isolate, "getBreakpointNumbers")));
489 v8::Handle<v8::Value> argv[] = { eventDetails.GetEventData() };
490 v8::Handle<v8::Value> hitBreakpoints = V8ScriptRunner::callInternalFunction(getBreakpointNumbersFunction, debuggerScript, WTF_ARRAY_LENGTH(argv), argv, m_isolate);
491 ASSERT(hitBreakpoints->IsArray());
492 RefPtr<JavaScriptCallFrame> topFrame = wrapCallFrames(eventDetails.GetExecutionState(), 1, NoScopes);
493 ScriptDebugListener::SkipPauseRequest skipRequest;
494 if (v8::Handle<v8::Array>::Cast(hitBreakpoints)->Length())
495 skipRequest = listener->shouldSkipBreakpointPause(topFrame);
497 skipRequest = listener->shouldSkipStepPause(topFrame);
498 if (executeSkipPauseRequest(skipRequest, eventDetails.GetExecutionState()))
500 handleProgramBreak(eventDetails, v8::Handle<v8::Value>(), hitBreakpoints.As<v8::Array>());
505 void ScriptDebugServer::dispatchDidParseSource(ScriptDebugListener* listener, v8::Handle<v8::Object> object)
507 v8::Handle<v8::Value> id = object->Get(v8AtomicString(m_isolate, "id"));
508 ASSERT(!id.IsEmpty() && id->IsInt32());
509 String sourceID = String::number(id->Int32Value());
511 ScriptDebugListener::Script script;
512 script.url = toCoreStringWithUndefinedOrNullCheck(object->Get(v8AtomicString(m_isolate, "name")));
513 script.source = toCoreStringWithUndefinedOrNullCheck(object->Get(v8AtomicString(m_isolate, "source")));
514 script.sourceMappingURL = toCoreStringWithUndefinedOrNullCheck(object->Get(v8AtomicString(m_isolate, "sourceMappingURL")));
515 script.startLine = object->Get(v8AtomicString(m_isolate, "startLine"))->ToInteger()->Value();
516 script.startColumn = object->Get(v8AtomicString(m_isolate, "startColumn"))->ToInteger()->Value();
517 script.endLine = object->Get(v8AtomicString(m_isolate, "endLine"))->ToInteger()->Value();
518 script.endColumn = object->Get(v8AtomicString(m_isolate, "endColumn"))->ToInteger()->Value();
519 script.isContentScript = object->Get(v8AtomicString(m_isolate, "isContentScript"))->ToBoolean()->Value();
521 listener->didParseSource(sourceID, script);
524 void ScriptDebugServer::ensureDebuggerScriptCompiled()
526 if (!m_debuggerScript.isEmpty())
529 v8::HandleScope scope(m_isolate);
530 v8::Context::Scope contextScope(v8::Debug::GetDebugContext());
531 v8::Handle<v8::String> source = v8String(m_isolate, String(reinterpret_cast<const char*>(DebuggerScriptSource_js), sizeof(DebuggerScriptSource_js)));
532 v8::Local<v8::Value> value = V8ScriptRunner::compileAndRunInternalScript(source, m_isolate);
533 ASSERT(!value.IsEmpty());
534 ASSERT(value->IsObject());
535 m_debuggerScript.set(m_isolate, v8::Handle<v8::Object>::Cast(value));
538 v8::Local<v8::Value> ScriptDebugServer::functionScopes(v8::Handle<v8::Function> function)
540 ensureDebuggerScriptCompiled();
542 v8::Handle<v8::Value> argv[] = { function };
543 return callDebuggerMethod("getFunctionScopes", 1, argv);
546 v8::Local<v8::Value> ScriptDebugServer::getInternalProperties(v8::Handle<v8::Object>& object)
548 if (m_debuggerScript.isEmpty())
549 return v8::Local<v8::Value>::New(m_isolate, v8::Undefined(m_isolate));
551 v8::Handle<v8::Value> argv[] = { object };
552 return callDebuggerMethod("getInternalProperties", 1, argv);
555 v8::Handle<v8::Value> ScriptDebugServer::setFunctionVariableValue(v8::Handle<v8::Value> functionValue, int scopeNumber, const String& variableName, v8::Handle<v8::Value> newValue)
557 v8::Local<v8::Context> debuggerContext = v8::Debug::GetDebugContext();
558 if (m_debuggerScript.isEmpty())
559 return m_isolate->ThrowException(v8::String::NewFromUtf8(m_isolate, "Debugging is not enabled."));
561 v8::Handle<v8::Value> argv[] = {
563 v8::Handle<v8::Value>(v8::Integer::New(debuggerContext->GetIsolate(), scopeNumber)),
564 v8String(debuggerContext->GetIsolate(), variableName),
567 return callDebuggerMethod("setFunctionVariableValue", 4, argv);
571 bool ScriptDebugServer::isPaused()
573 return !m_executionState.isEmpty();
576 void ScriptDebugServer::compileScript(ScriptState* scriptState, const String& expression, const String& sourceURL, String* scriptId, String* exceptionMessage)
578 if (scriptState->contextIsEmpty())
580 ScriptState::Scope scope(scriptState);
582 v8::Handle<v8::String> source = v8String(m_isolate, expression);
583 v8::TryCatch tryCatch;
584 v8::Local<v8::Script> script = V8ScriptRunner::compileScript(source, sourceURL, TextPosition(), 0, m_isolate);
585 if (tryCatch.HasCaught()) {
586 v8::Local<v8::Message> message = tryCatch.Message();
587 if (!message.IsEmpty())
588 *exceptionMessage = toCoreStringWithUndefinedOrNullCheck(message->Get());
591 if (script.IsEmpty())
594 *scriptId = String::number(script->GetId());
595 m_compiledScripts.set(*scriptId, adoptPtr(new ScopedPersistent<v8::Script>(m_isolate, script)));
598 void ScriptDebugServer::clearCompiledScripts()
600 m_compiledScripts.clear();
603 void ScriptDebugServer::runScript(ScriptState* scriptState, const String& scriptId, ScriptValue* result, bool* wasThrown, String* exceptionMessage)
605 if (!m_compiledScripts.contains(scriptId))
607 v8::HandleScope handleScope(m_isolate);
608 ScopedPersistent<v8::Script>* scriptHandle = m_compiledScripts.get(scriptId);
609 v8::Local<v8::Script> script = scriptHandle->newLocal(m_isolate);
610 m_compiledScripts.remove(scriptId);
611 if (script.IsEmpty())
614 if (scriptState->contextIsEmpty())
616 ScriptState::Scope scope(scriptState);
617 v8::TryCatch tryCatch;
618 v8::Local<v8::Value> value = V8ScriptRunner::runCompiledScript(script, scriptState->executionContext(), m_isolate);
620 if (tryCatch.HasCaught()) {
622 *result = ScriptValue(scriptState, tryCatch.Exception());
623 v8::Local<v8::Message> message = tryCatch.Message();
624 if (!message.IsEmpty())
625 *exceptionMessage = toCoreStringWithUndefinedOrNullCheck(message->Get());
627 *result = ScriptValue(scriptState, value);
631 PassOwnPtr<ScriptSourceCode> ScriptDebugServer::preprocess(LocalFrame*, const ScriptSourceCode&)
633 return PassOwnPtr<ScriptSourceCode>();
636 String ScriptDebugServer::preprocessEventListener(LocalFrame*, const String& source, const String& url, const String& functionName)
641 } // namespace WebCore