From 4cc4742d6c24fcd93dd9eaf0cdd7cbfef99f84c8 Mon Sep 17 00:00:00 2001 From: hns Date: Fri, 11 Jul 2003 12:55:52 +0000 Subject: [PATCH] Implement basic tracer support. App properties to activate: rhino.optlevel = -1 rhino.trace = true --- src/helma/scripting/rhino/RhinoEngine.java | 7 ++ src/helma/scripting/rhino/debug/Tracer.java | 112 ++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 src/helma/scripting/rhino/debug/Tracer.java diff --git a/src/helma/scripting/rhino/RhinoEngine.java b/src/helma/scripting/rhino/RhinoEngine.java index 16f74d93..4b1cf46f 100644 --- a/src/helma/scripting/rhino/RhinoEngine.java +++ b/src/helma/scripting/rhino/RhinoEngine.java @@ -26,6 +26,7 @@ import helma.objectmodel.*; import helma.objectmodel.db.DbMapping; import helma.objectmodel.db.Relation; import helma.scripting.*; +import helma.scripting.rhino.debug.Tracer; import helma.util.CacheMap; import helma.util.Updatable; import org.mozilla.javascript.*; @@ -146,6 +147,12 @@ public class RhinoEngine implements ScriptingEngine { context.setCompileFunctionsWithDynamicScope(true); context.setWrapFactory(core.wrapper); + boolean trace = "true".equals(app.getProperty("rhino.trace")); + + if (trace) { + context.setDebugger(new Tracer(getResponse()), null); + } + int optLevel = 0; try { diff --git a/src/helma/scripting/rhino/debug/Tracer.java b/src/helma/scripting/rhino/debug/Tracer.java new file mode 100644 index 00000000..00afface --- /dev/null +++ b/src/helma/scripting/rhino/debug/Tracer.java @@ -0,0 +1,112 @@ +/* + * Helma License Notice + * + * The contents of this file are subject to the Helma License + * Version 2.0 (the "License"). You may not use this file except in + * compliance with the License. A copy of the License is available at + * http://adele.helma.org/download/helma/license.txt + * + * Copyright 1998-2003 Helma Software. All Rights Reserved. + * + * $RCSfile$ + * $Author$ + * $Revision$ + * $Date$ + */ + +package helma.scripting.rhino.debug; + + +import org.mozilla.javascript.*; +import org.mozilla.javascript.debug.*; +import helma.framework.ResponseTrans; + +public class Tracer implements Debugger { + + ResponseTrans res; + + /** + * Create a tracer that writes to this response object + */ + public Tracer(ResponseTrans res) { + this.res = res; + } + + + /** + * Implementws handleCompilationDone in interface org.mozilla.javascript.debug.Debugger + */ + public void handleCompilationDone(Context cx, DebuggableScript script, String source) { + // res.debug("CompilationDone: "+toString(script)); + } + + /** + * Implementws getFrame in interface org.mozilla.javascript.debug.Debugger + */ + public DebugFrame getFrame(Context cx, DebuggableScript script) { + // res.debug("getFrame: "+toString(script)); + if (script.isFunction()) { + return new TracerFrame(script); + } + return null; + } + + static String toString(DebuggableScript script) { + if (script.isFunction()) { + return script.getSourceName()+": "+script.getFunctionName(); + } else { + return script.getSourceName(); + } + } + +class TracerFrame implements DebugFrame { + + DebuggableScript script; + + TracerFrame(DebuggableScript script) { + this.script = script; + } + + /** + * Called when execution is ready to start bytecode interpretation + * for entered a particular function or script. + */ + public void onEnter(Context cx, Scriptable activation, + Scriptable thisObj, Object[] args) { + StringBuffer b = new StringBuffer("Trace: "); + b.append(Tracer.toString(script)); + b.append("("); + for (int i=0; i