From 1c6fad54ec9aa8361e903b76aead9858959749a2 Mon Sep 17 00:00:00 2001 From: "Jurgen J. Vinju" Date: Thu, 4 Apr 2024 14:27:16 +0200 Subject: [PATCH] better handling of System.console --- src/org/rascalmpl/checker/StaticChecker.java | 2 +- src/org/rascalmpl/debug/IRascalMonitor.java | 7 +++---- src/org/rascalmpl/interpreter/Evaluator.java | 17 +++++++++++------ src/org/rascalmpl/interpreter/JavaToRascal.java | 4 +++- src/org/rascalmpl/library/util/Eval.java | 5 +++-- src/org/rascalmpl/library/util/Reflective.java | 9 +++++---- src/org/rascalmpl/parser/ParserGenerator.java | 2 +- .../rascalmpl/shell/ShellEvaluatorFactory.java | 9 +++------ .../RascalJUnitParallelRecursiveTestRunner.java | 9 ++++++--- test/org/rascalmpl/MatchFingerprintTest.java | 3 ++- .../functionality/ParallelEvaluatorsTests.java | 4 ++-- 11 files changed, 40 insertions(+), 31 deletions(-) diff --git a/src/org/rascalmpl/checker/StaticChecker.java b/src/org/rascalmpl/checker/StaticChecker.java index a781e418fd3..acb755fa842 100644 --- a/src/org/rascalmpl/checker/StaticChecker.java +++ b/src/org/rascalmpl/checker/StaticChecker.java @@ -45,7 +45,7 @@ public class StaticChecker { public StaticChecker(OutputStream stderr, OutputStream stdout) { GlobalEnvironment heap = new GlobalEnvironment(); ModuleEnvironment root = heap.addModule(new ModuleEnvironment("$staticchecker$", heap)); - eval = new Evaluator(ValueFactoryFactory.getValueFactory(), System.in, stderr, stdout, root, heap); + eval = new Evaluator(ValueFactoryFactory.getValueFactory(), System.in, stderr, stdout, root, heap, IRascalMonitor.buildConsoleMonitor(System.in, System.out)); eval.addRascalSearchPathContributor(StandardLibraryContributor.getInstance()); checkerEnabled = false; initialized = false; diff --git a/src/org/rascalmpl/debug/IRascalMonitor.java b/src/org/rascalmpl/debug/IRascalMonitor.java index 2c653dd5e5d..ec87c7f114a 100644 --- a/src/org/rascalmpl/debug/IRascalMonitor.java +++ b/src/org/rascalmpl/debug/IRascalMonitor.java @@ -107,12 +107,11 @@ default void jobStep(String name, String message) { * and otherwise default to a dumn terminal console progress logger. * @return */ - @SuppressWarnings("unchecked") - public static T buildConsoleMonitor(InputStream in, OutputStream out) { - return (T) ((System.console() != null) + public static IRascalMonitor buildConsoleMonitor(InputStream in, OutputStream out) { + return System.console() != null ? new TerminalProgressBarMonitor(out, in, TerminalFactory.get()) : new ConsoleRascalMonitor(new PrintStream(out)) - ); + ; } /** diff --git a/src/org/rascalmpl/interpreter/Evaluator.java b/src/org/rascalmpl/interpreter/Evaluator.java index 8c7d75acbb3..3dcb5f7b1f4 100755 --- a/src/org/rascalmpl/interpreter/Evaluator.java +++ b/src/org/rascalmpl/interpreter/Evaluator.java @@ -235,11 +235,19 @@ public void decCallNesting() { */ public Evaluator(IValueFactory f, InputStream input, OutputStream stderr, OutputStream stdout, IRascalMonitor monitor, ModuleEnvironment scope, GlobalEnvironment heap) { this(f, input, stderr, monitor instanceof OutputStream ? (OutputStream) monitor : stdout, scope, heap, new ArrayList(Collections.singleton(Evaluator.class.getClassLoader())), new RascalSearchPath()); + } + + /** + * If your monitor should wrap stdout (like TerminalProgressBarMonitor) then you can use this constructor. + */ + public Evaluator(IValueFactory f, InputStream input, OutputStream stderr, M monitor, ModuleEnvironment scope, GlobalEnvironment heap) { + this(f, input, stderr, monitor, scope, heap, new ArrayList(Collections.singleton(Evaluator.class.getClassLoader())), new RascalSearchPath()); setMonitor(monitor); } - public Evaluator(IValueFactory f, InputStream input, OutputStream stderr, OutputStream stdout, ModuleEnvironment scope, GlobalEnvironment heap) { - this(f, input, stderr, stdout, scope, heap, new ArrayList(Collections.singleton(Evaluator.class.getClassLoader())), new RascalSearchPath()); + public Evaluator(IValueFactory f, InputStream input, OutputStream stderr, OutputStream stdout, ModuleEnvironment scope, GlobalEnvironment heap, IRascalMonitor monitor) { + this(f, input, stderr, monitor instanceof OutputStream ? (OutputStream) monitor : stdout, scope, heap, new ArrayList(Collections.singleton(Evaluator.class.getClassLoader())), new RascalSearchPath()); + setMonitor(monitor); } public Evaluator(IValueFactory vf, InputStream input, OutputStream stderr, OutputStream stdout, ModuleEnvironment scope, GlobalEnvironment heap, List classLoaders, RascalSearchPath rascalPathResolver) { @@ -320,10 +328,7 @@ private Evaluator(Evaluator source, ModuleEnvironment scope) { * @param heap2 */ public Evaluator(IValueFactory vf, ModuleEnvironment root, GlobalEnvironment heap) { - this(vf, System.in, System.err, IRascalMonitor.buildConsoleMonitor(System.in, System.out), root, heap); - if (defStdout instanceof TerminalProgressBarMonitor) { - setMonitor((TerminalProgressBarMonitor) defStdout); - } + this(vf, System.in, System.err, System.out, IRascalMonitor.buildConsoleMonitor(System.in, System.out), root, heap); } public void resetJavaBridge() { diff --git a/src/org/rascalmpl/interpreter/JavaToRascal.java b/src/org/rascalmpl/interpreter/JavaToRascal.java index f1a0194b1ae..a08a34913f4 100644 --- a/src/org/rascalmpl/interpreter/JavaToRascal.java +++ b/src/org/rascalmpl/interpreter/JavaToRascal.java @@ -15,6 +15,7 @@ import java.util.ArrayList; import java.util.Hashtable; +import org.rascalmpl.debug.IRascalMonitor; import org.rascalmpl.interpreter.env.Environment; import org.rascalmpl.interpreter.env.GlobalEnvironment; import org.rascalmpl.interpreter.env.ModuleEnvironment; @@ -63,7 +64,8 @@ public Evaluator getEvaluator() { public JavaToRascal(InputStream input, OutputStream stdout, OutputStream stderr) { this.evaluator = new Evaluator(vf, input, stderr, stdout, - new ModuleEnvironment(ModuleEnvironment.SHELL_MODULE, heap), heap); + new ModuleEnvironment(ModuleEnvironment.SHELL_MODULE, heap), heap, + IRascalMonitor.buildConsoleMonitor(input, stdout)); } public JavaToRascal(Evaluator evaluator) { diff --git a/src/org/rascalmpl/library/util/Eval.java b/src/org/rascalmpl/library/util/Eval.java index f3d69b88929..b680b6475e0 100644 --- a/src/org/rascalmpl/library/util/Eval.java +++ b/src/org/rascalmpl/library/util/Eval.java @@ -20,6 +20,7 @@ import java.util.HashMap; import java.util.Map; +import org.rascalmpl.debug.IRascalMonitor; import org.rascalmpl.exceptions.RuntimeExceptionFactory; import org.rascalmpl.exceptions.Throw; import org.rascalmpl.interpreter.Evaluator; @@ -65,7 +66,7 @@ public class Eval { - public Eval(IValueFactory values, OutputStream out, OutputStream err, InputStream in, ClassLoader loader) { + public Eval(IValueFactory values, OutputStream out, OutputStream err, InputStream in, ClassLoader loader, IRascalMonitor monitor) { super(); this.values = values; this.tr = new TypeReifier(values); @@ -73,7 +74,7 @@ public Eval(IValueFactory values, OutputStream out, OutputStream err, InputStrea GlobalEnvironment heap = new GlobalEnvironment(); ModuleEnvironment root = new ModuleEnvironment("$eval$", heap); - this.eval = new Evaluator(values, in, err, out, root, heap); + this.eval = new Evaluator(values, in, err, out, root, heap, monitor); // TODO: this is to fix the course Question compiler with a workaround. // it would be better to parameterize eval with a PathConfig. diff --git a/src/org/rascalmpl/library/util/Reflective.java b/src/org/rascalmpl/library/util/Reflective.java index 98f50a5a981..44bb23ce3b1 100644 --- a/src/org/rascalmpl/library/util/Reflective.java +++ b/src/org/rascalmpl/library/util/Reflective.java @@ -23,8 +23,8 @@ import java.io.Writer; import java.net.URISyntaxException; +import org.rascalmpl.debug.IRascalMonitor; import org.rascalmpl.exceptions.RuntimeExceptionFactory; -import org.rascalmpl.interpreter.ConsoleRascalMonitor; import org.rascalmpl.interpreter.Evaluator; import org.rascalmpl.interpreter.IEvaluator; import org.rascalmpl.interpreter.env.GlobalEnvironment; @@ -69,10 +69,12 @@ public class Reflective { protected final IValueFactory values; + private final IRascalMonitor monitor; - public Reflective(IValueFactory values){ + public Reflective(IValueFactory values, IRascalMonitor monitor) { super(); this.values = values; + this.monitor = monitor; } public IString getRascalVersion() { @@ -101,9 +103,8 @@ IEvaluator getDefaultEvaluator(OutputStream stdout, OutputStream stderr) { GlobalEnvironment heap = new GlobalEnvironment(); ModuleEnvironment root = heap.addModule(new ModuleEnvironment(ModuleEnvironment.SHELL_MODULE, heap)); IValueFactory vf = ValueFactoryFactory.getValueFactory(); - Evaluator evaluator = new Evaluator(vf, System.in, stderr, stdout, root, heap); + Evaluator evaluator = new Evaluator(vf, System.in, stderr, stdout, root, heap, monitor); evaluator.addRascalSearchPathContributor(StandardLibraryContributor.getInstance()); - evaluator.setMonitor(new ConsoleRascalMonitor()); return evaluator; } diff --git a/src/org/rascalmpl/parser/ParserGenerator.java b/src/org/rascalmpl/parser/ParserGenerator.java index 2e8414540e3..a1f7698e26a 100644 --- a/src/org/rascalmpl/parser/ParserGenerator.java +++ b/src/org/rascalmpl/parser/ParserGenerator.java @@ -55,7 +55,7 @@ public class ParserGenerator { public ParserGenerator(IRascalMonitor monitor, OutputStream out, List loaders, IValueFactory factory, Configuration config) { GlobalEnvironment heap = new GlobalEnvironment(); ModuleEnvironment scope = new ModuleEnvironment("$parsergenerator$", heap); - this.evaluator = new Evaluator(ValueFactoryFactory.getValueFactory(), System.in, out, out, scope,heap); + this.evaluator = new Evaluator(ValueFactoryFactory.getValueFactory(), System.in, out, out, scope, heap, monitor); this.evaluator.getConfiguration().setRascalJavaClassPathProperty(config.getRascalJavaClassPathProperty()); this.evaluator.getConfiguration().setGeneratorProfiling(config.getGeneratorProfilingProperty()); evaluator.addRascalSearchPathContributor(StandardLibraryContributor.getInstance()); diff --git a/src/org/rascalmpl/shell/ShellEvaluatorFactory.java b/src/org/rascalmpl/shell/ShellEvaluatorFactory.java index 7507e2b8cc4..960f6c2bfe4 100644 --- a/src/org/rascalmpl/shell/ShellEvaluatorFactory.java +++ b/src/org/rascalmpl/shell/ShellEvaluatorFactory.java @@ -6,7 +6,7 @@ import java.io.OutputStream; import java.net.URISyntaxException; -import org.rascalmpl.interpreter.ConsoleRascalMonitor; +import org.rascalmpl.debug.IRascalMonitor; import org.rascalmpl.interpreter.Evaluator; import org.rascalmpl.interpreter.env.GlobalEnvironment; import org.rascalmpl.interpreter.env.ModuleEnvironment; @@ -31,10 +31,9 @@ public static Evaluator getDefaultEvaluator(InputStream input, OutputStream stdo GlobalEnvironment heap = new GlobalEnvironment(); ModuleEnvironment root = heap.addModule(new ModuleEnvironment(ModuleEnvironment.SHELL_MODULE, heap)); IValueFactory vf = ValueFactoryFactory.getValueFactory(); - Evaluator evaluator = new Evaluator(vf, input, stderr, stdout, root, heap); + Evaluator evaluator = new Evaluator(vf, input, stderr, stdout, root, heap, IRascalMonitor.buildConsoleMonitor(input, stdout)); evaluator.addRascalSearchPathContributor(StandardLibraryContributor.getInstance()); - evaluator.setMonitor(new ConsoleRascalMonitor()); URIResolverRegistry reg = URIResolverRegistry.getInstance(); if (!reg.getRegisteredInputSchemes().contains("project") && !reg.getRegisteredLogicalSchemes().contains("project")) { @@ -51,11 +50,9 @@ public static Evaluator getDefaultEvaluatorForLocation(File fileOrFolderInProjec GlobalEnvironment heap = new GlobalEnvironment(); ModuleEnvironment root = heap.addModule(new ModuleEnvironment(ModuleEnvironment.SHELL_MODULE, heap)); IValueFactory vf = ValueFactoryFactory.getValueFactory(); - Evaluator evaluator = new Evaluator(vf, input, stderr, stdout, root, heap); + Evaluator evaluator = new Evaluator(vf, input, stderr, stdout, root, heap, IRascalMonitor.buildConsoleMonitor(input, stdout)); evaluator.addRascalSearchPathContributor(StandardLibraryContributor.getInstance()); - evaluator.setMonitor(new ConsoleRascalMonitor()); - ISourceLocation rootFolder = inferProjectRoot(fileOrFolderInProject); if (rootFolder != null) { configureProjectEvaluator(evaluator, rootFolder); diff --git a/src/org/rascalmpl/test/infrastructure/RascalJUnitParallelRecursiveTestRunner.java b/src/org/rascalmpl/test/infrastructure/RascalJUnitParallelRecursiveTestRunner.java index 773871cb8ff..281c672ec98 100644 --- a/src/org/rascalmpl/test/infrastructure/RascalJUnitParallelRecursiveTestRunner.java +++ b/src/org/rascalmpl/test/infrastructure/RascalJUnitParallelRecursiveTestRunner.java @@ -181,8 +181,8 @@ public class ModuleTester extends Thread { private GlobalEnvironment heap; private ModuleEnvironment root; - private PrintWriter stderr = new PrintWriter(System.err); - private PrintWriter stdout = new PrintWriter(System.out); + private PrintWriter stderr; + private PrintWriter stdout; private Evaluator evaluator; private final List testModules = new ArrayList<>(); @@ -300,8 +300,11 @@ public Class annotationType() { private void initializeEvaluator() { heap = new GlobalEnvironment(); root = heap.addModule(new ModuleEnvironment("___junit_test___", heap)); + + evaluator = new Evaluator(ValueFactoryFactory.getValueFactory(), System.in, System.err, System.out, monitor, root, heap); + stdout = new PrintWriter(evaluator.getStdOut()); + stderr = new PrintWriter(evaluator.getStdErr()); - evaluator = new Evaluator(ValueFactoryFactory.getValueFactory(), root, heap); evaluator.addRascalSearchPathContributor(StandardLibraryContributor.getInstance()); evaluator.getConfiguration().setErrors(true); diff --git a/test/org/rascalmpl/MatchFingerprintTest.java b/test/org/rascalmpl/MatchFingerprintTest.java index 41614b94b05..b635997c314 100644 --- a/test/org/rascalmpl/MatchFingerprintTest.java +++ b/test/org/rascalmpl/MatchFingerprintTest.java @@ -17,6 +17,7 @@ import java.io.IOException; import java.io.StringReader; +import org.rascalmpl.debug.IRascalMonitor; import org.rascalmpl.interpreter.Evaluator; import org.rascalmpl.interpreter.env.GlobalEnvironment; import org.rascalmpl.interpreter.env.ModuleEnvironment; @@ -46,7 +47,7 @@ public class MatchFingerprintTest extends TestCase { private final GlobalEnvironment heap = new GlobalEnvironment(); private final ModuleEnvironment root = new ModuleEnvironment("root", heap); - private final Evaluator eval = new Evaluator(IRascalValueFactory.getInstance(), System.in, System.err, System.out, root, heap); + private final Evaluator eval = new Evaluator(IRascalValueFactory.getInstance(), System.in, System.err, System.out, root, heap, IRascalMonitor.buildConsoleMonitor(System.in, System.out)); private final RascalFunctionValueFactory VF = eval.getFunctionValueFactory(); private final TypeFactory TF = TypeFactory.getInstance(); diff --git a/test/org/rascalmpl/test/functionality/ParallelEvaluatorsTests.java b/test/org/rascalmpl/test/functionality/ParallelEvaluatorsTests.java index d260f369490..15134bb00bb 100644 --- a/test/org/rascalmpl/test/functionality/ParallelEvaluatorsTests.java +++ b/test/org/rascalmpl/test/functionality/ParallelEvaluatorsTests.java @@ -35,8 +35,8 @@ private static Evaluator freshEvaluator() { var heap = new GlobalEnvironment(); var root = heap.addModule(new ModuleEnvironment("___test___", heap)); - var evaluator = new Evaluator(ValueFactoryFactory.getValueFactory(), System.in, System.err, System.out, root, heap); - evaluator.setMonitor(monitor); + var evaluator = new Evaluator(ValueFactoryFactory.getValueFactory(), System.in, System.err, System.out, root, heap, monitor); + evaluator.addRascalSearchPathContributor(StandardLibraryContributor.getInstance()); evaluator.setTestResultListener(new ITestResultListener() { @Override