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/v8/ScriptDebugServer.h" | |
33 | |
34 #include "bindings/core/v8/V8JavaScriptCallFrame.h" | |
35 #include "bindings/v8/ScopedPersistent.h" | |
36 #include "bindings/v8/ScriptCallStackFactory.h" | |
37 #include "bindings/v8/ScriptController.h" | |
38 #include "bindings/v8/ScriptSourceCode.h" | |
39 #include "bindings/v8/ScriptValue.h" | |
40 #include "bindings/v8/V8Binding.h" | |
41 #include "bindings/v8/V8ScriptRunner.h" | |
42 #include "core/DebuggerScriptSource.h" | |
43 #include "core/inspector/JavaScriptCallFrame.h" | |
44 #include "core/inspector/ScriptDebugListener.h" | |
45 #include "platform/JSONValues.h" | |
46 #include "wtf/StdLibExtras.h" | |
47 #include "wtf/Vector.h" | |
48 #include "wtf/dtoa/utils.h" | |
49 #include "wtf/text/CString.h" | |
50 | |
51 namespace WebCore { | |
52 | |
53 namespace { | |
54 | |
55 class ClientDataImpl : public v8::Debug::ClientData { | |
56 public: | |
57 ClientDataImpl(PassOwnPtr<ScriptDebugServer::Task> task) : m_task(task) { } | |
58 virtual ~ClientDataImpl() { } | |
59 ScriptDebugServer::Task* task() const { return m_task.get(); } | |
60 private: | |
61 OwnPtr<ScriptDebugServer::Task> m_task; | |
62 }; | |
63 | |
64 const char stepIntoV8MethodName[] = "stepIntoStatement"; | |
65 const char stepOutV8MethodName[] = "stepOutOfFunction"; | |
66 } | |
67 | |
68 v8::Local<v8::Value> ScriptDebugServer::callDebuggerMethod(const char* functionN
ame, int argc, v8::Handle<v8::Value> argv[]) | |
69 { | |
70 v8::Handle<v8::Object> debuggerScript = m_debuggerScript.newLocal(m_isolate)
; | |
71 v8::Handle<v8::Function> function = v8::Local<v8::Function>::Cast(debuggerSc
ript->Get(v8AtomicString(m_isolate, functionName))); | |
72 ASSERT(m_isolate->InContext()); | |
73 return V8ScriptRunner::callInternalFunction(function, debuggerScript, argc,
argv, m_isolate); | |
74 } | |
75 | |
76 ScriptDebugServer::ScriptDebugServer(v8::Isolate* isolate) | |
77 : m_pauseOnExceptionsState(DontPauseOnExceptions) | |
78 , m_breakpointsActivated(true) | |
79 , m_isolate(isolate) | |
80 , m_runningNestedMessageLoop(false) | |
81 { | |
82 } | |
83 | |
84 ScriptDebugServer::~ScriptDebugServer() | |
85 { | |
86 } | |
87 | |
88 String ScriptDebugServer::setBreakpoint(const String& sourceID, const ScriptBrea
kpoint& scriptBreakpoint, int* actualLineNumber, int* actualColumnNumber, bool i
nterstatementLocation) | |
89 { | |
90 v8::HandleScope scope(m_isolate); | |
91 v8::Local<v8::Context> debuggerContext = v8::Debug::GetDebugContext(); | |
92 v8::Context::Scope contextScope(debuggerContext); | |
93 | |
94 v8::Local<v8::Object> info = v8::Object::New(m_isolate); | |
95 info->Set(v8AtomicString(m_isolate, "sourceID"), v8String(debuggerContext->G
etIsolate(), sourceID)); | |
96 info->Set(v8AtomicString(m_isolate, "lineNumber"), v8::Integer::New(debugger
Context->GetIsolate(), scriptBreakpoint.lineNumber)); | |
97 info->Set(v8AtomicString(m_isolate, "columnNumber"), v8::Integer::New(debugg
erContext->GetIsolate(), scriptBreakpoint.columnNumber)); | |
98 info->Set(v8AtomicString(m_isolate, "interstatementLocation"), v8Boolean(int
erstatementLocation, debuggerContext->GetIsolate())); | |
99 info->Set(v8AtomicString(m_isolate, "condition"), v8String(debuggerContext->
GetIsolate(), scriptBreakpoint.condition)); | |
100 | |
101 v8::Handle<v8::Function> setBreakpointFunction = v8::Local<v8::Function>::Ca
st(m_debuggerScript.newLocal(m_isolate)->Get(v8AtomicString(m_isolate, "setBreak
point"))); | |
102 v8::Handle<v8::Value> breakpointId = v8::Debug::Call(setBreakpointFunction,
info); | |
103 if (breakpointId.IsEmpty() || !breakpointId->IsString()) | |
104 return ""; | |
105 *actualLineNumber = info->Get(v8AtomicString(m_isolate, "lineNumber"))->Int3
2Value(); | |
106 *actualColumnNumber = info->Get(v8AtomicString(m_isolate, "columnNumber"))->
Int32Value(); | |
107 return toCoreString(breakpointId.As<v8::String>()); | |
108 } | |
109 | |
110 void ScriptDebugServer::removeBreakpoint(const String& breakpointId) | |
111 { | |
112 v8::HandleScope scope(m_isolate); | |
113 v8::Local<v8::Context> debuggerContext = v8::Debug::GetDebugContext(); | |
114 v8::Context::Scope contextScope(debuggerContext); | |
115 | |
116 v8::Local<v8::Object> info = v8::Object::New(m_isolate); | |
117 info->Set(v8AtomicString(m_isolate, "breakpointId"), v8String(debuggerContex
t->GetIsolate(), breakpointId)); | |
118 | |
119 v8::Handle<v8::Function> removeBreakpointFunction = v8::Local<v8::Function>:
:Cast(m_debuggerScript.newLocal(m_isolate)->Get(v8AtomicString(m_isolate, "remov
eBreakpoint"))); | |
120 v8::Debug::Call(removeBreakpointFunction, info); | |
121 } | |
122 | |
123 void ScriptDebugServer::clearBreakpoints() | |
124 { | |
125 ensureDebuggerScriptCompiled(); | |
126 v8::HandleScope scope(m_isolate); | |
127 v8::Local<v8::Context> debuggerContext = v8::Debug::GetDebugContext(); | |
128 v8::Context::Scope contextScope(debuggerContext); | |
129 | |
130 v8::Handle<v8::Function> clearBreakpoints = v8::Local<v8::Function>::Cast(m_
debuggerScript.newLocal(m_isolate)->Get(v8AtomicString(m_isolate, "clearBreakpoi
nts"))); | |
131 v8::Debug::Call(clearBreakpoints); | |
132 } | |
133 | |
134 void ScriptDebugServer::setBreakpointsActivated(bool activated) | |
135 { | |
136 ensureDebuggerScriptCompiled(); | |
137 v8::HandleScope scope(m_isolate); | |
138 v8::Local<v8::Context> debuggerContext = v8::Debug::GetDebugContext(); | |
139 v8::Context::Scope contextScope(debuggerContext); | |
140 | |
141 v8::Local<v8::Object> info = v8::Object::New(m_isolate); | |
142 info->Set(v8AtomicString(m_isolate, "enabled"), v8::Boolean::New(m_isolate,
activated)); | |
143 v8::Handle<v8::Function> setBreakpointsActivated = v8::Local<v8::Function>::
Cast(m_debuggerScript.newLocal(m_isolate)->Get(v8AtomicString(m_isolate, "setBre
akpointsActivated"))); | |
144 v8::Debug::Call(setBreakpointsActivated, info); | |
145 | |
146 m_breakpointsActivated = activated; | |
147 } | |
148 | |
149 ScriptDebugServer::PauseOnExceptionsState ScriptDebugServer::pauseOnExceptionsSt
ate() | |
150 { | |
151 ensureDebuggerScriptCompiled(); | |
152 v8::HandleScope scope(m_isolate); | |
153 v8::Context::Scope contextScope(v8::Debug::GetDebugContext()); | |
154 | |
155 v8::Handle<v8::Value> argv[] = { v8Undefined() }; | |
156 v8::Handle<v8::Value> result = callDebuggerMethod("pauseOnExceptionsState",
0, argv); | |
157 return static_cast<ScriptDebugServer::PauseOnExceptionsState>(result->Int32V
alue()); | |
158 } | |
159 | |
160 void ScriptDebugServer::setPauseOnExceptionsState(PauseOnExceptionsState pauseOn
ExceptionsState) | |
161 { | |
162 ensureDebuggerScriptCompiled(); | |
163 v8::HandleScope scope(m_isolate); | |
164 v8::Context::Scope contextScope(v8::Debug::GetDebugContext()); | |
165 | |
166 v8::Handle<v8::Value> argv[] = { v8::Int32::New(m_isolate, pauseOnExceptions
State) }; | |
167 callDebuggerMethod("setPauseOnExceptionsState", 1, argv); | |
168 } | |
169 | |
170 void ScriptDebugServer::setPauseOnNextStatement(bool pause) | |
171 { | |
172 ASSERT(!isPaused()); | |
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 return m_isolate->InContext(); | |
184 } | |
185 | |
186 void ScriptDebugServer::breakProgram() | |
187 { | |
188 if (!canBreakProgram()) | |
189 return; | |
190 | |
191 v8::HandleScope scope(m_isolate); | |
192 if (m_breakProgramCallbackTemplate.isEmpty()) { | |
193 v8::Handle<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(m_iso
late); | |
194 templ->SetCallHandler(&ScriptDebugServer::breakProgramCallback, v8::Exte
rnal::New(m_isolate, this)); | |
195 m_breakProgramCallbackTemplate.set(m_isolate, templ); | |
196 } | |
197 | |
198 v8::Handle<v8::Function> breakProgramFunction = m_breakProgramCallbackTempla
te.newLocal(m_isolate)->GetFunction(); | |
199 v8::Debug::Call(breakProgramFunction); | |
200 } | |
201 | |
202 void ScriptDebugServer::continueProgram() | |
203 { | |
204 if (isPaused()) | |
205 quitMessageLoopOnPause(); | |
206 m_pausedScriptState.clear(); | |
207 m_executionState.Clear(); | |
208 } | |
209 | |
210 void ScriptDebugServer::stepIntoStatement() | |
211 { | |
212 ASSERT(isPaused()); | |
213 ASSERT(!m_executionState.IsEmpty()); | |
214 v8::HandleScope handleScope(m_isolate); | |
215 v8::Handle<v8::Value> argv[] = { m_executionState }; | |
216 callDebuggerMethod(stepIntoV8MethodName, 1, argv); | |
217 continueProgram(); | |
218 } | |
219 | |
220 void ScriptDebugServer::stepOverStatement() | |
221 { | |
222 ASSERT(isPaused()); | |
223 ASSERT(!m_executionState.IsEmpty()); | |
224 v8::HandleScope handleScope(m_isolate); | |
225 v8::Handle<v8::Value> argv[] = { m_executionState }; | |
226 callDebuggerMethod("stepOverStatement", 1, argv); | |
227 continueProgram(); | |
228 } | |
229 | |
230 void ScriptDebugServer::stepOutOfFunction() | |
231 { | |
232 ASSERT(isPaused()); | |
233 ASSERT(!m_executionState.IsEmpty()); | |
234 v8::HandleScope handleScope(m_isolate); | |
235 v8::Handle<v8::Value> argv[] = { m_executionState }; | |
236 callDebuggerMethod(stepOutV8MethodName, 1, argv); | |
237 continueProgram(); | |
238 } | |
239 | |
240 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) | |
241 { | |
242 class EnableLiveEditScope { | |
243 public: | |
244 explicit EnableLiveEditScope(v8::Isolate* isolate) : m_isolate(isolate)
{ v8::Debug::SetLiveEditEnabled(m_isolate, true); } | |
245 ~EnableLiveEditScope() { v8::Debug::SetLiveEditEnabled(m_isolate, false)
; } | |
246 private: | |
247 v8::Isolate* m_isolate; | |
248 }; | |
249 | |
250 ensureDebuggerScriptCompiled(); | |
251 v8::HandleScope scope(m_isolate); | |
252 | |
253 OwnPtr<v8::Context::Scope> contextScope; | |
254 v8::Handle<v8::Context> debuggerContext = v8::Debug::GetDebugContext(); | |
255 if (!isPaused()) | |
256 contextScope = adoptPtr(new v8::Context::Scope(debuggerContext)); | |
257 | |
258 v8::Handle<v8::Value> argv[] = { v8String(m_isolate, sourceID), v8String(m_i
solate, newContent), v8Boolean(preview, m_isolate) }; | |
259 | |
260 v8::Local<v8::Value> v8result; | |
261 { | |
262 EnableLiveEditScope enableLiveEditScope(m_isolate); | |
263 v8::TryCatch tryCatch; | |
264 tryCatch.SetVerbose(false); | |
265 v8result = callDebuggerMethod("liveEditScriptSource", 3, argv); | |
266 if (tryCatch.HasCaught()) { | |
267 v8::Local<v8::Message> message = tryCatch.Message(); | |
268 if (!message.IsEmpty()) | |
269 *error = toCoreStringWithUndefinedOrNullCheck(message->Get()); | |
270 else | |
271 *error = "Unknown error."; | |
272 return false; | |
273 } | |
274 } | |
275 ASSERT(!v8result.IsEmpty()); | |
276 v8::Local<v8::Object> resultTuple = v8result->ToObject(); | |
277 int code = static_cast<int>(resultTuple->Get(0)->ToInteger()->Value()); | |
278 switch (code) { | |
279 case 0: | |
280 { | |
281 v8::Local<v8::Value> normalResult = resultTuple->Get(1); | |
282 RefPtr<JSONValue> jsonResult = v8ToJSONValue(m_isolate, normalResult
, JSONValue::maxDepth); | |
283 if (jsonResult) | |
284 *result = jsonResult->asObject(); | |
285 // Call stack may have changed after if the edited function was on t
he stack. | |
286 if (!preview && isPaused()) | |
287 *newCallFrames = currentCallFrames(); | |
288 return true; | |
289 } | |
290 // Compile error. | |
291 case 1: | |
292 { | |
293 RefPtr<TypeBuilder::Debugger::SetScriptSourceError::CompileError> co
mpileError = | |
294 TypeBuilder::Debugger::SetScriptSourceError::CompileError::creat
e() | |
295 .setMessage(toCoreStringWithUndefinedOrNullCheck(resultTuple
->Get(2))) | |
296 .setLineNumber(resultTuple->Get(3)->ToInteger()->Value()) | |
297 .setColumnNumber(resultTuple->Get(4)->ToInteger()->Value()); | |
298 | |
299 *error = toCoreStringWithUndefinedOrNullCheck(resultTuple->Get(1)); | |
300 errorData = TypeBuilder::Debugger::SetScriptSourceError::create(); | |
301 errorData->setCompileError(compileError); | |
302 return false; | |
303 } | |
304 } | |
305 *error = "Unknown error."; | |
306 return false; | |
307 } | |
308 | |
309 int ScriptDebugServer::frameCount() | |
310 { | |
311 ASSERT(isPaused()); | |
312 ASSERT(!m_executionState.IsEmpty()); | |
313 v8::Handle<v8::Value> argv[] = { m_executionState }; | |
314 v8::Handle<v8::Value> result = callDebuggerMethod("frameCount", WTF_ARRAY_LE
NGTH(argv), argv); | |
315 if (result->IsInt32()) | |
316 return result->Int32Value(); | |
317 return 0; | |
318 } | |
319 | |
320 PassRefPtrWillBeRawPtr<JavaScriptCallFrame> ScriptDebugServer::wrapCallFrames(in
t maximumLimit, ScopeInfoDetails scopeDetails) | |
321 { | |
322 const int scopeBits = 2; | |
323 COMPILE_ASSERT(NoScopes < (1 << scopeBits), not_enough_bits_to_encode_ScopeI
nfoDetails); | |
324 | |
325 ASSERT(maximumLimit >= 0); | |
326 int data = (maximumLimit << scopeBits) | scopeDetails; | |
327 v8::Handle<v8::Value> currentCallFrameV8; | |
328 if (m_executionState.IsEmpty()) { | |
329 v8::Handle<v8::Function> currentCallFrameFunction = v8::Local<v8::Functi
on>::Cast(m_debuggerScript.newLocal(m_isolate)->Get(v8AtomicString(m_isolate, "c
urrentCallFrame"))); | |
330 currentCallFrameV8 = v8::Debug::Call(currentCallFrameFunction, v8::Integ
er::New(m_isolate, data)); | |
331 } else { | |
332 v8::Handle<v8::Value> argv[] = { m_executionState, v8::Integer::New(m_is
olate, data) }; | |
333 currentCallFrameV8 = callDebuggerMethod("currentCallFrame", WTF_ARRAY_LE
NGTH(argv), argv); | |
334 } | |
335 ASSERT(!currentCallFrameV8.IsEmpty()); | |
336 if (!currentCallFrameV8->IsObject()) | |
337 return nullptr; | |
338 return JavaScriptCallFrame::create(v8::Debug::GetDebugContext(), v8::Handle<
v8::Object>::Cast(currentCallFrameV8)); | |
339 } | |
340 | |
341 ScriptValue ScriptDebugServer::currentCallFramesInner(ScopeInfoDetails scopeDeta
ils) | |
342 { | |
343 if (!m_isolate->InContext()) | |
344 return ScriptValue(); | |
345 v8::HandleScope handleScope(m_isolate); | |
346 | |
347 RefPtrWillBeRawPtr<JavaScriptCallFrame> currentCallFrame = wrapCallFrames(0,
scopeDetails); | |
348 if (!currentCallFrame) | |
349 return ScriptValue(); | |
350 | |
351 ScriptState* scriptState = m_pausedScriptState ? m_pausedScriptState.get() :
ScriptState::current(m_isolate); | |
352 ScriptState::Scope scope(scriptState); | |
353 return ScriptValue(scriptState, toV8(currentCallFrame.release(), scriptState
->context()->Global(), m_isolate)); | |
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 PassRefPtrWillBeRawPtr<JavaScriptCallFrame> ScriptDebugServer::topCallFrameNoSco
pes() | |
367 { | |
368 return wrapCallFrames(1, NoScopes); | |
369 } | |
370 | |
371 void ScriptDebugServer::interruptAndRun(PassOwnPtr<Task> task, v8::Isolate* isol
ate) | |
372 { | |
373 v8::Debug::DebugBreakForCommand(isolate, new ClientDataImpl(task)); | |
374 } | |
375 | |
376 void ScriptDebugServer::runPendingTasks() | |
377 { | |
378 v8::Debug::ProcessDebugMessages(); | |
379 } | |
380 | |
381 static ScriptDebugServer* toScriptDebugServer(v8::Handle<v8::Value> data) | |
382 { | |
383 void* p = v8::Handle<v8::External>::Cast(data)->Value(); | |
384 return static_cast<ScriptDebugServer*>(p); | |
385 } | |
386 | |
387 void ScriptDebugServer::breakProgramCallback(const v8::FunctionCallbackInfo<v8::
Value>& info) | |
388 { | |
389 ASSERT(2 == info.Length()); | |
390 ScriptDebugServer* thisPtr = toScriptDebugServer(info.Data()); | |
391 ScriptState* pausedScriptState = ScriptState::current(thisPtr->m_isolate); | |
392 v8::Handle<v8::Value> exception; | |
393 v8::Handle<v8::Array> hitBreakpoints; | |
394 thisPtr->handleProgramBreak(pausedScriptState, v8::Handle<v8::Object>::Cast(
info[0]), exception, hitBreakpoints); | |
395 } | |
396 | |
397 void ScriptDebugServer::handleProgramBreak(ScriptState* pausedScriptState, v8::H
andle<v8::Object> executionState, v8::Handle<v8::Value> exception, v8::Handle<v8
::Array> hitBreakpointNumbers) | |
398 { | |
399 // Don't allow nested breaks. | |
400 if (isPaused()) | |
401 return; | |
402 | |
403 ScriptDebugListener* listener = getDebugListenerForContext(pausedScriptState
->context()); | |
404 if (!listener) | |
405 return; | |
406 | |
407 Vector<String> breakpointIds; | |
408 if (!hitBreakpointNumbers.IsEmpty()) { | |
409 breakpointIds.resize(hitBreakpointNumbers->Length()); | |
410 for (size_t i = 0; i < hitBreakpointNumbers->Length(); i++) { | |
411 v8::Handle<v8::Value> hitBreakpointNumber = hitBreakpointNumbers->Ge
t(i); | |
412 ASSERT(!hitBreakpointNumber.IsEmpty() && hitBreakpointNumber->IsInt3
2()); | |
413 breakpointIds[i] = String::number(hitBreakpointNumber->Int32Value())
; | |
414 } | |
415 } | |
416 | |
417 m_pausedScriptState = pausedScriptState; | |
418 m_executionState = executionState; | |
419 ScriptDebugListener::SkipPauseRequest result = listener->didPause(pausedScri
ptState, currentCallFrames(), ScriptValue(pausedScriptState, exception), breakpo
intIds); | |
420 if (result == ScriptDebugListener::NoSkip) { | |
421 m_runningNestedMessageLoop = true; | |
422 runMessageLoopOnPause(pausedScriptState->context()); | |
423 m_runningNestedMessageLoop = false; | |
424 } | |
425 m_pausedScriptState.clear(); | |
426 m_executionState.Clear(); | |
427 | |
428 if (result == ScriptDebugListener::StepInto) { | |
429 v8::Handle<v8::Value> argv[] = { executionState }; | |
430 callDebuggerMethod(stepIntoV8MethodName, 1, argv); | |
431 } else if (result == ScriptDebugListener::StepOut) { | |
432 v8::Handle<v8::Value> argv[] = { executionState }; | |
433 callDebuggerMethod(stepOutV8MethodName, 1, argv); | |
434 } | |
435 } | |
436 | |
437 void ScriptDebugServer::v8DebugEventCallback(const v8::Debug::EventDetails& even
tDetails) | |
438 { | |
439 ScriptDebugServer* thisPtr = toScriptDebugServer(eventDetails.GetCallbackDat
a()); | |
440 thisPtr->handleV8DebugEvent(eventDetails); | |
441 } | |
442 | |
443 void ScriptDebugServer::handleV8DebugEvent(const v8::Debug::EventDetails& eventD
etails) | |
444 { | |
445 v8::DebugEvent event = eventDetails.GetEvent(); | |
446 | |
447 if (event == v8::BreakForCommand) { | |
448 ClientDataImpl* data = static_cast<ClientDataImpl*>(eventDetails.GetClie
ntData()); | |
449 data->task()->run(); | |
450 return; | |
451 } | |
452 | |
453 if (event != v8::Break && event != v8::Exception && event != v8::AfterCompil
e && event != v8::BeforeCompile) | |
454 return; | |
455 | |
456 v8::Handle<v8::Context> eventContext = eventDetails.GetEventContext(); | |
457 ASSERT(!eventContext.IsEmpty()); | |
458 | |
459 ScriptDebugListener* listener = getDebugListenerForContext(eventContext); | |
460 if (listener) { | |
461 v8::HandleScope scope(m_isolate); | |
462 v8::Handle<v8::Object> debuggerScript = m_debuggerScript.newLocal(m_isol
ate); | |
463 if (event == v8::BeforeCompile) { | |
464 preprocessBeforeCompile(eventDetails); | |
465 } else if (event == v8::AfterCompile) { | |
466 v8::Context::Scope contextScope(v8::Debug::GetDebugContext()); | |
467 v8::Handle<v8::Function> getAfterCompileScript = v8::Local<v8::Funct
ion>::Cast(debuggerScript->Get(v8AtomicString(m_isolate, "getAfterCompileScript"
))); | |
468 v8::Handle<v8::Value> argv[] = { eventDetails.GetEventData() }; | |
469 v8::Handle<v8::Value> value = V8ScriptRunner::callInternalFunction(g
etAfterCompileScript, debuggerScript, WTF_ARRAY_LENGTH(argv), argv, m_isolate); | |
470 ASSERT(value->IsObject()); | |
471 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(value); | |
472 dispatchDidParseSource(listener, object); | |
473 } else if (event == v8::Exception) { | |
474 v8::Local<v8::StackTrace> stackTrace = v8::StackTrace::CurrentStackT
race(m_isolate, 1); | |
475 // Stack trace is empty in case of syntax error. Silently continue e
xecution in such cases. | |
476 if (!stackTrace->GetFrameCount()) | |
477 return; | |
478 v8::Handle<v8::Object> eventData = eventDetails.GetEventData(); | |
479 v8::Handle<v8::Value> exceptionGetterValue = eventData->Get(v8Atomic
String(m_isolate, "exception")); | |
480 ASSERT(!exceptionGetterValue.IsEmpty() && exceptionGetterValue->IsFu
nction()); | |
481 v8::Handle<v8::Value> exception = V8ScriptRunner::callInternalFuncti
on(v8::Handle<v8::Function>::Cast(exceptionGetterValue), eventData, 0, 0, m_isol
ate); | |
482 handleProgramBreak(ScriptState::from(eventContext), eventDetails.Get
ExecutionState(), exception, v8::Handle<v8::Array>()); | |
483 } else if (event == v8::Break) { | |
484 v8::Handle<v8::Function> getBreakpointNumbersFunction = v8::Local<v8
::Function>::Cast(debuggerScript->Get(v8AtomicString(m_isolate, "getBreakpointNu
mbers"))); | |
485 v8::Handle<v8::Value> argv[] = { eventDetails.GetEventData() }; | |
486 v8::Handle<v8::Value> hitBreakpoints = V8ScriptRunner::callInternalF
unction(getBreakpointNumbersFunction, debuggerScript, WTF_ARRAY_LENGTH(argv), ar
gv, m_isolate); | |
487 ASSERT(hitBreakpoints->IsArray()); | |
488 handleProgramBreak(ScriptState::from(eventContext), eventDetails.Get
ExecutionState(), v8::Handle<v8::Value>(), hitBreakpoints.As<v8::Array>()); | |
489 } | |
490 } | |
491 } | |
492 | |
493 void ScriptDebugServer::dispatchDidParseSource(ScriptDebugListener* listener, v8
::Handle<v8::Object> object) | |
494 { | |
495 v8::Handle<v8::Value> id = object->Get(v8AtomicString(m_isolate, "id")); | |
496 ASSERT(!id.IsEmpty() && id->IsInt32()); | |
497 String sourceID = String::number(id->Int32Value()); | |
498 | |
499 ScriptDebugListener::Script script; | |
500 script.url = toCoreStringWithUndefinedOrNullCheck(object->Get(v8AtomicString
(m_isolate, "name"))); | |
501 script.source = toCoreStringWithUndefinedOrNullCheck(object->Get(v8AtomicStr
ing(m_isolate, "source"))); | |
502 script.sourceMappingURL = toCoreStringWithUndefinedOrNullCheck(object->Get(v
8AtomicString(m_isolate, "sourceMappingURL"))); | |
503 script.startLine = object->Get(v8AtomicString(m_isolate, "startLine"))->ToIn
teger()->Value(); | |
504 script.startColumn = object->Get(v8AtomicString(m_isolate, "startColumn"))->
ToInteger()->Value(); | |
505 script.endLine = object->Get(v8AtomicString(m_isolate, "endLine"))->ToIntege
r()->Value(); | |
506 script.endColumn = object->Get(v8AtomicString(m_isolate, "endColumn"))->ToIn
teger()->Value(); | |
507 script.isContentScript = object->Get(v8AtomicString(m_isolate, "isContentScr
ipt"))->ToBoolean()->Value(); | |
508 | |
509 listener->didParseSource(sourceID, script); | |
510 } | |
511 | |
512 void ScriptDebugServer::ensureDebuggerScriptCompiled() | |
513 { | |
514 if (!m_debuggerScript.isEmpty()) | |
515 return; | |
516 | |
517 v8::HandleScope scope(m_isolate); | |
518 v8::Context::Scope contextScope(v8::Debug::GetDebugContext()); | |
519 v8::Handle<v8::String> source = v8String(m_isolate, String(reinterpret_cast<
const char*>(DebuggerScriptSource_js), sizeof(DebuggerScriptSource_js))); | |
520 v8::Local<v8::Value> value = V8ScriptRunner::compileAndRunInternalScript(sou
rce, m_isolate); | |
521 ASSERT(!value.IsEmpty()); | |
522 ASSERT(value->IsObject()); | |
523 m_debuggerScript.set(m_isolate, v8::Handle<v8::Object>::Cast(value)); | |
524 } | |
525 | |
526 void ScriptDebugServer::discardDebuggerScript() | |
527 { | |
528 ASSERT(!m_debuggerScript.isEmpty()); | |
529 m_debuggerScript.clear(); | |
530 } | |
531 | |
532 v8::Local<v8::Value> ScriptDebugServer::functionScopes(v8::Handle<v8::Function>
function) | |
533 { | |
534 ensureDebuggerScriptCompiled(); | |
535 | |
536 v8::Handle<v8::Value> argv[] = { function }; | |
537 return callDebuggerMethod("getFunctionScopes", 1, argv); | |
538 } | |
539 | |
540 v8::Local<v8::Value> ScriptDebugServer::getInternalProperties(v8::Handle<v8::Obj
ect>& object) | |
541 { | |
542 if (m_debuggerScript.isEmpty()) | |
543 return v8::Local<v8::Value>::New(m_isolate, v8::Undefined(m_isolate)); | |
544 | |
545 v8::Handle<v8::Value> argv[] = { object }; | |
546 return callDebuggerMethod("getInternalProperties", 1, argv); | |
547 } | |
548 | |
549 v8::Handle<v8::Value> ScriptDebugServer::setFunctionVariableValue(v8::Handle<v8:
:Value> functionValue, int scopeNumber, const String& variableName, v8::Handle<v
8::Value> newValue) | |
550 { | |
551 v8::Local<v8::Context> debuggerContext = v8::Debug::GetDebugContext(); | |
552 if (m_debuggerScript.isEmpty()) | |
553 return m_isolate->ThrowException(v8::String::NewFromUtf8(m_isolate, "Deb
ugging is not enabled.")); | |
554 | |
555 v8::Handle<v8::Value> argv[] = { | |
556 functionValue, | |
557 v8::Handle<v8::Value>(v8::Integer::New(debuggerContext->GetIsolate(), sc
opeNumber)), | |
558 v8String(debuggerContext->GetIsolate(), variableName), | |
559 newValue | |
560 }; | |
561 return callDebuggerMethod("setFunctionVariableValue", 4, argv); | |
562 } | |
563 | |
564 | |
565 bool ScriptDebugServer::isPaused() | |
566 { | |
567 return m_pausedScriptState; | |
568 } | |
569 | |
570 void ScriptDebugServer::compileScript(ScriptState* scriptState, const String& ex
pression, const String& sourceURL, String* scriptId, String* exceptionDetailsTex
t, int* lineNumber, int* columnNumber, RefPtrWillBeRawPtr<ScriptCallStack>* stac
kTrace) | |
571 { | |
572 if (scriptState->contextIsEmpty()) | |
573 return; | |
574 ScriptState::Scope scope(scriptState); | |
575 | |
576 v8::Handle<v8::String> source = v8String(m_isolate, expression); | |
577 v8::TryCatch tryCatch; | |
578 v8::Local<v8::Script> script = V8ScriptRunner::compileScript(source, sourceU
RL, TextPosition(), 0, m_isolate); | |
579 if (tryCatch.HasCaught()) { | |
580 v8::Local<v8::Message> message = tryCatch.Message(); | |
581 if (!message.IsEmpty()) { | |
582 *exceptionDetailsText = toCoreStringWithUndefinedOrNullCheck(message
->Get()); | |
583 *lineNumber = message->GetLineNumber(); | |
584 *columnNumber = message->GetStartColumn(); | |
585 *stackTrace = createScriptCallStack(message->GetStackTrace(), messag
e->GetStackTrace()->GetFrameCount(), m_isolate); | |
586 } | |
587 return; | |
588 } | |
589 if (script.IsEmpty()) | |
590 return; | |
591 | |
592 *scriptId = String::number(script->GetUnboundScript()->GetId()); | |
593 m_compiledScripts.set(*scriptId, adoptPtr(new ScopedPersistent<v8::Script>(m
_isolate, script))); | |
594 } | |
595 | |
596 void ScriptDebugServer::clearCompiledScripts() | |
597 { | |
598 m_compiledScripts.clear(); | |
599 } | |
600 | |
601 void ScriptDebugServer::runScript(ScriptState* scriptState, const String& script
Id, ScriptValue* result, bool* wasThrown, String* exceptionDetailsText, int* lin
eNumber, int* columnNumber, RefPtrWillBeRawPtr<ScriptCallStack>* stackTrace) | |
602 { | |
603 if (!m_compiledScripts.contains(scriptId)) | |
604 return; | |
605 v8::HandleScope handleScope(m_isolate); | |
606 ScopedPersistent<v8::Script>* scriptHandle = m_compiledScripts.get(scriptId)
; | |
607 v8::Local<v8::Script> script = scriptHandle->newLocal(m_isolate); | |
608 m_compiledScripts.remove(scriptId); | |
609 if (script.IsEmpty()) | |
610 return; | |
611 | |
612 if (scriptState->contextIsEmpty()) | |
613 return; | |
614 ScriptState::Scope scope(scriptState); | |
615 v8::TryCatch tryCatch; | |
616 v8::Local<v8::Value> value = V8ScriptRunner::runCompiledScript(script, scrip
tState->executionContext(), m_isolate); | |
617 *wasThrown = false; | |
618 if (tryCatch.HasCaught()) { | |
619 *wasThrown = true; | |
620 *result = ScriptValue(scriptState, tryCatch.Exception()); | |
621 v8::Local<v8::Message> message = tryCatch.Message(); | |
622 if (!message.IsEmpty()) { | |
623 *exceptionDetailsText = toCoreStringWithUndefinedOrNullCheck(message
->Get()); | |
624 *lineNumber = message->GetLineNumber(); | |
625 *columnNumber = message->GetStartColumn(); | |
626 *stackTrace = createScriptCallStack(message->GetStackTrace(), messag
e->GetStackTrace()->GetFrameCount(), m_isolate); | |
627 } | |
628 } else { | |
629 *result = ScriptValue(scriptState, value); | |
630 } | |
631 } | |
632 | |
633 PassOwnPtr<ScriptSourceCode> ScriptDebugServer::preprocess(LocalFrame*, const Sc
riptSourceCode&) | |
634 { | |
635 return PassOwnPtr<ScriptSourceCode>(); | |
636 } | |
637 | |
638 String ScriptDebugServer::preprocessEventListener(LocalFrame*, const String& sou
rce, const String& url, const String& functionName) | |
639 { | |
640 return source; | |
641 } | |
642 | |
643 } // namespace WebCore | |
OLD | NEW |