/hudson-test-framework/src/main/java/org/jvnet/hudson/test/rhino/JavaScriptDebugger.java

http://github.com/hudson/hudson · Java · 75 lines · 22 code · 6 blank · 47 comment · 1 complexity · a7c5d2baaa3d4bb2b6e609a40d64088e MD5 · raw file

  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package org.jvnet.hudson.test.rhino;
  25. import net.sourceforge.htmlunit.corejs.javascript.Context;
  26. import net.sourceforge.htmlunit.corejs.javascript.debug.DebugFrame;
  27. import net.sourceforge.htmlunit.corejs.javascript.debug.DebuggableScript;
  28. import net.sourceforge.htmlunit.corejs.javascript.debug.Debugger;
  29. import org.jvnet.hudson.test.HudsonTestCase;
  30. import java.util.List;
  31. import java.util.ArrayList;
  32. /**
  33. * Monitors the execution of the JavaScript inside HtmlUnit, and provides debug information
  34. * such as call stacks, variables, arguments, etc.
  35. *
  36. * <h2>Usage</h2>
  37. * <p>
  38. * When you set a break point in Java code that are directly/indirectly invoked through JavaScript,
  39. * use {@link #toString()} to see the JavaScript stack trace (and variables at each stack frame.)
  40. * This helps you see where the problem is.
  41. *
  42. * <p>
  43. * TODO: add programmatic break point API, selective method invocation tracing, and
  44. * allow arbitrary script evaluation in arbitrary stack frame.
  45. *
  46. * @author Kohsuke Kawaguchi
  47. * @see HudsonTestCase#jsDebugger
  48. */
  49. public class JavaScriptDebugger implements Debugger {
  50. /**
  51. * Call stack as a list. The list grows at the end, so the first element in the list
  52. * is the oldest stack frame.
  53. */
  54. public final List<CallStackFrame> callStack = new ArrayList<CallStackFrame>();
  55. public void handleCompilationDone(Context cx, DebuggableScript fnOrScript, String source) {
  56. }
  57. public DebugFrame getFrame(Context cx, DebuggableScript fnOrScript) {
  58. return new CallStackFrame(this,fnOrScript);
  59. }
  60. /**
  61. * Formats the current call stack into a human readable string.
  62. */
  63. public String toString() {
  64. StringBuilder buf = new StringBuilder();
  65. for( int i=callStack.size()-1; i>=0; i-- )
  66. buf.append(callStack.get(i)).append('\n');
  67. return buf.toString();
  68. }
  69. }