Skip to content

Commit 6c21f96

Browse files
committed
Use ScalaRunTime.stringOf in REPL
1 parent 6e175cf commit 6c21f96

File tree

1 file changed

+7
-14
lines changed

1 file changed

+7
-14
lines changed

compiler/src/dotty/tools/repl/Rendering.scala

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,25 +58,17 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None) {
5858
// `ScalaRunTime.replStringOf`. Probe for new API without extraneous newlines.
5959
// For old API, try to clean up extraneous newlines by stripping suffix and maybe prefix newline.
6060
val scalaRuntime = Class.forName("scala.runtime.ScalaRunTime", true, myClassLoader)
61+
val renderer = "stringOf" // was: replStringOf
6162
try {
62-
val meth = scalaRuntime.getMethod("replStringOf", classOf[Object], classOf[Int], classOf[Boolean])
63+
val meth = scalaRuntime.getMethod(renderer, classOf[Object], classOf[Int], classOf[Boolean])
6364
val truly = java.lang.Boolean.TRUE
6465

6566
(value: Object) => meth.invoke(null, value, Integer.valueOf(MaxStringElements), truly).asInstanceOf[String]
6667
} catch {
6768
case _: NoSuchMethodException =>
68-
val meth = scalaRuntime.getMethod("replStringOf", classOf[Object], classOf[Int])
69-
70-
(value: Object) => {
71-
val res = meth.invoke(null, value, Integer.valueOf(MaxStringElements)).asInstanceOf[String]
72-
val len = res.length()
73-
if len == 0 || res.charAt(len-1) != '\n' then
74-
res
75-
else if len == 1 || res.charAt(0) != '\n' then
76-
res.substring(0, len-1)
77-
else
78-
res.substring(1, len-1)
79-
}
69+
val meth = scalaRuntime.getMethod(renderer, classOf[Object], classOf[Int])
70+
71+
(value: Object) => meth.invoke(null, value, Integer.valueOf(MaxStringElements)).asInstanceOf[String]
8072
}
8173
}
8274
myClassLoader
@@ -99,7 +91,8 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None) {
9991
private[repl] def replStringOf(value: Object)(using Context): String = {
10092
assert(myReplStringOf != null,
10193
"replStringOf should only be called on values creating using `classLoader()`, but `classLoader()` has not been called so far")
102-
truncate(myReplStringOf(value))
94+
val res = myReplStringOf(value)
95+
if res == null then "null // non-null reference has null-valued toString" else truncate(res)
10396
}
10497

10598
/** Load the value of the symbol using reflection.

0 commit comments

Comments
 (0)