From 289a414fb0005dbb3f6ef4b00ae449c012c7a586 Mon Sep 17 00:00:00 2001 From: kohsuke Date: Mon, 25 Aug 2008 18:14:28 +0000 Subject: [PATCH] Added JavaScript debug assistance to figure out the problems inside JavaScript git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@11722 71c3de6d-444a-0410-be80-ed276b4c234a --- .../org/jvnet/hudson/test/HudsonTestCase.java | 28 ++++++ .../hudson/test/rhino/CallStackFrame.java | 99 +++++++++++++++++++ .../hudson/test/rhino/JavaScriptDebugger.java | 50 ++++++++++ 3 files changed, 177 insertions(+) create mode 100644 test/src/main/java/org/jvnet/hudson/test/rhino/CallStackFrame.java create mode 100644 test/src/main/java/org/jvnet/hudson/test/rhino/JavaScriptDebugger.java diff --git a/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java b/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java index b33b621ada..733e98a536 100644 --- a/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java +++ b/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java @@ -6,6 +6,7 @@ import com.gargoylesoftware.htmlunit.Page; import com.gargoylesoftware.htmlunit.WebRequestSettings; import com.gargoylesoftware.htmlunit.html.HtmlForm; import com.gargoylesoftware.htmlunit.html.HtmlPage; +import com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine; import com.gargoylesoftware.htmlunit.javascript.host.Stylesheet; import hudson.model.Descriptor; import hudson.model.FreeStyleProject; @@ -16,6 +17,7 @@ import junit.framework.TestCase; import org.jvnet.hudson.test.HudsonHomeLoader.CopyExisting; import org.jvnet.hudson.test.recipes.Recipe; import org.jvnet.hudson.test.recipes.Recipe.Runner; +import org.jvnet.hudson.test.rhino.JavaScriptDebugger; import org.mortbay.jetty.Server; import org.mortbay.jetty.bio.SocketConnector; import org.mortbay.jetty.security.HashUserRealm; @@ -23,6 +25,8 @@ import org.mortbay.jetty.security.UserRealm; import org.mortbay.jetty.webapp.Configuration; import org.mortbay.jetty.webapp.WebAppContext; import org.mortbay.jetty.webapp.WebXmlConfiguration; +import org.mozilla.javascript.Context; +import org.mozilla.javascript.ContextFactory; import org.w3c.css.sac.CSSException; import org.w3c.css.sac.CSSParseException; import org.w3c.css.sac.ErrorHandler; @@ -72,6 +76,17 @@ public abstract class HudsonTestCase extends TestCase { */ private List> clients = new ArrayList>(); + /** + * JavaScript "debugger" that provides you information about the JavaScript call stack + * and the current values of the local variables in those stack frame. + * + *

+ * Unlike Java debugger, which you as a human interfaces directly and interactively, + * this JavaScript debugger is to be interfaced by your program (or through the + * expression evaluation capability of your Java debugger.) + */ + protected JavaScriptDebugger jsDebugger = new JavaScriptDebugger(); + protected HudsonTestCase(String name) { super(name); } @@ -108,6 +123,19 @@ public abstract class HudsonTestCase extends TestCase { env.dispose(); } + protected void runTest() throws Throwable { + new JavaScriptEngine(null); // ensure that ContextFactory is initialized + Context cx= ContextFactory.getGlobal().enterContext(); + try { + cx.setOptimizationLevel(-1); + cx.setDebugger(jsDebugger,null); + + super.runTest(); + } finally { + Context.exit(); + } + } + /** * Creates a new instance of {@link Hudson}. If the derived class wants to create it in a different way, * you can override it. diff --git a/test/src/main/java/org/jvnet/hudson/test/rhino/CallStackFrame.java b/test/src/main/java/org/jvnet/hudson/test/rhino/CallStackFrame.java new file mode 100644 index 0000000000..56f6d2a11d --- /dev/null +++ b/test/src/main/java/org/jvnet/hudson/test/rhino/CallStackFrame.java @@ -0,0 +1,99 @@ +package org.jvnet.hudson.test.rhino; + +import org.mozilla.javascript.Context; +import org.mozilla.javascript.Scriptable; +import org.mozilla.javascript.debug.DebugFrame; +import org.mozilla.javascript.debug.DebuggableScript; + +import java.util.SortedMap; +import java.util.TreeMap; + +/** + * Stack frame. + * + * @author Kohsuke Kawaguchi + */ +public class CallStackFrame implements DebugFrame { + /** + * {@link JavaScriptDebugger} that this stack frame lives in. + */ + public final JavaScriptDebugger owner; + /** + * The function being executed. + */ + public final DebuggableScript fnOrScript; + + private Scriptable activation; + private Scriptable thisObj; + private Object[] args; + private int line; + + public CallStackFrame(JavaScriptDebugger owner, DebuggableScript fnOrScript) { + this.owner = owner; + this.fnOrScript = fnOrScript; + } + + public void onEnter(Context cx, Scriptable activation, Scriptable thisObj, Object[] args) { + this.activation = activation; + this.thisObj = thisObj; + this.args = args; + owner.callStack.add(this); + } + + public void onExit(Context cx, boolean byThrow, Object resultOrException) { + // can't simply call removeFirst, because due to tail call elimination, + // intermediate frames can be dropped at any time + // TODO: shouldn't it be suffice to just check the end? + for( int i=owner.callStack.size()-1; i>=0; i-- ) + if(owner.callStack.get(i)==this) { + owner.callStack.remove(i); + break; + } + + activation = null; + thisObj = null; + args = null; + line = -1; + } + + public void onLineChange(Context cx, int lineNumber) { + this.line = lineNumber; + } + + public void onExceptionThrown(Context cx, Throwable ex) { + } + + public void onDebuggerStatement(Context cx) { + } + + /** + * In-scope variables. + */ + public SortedMap getVariables() { + SortedMap r = new TreeMap(); + for( int i=fnOrScript.getParamAndVarCount()-1; i>=0; i-- ) { + String name =fnOrScript.getParamOrVarName(i); + r.put(name,activation.get(name,activation)); + } + return r; + } + + /** + * Formats this call stack, arguments, and its local variables as a human readable string. + */ + public String toString() { + StringBuilder buf = new StringBuilder(); + buf.append(fnOrScript.getFunctionName()); + buf.append('('); + for( int i=0; i