Skip to content

Commit dfe3caa

Browse files
committed
Add a way to know thew classloader of the class in the callsite
1 parent 07c1791 commit dfe3caa

File tree

2 files changed

+13
-3
lines changed
  • library/src/scala/quoted
  • sbt-dotty/sbt-test/sbt-dotty/quoted-example-project/src/main/scala/hello

2 files changed

+13
-3
lines changed

library/src/scala/quoted/Toolbox.scala

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

19-
def make2(obj: Any)(implicit settings: Settings): Toolbox = {
19+
@forceInline def make4(implicit settings: Settings): Toolbox = {
20+
// TODO find the name of the class that needs to be loaded. Maybe from a stack trace.
21+
22+
// We force inline to make forName use the classloader of the class at callsite
23+
val cl = Class.forName("hello.Main").getClassLoader
24+
make2(cl)
25+
}
26+
27+
def make2(cl: ClassLoader)(implicit settings: Settings): Toolbox = {
2028
try {
21-
val cl = obj.getClass().getClassLoader()
2229
val toolboxImplCls = cl.loadClass("dotty.tools.dotc.quoted.ToolboxImpl")
2330
val makeMeth = toolboxImplCls.getMethod("make2", classOf[Settings], classOf[ClassLoader])
2431
makeMeth.invoke(null, settings, cl).asInstanceOf[Toolbox]

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import scala.quoted._
55

66
object Main {
77

8-
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make2(this)
8+
// val cl = Class.forName("hello.Main").getClassLoader
9+
// implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make2(cl)
10+
11+
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make4
912

1013
def main(args: Array[String]): Unit = {
1114
val square = stagedPower(2)

0 commit comments

Comments
 (0)