changeset 22110:c2cb9f1c8688

Replacing the langClass.newInstance() hack in Debugger with proper way to obtain the language instance
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Wed, 26 Aug 2015 15:22:31 +0200
parents b5eaddcdf86a
children 0aad723479e8
files truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/InitializationTest.java truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/TruffleLanguage.java truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/debug/Debugger.java truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/debug/LineBreakpointFactory.java truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/debug/TagBreakpointFactory.java truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/Accessor.java
diffstat 6 files changed, 95 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/InitializationTest.java	Wed Aug 26 14:13:43 2015 +0200
+++ b/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/InitializationTest.java	Wed Aug 26 15:22:31 2015 +0200
@@ -37,6 +37,7 @@
 import com.oracle.truffle.api.instrument.*;
 import com.oracle.truffle.api.nodes.*;
 import com.oracle.truffle.api.source.Source;
+import com.oracle.truffle.api.source.SourceSection;
 import com.oracle.truffle.api.vm.EventConsumer;
 import com.oracle.truffle.api.vm.TruffleVM;
 import java.io.IOException;
@@ -47,6 +48,9 @@
  * It has been reported that calling {@link Env#importSymbol(java.lang.String)} in
  * {@link TruffleLanguage TruffleLanguage.createContext(env)} yields a {@link NullPointerException}.
  * <p>
+ * The other report was related to specifying an abstract language class in the RootNode and
+ * problems with debugging later on. That is what the other part of this test - once it obtains
+ * Debugger instance simulates.
  */
 public class InitializationTest {
     @Test
@@ -65,19 +69,28 @@
 
         assertNotNull("Debugger found", arr[0]);
 
-        Debugger d = arr[0];
-        Breakpoint b = d.setLineBreakpoint(0, source.createLineLocation(1), true);
-        b.setCondition("true");
+        try {
+            Debugger d = arr[0];
+            Breakpoint b = d.setLineBreakpoint(0, source.createLineLocation(1), true);
+            assertTrue(b.isEnabled());
+            b.setCondition("true");
 
-        vm.eval(source);
+            vm.eval(source);
+        } catch (InstrumentOKException ex) {
+            // OK
+            return;
+        }
+        fail("We should properly call up to TestLanguage.createAdvancedInstrumentRootFactory");
     }
 
     private static final class MMRootNode extends RootNode {
         @Child ANode node;
 
-        MMRootNode() {
-            super(AbstractLanguage.class, null, null);
+        MMRootNode(SourceSection ss) {
+            super(AbstractLanguage.class, ss, null);
             node = new ANode(42);
+            adoptChildren();
+            node.probe().tagAs(StandardSyntaxTag.STATEMENT, this);
         }
 
         @Override
@@ -86,13 +99,55 @@
         }
     }
 
-    private static final class ANode extends Node {
+    private static class ANode extends Node {
         private final int constant;
 
         public ANode(int constant) {
             this.constant = constant;
         }
 
+        @Override
+        public SourceSection getSourceSection() {
+            return getRootNode().getSourceSection();
+        }
+
+        @Override
+        public boolean isInstrumentable() {
+            return true;
+        }
+
+        @Override
+        public ProbeNode.WrapperNode createWrapperNode() {
+            class WN extends ANode implements ProbeNode.WrapperNode {
+                private ProbeNode probeNode;
+
+                public WN(int constant) {
+                    super(constant);
+                }
+
+                @Override
+                public Node getChild() {
+                    return ANode.this;
+                }
+
+                @Override
+                public Probe getProbe() {
+                    return probeNode.getProbe();
+                }
+
+                @Override
+                public void insertProbe(ProbeNode pn) {
+                    this.probeNode = pn;
+                }
+
+                @Override
+                public String instrumentationInfo() {
+                    throw new UnsupportedOperationException();
+                }
+            }
+            return new WN(constant);
+        }
+
         Object constant() {
             return constant;
         }
@@ -114,7 +169,7 @@
 
         @Override
         protected CallTarget parse(Source code, Node context, String... argumentNames) throws IOException {
-            return Truffle.getRuntime().createCallTarget(new MMRootNode());
+            return Truffle.getRuntime().createCallTarget(new MMRootNode(code.createSection("1st line", 1)));
         }
 
         @Override
@@ -149,7 +204,7 @@
 
         @Override
         public AdvancedInstrumentRootFactory createAdvancedInstrumentRootFactory(String expr, AdvancedInstrumentResultListener resultListener) throws DebugSupportException {
-            throw new UnsupportedOperationException();
+            throw new InstrumentOKException();
         }
 
         @Override
@@ -162,4 +217,8 @@
             throw new UnsupportedOperationException();
         }
     }
+
+    private static final class InstrumentOKException extends RuntimeException {
+        static final long serialVersionUID = 1L;
+    }
 }
--- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/TruffleLanguage.java	Wed Aug 26 14:13:43 2015 +0200
+++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/TruffleLanguage.java	Wed Aug 26 15:22:31 2015 +0200
@@ -335,6 +335,11 @@
         }
 
         @Override
+        protected TruffleLanguage<?> findLanguage(Env env) {
+            return env.lang;
+        }
+
+        @Override
         protected Object languageGlobal(TruffleLanguage.Env env) {
             return env.langCtx.getLanguageGlobal();
         }
--- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/debug/Debugger.java	Wed Aug 26 14:13:43 2015 +0200
+++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/debug/Debugger.java	Wed Aug 26 15:22:31 2015 +0200
@@ -117,8 +117,8 @@
             }
         };
 
-        this.lineBreaks = new LineBreakpointFactory(breakpointCallback, warningLog);
-        this.tagBreaks = new TagBreakpointFactory(breakpointCallback, warningLog);
+        this.lineBreaks = new LineBreakpointFactory(this, breakpointCallback, warningLog);
+        this.tagBreaks = new TagBreakpointFactory(this, breakpointCallback, warningLog);
     }
 
     TruffleVM vm() {
@@ -267,17 +267,11 @@
      *             formed.
      */
     @SuppressWarnings("rawtypes")
-    static AdvancedInstrumentRootFactory createAdvancedInstrumentRootFactory(Probe probe, String expr, AdvancedInstrumentResultListener resultListener) throws IOException {
+    AdvancedInstrumentRootFactory createAdvancedInstrumentRootFactory(Probe probe, String expr, AdvancedInstrumentResultListener resultListener) throws IOException {
         try {
             Class<? extends TruffleLanguage> langugageClass = ACCESSOR.findLanguage(probe);
-            TruffleLanguage<?> l;
-            try {
-                l = langugageClass.newInstance();
-            } catch (InstantiationException ex) {
-                throw new IllegalStateException(ex);
-            } catch (IllegalAccessException ex) {
-                throw new IllegalStateException(ex);
-            }
+            TruffleLanguage.Env env = ACCESSOR.findLanguage(vm, langugageClass);
+            TruffleLanguage<?> l = ACCESSOR.findLanguage(env);
             DebugSupportProvider dsp = ACCESSOR.getDebugSupport(l);
             return dsp.createAdvancedInstrumentRootFactory(expr, resultListener);
         } catch (DebugSupportException ex) {
@@ -827,6 +821,11 @@
         }
 
         @Override
+        protected TruffleLanguage<?> findLanguage(TruffleLanguage.Env env) {
+            return super.findLanguage(env);
+        }
+
+        @Override
         protected DebugSupportProvider getDebugSupport(TruffleLanguage<?> l) {
             return super.getDebugSupport(l);
         }
--- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/debug/LineBreakpointFactory.java	Wed Aug 26 14:13:43 2015 +0200
+++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/debug/LineBreakpointFactory.java	Wed Aug 26 15:22:31 2015 +0200
@@ -111,8 +111,10 @@
      */
     @CompilationFinal private boolean breakpointsActive = true;
     private final CyclicAssumption breakpointsActiveUnchanged = new CyclicAssumption(BREAKPOINT_NAME + " globally active");
+    private final Debugger debugger;
 
-    LineBreakpointFactory(BreakpointCallback breakpointCallback, final WarningLog warningLog) {
+    LineBreakpointFactory(Debugger debugger, BreakpointCallback breakpointCallback, final WarningLog warningLog) {
+        this.debugger = debugger;
         this.breakpointCallback = breakpointCallback;
         this.warningLog = warningLog;
 
@@ -363,7 +365,7 @@
             if (conditionExpr == null) {
                 newInstrument = Instrument.create(new UnconditionalLineBreakInstrumentListener(), BREAKPOINT_NAME);
             } else {
-                newInstrument = Instrument.create(this, Debugger.createAdvancedInstrumentRootFactory(newProbe, conditionExpr, this), Boolean.class, BREAKPOINT_NAME);
+                newInstrument = Instrument.create(this, debugger.createAdvancedInstrumentRootFactory(newProbe, conditionExpr, this), Boolean.class, BREAKPOINT_NAME);
             }
             newProbe.attach(newInstrument);
             instruments.add(newInstrument);
--- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/debug/TagBreakpointFactory.java	Wed Aug 26 14:13:43 2015 +0200
+++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/debug/TagBreakpointFactory.java	Wed Aug 26 15:22:31 2015 +0200
@@ -102,8 +102,10 @@
      */
     @CompilationFinal private boolean breakpointsActive = true;
     private final CyclicAssumption breakpointsActiveUnchanged = new CyclicAssumption(BREAKPOINT_NAME + " globally active");
+    private final Debugger debugger;
 
-    TagBreakpointFactory(BreakpointCallback breakpointCallback, final WarningLog warningLog) {
+    TagBreakpointFactory(Debugger debugger, BreakpointCallback breakpointCallback, final WarningLog warningLog) {
+        this.debugger = debugger;
         this.breakpointCallback = breakpointCallback;
         this.warningLog = warningLog;
 
@@ -327,7 +329,7 @@
             if (conditionExpr == null) {
                 newInstrument = Instrument.create(new UnconditionalTagBreakInstrumentListener(), BREAKPOINT_NAME);
             } else {
-                newInstrument = Instrument.create(this, Debugger.createAdvancedInstrumentRootFactory(newProbe, conditionExpr, this), Boolean.class, BREAKPOINT_NAME);
+                newInstrument = Instrument.create(this, debugger.createAdvancedInstrumentRootFactory(newProbe, conditionExpr, this), Boolean.class, BREAKPOINT_NAME);
             }
             newProbe.attach(newInstrument);
             instruments.add(newInstrument);
--- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/Accessor.java	Wed Aug 26 14:13:43 2015 +0200
+++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/Accessor.java	Wed Aug 26 15:22:31 2015 +0200
@@ -241,4 +241,8 @@
     protected Object findContext(Env env) {
         return API.findContext(env);
     }
+
+    protected TruffleLanguage<?> findLanguage(Env env) {
+        return API.findLanguage(env);
+    }
 }