From 7aca20d8d31790569557c87406a74c0c7377de79 Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Wed, 2 Feb 2011 08:23:36 +0000 Subject: Added javap to the repl. so those hypothetical people who use non-sun jvms won't be thrown off kilter. You can give it a file argument or a class name, including classes you just defined in the repl, and it will clean up the repl mangling on the way out. scala> class Bippy { def hackenkraks(x: Float, y: Double) = 5 } defined class Bippy scala> :javap Bippy Compiled from "" public class Bippy extends java.lang.Object implements scala.ScalaObject [...] public int hackenkraks(float, double); Signature: (FD)I Code: Stack=1, Locals=4, Args_size=3 0: iconst_5 1: ireturn scala> :javap scala.collection.Map public interface scala.collection.Map extends scala.collection.Iterable,scala.collection.MapLike,scala.ScalaObject [...] const #7 = Asciz ScalaSig; const #8 = Asciz Ljava/lang/Object;Lscala/collection/Iterable;>;Lscala/collection/MapLike;>;Lscala/ScalaObject;; const #9 = Asciz Lscala/reflect/ScalaSignature;; const #10 = Asciz bytes; const #11 = Asciz [line elided for control chars: possibly a scala signature] scala> :javap java.util.Enumeration Compiled from "Enumeration.java" public interface java.util.Enumeration [...] scala> :javap /scala/trunk/build/quick/classes/compiler/scala/tools/util/Javap.class Compiled from "Javap.scala" public class scala.tools.util.Javap extends java.lang.Object implements scala.ScalaObject [...] No review. --- .../scala/tools/nsc/interpreter/IMain.scala | 199 ++++++++++++--------- 1 file changed, 118 insertions(+), 81 deletions(-) (limited to 'src/compiler/scala/tools/nsc/interpreter/IMain.scala') diff --git a/src/compiler/scala/tools/nsc/interpreter/IMain.scala b/src/compiler/scala/tools/nsc/interpreter/IMain.scala index 492b79cb84..3c54c18fe1 100644 --- a/src/compiler/scala/tools/nsc/interpreter/IMain.scala +++ b/src/compiler/scala/tools/nsc/interpreter/IMain.scala @@ -25,45 +25,37 @@ import scala.util.control.Exception.{ ultimately } import scala.reflect.NameTransformer import IMain._ -/**

- * An interpreter for Scala code. - *

- *

- * The main public entry points are compile(), - * interpret(), and bind(). - * The compile() method loads a - * complete Scala file. The interpret() method executes one - * line of Scala code at the request of the user. The bind() - * method binds an object to a variable that can then be used by later - * interpreted code. - *

- *

- * The overall approach is based on compiling the requested code and then - * using a Java classloader and Java reflection to run the code - * and access its results. - *

- *

- * In more detail, a single compiler instance is used - * to accumulate all successfully compiled or interpreted Scala code. To - * "interpret" a line of code, the compiler generates a fresh object that - * includes the line of code and which has public member(s) to export - * all variables defined by that code. To extract the result of an - * interpreted line to show the user, a second "result object" is created - * which imports the variables exported by the above object and then - * exports a single member named "$export". To accomodate user expressions - * that read from variables or methods defined in previous statements, "import" - * statements are used. - *

- *

- * This interpreter shares the strengths and weaknesses of using the - * full compiler-to-Java. The main strength is that interpreted code - * behaves exactly as does compiled code, including running at full speed. - * The main weakness is that redefining classes and methods is not handled - * properly, because rebinding at the Java level is technically difficult. - *

+/** An interpreter for Scala code. * - * @author Moez A. Abdel-Gawad - * @author Lex Spoon + * The main public entry points are compile(), interpret(), and bind(). + * The compile() method loads a complete Scala file. The interpret() method + * executes one line of Scala code at the request of the user. The bind() + * method binds an object to a variable that can then be used by later + * interpreted code. + * + * The overall approach is based on compiling the requested code and then + * using a Java classloader and Java reflection to run the code + * and access its results. + * + * In more detail, a single compiler instance is used + * to accumulate all successfully compiled or interpreted Scala code. To + * "interpret" a line of code, the compiler generates a fresh object that + * includes the line of code and which has public member(s) to export + * all variables defined by that code. To extract the result of an + * interpreted line to show the user, a second "result object" is created + * which imports the variables exported by the above object and then + * exports a single member named "$export". To accomodate user expressions + * that read from variables or methods defined in previous statements, "import" + * statements are used. + * + * This interpreter shares the strengths and weaknesses of using the + * full compiler-to-Java. The main strength is that interpreted code + * behaves exactly as does compiled code, including running at full speed. + * The main weakness is that redefining classes and methods is not handled + * properly, because rebinding at the Java level is technically difficult. + * + * @author Moez A. Abdel-Gawad + * @author Lex Spoon */ class IMain(val settings: Settings, protected val out: PrintWriter) { intp => @@ -275,6 +267,17 @@ class IMain(val settings: Settings, protected val out: PrintWriter) { private def allReqAndHandlers = prevRequests.toList flatMap (req => req.handlers map (req -> _)) private def importHandlers = allHandlers collect { case x: ImportHandler => x } + /** Given a simple repl-defined name, returns the real name of + * the class representing it, e.g. for "Bippy" it may return + * + * $line19.$read$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$Bippy + */ + def pathToFlatName(id: String): String = { + requestForIdent(id) match { + case Some(req) => req fullFlatName id + case _ => id + } + } def allDefinedNames = definedNameMap.keys.toList sortBy (_.toString) def pathToType(id: String): String = pathToName(newTypeName(id)) def pathToTerm(id: String): String = pathToName(newTermName(id)) @@ -484,8 +487,7 @@ class IMain(val settings: Settings, protected val out: PrintWriter) { compileSources(new BatchSourceFile("