Skip to content

Commit b8034c2

Browse files
aherlihydwijnand
authored andcommitted
Add updateClassPath + reset rootctx
1 parent f4968e7 commit b8034c2

File tree

3 files changed

+39
-21
lines changed

3 files changed

+39
-21
lines changed

compiler/src/dotty/tools/dotc/config/JavaPlatform.scala

+6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ class JavaPlatform extends Platform {
2727
case _ => false
2828
})
2929

30+
def addToClassPath(cPath: ClassPath): Unit = currentClassPath.get match {
31+
case AggregateClassPath(entries) =>
32+
currentClassPath = Some(AggregateClassPath(entries :+ cPath))
33+
case cp: ClassPath =>
34+
currentClassPath = Some(AggregateClassPath(cp :: cPath :: Nil))
35+
}
3036
/** Update classpath with a substituted subentry */
3137
def updateClassPath(subst: Map[ClassPath, ClassPath]): Unit = currentClassPath.get match {
3238
case AggregateClassPath(entries) =>

compiler/src/dotty/tools/dotc/config/Platform.scala

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ abstract class Platform {
2020

2121
/** Update classpath with a substitution that maps entries to entries */
2222
def updateClassPath(subst: Map[ClassPath, ClassPath]): Unit
23+
def addToClassPath(cPath: ClassPath): Unit
2324

2425
/** Any platform-specific phases. */
2526
//def platformPhases: List[SubComponent]

compiler/src/dotty/tools/repl/ReplDriver.scala

+32-21
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package dotty.tools.repl
22

33
import scala.language.unsafeNulls
4-
5-
import java.io.{File => JFile, PrintStream}
4+
import java.io.{PrintStream, File as JFile}
65
import java.nio.charset.StandardCharsets
7-
86
import dotty.tools.dotc.ast.Trees.*
97
import dotty.tools.dotc.ast.{tpd, untpd}
8+
import dotty.tools.dotc.classpath.{AggregateClassPath, ClassPathFactory, ZipAndJarClassPathFactory}
109
import dotty.tools.dotc.config.CommandLineParser.tokenize
1110
import dotty.tools.dotc.config.Properties.{javaVersion, javaVmName, simpleVersionString}
1211
import dotty.tools.dotc.core.Contexts.*
1312
import dotty.tools.dotc.core.Decorators.*
14-
import dotty.tools.dotc.core.Phases.{unfusedPhases, typerPhase}
13+
import dotty.tools.dotc.core.Phases.{typerPhase, unfusedPhases}
1514
import dotty.tools.dotc.core.Denotations.Denotation
1615
import dotty.tools.dotc.core.Flags.*
1716
import dotty.tools.dotc.core.Mode
@@ -534,24 +533,36 @@ class ReplDriver(settings: Array[String],
534533
}
535534
}
536535

537-
// TODO: no idea how to access and reload the class paths
538-
val newClassPath = state.context.platform.classPath(using state.context).asURLs :+ f.toURI.toURL
539-
println(s"new class path=${newClassPath.mkString(", ")}")
540-
val clsl = rendering.classLoader()(using state.context)
541-
val newClsl = fromURLsParallelCapable(newClassPath, clsl)
542-
println(s"newClsl getResource=${newClsl.getURLs.toList}")
543-
newClsl.asContext(state.context)
544-
545-
// Scala 2:
546-
// def alreadyDefined(clsName: String) = classLoader.tryToLoadClass(clsName).isDefined
547-
// val existingClass = entries.filter(_.ext.isClass).map(classNameOf).find(alreadyDefined)
548-
549-
// if (existingClass.nonEmpty) out.println(s"The path '$f' cannot be loaded, it contains a classfile that already exists on the classpath: ${existingClass.get}")
550-
// else {
551-
// intp.addUrlsToClassPath(f.toURI.toURL)
536+
def alreadyDefined(clsName: String) = state.context.platform.classPath(using state.context).findClassFile(clsName).isDefined
537+
val existingClass = entries.filter(_.ext.isClass).map(classNameOf).find(alreadyDefined)
538+
if (existingClass.nonEmpty)
539+
out.println(s"The path '$f' cannot be loaded, it contains a classfile that already exists on the classpath: ${existingClass.get}")
540+
else
541+
// val cp = state.context.platform.classPath(using state.context).asClassPathString
542+
// println(s"CURRENT CP STRING: $cp")
543+
// val newCP = s"$cp${JFile.pathSeparator}$path"
544+
// println(s"UPDATED CP: $newCP")
545+
546+
// add to compiler class path
547+
println(s"INIT state classPath = ${state.context.platform.classPath(using state.context).asClassPathString}")
548+
val cpCP = ClassPathFactory.newClassPath(jarFile)(using state.context)
549+
state.context.platform.addToClassPath(cpCP)
550+
println(s"classPath after add = ${state.context.platform.classPath(using state.context).asClassPathString}")
551+
552+
// create initial context
553+
rootCtx = setupRootCtx(Array(), rootCtx)
554+
state.copy(context = rootCtx)
555+
556+
557+
// new class loader
558+
// val newClassPath = state.context.platform.classPath(using state.context).asURLs :+ f.toURI.toURL
559+
// val oldCL = rendering.classLoader()(using state.context)
560+
// val newCL = fromURLsParallelCapable(newClassPath, oldCL)
561+
// println(s"new CL class path = ${newCL.getURLs.toList}")
562+
// println(s"\nclass name = ${cpCP.className}")
563+
// rendering.myClassLoader = new AbstractFileClassLoader(state.context.settings.outputDir.default, newCL)
552564
// out.println(s"Added '$path' to classpath.")
553-
// out.println("Added '%s'. Your new classpath is:\n\"%s\"".format(f.path, intp.classPathString))
554-
// }
565+
println(s"after setupRootCtx classPath = ${state.context.platform.classPath(using state.context).asClassPathString}")
555566
state
556567

557568
case KindOf(expr) =>

0 commit comments

Comments
 (0)