Skip to content

Commit a1b96bf

Browse files
committed
WIP
1 parent 55ca222 commit a1b96bf

File tree

6 files changed

+55
-6
lines changed

6 files changed

+55
-6
lines changed

compiler/src/dotty/tools/dotc/quoted/QuoteCompiler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class QuoteCompiler extends Compiler {
3737
new ExprRun(this, ctx.addMode(Mode.ReadPositions))
3838
}
3939

40-
def outputClassName: TypeName = "Quoted".toTypeName
40+
def outputClassName: TypeName = "Quoted$Code".toTypeName
4141

4242
/** Frontend that receives a scala.quoted.Expr or scala.quoted.Type as input */
4343
class QuotedFrontend(putInClass: Boolean) extends Phase {

compiler/src/dotty/tools/dotc/quoted/QuoteDriver.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import scala.quoted.{Expr, Type}
1111
import scala.quoted.Toolbox
1212
import java.net.URLClassLoader
1313

14-
class QuoteDriver extends Driver {
14+
class QuoteDriver(cl: ClassLoader) extends Driver {
1515
import tpd._
1616

1717
private[this] val contextBase: ContextBase = new ContextBase
@@ -26,12 +26,17 @@ class QuoteDriver extends Driver {
2626
new VirtualDirectory("<quote compilation output>")
2727
}
2828

29+
println("ToolboxImpl")
30+
settings.compilerArgs.foreach(println)
31+
2932
val (_, ctx0: Context) = setup(settings.compilerArgs.toArray :+ "dummy.scala", initCtx.fresh)
3033
val ctx = setToolboxSettings(ctx0.fresh.setSetting(ctx0.settings.outputDir, outDir), settings)
3134

3235
val driver = new QuoteCompiler
3336
driver.newRun(ctx).compileExpr(expr)
3437

38+
assert(!ctx.reporter.hasErrors)
39+
3540
val classLoader = new AbstractFileClassLoader(outDir, this.getClass.getClassLoader)
3641

3742
val clazz = classLoader.loadClass(driver.outputClassName.toString)

compiler/src/dotty/tools/dotc/quoted/ToolboxImpl.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,21 @@ object ToolboxImpl {
2626

2727
def show[T](tpe: Type[T]): String = synchronized(driver.show(tpe, settings))
2828
}
29+
30+
def make2(settings: scala.quoted.Toolbox.Settings): scala.quoted.Toolbox = new scala.quoted.Toolbox {
31+
32+
private[this] val driver: QuoteDriver = new QuoteDriver()
33+
34+
def run[T](expr: Expr[T]): T = expr match {
35+
case expr: LiftedExpr[T] =>
36+
expr.value
37+
case expr: TastyTreeExpr[Tree] @unchecked =>
38+
throw new Exception("Cannot call `Expr.run` on an `Expr` that comes from a macro argument.")
39+
case _ =>
40+
synchronized(driver.run(expr, settings))
41+
}
42+
43+
def show[T](expr: Expr[T]): String = synchronized(driver.show(expr, settings))
44+
45+
}
2946
}

library/src/scala/quoted/Toolbox.scala

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,25 @@ object Toolbox {
1616
implicit def make(implicit settings: Settings): Toolbox = Toolbox.make
1717
}
1818

19+
def make2(cl: ClassLoader)(implicit settings: Settings): Toolbox = {
20+
try {
21+
val toolboxImplCls = cl.loadClass("dotty.tools.dotc.quoted.ToolboxImpl")
22+
val makeMeth = toolboxImplCls.getMethod("make2", classOf[Settings])
23+
makeMeth.invoke(null, settings, cl).asInstanceOf[Toolbox]
24+
}
25+
catch {
26+
case ex: ClassNotFoundException =>
27+
throw new ToolboxNotFoundException(
28+
s"""Could not load the Toolbox class `${ex.getMessage}` from the JVM classpath. Make sure that the compiler is on the JVM classpath.""",
29+
ex
30+
)
31+
}
32+
}
33+
1934
def make(implicit settings: Settings): Toolbox = {
2035
val cl = getClass.getClassLoader
21-
println(">>>>>")
36+
println("Toolbox CL")
37+
println(cl.asInstanceOf[java.net.URLClassLoader])
2238
cl.asInstanceOf[java.net.URLClassLoader].getURLs().toList.foreach(println)
2339

2440
try {

sbt-dotty/sbt-test/sbt-dotty/quoted-example-project/src/main/scala/hello/Hello.scala

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ import scala.quoted._
66
object Main {
77

88
// Needed to run or show quotes
9-
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make
9+
val cl = getClass.getClassLoader
10+
println("BEFORE")
11+
println(cl.asInstanceOf[java.net.URLClassLoader])
12+
cl.asInstanceOf[java.net.URLClassLoader].getURLs().toList.foreach(println)
13+
14+
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make2(cl)
1015

1116
def main(args: Array[String]): Unit = {
1217
val square = stagedPower(2)
@@ -27,7 +32,7 @@ object Main {
2732
val code = '{ (x: Double) => ${powerCode(n, '{x})}}
2833

2934
println(s"staged power for n=" + n + ":")
30-
println(code.show)
35+
// println(code.show)
3136

3237
// Evaluate the contents of the code and return it's value
3338
code.run

sbt-dotty/src/dotty/tools/sbtplugin/DottyPlugin.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,14 @@ object DottyPlugin extends AutoPlugin {
391391
def makeScalaInstance(
392392
state: State, dottyVersion: String, scalaLibrary: File, dottyLibrary: File, compiler: File, all: Seq[File]
393393
): ScalaInstance = {
394+
println("all")
395+
all.foreach(println)
394396
val loader = state.classLoaderCache(all.toList)
395-
val loaderLibraryOnly = state.classLoaderCache(List(dottyLibrary, scalaLibrary))
397+
println("DottyPlugin: " + loader)
398+
val dottyInterfaces = all.find(a => a.toString.contains("dotty-interfaces")).get
399+
val loaderLibraryOnly = state.classLoaderCache(List(compiler, dottyInterfaces, dottyLibrary, scalaLibrary))
400+
println("DottyPlugin lib: " + loaderLibraryOnly)
401+
396402
new ScalaInstance(
397403
dottyVersion,
398404
loader,

0 commit comments

Comments
 (0)