From 3d969843d849f0ec1400695cbcbf004952714d96 Mon Sep 17 00:00:00 2001 From: Raphael Jolly Date: Mon, 18 May 2020 11:41:09 +0200 Subject: [PATCH] Provide script engine rendering object with parent class loader The point of this change is to make it possible to cast script engine's output to classes from the calling context, which is currently impossible due to lack of a common parent classloader, see: https://stackoverflow.com/questions/2591779/cast-across-classloader --- compiler/src/dotty/tools/repl/ScriptEngine.scala | 2 +- tests/run-with-compiler/scripting.check | 1 + tests/run-with-compiler/scripting.scala | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/compiler/src/dotty/tools/repl/ScriptEngine.scala b/compiler/src/dotty/tools/repl/ScriptEngine.scala index 7a03fa3eab8e..786279ff5a07 100644 --- a/compiler/src/dotty/tools/repl/ScriptEngine.scala +++ b/compiler/src/dotty/tools/repl/ScriptEngine.scala @@ -24,7 +24,7 @@ class ScriptEngine extends AbstractScriptEngine { "-color:never", "-Xrepl-disable-display" ), Console.out, None) - private val rendering = new Rendering + private val rendering = new Rendering(Some(getClass.getClassLoader)) private var state: State = driver.initialState def getFactory: ScriptEngineFactory = new ScriptEngine.Factory diff --git a/tests/run-with-compiler/scripting.check b/tests/run-with-compiler/scripting.check index d81cc0710eb6..daaac9e30302 100644 --- a/tests/run-with-compiler/scripting.check +++ b/tests/run-with-compiler/scripting.check @@ -1 +1,2 @@ 42 +42 diff --git a/tests/run-with-compiler/scripting.scala b/tests/run-with-compiler/scripting.scala index 4b5807b29385..27d13cba601c 100644 --- a/tests/run-with-compiler/scripting.scala +++ b/tests/run-with-compiler/scripting.scala @@ -3,5 +3,6 @@ object Test { val m = new javax.script.ScriptEngineManager(getClass().getClassLoader()) val e = m.getEngineByName("scala") println(e.eval("42")) + println(e.eval("Some(42)").asInstanceOf[Option[Int]].get) } }