From 97abbae86af0c2b508583394e088291ac8241f29 Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Wed, 26 May 2010 00:03:51 +0000 Subject: Made interpreter robust against a missing class... Made interpreter robust against a missing classpath and had it issue informative message in such cases, as per suggestion by Razvan Cojocaru. No review. % java -classpath build/pack/lib/'*' scala.tools.nsc.MainGenericRunner scala> Failed to initialize compiler: class scala.runtime.BooleanRef not found. ** Note that as of 2.8 scala does not assume use of the java classpath. ** For the old behavior pass -usejavacp to scala, or if using a Settings ** object programatically, settings.usejavacp.value = true. --- src/compiler/scala/tools/nsc/Interpreter.scala | 37 +++++++++++++++------- src/compiler/scala/tools/nsc/InterpreterLoop.scala | 7 ++-- 2 files changed, 31 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/compiler/scala/tools/nsc/Interpreter.scala b/src/compiler/scala/tools/nsc/Interpreter.scala index 868af7a4bc..128c378e23 100644 --- a/src/compiler/scala/tools/nsc/Interpreter.scala +++ b/src/compiler/scala/tools/nsc/Interpreter.scala @@ -75,6 +75,11 @@ import Interpreter._ class Interpreter(val settings: Settings, out: PrintWriter) { repl => + def println(x: Any) = { + out.println(x) + out.flush() + } + /** construct an interpreter that reports to Console */ def this(settings: Settings) = this(settings, new NewLinePrintWriter(new ConsoleWriter, true)) def this() = this(new Settings()) @@ -108,28 +113,37 @@ class Interpreter(val settings: Settings, out: PrintWriter) { |} |""".stripMargin - val run = new _compiler.Run() - run compileSources List(new BatchSourceFile("", source)) - if (settings.debug.value) { - out println "Repl compiler initialized." - out.flush() + try { + new _compiler.Run() compileSources List(new BatchSourceFile("", source)) + if (isReplDebug || settings.debug.value) + println("Repl compiler initialized.") + true + } + catch { + case MissingRequirementError(msg) => println(""" + |Failed to initialize compiler: %s not found. + |** Note that as of 2.8 scala does not assume use of the java classpath. + |** For the old behavior pass -usejavacp to scala, or if using a Settings + |** object programatically, settings.usejavacp.value = true.""".stripMargin.format(msg) + ) + false } - true } // set up initialization future - private var _isInitialized: () => Boolean = () => false + private var _isInitialized: () => Boolean = null def initialize() = synchronized { - if (!_isInitialized()) + if (_isInitialized == null) _isInitialized = scala.concurrent.ops future _initialize() } /** the public, go through the future compiler */ lazy val compiler: Global = { initialize() - _isInitialized() // blocks until it is - _compiler + // blocks until it is ; false means catastrophic failure + if (_isInitialized()) _compiler + else null } import compiler.{ Traverser, CompilationUnit, Symbol, Name, Type } @@ -573,7 +587,8 @@ class Interpreter(val settings: Settings, out: PrintWriter) { else IR.Error } - requestFromLine(line, synthetic) match { + if (compiler == null) IR.Error + else requestFromLine(line, synthetic) match { case Left(result) => result case Right(req) => // null indicates a disallowed statement type; otherwise compile and diff --git a/src/compiler/scala/tools/nsc/InterpreterLoop.scala b/src/compiler/scala/tools/nsc/InterpreterLoop.scala index ce6d3149a3..f68ce4a5b4 100644 --- a/src/compiler/scala/tools/nsc/InterpreterLoop.scala +++ b/src/compiler/scala/tools/nsc/InterpreterLoop.scala @@ -355,8 +355,11 @@ class InterpreterLoop(in0: Option[BufferedReader], protected val out: PrintWrite def ambiguous(cmds: List[Command]) = "Ambiguous: did you mean " + cmds.map(":" + _.name).mkString(" or ") + "?" // not a command - if (!line.startsWith(":")) - return Result(true, interpretStartingWith(line)) + if (!line.startsWith(":")) { + // Notice failure to create compiler + if (interpreter.compiler == null) return Result(false, None) + else return Result(true, interpretStartingWith(line)) + } val tokens = (line drop 1 split """\s+""").toList if (tokens.isEmpty) -- cgit v1.2.3