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 1 commit
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
13 changes: 11 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ import dotty.tools.dotc.core.quoted._
class ReifyQuotes extends MacroTransformWithImplicits with InfoTransformer {
import ast.tpd._

private lazy val splicer: Splicer = new Splicer
/** Classloader used for loading macros */
private var macroClassLoader: java.lang.ClassLoader = _
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private[this] also usually we put the field and the getter next to each other, and we call the field myXXX and the getter XXX.


override def phaseName: String = "reifyQuotes"

Expand Down Expand Up @@ -544,7 +545,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, getMacroClassLoader).withPos(tree.pos)
transform(cpy.Inlined(tree)(call2, bindings, spliced))
}
else super.transform(tree)
Expand Down Expand Up @@ -622,6 +623,14 @@ class ReifyQuotes extends MacroTransformWithImplicits with InfoTransformer {
transform(tp)
}

private def getMacroClassLoader(implicit ctx: Context): ClassLoader = {
if (macroClassLoader == null) {
val urls = ctx.settings.classpath.value.split(':').map(cp => java.nio.file.Paths.get(cp).toUri.toURL)
macroClassLoader = new java.net.URLClassLoader(urls, getClass.getClassLoader)
}
macroClassLoader
}

override protected def mayChange(sym: Symbol)(implicit ctx: Context): Boolean = sym.is(Macro)

/** Returns the type of the compiled macro as a lambda: Seq[Any] => Object */
Expand Down
26 changes: 8 additions & 18 deletions compiler/src/dotty/tools/dotc/transform/Splicer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,22 @@ import dotty.tools.dotc.util.Positions.Position
import scala.reflect.ClassTag

/** Utility class to splice quoted expressions */
class Splicer {
object Splicer {
import tpd._

private var classLoader: URLClassLoader = _

/** Splice the Tree for a Quoted expression. `~'(xyz)` becomes `xyz`
* and for `~xyz` the tree of `xyz` is interpreted for which the
* resulting expression is returned as a `Tree`
*
* 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 @@ -100,12 +95,7 @@ class 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) {

if (classLoader == null) {
val urls = ctx.settings.classpath.value.split(':').map(cp => java.nio.file.Paths.get(cp).toUri.toURL)
classLoader = 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