Skip to content

Cache classloaders in Splicer #4235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ import dotty.tools.dotc.core.quoted._
class ReifyQuotes extends MacroTransformWithImplicits with InfoTransformer {
import ast.tpd._

/** Classloader used for loading macros */
private[this] var myMacroClassLoader: java.lang.ClassLoader = _
private def macroClassLoader(implicit ctx: Context): ClassLoader = {
if (myMacroClassLoader == null) {
val urls = ctx.settings.classpath.value.split(':').map(cp => java.nio.file.Paths.get(cp).toUri.toURL)
myMacroClassLoader = new java.net.URLClassLoader(urls, getClass.getClassLoader)
}
myMacroClassLoader
}

override def phaseName: String = "reifyQuotes"

override def run(implicit ctx: Context): Unit =
Expand Down Expand Up @@ -542,7 +552,7 @@ class ReifyQuotes extends MacroTransformWithImplicits with InfoTransformer {
// Simplification of the call done in PostTyper for non-macros can also be performed now
// see PostTyper `case Inlined(...) =>` for description of the simplification
val call2 = Ident(call.symbol.topLevelClass.typeRef).withPos(call.pos)
val spliced = Splicer.splice(body, call, bindings, tree.pos).withPos(tree.pos)
val spliced = Splicer.splice(body, call, bindings, tree.pos, macroClassLoader).withPos(tree.pos)
transform(cpy.Inlined(tree)(call2, bindings, spliced))
}
else super.transform(tree)
Expand Down
22 changes: 7 additions & 15 deletions compiler/src/dotty/tools/dotc/transform/Splicer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,13 @@ object Splicer {
*
* See: `ReifyQuotes`
*/
def splice(tree: Tree, call: Tree, bindings: List[Tree], pos: Position)(implicit ctx: Context): Tree = tree match {
def splice(tree: Tree, call: Tree, bindings: List[Tree], pos: Position, classLoader: ClassLoader)(implicit ctx: Context): Tree = tree match {
case Quoted(quotedTree) => quotedTree
case _ => reflectiveSplice(tree, call, bindings, pos)
}

private def reflectiveSplice(tree: Tree, call: Tree, bindings: List[Tree], pos: Position)(implicit ctx: Context): Tree = {
val liftedArgs = getLiftedArgs(call, bindings)
val interpreter = new Interpreter(pos)
val interpreted = interpreter.interpretCallToSymbol[Seq[Any] => Object](call.symbol)
interpreted.flatMap(lambda => evaluateLambda(lambda, liftedArgs, pos)).fold(tree)(PickledQuotes.quotedExprToTree)
case _ =>
val liftedArgs = getLiftedArgs(call, bindings)
val interpreter = new Interpreter(pos, classLoader)
val interpreted = interpreter.interpretCallToSymbol[Seq[Any] => Object](call.symbol)
interpreted.flatMap(lambda => evaluateLambda(lambda, liftedArgs, pos)).fold(tree)(PickledQuotes.quotedExprToTree)
}

/** Given the inline code and bindings, compute the lifted arguments that will be used to execute the macro
Expand Down Expand Up @@ -98,12 +95,7 @@ object Splicer {
* The interpreter assumes that all calls in the trees are to code that was
* previously compiled and is present in the classpath of the current context.
*/
private class Interpreter(pos: Position)(implicit ctx: Context) {

private[this] val classLoader = {
val urls = ctx.settings.classpath.value.split(':').map(cp => java.nio.file.Paths.get(cp).toUri.toURL)
new URLClassLoader(urls, getClass.getClassLoader)
}
private class Interpreter(pos: Position, classLoader: ClassLoader)(implicit ctx: Context) {

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