summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2010-11-27 22:53:15 +0000
committerPaul Phillips <paulp@improving.org>2010-11-27 22:53:15 +0000
commit25757de1db52311a94ed2672639c0f09fc28c31a (patch)
tree8a47cdfa32681dd22a2d7afedaa9615ff351aeb8 /src
parent49d47cb3723c9d3160e3f215808b571a05d1b252 (diff)
downloadscala-25757de1db52311a94ed2672639c0f09fc28c31a.tar.gz
scala-25757de1db52311a94ed2672639c0f09fc28c31a.tar.bz2
scala-25757de1db52311a94ed2672639c0f09fc28c31a.zip
Some cleanups in the *Runners and a few compile...
Some cleanups in the *Runners and a few compiler I/O conveniences. No review.
Diffstat (limited to 'src')
-rw-r--r--src/compiler/scala/tools/nsc/MainGenericRunner.scala22
-rw-r--r--src/compiler/scala/tools/nsc/ObjectRunner.scala4
-rw-r--r--src/compiler/scala/tools/nsc/ScriptRunner.scala2
-rw-r--r--src/compiler/scala/tools/nsc/io/Path.scala2
-rw-r--r--src/compiler/scala/tools/nsc/io/Streamable.scala9
-rw-r--r--src/compiler/scala/tools/nsc/io/VirtualFile.scala4
-rw-r--r--src/compiler/scala/tools/nsc/util/package.scala12
7 files changed, 38 insertions, 17 deletions
diff --git a/src/compiler/scala/tools/nsc/MainGenericRunner.scala b/src/compiler/scala/tools/nsc/MainGenericRunner.scala
index 84d9eb7d22..a2d2b5b7c4 100644
--- a/src/compiler/scala/tools/nsc/MainGenericRunner.scala
+++ b/src/compiler/scala/tools/nsc/MainGenericRunner.scala
@@ -12,7 +12,7 @@ import java.net.{ URL, MalformedURLException }
import scala.tools.util.PathResolver
import io.{ File, Process }
-import util.{ ClassPath, ScalaClassLoader, waitingForThreads }
+import util.{ ClassPath, ScalaClassLoader }
import Properties.{ versionString, copyrightString }
/** An object that runs Scala code. It has three possible
@@ -31,7 +31,7 @@ object MainGenericRunner {
def main(args: Array[String]) {
if (!process(args))
- exit(1)
+ System.exit(1)
}
def process(args: Array[String]): Boolean = {
@@ -85,17 +85,13 @@ object MainGenericRunner {
case "guess" => ScalaClassLoader.classExists(classpath, thingToRun)
}
- if (isObjectName) {
- ObjectRunner.runAndCatch(classpath, thingToRun, command.arguments) match {
- case Left(ex) => errorFn(ex)
- case _ => true
- }
- }
- else {
- ScriptRunner.runScriptAndCatch(settings, thingToRun, command.arguments) match {
- case Left(ex) => errorFn(ex)
- case Right(b) => b
- }
+ val result =
+ if (isObjectName) ObjectRunner.runAndCatch(classpath, thingToRun, command.arguments)
+ else ScriptRunner.runScriptAndCatch(settings, thingToRun, command.arguments)
+
+ result match {
+ case Left(ex) => errorFn(ex)
+ case Right(b) => b
}
}
}
diff --git a/src/compiler/scala/tools/nsc/ObjectRunner.scala b/src/compiler/scala/tools/nsc/ObjectRunner.scala
index 16793996bb..d10f91a7d2 100644
--- a/src/compiler/scala/tools/nsc/ObjectRunner.scala
+++ b/src/compiler/scala/tools/nsc/ObjectRunner.scala
@@ -36,8 +36,8 @@ object ObjectRunner {
/** Catches exceptions enumerated by run (in the case of InvocationTargetException,
* unwrapping it) and returns it any thrown in Left(x).
*/
- def runAndCatch(urls: List[URL], objectName: String, arguments: Seq[String]): Either[Throwable, Unit] = {
- try Right(run(urls, objectName, arguments))
+ def runAndCatch(urls: List[URL], objectName: String, arguments: Seq[String]): Either[Throwable, Boolean] = {
+ try { run(urls, objectName, arguments) ; Right(true) }
catch { case e => Left(unwrap(e)) }
}
}
diff --git a/src/compiler/scala/tools/nsc/ScriptRunner.scala b/src/compiler/scala/tools/nsc/ScriptRunner.scala
index c43542c348..1a5bcccddd 100644
--- a/src/compiler/scala/tools/nsc/ScriptRunner.scala
+++ b/src/compiler/scala/tools/nsc/ScriptRunner.scala
@@ -226,7 +226,7 @@ object ScriptRunner {
else recompile() // jar old - recompile the script.
}
// don't use a cache jar at all--just use the class files
- else compile map (cp => handler(cp.path)) getOrElse false
+ else compile exists (cp => handler(cp.path))
}
}
diff --git a/src/compiler/scala/tools/nsc/io/Path.scala b/src/compiler/scala/tools/nsc/io/Path.scala
index 5d3fcee11e..b6699ce901 100644
--- a/src/compiler/scala/tools/nsc/io/Path.scala
+++ b/src/compiler/scala/tools/nsc/io/Path.scala
@@ -28,6 +28,8 @@ import scala.util.Random.alphanumeric
*/
object Path {
+ // See http://download.java.net/jdk7/docs/api/java/nio/file/Path.html
+ // for some ideas.
private val ZipMagicNumber = List[Byte](80, 75, 3, 4)
private def magicNumberIsZip(f: Path) = f.isFile && (f.toFile.bytes().take(4).toList == ZipMagicNumber)
diff --git a/src/compiler/scala/tools/nsc/io/Streamable.scala b/src/compiler/scala/tools/nsc/io/Streamable.scala
index 93f17ca7ba..7839de9df1 100644
--- a/src/compiler/scala/tools/nsc/io/Streamable.scala
+++ b/src/compiler/scala/tools/nsc/io/Streamable.scala
@@ -105,4 +105,13 @@ object Streamable {
def slurp(): String = slurp(creationCodec)
def slurp(codec: Codec) = chars(codec).mkString
}
+
+ def bytes(is: InputStream): Array[Byte] =
+ new Bytes { val inputStream = is } toByteArray
+
+ def slurp(is: InputStream)(implicit codec: Codec): String =
+ new Chars { val inputStream = is } slurp codec
+
+ def slurp(url: URL)(implicit codec: Codec): String =
+ slurp(url.openStream())
}
diff --git a/src/compiler/scala/tools/nsc/io/VirtualFile.scala b/src/compiler/scala/tools/nsc/io/VirtualFile.scala
index d22eaa86ae..f02a43c981 100644
--- a/src/compiler/scala/tools/nsc/io/VirtualFile.scala
+++ b/src/compiler/scala/tools/nsc/io/VirtualFile.scala
@@ -67,7 +67,9 @@ class VirtualFile(val name: String, _path: String) extends AbstractFile
def isDirectory: Boolean = false
/** Returns the time that this abstract file was last modified. */
- def lastModified: Long = Long.MinValue
+ private var _lastModified: Long = 0
+ def lastModified: Long = _lastModified
+ def lastModified_=(x: Long) = _lastModified = x
/** Returns all abstract subfiles of this abstract directory. */
def iterator: Iterator[AbstractFile] = {
diff --git a/src/compiler/scala/tools/nsc/util/package.scala b/src/compiler/scala/tools/nsc/util/package.scala
index 7924d4debd..800cb75b97 100644
--- a/src/compiler/scala/tools/nsc/util/package.scala
+++ b/src/compiler/scala/tools/nsc/util/package.scala
@@ -46,6 +46,18 @@ package object util {
result
}
+ /** Given a function and a block of code, evaluates code block,
+ * calls function with milliseconds elapsed, and returns block result.
+ */
+ def millisElapsedTo[T](f: Long => Unit)(body: => T): T = {
+ val start = System.currentTimeMillis
+ val result = body
+ val end = System.currentTimeMillis
+
+ f(end - start)
+ result
+ }
+
/** Generate a string using a routine that wants to write on a stream. */
def stringFromWriter(writer: PrintWriter => Unit): String = {
val stringWriter = new StringWriter()