OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2012 Google Inc. All rights reserved. | 2 * Copyright (C) 2012 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 void DartDebugServer::disable() | 192 void DartDebugServer::disable() |
193 { | 193 { |
194 V8Scope v8Scope(v8::Debug::GetDebugContext()); | 194 V8Scope v8Scope(v8::Debug::GetDebugContext()); |
195 // Only invoke the disable method when Dart code was executed on the page. | 195 // Only invoke the disable method when Dart code was executed on the page. |
196 if (m_dartDebugObject.isEmpty()) | 196 if (m_dartDebugObject.isEmpty()) |
197 return; | 197 return; |
198 v8::Local<v8::Function> disable = v8::Local<v8::Function>::Cast(dartDebugObj
ect()->Get(v8::String::New("disable"))); | 198 v8::Local<v8::Function> disable = v8::Local<v8::Function>::Cast(dartDebugObj
ect()->Get(v8::String::New("disable"))); |
199 disable->Call(dartDebugObject(), 0, 0); | 199 disable->Call(dartDebugObject(), 0, 0); |
200 } | 200 } |
201 | 201 |
| 202 bool lookupTokenLineNumber(const Dart_CodeLocation& location, int* lineNumber, i
nt* columnNumber) |
| 203 { |
| 204 Dart_Handle info = Dart_ScriptGetTokenInfo(location.library_id, location.scr
ipt_url); |
| 205 ASSERT(Dart_IsList(info)); |
| 206 intptr_t infoLength = 0; |
| 207 Dart_Handle res = Dart_ListLength(info, &infoLength); |
| 208 ASSERT(!Dart_IsError(res)); |
| 209 Dart_Handle elem; |
| 210 bool lineStart = true; |
| 211 int currentLineNumber = 0; |
| 212 for (intptr_t i = 0; i < infoLength; i++) { |
| 213 elem = Dart_ListGetAt(info, i); |
| 214 if (Dart_IsNull(elem)) { |
| 215 lineStart = true; |
| 216 } else { |
| 217 ASSERT(Dart_IsInteger(elem)); |
| 218 Dart_Handle exception = 0; |
| 219 int64_t value = DartUtilities::toInteger(elem, exception); |
| 220 ASSERT(!exception); |
| 221 if (lineStart) { |
| 222 // Line number. |
| 223 currentLineNumber = value; |
| 224 lineStart = false; |
| 225 } else { |
| 226 // Token offset. |
| 227 if (value == location.token_pos) { |
| 228 *lineNumber = currentLineNumber; |
| 229 ASSERT(i + 1 < infoLength); |
| 230 *columnNumber = DartUtilities::toInteger(Dart_ListGetAt(info
, i + 1), exception); |
| 231 ASSERT(!exception); |
| 232 return true; |
| 233 } |
| 234 i++; // skip columnNumber. |
| 235 } |
| 236 } |
| 237 } |
| 238 return false; |
| 239 } |
| 240 |
202 v8::Handle<v8::Object> DartDebugServer::createExecutionState(Dart_StackTrace tra
ce) | 241 v8::Handle<v8::Object> DartDebugServer::createExecutionState(Dart_StackTrace tra
ce) |
203 { | 242 { |
204 intptr_t length = 0; | 243 intptr_t length = 0; |
205 Dart_Handle result = Dart_StackTraceLength(trace, &length); | 244 Dart_Handle result = Dart_StackTraceLength(trace, &length); |
206 ASSERT(!Dart_IsError(result)); | 245 ASSERT(!Dart_IsError(result)); |
207 UNUSED_PARAM(result); | 246 UNUSED_PARAM(result); |
208 ASSERT(length); | 247 ASSERT(length); |
209 ASSERT(Dart_CurrentIsolate()); | 248 ASSERT(Dart_CurrentIsolate()); |
210 int isolateHandle = isolateMap().getByValue(Dart_CurrentIsolate()); | 249 int isolateHandle = isolateMap().getByValue(Dart_CurrentIsolate()); |
211 | 250 |
212 v8::Local<v8::Array> callFrames = v8::Array::New(length); | 251 v8::Local<v8::Array> callFrames = v8::Array::New(length); |
213 for (int i = length - 1; i >=0; --i) { | 252 for (int i = length - 1; i >= 0; --i) { |
214 Dart_ActivationFrame frame = 0; | 253 Dart_ActivationFrame frame = 0; |
215 result = Dart_GetActivationFrame(trace, i, &frame); | 254 result = Dart_GetActivationFrame(trace, i, &frame); |
216 ASSERT(!Dart_IsError(result)); | 255 ASSERT(!Dart_IsError(result)); |
217 Dart_Handle functionName = 0; | 256 Dart_Handle functionName = 0; |
218 Dart_Handle scriptURL = 0; | 257 Dart_Handle function = 0; |
219 intptr_t lineNumber = 0; | |
220 intptr_t libraryId = 0; | |
221 | 258 |
222 result = Dart_ActivationFrameInfo(frame, &functionName, &scriptURL, &lin
eNumber, &libraryId); | 259 Dart_CodeLocation location; |
| 260 result = Dart_ActivationFrameGetLocation(frame, &functionName, &function
, &location); |
| 261 int lineNumber = 0; |
| 262 int columnNumber = 0; |
| 263 lookupTokenLineNumber(location, &lineNumber, &columnNumber); |
223 ASSERT(!Dart_IsError(result)); | 264 ASSERT(!Dart_IsError(result)); |
224 Dart_Handle libraryURL = Dart_GetLibraryURL(libraryId); | 265 Dart_Handle libraryURL = Dart_GetLibraryURL(location.library_id); |
225 ASSERT(!Dart_IsError(libraryURL)); | 266 ASSERT(!Dart_IsError(libraryURL)); |
226 Dart_Handle library = Dart_LookupLibrary(libraryURL); | 267 Dart_Handle library = Dart_LookupLibrary(libraryURL); |
| 268 Dart_Handle localVariablesHandle = Dart_GetLocalVariables(frame); |
227 | 269 |
228 v8::Local<v8::Object> callFrame = v8::Object::New(); | 270 v8::Local<v8::Object> callFrame = v8::Object::New(); |
229 callFrame->Set(v8::String::New("functionName"), V8Converter::stringToV8(
functionName)); | 271 callFrame->Set(v8::String::New("functionName"), V8Converter::stringToV8(
functionName)); |
230 callFrame->Set(v8::String::New("scriptURL"), V8Converter::stringToV8(scr
iptURL)); | 272 callFrame->Set(v8::String::New("functionProxy"), DartHandleProxy::create
(function)); |
| 273 callFrame->Set(v8::String::New("scriptURL"), V8Converter::stringToV8(loc
ation.script_url)); |
231 callFrame->Set(v8::String::New("lineNumber"), v8::Number::New(lineNumber
- 1)); | 274 callFrame->Set(v8::String::New("lineNumber"), v8::Number::New(lineNumber
- 1)); |
232 callFrame->Set(v8::String::New("libraryProxy"), DartHandleProxy::createL
ibraryProxy(library, libraryId, Dart_Null())); | 275 callFrame->Set(v8::String::New("columnNumber"), v8::Number::New(columnNu
mber - 1)); |
| 276 callFrame->Set(v8::String::New("libraryProxy"), DartHandleProxy::createL
ibraryProxy(library, location.library_id, Dart_Null())); |
233 callFrame->Set(v8::String::New("localScopeProxy"), DartHandleProxy::crea
teLocalScopeProxy( | 277 callFrame->Set(v8::String::New("localScopeProxy"), DartHandleProxy::crea
teLocalScopeProxy( |
234 Dart_GetLocalVariables(frame))); | 278 localVariablesHandle)); |
| 279 callFrame->Set(v8::String::New("localVariables"), DartHandleProxy::creat
e(localVariablesHandle)); |
235 callFrame->Set(v8::String::New("isolateHandle"), v8::Number::New(isolate
Handle)); | 280 callFrame->Set(v8::String::New("isolateHandle"), v8::Number::New(isolate
Handle)); |
236 callFrames->Set(i, callFrame); | 281 callFrames->Set(i, callFrame); |
237 } | 282 } |
238 | 283 |
239 ASSERT(!m_dartDebugObject.isEmpty()); | 284 ASSERT(!m_dartDebugObject.isEmpty()); |
240 v8::Handle<v8::Function> executionStateConstructor = v8::Local<v8::Function>
::Cast(dartDebugObject()->Get(v8::String::New("ExecutionState"))); | 285 v8::Handle<v8::Function> executionStateConstructor = v8::Local<v8::Function>
::Cast(dartDebugObject()->Get(v8::String::New("ExecutionState"))); |
241 v8::Handle<v8::Value> args[] = { callFrames }; | 286 v8::Handle<v8::Value> args[] = { callFrames }; |
242 return v8::Local<v8::Object>::Cast(executionStateConstructor->CallAsConstruc
tor(1, args)); | 287 return v8::Local<v8::Object>::Cast(executionStateConstructor->CallAsConstruc
tor(1, args)); |
243 } | 288 } |
244 | 289 |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
399 } | 444 } |
400 | 445 |
401 static void stepOut(const v8::FunctionCallbackInfo<v8::Value>& args) | 446 static void stepOut(const v8::FunctionCallbackInfo<v8::Value>& args) |
402 { | 447 { |
403 Dart_SetStepOut(); | 448 Dart_SetStepOut(); |
404 } | 449 } |
405 | 450 |
406 static void evaluateInScope(const v8::FunctionCallbackInfo<v8::Value>& args) | 451 static void evaluateInScope(const v8::FunctionCallbackInfo<v8::Value>& args) |
407 { | 452 { |
408 v8::Handle<v8::String> expression = args[0]->ToString(); | 453 v8::Handle<v8::String> expression = args[0]->ToString(); |
409 v8::Handle<v8::Object> receiver = args[1].As<v8::Object>(); | 454 v8::Handle<v8::Value> receiver = args[1]; |
410 v8::Handle<v8::Value> scopeObjectGlobal = args[2]; | 455 v8::Handle<v8::Object> functionProxy = args[2].As<v8::Object>(); |
411 v8::Handle<v8::Value> scopeObjectLocal = args[3]; | 456 v8::Handle<v8::Value> localVariablesProxy = args[3]; |
412 v8::Handle<v8::Value> scopes[] = { scopeObjectGlobal, scopeObjectLocal }; | |
413 | 457 |
| 458 DartScopes scopes(functionProxy); |
| 459 Dart_Handle target = 0; |
414 if (receiver->IsNull() || receiver->IsUndefined()) { | 460 if (receiver->IsNull() || receiver->IsUndefined()) { |
415 // There is no receiver which indicates we are in a static method | 461 Dart_Handle functionHandle = scopes.handle; |
416 // scope in Dart. We should ignore the receiver for this case. | 462 ASSERT(Dart_IsFunction(functionHandle)); |
417 // Using null or undefined as the receiver for a function triggers | 463 target = Dart_FunctionOwner(functionHandle); |
418 // asserts in V8 while running in Debug mode if an exception occurs. | |
419 receiver = v8::Object::New(); | |
420 expression = v8::String::Concat(v8::String::New( | |
421 "(function($$scopeObjectGlobal, $$scopeObjectLocal) { " | |
422 "with ($$scopeObjectGlobal) with($$scopeObjectLocal) { " | |
423 "return "), | |
424 expression); | |
425 expression = v8::String::Concat(expression, v8::String::New("} })")); | |
426 } else { | 464 } else { |
427 expression = v8::String::Concat(v8::String::New( | 465 target = DartHandleProxy::unwrapValue(receiver); |
428 "(function($$scopeObjectGlobal, $$scopeObjectLocal) { " | |
429 "with ($$scopeObjectGlobal) with (this) with($$scopeObjectLocal) { " | |
430 "return "), | |
431 expression); | |
432 expression = v8::String::Concat(expression, v8::String::New("} })")); | |
433 } | 466 } |
| 467 ASSERT(!Dart_IsError(target)); |
| 468 Dart_Handle localVariables = DartHandleProxy::unwrapValue(localVariablesProx
y); |
| 469 ASSERT(Dart_IsList(localVariables)); |
| 470 intptr_t localVariablesLength = 0; |
| 471 Dart_ListLength(localVariables, &localVariablesLength); |
434 | 472 |
435 v8::TryCatch tryCatch; | 473 Dart_Handle wrapExpressionArgs[2] = { V8Converter::stringToDart(expression),
localVariables }; |
436 v8::Handle<v8::Function> function = V8ScriptRunner::compileAndRunInternalScr
ipt(expression, args.GetIsolate()).As<v8::Function>(); | |
437 | 474 |
438 if (tryCatch.HasCaught()) { | 475 Dart_Handle wrappedExpressionTuple = |
439 v8SetReturnValue(args, tryCatch.ReThrow()); | 476 DartUtilities::invokeUtilsMethod("wrapExpressionAsClosure", 2, wrapExpre
ssionArgs); |
440 return; | 477 ASSERT(Dart_IsList(wrappedExpressionTuple)); |
| 478 Dart_Handle wrappedExpression = Dart_ListGetAt(wrappedExpressionTuple, 0); |
| 479 Dart_Handle wrappedExpressionArgs = Dart_ListGetAt(wrappedExpressionTuple, 1
); |
| 480 |
| 481 ASSERT(Dart_IsString(wrappedExpression)); |
| 482 Dart_Handle closure = Dart_EvaluateExpr(target, wrappedExpression); |
| 483 if (Dart_IsError(closure)) { |
| 484 // There was a parse error. FIXME: consider cleaning up the line |
| 485 // numbers in the error message. |
| 486 V8ThrowException::throwError(v8::String::New(Dart_GetError(closure))); |
| 487 } else { |
| 488 // Invoke the closure passing in the expression arguments specified by |
| 489 // wrappedExpressionTuple. |
| 490 ASSERT(Dart_IsClosure(closure)); |
| 491 intptr_t length = 0; |
| 492 Dart_ListLength(wrappedExpressionArgs, &length); |
| 493 Vector<Dart_Handle> dartFunctionArgs; |
| 494 for (uint32_t i = 0; i < length; i ++) { |
| 495 dartFunctionArgs.append(Dart_ListGetAt(wrappedExpressionArgs, i)); |
| 496 } |
| 497 |
| 498 Dart_Handle result = Dart_InvokeClosure(closure, dartFunctionArgs.size()
, dartFunctionArgs.data()); |
| 499 if (Dart_IsError(result)) { |
| 500 V8ThrowException::throwError(v8::String::New(Dart_GetError(result)))
; |
| 501 } else { |
| 502 v8SetReturnValue(args, DartHandleProxy::create(result)); |
| 503 } |
441 } | 504 } |
442 | |
443 v8::Local<v8::Value> result = V8ScriptRunner::callFunction( | |
444 function, getScriptExecutionContext(), | |
445 receiver, | |
446 2, scopes); | |
447 | |
448 if (tryCatch.HasCaught()) { | |
449 v8SetReturnValue(args, tryCatch.ReThrow()); | |
450 return; | |
451 } | |
452 crashIfV8IsDead(); | |
453 v8SetReturnValue(args, result); | |
454 } | 505 } |
455 | 506 |
456 void DartDebugServer::ensureHooksInstalled() | 507 void DartDebugServer::ensureHooksInstalled() |
457 { | 508 { |
458 DEFINE_STATIC_LOCAL(bool, hooksInstalled, (false)); | 509 DEFINE_STATIC_LOCAL(bool, hooksInstalled, (false)); |
459 | 510 |
460 if (hooksInstalled) | 511 if (hooksInstalled) |
461 return; | 512 return; |
462 | 513 |
463 hooksInstalled = true; | 514 hooksInstalled = true; |
(...skipping 16 matching lines...) Expand all Loading... |
480 nativeCallbacks->Set(v8::String::New("handleDebugEvent"), v8::FunctionTempla
te::New(&handleDebugEvent)->GetFunction()); | 531 nativeCallbacks->Set(v8::String::New("handleDebugEvent"), v8::FunctionTempla
te::New(&handleDebugEvent)->GetFunction()); |
481 nativeCallbacks->Set(v8::String::New("scriptsForIsolate"), v8::FunctionTempl
ate::New(&scriptsForIsolate)->GetFunction()); | 532 nativeCallbacks->Set(v8::String::New("scriptsForIsolate"), v8::FunctionTempl
ate::New(&scriptsForIsolate)->GetFunction()); |
482 nativeCallbacks->Set(v8::String::New("setBreakpoint"), v8::FunctionTemplate:
:New(&setBreakpoint)->GetFunction()); | 533 nativeCallbacks->Set(v8::String::New("setBreakpoint"), v8::FunctionTemplate:
:New(&setBreakpoint)->GetFunction()); |
483 nativeCallbacks->Set(v8::String::New("removeBreakpoint"), v8::FunctionTempla
te::New(&removeBreakpoint)->GetFunction()); | 534 nativeCallbacks->Set(v8::String::New("removeBreakpoint"), v8::FunctionTempla
te::New(&removeBreakpoint)->GetFunction()); |
484 nativeCallbacks->Set(v8::String::New("getBreakpointLine"), v8::FunctionTempl
ate::New(&getBreakpointLine)->GetFunction()); | 535 nativeCallbacks->Set(v8::String::New("getBreakpointLine"), v8::FunctionTempl
ate::New(&getBreakpointLine)->GetFunction()); |
485 nativeCallbacks->Set(v8::String::New("setExceptionPauseInfo"), v8::FunctionT
emplate::New(&setExceptionPauseInfo)->GetFunction()); | 536 nativeCallbacks->Set(v8::String::New("setExceptionPauseInfo"), v8::FunctionT
emplate::New(&setExceptionPauseInfo)->GetFunction()); |
486 nativeCallbacks->Set(v8::String::New("stepInto"), v8::FunctionTemplate::New(
&stepInto)->GetFunction()); | 537 nativeCallbacks->Set(v8::String::New("stepInto"), v8::FunctionTemplate::New(
&stepInto)->GetFunction()); |
487 nativeCallbacks->Set(v8::String::New("stepOver"), v8::FunctionTemplate::New(
&stepOver)->GetFunction()); | 538 nativeCallbacks->Set(v8::String::New("stepOver"), v8::FunctionTemplate::New(
&stepOver)->GetFunction()); |
488 nativeCallbacks->Set(v8::String::New("stepOut"), v8::FunctionTemplate::New(&
stepOut)->GetFunction()); | 539 nativeCallbacks->Set(v8::String::New("stepOut"), v8::FunctionTemplate::New(&
stepOut)->GetFunction()); |
489 nativeCallbacks->Set(v8::String::New("evaluateInScope"), evaluateInScopeFunc
tion); | 540 nativeCallbacks->Set(v8::String::New("evaluateInScope"), evaluateInScopeFunc
tion); |
| 541 { |
| 542 // Trampoline script is required to properly set calling context before |
| 543 // invoking Dart code because of security checks in console.log |
| 544 // implementation (see InjectedScriptManager::canAccessInspectedWindow). |
| 545 V8Scope v8scope; |
| 546 v8::Handle<v8::String> trampolineScript = v8::String::New("(function (fu
nc, args) { return func.apply(this, args); })"); |
| 547 v8::Local<v8::Function> trampoline = v8::Local<v8::Function>::Cast(v8::S
cript::Compile(trampolineScript)->Run()); |
| 548 nativeCallbacks->Set(v8::String::New("invocationTrampoline"), trampoline
); |
| 549 } |
490 dartDebugObject()->Set(v8::String::New("nativeCallbacks"), nativeCallbacks); | 550 dartDebugObject()->Set(v8::String::New("nativeCallbacks"), nativeCallbacks); |
491 } | 551 } |
492 | 552 |
493 v8::Local<v8::Object> DartDebugServer::dartDebugObject() | 553 v8::Local<v8::Object> DartDebugServer::dartDebugObject() |
494 { | 554 { |
495 return m_dartDebugObject.newLocal(v8::Isolate::GetCurrent()); | 555 return m_dartDebugObject.newLocal(v8::Isolate::GetCurrent()); |
496 } | 556 } |
497 | 557 |
498 } | 558 } |
OLD | NEW |