Skip to content

Commit df84901

Browse files
committed
Add loader support for dotty bridge
1 parent a00a972 commit df84901

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

bridge/src/main/scala/xsbt/ConsoleInterface.scala

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import scala.tools.nsc.interpreter.InteractiveReader
99
import scala.tools.nsc.reporters.Reporter
1010
import scala.tools.nsc.util.ClassPath
1111

12+
import dotty.tools.dotc.core.Contexts.Context
1213
import dotty.tools.dotc.repl.REPL
1314
import dotty.tools.dotc.repl.REPL.Config
1415

@@ -39,7 +40,8 @@ class ConsoleInterface {
3940
val repl = ConsoleInterface.customRepl(
4041
initialCommands :: Nil,
4142
cleanupCommands :: Nil,
42-
bindNames zip bindValues
43+
bindNames zip bindValues,
44+
loader
4345
)
4446
repl.process(completeArgs)
4547
}
@@ -49,11 +51,13 @@ object ConsoleInterface {
4951
def customConfig(
5052
initCmds: List[String],
5153
cleanupCmds: List[String],
52-
boundVals: Array[(String, Any)]
54+
boundVals: Array[(String, Any)],
55+
loader: ClassLoader
5356
) = new Config {
5457
override val initialCommands: List[String] = initCmds
5558
override val cleanupCommands: List[String] = cleanupCmds
5659
override val boundValues: Array[(String, Any)] = boundVals
60+
override val classLoader: Option[ClassLoader] = Option(loader)
5761
}
5862

5963
def customRepl(cfg: Config): REPL = new REPL {
@@ -63,6 +67,7 @@ object ConsoleInterface {
6367
def customRepl(
6468
initCmds: List[String],
6569
cleanupCmds: List[String],
66-
boundVals: Array[(String, Any)]
67-
): REPL = customRepl(customConfig(initCmds, cleanupCmds, boundVals))
70+
boundVals: Array[(String, Any)],
71+
loader: ClassLoader
72+
): REPL = customRepl(customConfig(initCmds, cleanupCmds, boundVals, loader))
6873
}

src/dotty/tools/dotc/repl/CompilingInterpreter.scala

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ import printing.SyntaxHighlighting
6060
* @param ictx The context to use for initialization of the interpreter,
6161
* needed to access the current classpath.
6262
*/
63-
class CompilingInterpreter(out: PrintWriter, ictx: Context) extends Compiler with Interpreter {
63+
class CompilingInterpreter(
64+
out: PrintWriter,
65+
ictx: Context,
66+
parentClassLoader: Option[ClassLoader]
67+
) extends Compiler with Interpreter {
6468
import ast.untpd._
6569
import CompilingInterpreter._
6670

@@ -136,8 +140,6 @@ class CompilingInterpreter(out: PrintWriter, ictx: Context) extends Compiler wit
136140
/** the compiler's classpath, as URL's */
137141
val compilerClasspath: List[URL] = ictx.platform.classPath(ictx).asURLs
138142

139-
protected def parentClassLoader: ClassLoader = classOf[Interpreter].getClassLoader
140-
141143
/* A single class loader is used for all commands interpreted by this Interpreter.
142144
It would also be possible to create a new class loader for each command
143145
to interpret. The advantages of the current approach are:
@@ -153,8 +155,10 @@ class CompilingInterpreter(out: PrintWriter, ictx: Context) extends Compiler wit
153155
*/
154156
/** class loader used to load compiled code */
155157
val classLoader: ClassLoader = {
156-
val parent = new URLClassLoader(compilerClasspath.toArray, parentClassLoader)
157-
new AbstractFileClassLoader(virtualDirectory, parent)
158+
lazy val parent = new URLClassLoader(compilerClasspath.toArray,
159+
classOf[Interpreter].getClassLoader)
160+
161+
new AbstractFileClassLoader(virtualDirectory, parentClassLoader.getOrElse(parent))
158162
}
159163

160164
// Set the current Java "context" class loader to this interpreter's class loader

src/dotty/tools/dotc/repl/REPL.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ package repl
44

55
import core.Contexts.Context
66
import reporting.Reporter
7-
import java.io.{BufferedReader, File, FileReader, PrintWriter}
7+
import io.{AbstractFile, PlainFile, VirtualDirectory}
8+
import scala.reflect.io.{PlainDirectory, Directory}
9+
import java.io.{BufferedReader, File => JFile, FileReader, PrintWriter}
10+
import java.net.{URL, URLClassLoader}
811

912
/** A compiler which stays resident between runs.
1013
* Usage:
@@ -31,7 +34,7 @@ class REPL extends Driver {
3134
}
3235

3336
override def newCompiler(implicit ctx: Context): Compiler =
34-
new repl.CompilingInterpreter(config.output, ctx)
37+
new repl.CompilingInterpreter(config.output, ctx, config.classLoader)
3538

3639
override def sourcesRequired = false
3740

@@ -80,6 +83,9 @@ object REPL {
8083
*/
8184
val boundValues: Array[(String, Any)] = Array.empty[(String, Any)]
8285

86+
/** To pass a custom ClassLoader to the Dotty REPL, overwride this value */
87+
val classLoader: Option[ClassLoader] = None
88+
8389
/** The default input reader */
8490
def input(in: Interpreter)(implicit ctx: Context): InteractiveReader = {
8591
val emacsShell = System.getProperty("env.emacs", "") != ""

0 commit comments

Comments
 (0)