Skip to content

Commit 2174d04

Browse files
Merge pull request #4235 from dotty-staging/cache-classloader-in-splicer
Cache classloaders in Splicer
2 parents ac8f816 + 5b7c02f commit 2174d04

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ import dotty.tools.dotc.core.quoted._
8888
class ReifyQuotes extends MacroTransformWithImplicits with InfoTransformer {
8989
import ast.tpd._
9090

91+
/** Classloader used for loading macros */
92+
private[this] var myMacroClassLoader: java.lang.ClassLoader = _
93+
private def macroClassLoader(implicit ctx: Context): ClassLoader = {
94+
if (myMacroClassLoader == null) {
95+
val urls = ctx.settings.classpath.value.split(':').map(cp => java.nio.file.Paths.get(cp).toUri.toURL)
96+
myMacroClassLoader = new java.net.URLClassLoader(urls, getClass.getClassLoader)
97+
}
98+
myMacroClassLoader
99+
}
100+
91101
override def phaseName: String = "reifyQuotes"
92102

93103
override def run(implicit ctx: Context): Unit =
@@ -542,7 +552,7 @@ class ReifyQuotes extends MacroTransformWithImplicits with InfoTransformer {
542552
// Simplification of the call done in PostTyper for non-macros can also be performed now
543553
// see PostTyper `case Inlined(...) =>` for description of the simplification
544554
val call2 = Ident(call.symbol.topLevelClass.typeRef).withPos(call.pos)
545-
val spliced = Splicer.splice(body, call, bindings, tree.pos).withPos(tree.pos)
555+
val spliced = Splicer.splice(body, call, bindings, tree.pos, macroClassLoader).withPos(tree.pos)
546556
transform(cpy.Inlined(tree)(call2, bindings, spliced))
547557
}
548558
else super.transform(tree)

compiler/src/dotty/tools/dotc/transform/Splicer.scala

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,13 @@ object Splicer {
3030
*
3131
* See: `ReifyQuotes`
3232
*/
33-
def splice(tree: Tree, call: Tree, bindings: List[Tree], pos: Position)(implicit ctx: Context): Tree = tree match {
33+
def splice(tree: Tree, call: Tree, bindings: List[Tree], pos: Position, classLoader: ClassLoader)(implicit ctx: Context): Tree = tree match {
3434
case Quoted(quotedTree) => quotedTree
35-
case _ => reflectiveSplice(tree, call, bindings, pos)
36-
}
37-
38-
private def reflectiveSplice(tree: Tree, call: Tree, bindings: List[Tree], pos: Position)(implicit ctx: Context): Tree = {
39-
val liftedArgs = getLiftedArgs(call, bindings)
40-
val interpreter = new Interpreter(pos)
41-
val interpreted = interpreter.interpretCallToSymbol[Seq[Any] => Object](call.symbol)
42-
interpreted.flatMap(lambda => evaluateLambda(lambda, liftedArgs, pos)).fold(tree)(PickledQuotes.quotedExprToTree)
35+
case _ =>
36+
val liftedArgs = getLiftedArgs(call, bindings)
37+
val interpreter = new Interpreter(pos, classLoader)
38+
val interpreted = interpreter.interpretCallToSymbol[Seq[Any] => Object](call.symbol)
39+
interpreted.flatMap(lambda => evaluateLambda(lambda, liftedArgs, pos)).fold(tree)(PickledQuotes.quotedExprToTree)
4340
}
4441

4542
/** Given the inline code and bindings, compute the lifted arguments that will be used to execute the macro
@@ -98,12 +95,7 @@ object Splicer {
9895
* The interpreter assumes that all calls in the trees are to code that was
9996
* previously compiled and is present in the classpath of the current context.
10097
*/
101-
private class Interpreter(pos: Position)(implicit ctx: Context) {
102-
103-
private[this] val classLoader = {
104-
val urls = ctx.settings.classpath.value.split(':').map(cp => java.nio.file.Paths.get(cp).toUri.toURL)
105-
new URLClassLoader(urls, getClass.getClassLoader)
106-
}
98+
private class Interpreter(pos: Position, classLoader: ClassLoader)(implicit ctx: Context) {
10799

108100
/** Returns the interpreted result of interpreting the code a call to the symbol with default arguments.
109101
* Return Some of the result or None if some error happen during the interpretation.

0 commit comments

Comments
 (0)