Skip to content

Commit cbf7f8f

Browse files
committed
Add updateClassPath + reset rootctx
1 parent cd3f017 commit cbf7f8f

File tree

3 files changed

+39
-21
lines changed

3 files changed

+39
-21
lines changed

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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 32 additions & 21 deletions
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
@@ -529,24 +528,36 @@ class ReplDriver(settings: Array[String],
529528
}
530529
}
531530

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

552563
case TypeOf(expr) =>

0 commit comments

Comments
 (0)