Skip to content

Commit

Permalink
Support straight-to-JAR, () => ByteBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
retronym committed Nov 19, 2018
1 parent 68a096a commit 520f9a5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
18 changes: 14 additions & 4 deletions src/compiler/scala/tools/nsc/PipelineMain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ import scala.reflect.internal.pickling.PickleBuffer
import scala.reflect.internal.util.FakePos
import scala.reflect.io.{VirtualDirectory, VirtualFile}
import scala.tools.nsc.backend.{ClassfileInfo, JavaPlatform, ScalaClass, ScalaRawClass}
import scala.tools.nsc.classpath.{DirectoryClassPath, VirtualDirectoryClassPath}
import scala.tools.nsc.classpath.{DirectoryClassPath, VirtualDirectoryClassPath, ZipArchiveFileLookup}
import scala.tools.nsc.io.AbstractFile
import scala.tools.nsc.reporters.{ConsoleReporter, Reporter}
import scala.tools.nsc.util.ClassPath
import scala.util.{Failure, Success}
import scala.concurrent.duration.Duration
import scala.tools.nsc.classpath.ZipAndJarClassPathFactory.ZipArchiveClassPath

class PipelineMainClass(label: String, parallelism: Int, strategy: BuildStrategy) {
/** Forward errors to the (current) reporter. */
Expand Down Expand Up @@ -59,20 +60,20 @@ class PipelineMainClass(label: String, parallelism: Int, strategy: BuildStrategy
val base = packageDir(symbol.owner)
if (symbol.isClass) {
val primary = base.fileNamed(symbol.encodedName + ".class")
classInfo(primary) = ScalaClass(symbol.fullNameString, ByteBuffer.wrap(pickle.bytes))
classInfo(primary) = ScalaClass(symbol.fullNameString, () => ByteBuffer.wrap(pickle.bytes))
if (symbol.companionModule.exists) {
val secondary = base.fileNamed(symbol.companionModule.encodedName + "$.class")
classInfo(secondary) = ScalaRawClass(symbol.companionModule.fullNameString)
}
} else if (symbol.isModule) {
if (symbol.companionClass.exists) {
val primary = base.fileNamed(symbol.encodedName + ".class")
classInfo(primary) = ScalaClass(symbol.fullNameString, ByteBuffer.wrap(pickle.bytes))
classInfo(primary) = ScalaClass(symbol.fullNameString, () => ByteBuffer.wrap(pickle.bytes))
val secondary = base.fileNamed(symbol.companionModule.encodedName + "$.class")
classInfo(secondary) = ScalaRawClass(symbol.companionModule.fullNameString)
} else {
val primary = base.fileNamed(symbol.encodedName + "$.class")
classInfo(primary) = ScalaClass(symbol.fullNameString, ByteBuffer.wrap(pickle.bytes))
classInfo(primary) = ScalaClass(symbol.fullNameString, () => ByteBuffer.wrap(pickle.bytes))
}
}
}
Expand Down Expand Up @@ -394,6 +395,15 @@ class PipelineMainClass(label: String, parallelism: Int, strategy: BuildStrategy
val replacements = mutable.Buffer[PickleClassPath[_]]()
override def modifyClassPath(classPath: Seq[ClassPath]): Seq[ClassPath] = {
classPath.flatMap {
case zcp: ZipArchiveFileLookup[_] =>
val path = zcp.zipFile.toPath.toRealPath().normalize()
allPickleData.get(path) match {
case null =>
zcp :: Nil
case pcp =>
replacements += pcp
pcp.classpath :: zcp :: Nil // leaving the original classpath for Java compiled files for now
}
case dcp: DirectoryClassPath =>
val path = dcp.dir.toPath.toRealPath().normalize()
allPickleData.get(path) match {
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/scala/tools/nsc/backend/Platform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ trait Platform {
}

sealed abstract class ClassfileInfo {}
final case class ClassBytes(data: ByteBuffer) extends ClassfileInfo
final case class ClassBytes(data: () => ByteBuffer) extends ClassfileInfo
final case class ScalaRawClass(className: String) extends ClassfileInfo
final case class ScalaClass(className: String, pickle: ByteBuffer) extends ClassfileInfo
final case class ScalaClass(className: String, pickle: () => ByteBuffer) extends ClassfileInfo

Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ abstract class ClassfileParser {
isScalaRaw = true
currentClass = TermName(className)
case ScalaClass(className, pickle) =>
val pickle1 = pickle
val pickle1 = pickle()
isScala = true
currentClass = TermName(className)
if (pickle1.hasArray) {
Expand All @@ -177,7 +177,7 @@ abstract class ClassfileParser {
unpickler.unpickle(array, 0, clazz, staticModule, file.name)
}
case ClassBytes(data) =>
val data1 = data.duplicate()
val data1 = data()
val array = new Array[Byte](data1.remaining)
data1.get(array)
this.in = new AbstractFileReader(file, array)
Expand All @@ -191,7 +191,7 @@ abstract class ClassfileParser {
this.pool = newConstantPool
parseClass()
if (!(isScala || isScalaRaw))
loaders.platform.classFileInfoParsed(file, clazz, ClassBytes(ByteBuffer.wrap(in.buf)))
loaders.platform.classFileInfoParsed(file, clazz, ClassBytes(() => ByteBuffer.wrap(in.buf)))
}
}
}
Expand Down Expand Up @@ -933,7 +933,7 @@ abstract class ClassfileParser {
val bytes =
san.assocs.find({ _._1 == nme.bytes }).get._2.asInstanceOf[ScalaSigBytes].bytes
unpickler.unpickle(bytes, 0, clazz, staticModule, in.file.name)
loaders.platform.classFileInfoParsed(file, clazz, ScalaClass(this.currentClass.toString, ByteBuffer.wrap(bytes)))
loaders.platform.classFileInfoParsed(file, clazz, ScalaClass(this.currentClass.toString, () => ByteBuffer.wrap(bytes)))
case None =>
throw new RuntimeException("Scala class file does not contain Scala annotation")
}
Expand Down

0 comments on commit 520f9a5

Please sign in to comment.