From eaaeceda066bb3ba1d2e3484a3f35de491846787 Mon Sep 17 00:00:00 2001 From: "igor%mir2.org" Date: Fri, 24 Sep 2004 20:35:42 +0000 Subject: [PATCH] Allow to specify explicit ContextFactory with the debugger. --- .../javascript/tools/debugger/Dim.java | 5 +- .../javascript/tools/debugger/Main.java | 85 ++++++++++++------- 2 files changed, 54 insertions(+), 36 deletions(-) diff --git a/js/rhino/toolsrc/org/mozilla/javascript/tools/debugger/Dim.java b/js/rhino/toolsrc/org/mozilla/javascript/tools/debugger/Dim.java index 394469d55a5..f410cbef380 100644 --- a/js/rhino/toolsrc/org/mozilla/javascript/tools/debugger/Dim.java +++ b/js/rhino/toolsrc/org/mozilla/javascript/tools/debugger/Dim.java @@ -535,10 +535,9 @@ class Dim { } } - void enableForAllNewContexts() + void attachTo(ContextFactory factory) { - DimIProxy listener = new DimIProxy(this, IPROXY_LISTEN); - ContextFactory.getGlobal().addListener(listener); + factory.addListener(new DimIProxy(this, IPROXY_LISTEN)); } FunctionSource getFunctionSource(DebuggableScript fnOrScript) diff --git a/js/rhino/toolsrc/org/mozilla/javascript/tools/debugger/Main.java b/js/rhino/toolsrc/org/mozilla/javascript/tools/debugger/Main.java index 5dd2b1a1460..a745f6a27d7 100644 --- a/js/rhino/toolsrc/org/mozilla/javascript/tools/debugger/Main.java +++ b/js/rhino/toolsrc/org/mozilla/javascript/tools/debugger/Main.java @@ -43,23 +43,46 @@ import org.mozilla.javascript.*; import org.mozilla.javascript.debug.*; import java.io.*; -public class Main { - - +public class Main +{ Dim dim; DebugGui debugGui; + /** + * Class to consolidate all internal implementations of interfaces + * to avoid class generation bloat. + */ + private static class IProxy implements Runnable, ScopeProvider + { + static final int EXIT_ACTION = 1; + static final int SCOPE_PROVIDER = 2; + + private final int type; + + IProxy(int type) + { + this.type = type; + } + + public void run() + { + if (type != EXIT_ACTION) Kit.codeBug(); + System.exit(0); + } + + public Scriptable getScope() + { + if (type != SCOPE_PROVIDER) Kit.codeBug(); + return org.mozilla.javascript.tools.shell.Main.getScope(); + } + } + // // public interface // public Main(String title) { - if (!ContextFactory.hasExplicitGlobal()) { - ContextFactory.initGlobal( - org.mozilla.javascript.tools.shell.Main.shellContextFactory); - } - dim = new Dim(); debugGui = new DebugGui(dim, title); dim.callback = debugGui; @@ -163,9 +186,9 @@ public class Main { return debugGui.isVisible(); } - public void enableForAllNewContexts() + public void attachTo(ContextFactory factory) { - dim.enableForAllNewContexts(); + dim.attachTo(factory); } public static void main(String[] args) @@ -173,20 +196,15 @@ public class Main { { Main main = new Main("Rhino JavaScript Debugger"); main.doBreak(); - main.setExitAction(new Runnable() { - public void run() { - System.exit(0); - } - }); + main.setExitAction(new IProxy(IProxy.EXIT_ACTION)); System.setIn(main.getIn()); System.setOut(main.getOut()); System.setErr(main.getErr()); - main.enableForAllNewContexts(); - main.setScopeProvider(new ScopeProvider() { - public Scriptable getScope() { - return org.mozilla.javascript.tools.shell.Main.getScope(); - } - }); + + main.attachTo( + org.mozilla.javascript.tools.shell.Main.shellContextFactory); + + main.setScopeProvider(new IProxy(IProxy.SCOPE_PROVIDER)); main.pack(); main.setSize(600, 460); @@ -195,26 +213,27 @@ public class Main { org.mozilla.javascript.tools.shell.Main.exec(args); } - // same as plain main(), stdin/out/err redirection removed public static void mainEmbedded(String title) + { + IProxy scopeProvider = new IProxy(IProxy.SCOPE_PROVIDER); + mainEmbedded(ContextFactory.getGlobal(), scopeProvider, title); + } + + // same as plain main(), stdin/out/err redirection removed and + // explicit ContextFactory and ScopeProvider + public static void mainEmbedded(ContextFactory factory, + ScopeProvider scopeProvider, + String title) { if (title == null) { title = "Rhino JavaScript Debugger (embedded usage)"; } Main main = new Main(title); main.doBreak(); - main.setExitAction(new Runnable() { - public void run() { - System.exit(0); - } - }); + main.setExitAction(new IProxy(IProxy.EXIT_ACTION)); - main.enableForAllNewContexts(); - main.setScopeProvider(new ScopeProvider() { - public Scriptable getScope() { - return org.mozilla.javascript.tools.shell.Main.getScope(); - } - }); + main.attachTo(factory); + main.setScopeProvider(scopeProvider); main.pack(); main.setSize(600, 460);