Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / v8 / ScriptDebugServer.cpp
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/v8/ScriptDebugServer.h"
33
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"
49
50 namespace WebCore {
51
52 namespace {
53
54 class ClientDataImpl : public v8::Debug::ClientData {
55 public:
56     ClientDataImpl(PassOwnPtr<ScriptDebugServer::Task> task) : m_task(task) { }
57     virtual ~ClientDataImpl() { }
58     ScriptDebugServer::Task* task() const { return m_task.get(); }
59 private:
60     OwnPtr<ScriptDebugServer::Task> m_task;
61 };
62
63 const char stepIntoV8MethodName[] = "stepIntoStatement";
64 const char stepOutV8MethodName[] = "stepOutOfFunction";
65 }
66
67 v8::Local<v8::Value> ScriptDebugServer::callDebuggerMethod(const char* functionName, int argc, v8::Handle<v8::Value> argv[])
68 {
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);
73 }
74
75 ScriptDebugServer::ScriptDebugServer(v8::Isolate* isolate)
76     : m_pauseOnExceptionsState(DontPauseOnExceptions)
77     , m_breakpointsActivated(true)
78     , m_isolate(isolate)
79     , m_runningNestedMessageLoop(false)
80 {
81 }
82
83 ScriptDebugServer::~ScriptDebugServer()
84 {
85 }
86
87 String ScriptDebugServer::setBreakpoint(const String& sourceID, const ScriptBreakpoint& scriptBreakpoint, int* actualLineNumber, int* actualColumnNumber, bool interstatementLocation)
88 {
89     v8::HandleScope scope(m_isolate);
90     v8::Local<v8::Context> debuggerContext = v8::Debug::GetDebugContext();
91     v8::Context::Scope contextScope(debuggerContext);
92
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));
99
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())
103         return "";
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>());
107 }
108
109 void ScriptDebugServer::removeBreakpoint(const String& breakpointId)
110 {
111     v8::HandleScope scope(m_isolate);
112     v8::Local<v8::Context> debuggerContext = v8::Debug::GetDebugContext();
113     v8::Context::Scope contextScope(debuggerContext);
114
115     v8::Local<v8::Object> info = v8::Object::New(m_isolate);
116     info->Set(v8AtomicString(m_isolate, "breakpointId"), v8String(debuggerContext->GetIsolate(), breakpointId));
117
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);
120 }
121
122 void ScriptDebugServer::clearBreakpoints()
123 {
124     ensureDebuggerScriptCompiled();
125     v8::HandleScope scope(m_isolate);
126     v8::Local<v8::Context> debuggerContext = v8::Debug::GetDebugContext();
127     v8::Context::Scope contextScope(debuggerContext);
128
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);
131 }
132
133 void ScriptDebugServer::setBreakpointsActivated(bool activated)
134 {
135     ensureDebuggerScriptCompiled();
136     v8::HandleScope scope(m_isolate);
137     v8::Local<v8::Context> debuggerContext = v8::Debug::GetDebugContext();
138     v8::Context::Scope contextScope(debuggerContext);
139
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);
144
145     m_breakpointsActivated = activated;
146 }
147
148 ScriptDebugServer::PauseOnExceptionsState ScriptDebugServer::pauseOnExceptionsState()
149 {
150     ensureDebuggerScriptCompiled();
151     v8::HandleScope scope(m_isolate);
152     v8::Context::Scope contextScope(v8::Debug::GetDebugContext());
153
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());
157 }
158
159 void ScriptDebugServer::setPauseOnExceptionsState(PauseOnExceptionsState pauseOnExceptionsState)
160 {
161     ensureDebuggerScriptCompiled();
162     v8::HandleScope scope(m_isolate);
163     v8::Context::Scope contextScope(v8::Debug::GetDebugContext());
164
165     v8::Handle<v8::Value> argv[] = { v8::Int32::New(m_isolate, pauseOnExceptionsState) };
166     callDebuggerMethod("setPauseOnExceptionsState", 1, argv);
167 }
168
169 void ScriptDebugServer::setPauseOnNextStatement(bool pause)
170 {
171     if (isPaused())
172         return;
173     if (pause)
174         v8::Debug::DebugBreak(m_isolate);
175     else
176         v8::Debug::CancelDebugBreak(m_isolate);
177 }
178
179 bool ScriptDebugServer::canBreakProgram()
180 {
181     if (!m_breakpointsActivated)
182         return false;
183     v8::HandleScope scope(m_isolate);
184     return !m_isolate->GetCurrentContext().IsEmpty();
185 }
186
187 void ScriptDebugServer::breakProgram()
188 {
189     if (!canBreakProgram())
190         return;
191
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);
197     }
198
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();
203 }
204
205 void ScriptDebugServer::continueProgram()
206 {
207     if (isPaused())
208         quitMessageLoopOnPause();
209     m_executionState.clear();
210 }
211
212 void ScriptDebugServer::stepIntoStatement()
213 {
214     ASSERT(isPaused());
215     v8::HandleScope handleScope(m_isolate);
216     v8::Handle<v8::Value> argv[] = { m_executionState.newLocal(m_isolate) };
217     callDebuggerMethod(stepIntoV8MethodName, 1, argv);
218     continueProgram();
219 }
220
221 void ScriptDebugServer::stepCommandWithFrame(const char* functionName, const ScriptValue& frame)
222 {
223     ASSERT(isPaused());
224     v8::HandleScope handleScope(m_isolate);
225     v8::Handle<v8::Value> callFrame;
226     if (frame.isEmpty()) {
227         callFrame = v8::Undefined(m_isolate);
228     } else {
229         JavaScriptCallFrame* impl = V8JavaScriptCallFrame::toNative(v8::Handle<v8::Object>::Cast(frame.v8Value()));
230         callFrame = impl->innerCallFrame();
231     }
232
233     v8::Handle<v8::Value> argv[] = {
234         m_executionState.newLocal(m_isolate),
235         callFrame
236     };
237
238     callDebuggerMethod(functionName, 2, argv);
239     continueProgram();
240 }
241
242 void ScriptDebugServer::stepOverStatement(const ScriptValue& frame)
243 {
244     stepCommandWithFrame("stepOverStatement", frame);
245 }
246
247 void ScriptDebugServer::stepOutOfFunction(const ScriptValue& frame)
248 {
249     stepCommandWithFrame(stepOutV8MethodName, frame);
250 }
251
252 bool ScriptDebugServer::setScriptSource(const String& sourceID, const String& newContent, bool preview, String* error, RefPtr<TypeBuilder::Debugger::SetScriptSourceError>& errorData, ScriptValue* newCallFrames, RefPtr<JSONObject>* result)
253 {
254     class EnableLiveEditScope {
255     public:
256         explicit EnableLiveEditScope(v8::Isolate* isolate) : m_isolate(isolate) { v8::Debug::SetLiveEditEnabled(m_isolate, true); }
257         ~EnableLiveEditScope() { v8::Debug::SetLiveEditEnabled(m_isolate, false); }
258     private:
259         v8::Isolate* m_isolate;
260     };
261
262     ensureDebuggerScriptCompiled();
263     v8::HandleScope scope(m_isolate);
264
265     OwnPtr<v8::Context::Scope> contextScope;
266     v8::Handle<v8::Context> debuggerContext = v8::Debug::GetDebugContext();
267     if (!isPaused())
268         contextScope = adoptPtr(new v8::Context::Scope(debuggerContext));
269
270     v8::Handle<v8::Value> argv[] = { v8String(m_isolate, sourceID), v8String(m_isolate, newContent), v8Boolean(preview, m_isolate) };
271
272     v8::Local<v8::Value> v8result;
273     {
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());
282             else
283                 *error = "Unknown error.";
284             return false;
285         }
286     }
287     ASSERT(!v8result.IsEmpty());
288     v8::Local<v8::Object> resultTuple = v8result->ToObject();
289     int code = static_cast<int>(resultTuple->Get(0)->ToInteger()->Value());
290     switch (code) {
291     case 0:
292         {
293             v8::Local<v8::Value> normalResult = resultTuple->Get(1);
294             RefPtr<JSONValue> jsonResult = v8ToJSONValue(m_isolate, normalResult, JSONValue::maxDepth);
295             if (jsonResult)
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();
300             return true;
301         }
302     // Compile error.
303     case 1:
304         {
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());
310
311             *error = toCoreStringWithUndefinedOrNullCheck(resultTuple->Get(1));
312             errorData = TypeBuilder::Debugger::SetScriptSourceError::create();
313             errorData->setCompileError(compileError);
314             return false;
315         }
316     }
317     *error = "Unknown error.";
318     return false;
319 }
320
321 PassRefPtr<JavaScriptCallFrame> ScriptDebugServer::wrapCallFrames(v8::Handle<v8::Object> executionState, int maximumLimit, ScopeInfoDetails scopeDetails)
322 {
323     const int scopeBits = 2;
324     COMPILE_ASSERT(NoScopes < (1 << scopeBits), not_enough_bits_to_encode_ScopeInfoDetails);
325
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));
331     } else {
332         v8::Handle<v8::Value> argv[] = { executionState, v8::Integer::New(m_isolate, data) };
333         currentCallFrameV8 = callDebuggerMethod("currentCallFrame", WTF_ARRAY_LENGTH(argv), argv);
334     }
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));
339 }
340
341 ScriptValue ScriptDebugServer::currentCallFramesInner(ScopeInfoDetails scopeDetails)
342 {
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();
347
348     RefPtr<JavaScriptCallFrame> currentCallFrame = wrapCallFrames(m_executionState.newLocal(m_isolate), 0, scopeDetails);
349     if (!currentCallFrame)
350         return ScriptValue();
351
352     v8::Context::Scope contextScope(pausedContext);
353     return ScriptValue(ScriptState::from(pausedContext), toV8(currentCallFrame.release(), v8::Handle<v8::Object>(), pausedContext->GetIsolate()));
354 }
355
356 ScriptValue ScriptDebugServer::currentCallFrames()
357 {
358     return currentCallFramesInner(AllScopes);
359 }
360
361 ScriptValue ScriptDebugServer::currentCallFramesForAsyncStack()
362 {
363     return currentCallFramesInner(FastAsyncScopes);
364 }
365
366 void ScriptDebugServer::interruptAndRun(PassOwnPtr<Task> task, v8::Isolate* isolate)
367 {
368     v8::Debug::DebugBreakForCommand(isolate, new ClientDataImpl(task));
369 }
370
371 void ScriptDebugServer::runPendingTasks()
372 {
373     v8::Debug::ProcessDebugMessages();
374 }
375
376 static ScriptDebugServer* toScriptDebugServer(v8::Handle<v8::Value> data)
377 {
378     void* p = v8::Handle<v8::External>::Cast(data)->Value();
379     return static_cast<ScriptDebugServer*>(p);
380 }
381
382 void ScriptDebugServer::breakProgramCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
383 {
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);
389 }
390
391 void ScriptDebugServer::handleProgramBreak(v8::Handle<v8::Object> executionState, v8::Handle<v8::Value> exception, v8::Handle<v8::Array> hitBreakpointNumbers)
392 {
393     // Don't allow nested breaks.
394     if (isPaused())
395         return;
396
397     ScriptDebugListener* listener = getDebugListenerForContext(m_pausedContext);
398     if (!listener)
399         return;
400
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());
408         }
409     }
410
411     m_executionState.set(m_isolate, executionState);
412     listener->didPause(ScriptState::from(m_pausedContext), currentCallFrames(), ScriptValue(ScriptState::from(m_pausedContext), exception), breakpointIds);
413
414     m_runningNestedMessageLoop = true;
415     runMessageLoopOnPause(m_pausedContext);
416     m_runningNestedMessageLoop = false;
417 }
418
419 void ScriptDebugServer::handleProgramBreak(const v8::Debug::EventDetails& eventDetails, v8::Handle<v8::Value> exception, v8::Handle<v8::Array> hitBreakpointNumbers)
420 {
421     m_pausedContext = eventDetails.GetEventContext();
422     handleProgramBreak(eventDetails.GetExecutionState(), exception, hitBreakpointNumbers);
423     m_pausedContext.Clear();
424 }
425
426 void ScriptDebugServer::v8DebugEventCallback(const v8::Debug::EventDetails& eventDetails)
427 {
428     ScriptDebugServer* thisPtr = toScriptDebugServer(eventDetails.GetCallbackData());
429     thisPtr->handleV8DebugEvent(eventDetails);
430 }
431
432 bool ScriptDebugServer::executeSkipPauseRequest(ScriptDebugListener::SkipPauseRequest request, v8::Handle<v8::Object> executionState)
433 {
434     if (request == ScriptDebugListener::NoSkip)
435         return false;
436     if (request == ScriptDebugListener::Continue)
437         return true;
438     ASSERT(request == ScriptDebugListener::StepInto || request == ScriptDebugListener::StepOut);
439     v8::Handle<v8::Value> argv[] = { executionState };
440     callDebuggerMethod(request == ScriptDebugListener::StepInto ? stepIntoV8MethodName : stepOutV8MethodName, 1, argv);
441     return true;
442 }
443
444 void ScriptDebugServer::handleV8DebugEvent(const v8::Debug::EventDetails& eventDetails)
445 {
446     v8::DebugEvent event = eventDetails.GetEvent();
447
448     if (event == v8::BreakForCommand) {
449         ClientDataImpl* data = static_cast<ClientDataImpl*>(eventDetails.GetClientData());
450         data->task()->run();
451         return;
452     }
453
454     if (event != v8::Break && event != v8::Exception && event != v8::AfterCompile && event != v8::BeforeCompile)
455         return;
456
457     v8::Handle<v8::Context> eventContext = eventDetails.GetEventContext();
458     ASSERT(!eventContext.IsEmpty());
459
460     ScriptDebugListener* listener = getDebugListenerForContext(eventContext);
461     if (listener) {
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())
478                 return;
479             RefPtr<JavaScriptCallFrame> topFrame = wrapCallFrames(eventDetails.GetExecutionState(), 1, NoScopes);
480             if (executeSkipPauseRequest(listener->shouldSkipExceptionPause(topFrame), eventDetails.GetExecutionState()))
481                 return;
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);
496             else
497                 skipRequest = listener->shouldSkipStepPause(topFrame);
498             if (executeSkipPauseRequest(skipRequest, eventDetails.GetExecutionState()))
499                 return;
500             handleProgramBreak(eventDetails, v8::Handle<v8::Value>(), hitBreakpoints.As<v8::Array>());
501         }
502     }
503 }
504
505 void ScriptDebugServer::dispatchDidParseSource(ScriptDebugListener* listener, v8::Handle<v8::Object> object)
506 {
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());
510
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();
520
521     listener->didParseSource(sourceID, script);
522 }
523
524 void ScriptDebugServer::ensureDebuggerScriptCompiled()
525 {
526     if (!m_debuggerScript.isEmpty())
527         return;
528
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));
536 }
537
538 v8::Local<v8::Value> ScriptDebugServer::functionScopes(v8::Handle<v8::Function> function)
539 {
540     ensureDebuggerScriptCompiled();
541
542     v8::Handle<v8::Value> argv[] = { function };
543     return callDebuggerMethod("getFunctionScopes", 1, argv);
544 }
545
546 v8::Local<v8::Value> ScriptDebugServer::getInternalProperties(v8::Handle<v8::Object>& object)
547 {
548     if (m_debuggerScript.isEmpty())
549         return v8::Local<v8::Value>::New(m_isolate, v8::Undefined(m_isolate));
550
551     v8::Handle<v8::Value> argv[] = { object };
552     return callDebuggerMethod("getInternalProperties", 1, argv);
553 }
554
555 v8::Handle<v8::Value> ScriptDebugServer::setFunctionVariableValue(v8::Handle<v8::Value> functionValue, int scopeNumber, const String& variableName, v8::Handle<v8::Value> newValue)
556 {
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."));
560
561     v8::Handle<v8::Value> argv[] = {
562         functionValue,
563         v8::Handle<v8::Value>(v8::Integer::New(debuggerContext->GetIsolate(), scopeNumber)),
564         v8String(debuggerContext->GetIsolate(), variableName),
565         newValue
566     };
567     return callDebuggerMethod("setFunctionVariableValue", 4, argv);
568 }
569
570
571 bool ScriptDebugServer::isPaused()
572 {
573     return !m_executionState.isEmpty();
574 }
575
576 void ScriptDebugServer::compileScript(ScriptState* scriptState, const String& expression, const String& sourceURL, String* scriptId, String* exceptionMessage)
577 {
578     if (scriptState->contextIsEmpty())
579         return;
580     ScriptState::Scope scope(scriptState);
581
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());
589         return;
590     }
591     if (script.IsEmpty())
592         return;
593
594     *scriptId = String::number(script->GetId());
595     m_compiledScripts.set(*scriptId, adoptPtr(new ScopedPersistent<v8::Script>(m_isolate, script)));
596 }
597
598 void ScriptDebugServer::clearCompiledScripts()
599 {
600     m_compiledScripts.clear();
601 }
602
603 void ScriptDebugServer::runScript(ScriptState* scriptState, const String& scriptId, ScriptValue* result, bool* wasThrown, String* exceptionMessage)
604 {
605     if (!m_compiledScripts.contains(scriptId))
606         return;
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())
612         return;
613
614     if (scriptState->contextIsEmpty())
615         return;
616     ScriptState::Scope scope(scriptState);
617     v8::TryCatch tryCatch;
618     v8::Local<v8::Value> value = V8ScriptRunner::runCompiledScript(script, scriptState->executionContext(), m_isolate);
619     *wasThrown = false;
620     if (tryCatch.HasCaught()) {
621         *wasThrown = true;
622         *result = ScriptValue(scriptState, tryCatch.Exception());
623         v8::Local<v8::Message> message = tryCatch.Message();
624         if (!message.IsEmpty())
625             *exceptionMessage = toCoreStringWithUndefinedOrNullCheck(message->Get());
626     } else {
627         *result = ScriptValue(scriptState, value);
628     }
629 }
630
631 PassOwnPtr<ScriptSourceCode> ScriptDebugServer::preprocess(LocalFrame*, const ScriptSourceCode&)
632 {
633     return PassOwnPtr<ScriptSourceCode>();
634 }
635
636 String ScriptDebugServer::preprocessEventListener(LocalFrame*, const String& source, const String& url, const String& functionName)
637 {
638     return source;
639 }
640
641 } // namespace WebCore