summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/io
diff options
context:
space:
mode:
authorLex Spoon <lex@lexspoon.org>2008-05-05 14:09:00 +0000
committerLex Spoon <lex@lexspoon.org>2008-05-05 14:09:00 +0000
commit06efde1f28ec6ad3f0008e3aaf08f292f0ae452f (patch)
treed1c81a7504a4ada89ff193282d638d3bd79a5c02 /src/compiler/scala/tools/nsc/io
parent72615dc18e18dabd211a684a5b395daf3373dfed (diff)
downloadscala-06efde1f28ec6ad3f0008e3aaf08f292f0ae452f.tar.gz
scala-06efde1f28ec6ad3f0008e3aaf08f292f0ae452f.tar.bz2
scala-06efde1f28ec6ad3f0008e3aaf08f292f0ae452f.zip
The interpreter no longer generates class files...
The interpreter no longer generates class files to disk. It instead uses an in-memory virtual directory.
Diffstat (limited to 'src/compiler/scala/tools/nsc/io')
-rw-r--r--src/compiler/scala/tools/nsc/io/AbstractFile.scala37
-rw-r--r--src/compiler/scala/tools/nsc/io/PlainFile.scala3
-rw-r--r--src/compiler/scala/tools/nsc/io/VirtualDirectory.scala67
-rw-r--r--src/compiler/scala/tools/nsc/io/VirtualFile.scala23
-rw-r--r--src/compiler/scala/tools/nsc/io/ZipArchive.scala2
5 files changed, 127 insertions, 5 deletions
diff --git a/src/compiler/scala/tools/nsc/io/AbstractFile.scala b/src/compiler/scala/tools/nsc/io/AbstractFile.scala
index 36c17ef6c9..26782dfeb9 100644
--- a/src/compiler/scala/tools/nsc/io/AbstractFile.scala
+++ b/src/compiler/scala/tools/nsc/io/AbstractFile.scala
@@ -7,7 +7,7 @@
package scala.tools.nsc.io
-import java.io.{File, IOException, InputStream}
+import java.io.{File, FileOutputStream, IOException, InputStream, OutputStream}
import java.net.URL
import scala.collection.mutable.ArrayBuffer
@@ -117,6 +117,9 @@ abstract class AbstractFile extends AnyRef with Iterable[AbstractFile] {
/** returns an input stream so the file can be read */
def input: InputStream
+ /** Returns an output stream for writing the file */
+ def output: OutputStream
+
/** size of this file if it is a concrete file. */
def size: Option[Int] = None
@@ -187,6 +190,38 @@ abstract class AbstractFile extends AnyRef with Iterable[AbstractFile] {
file
}
+ /**
+ * Get the file in this directory with the given name,
+ * creating an empty file if it does not already existing.
+ */
+ def fileNamed(name: String): AbstractFile = {
+ assert(isDirectory)
+ val existing = lookupName(name, false)
+ if (existing == null) {
+ val newFile = new File(file, name)
+ newFile.createNewFile()
+ new PlainFile(newFile)
+ } else {
+ existing
+ }
+ }
+
+ /**
+ * Get the subdirectory with a given name, creating it if it
+ * does not already exist.
+ */
+ def subdirectoryNamed(name: String): AbstractFile = {
+ assert (isDirectory)
+ val existing = lookupName(name, true)
+ if (existing == null) {
+ val dir = new File(file, name)
+ dir.mkdir()
+ new PlainFile(dir)
+ } else {
+ existing
+ }
+ }
+
/** Returns the path of this abstract file. */
override def toString() = path
diff --git a/src/compiler/scala/tools/nsc/io/PlainFile.scala b/src/compiler/scala/tools/nsc/io/PlainFile.scala
index a489dd1694..924822401e 100644
--- a/src/compiler/scala/tools/nsc/io/PlainFile.scala
+++ b/src/compiler/scala/tools/nsc/io/PlainFile.scala
@@ -7,7 +7,7 @@
package scala.tools.nsc.io
-import java.io.{File, FileInputStream, IOException}
+import java.io.{File, FileInputStream, FileOutputStream, IOException}
object PlainFile {
/**
@@ -37,6 +37,7 @@ class PlainFile(val file: File) extends AbstractFile {
override def container : AbstractFile = new PlainFile(file.getParentFile)
override def input = new FileInputStream(file)
+ override def output = new FileOutputStream(file)
override def size = Some(file.length.toInt)
diff --git a/src/compiler/scala/tools/nsc/io/VirtualDirectory.scala b/src/compiler/scala/tools/nsc/io/VirtualDirectory.scala
new file mode 100644
index 0000000000..c4ffa4892b
--- /dev/null
+++ b/src/compiler/scala/tools/nsc/io/VirtualDirectory.scala
@@ -0,0 +1,67 @@
+/* NSC -- new Scala compiler
+ * Copyright 2005-2007 LAMP/EPFL
+ */
+// $Id$
+package scala.tools.nsc.io
+import scala.collection.{mutable=>mut}
+
+/**
+ * An in-memory directory.
+ *
+ * @author Lex Spoon
+ */
+class VirtualDirectory(val name: String, maybeContainer: Option[VirtualDirectory])
+extends AbstractFile {
+ def path: String =
+ maybeContainer match {
+ case None => name
+ case Some(parent) => parent.path+'/'+ name
+ }
+ def container = maybeContainer.get
+ def isDirectory = true
+ var lastModified: Long = System.currentTimeMillis
+ private def updateLastModified {
+ lastModified = System.currentTimeMillis
+ }
+ override def file = null
+ override def input = error("directories cannot be read")
+ override def output = error("directories cannot be written")
+
+ private val files = mut.Map.empty[String, AbstractFile]
+
+ // the toList is so that the directory may continue to be
+ // modified while its elements are iterated
+ def elements = files.values.toList.elements
+
+ override def lookupName(name: String, directory: Boolean): AbstractFile = {
+ files.get(name) match {
+ case None => null
+ case Some(file) =>
+ if (file.isDirectory == directory)
+ file
+ else
+ null
+ }
+ }
+
+ override def fileNamed(name: String): AbstractFile = {
+ val existing = lookupName(name, false)
+ if (existing == null) {
+ val newFile = new VirtualFile(name, path+'/'+name)
+ files(name) = newFile
+ newFile
+ } else {
+ existing
+ }
+ }
+
+ override def subdirectoryNamed(name: String): AbstractFile = {
+ val existing = lookupName(name, true)
+ if (existing == null) {
+ val dir = new VirtualDirectory(name, Some(this))
+ dir
+ } else {
+ existing
+ }
+ }
+}
diff --git a/src/compiler/scala/tools/nsc/io/VirtualFile.scala b/src/compiler/scala/tools/nsc/io/VirtualFile.scala
index ec1e4396c7..f8fcb0eba1 100644
--- a/src/compiler/scala/tools/nsc/io/VirtualFile.scala
+++ b/src/compiler/scala/tools/nsc/io/VirtualFile.scala
@@ -7,9 +7,10 @@
package scala.tools.nsc.io
-import java.io.{File, InputStream}
+import java.io.{ByteArrayInputStream, ByteArrayOutputStream,
+ File, InputStream, OutputStream}
-/** This class implements an empty abstract regular file.
+/** This class implements an in-memory file.
*
* @author Philippe Altherr
* @version 1.0, 23/03/2004
@@ -36,6 +37,11 @@ class VirtualFile(val name: String, _path: String) extends AbstractFile {
case _ => false
}
+
+ //########################################################################
+ // Private data
+ private var content = new Array[Byte](0)
+
//########################################################################
// Public Methods
@@ -44,7 +50,18 @@ class VirtualFile(val name: String, _path: String) extends AbstractFile {
/** Returns null. */
final def file: File = null
- def input : InputStream = throw new Error("not supported");
+ override def size: Option[Int] = Some(content.size)
+
+ def input : InputStream = new ByteArrayInputStream(content);
+
+ override def output: OutputStream = {
+ new ByteArrayOutputStream() {
+ override def close() {
+ super.close()
+ content = toByteArray()
+ }
+ }
+ }
def container : AbstractFile = throw new Error("not supported")
diff --git a/src/compiler/scala/tools/nsc/io/ZipArchive.scala b/src/compiler/scala/tools/nsc/io/ZipArchive.scala
index 3267163363..2c7eadc19e 100644
--- a/src/compiler/scala/tools/nsc/io/ZipArchive.scala
+++ b/src/compiler/scala/tools/nsc/io/ZipArchive.scala
@@ -238,6 +238,8 @@ final class URLZipArchive(url: URL) extends AbstractFile {
def input: InputStream = url.openStream()
+ def output = throw new Error("unsupported")
+
override def elements: Iterator[AbstractFile] = {
if (root eq null) load()
root.elements