From 3a255fbf0f673fdaebe5cf04d4a78aa4d276372a Mon Sep 17 00:00:00 2001 From: Raphael Jolly Date: Thu, 21 May 2020 22:46:10 +0200 Subject: [PATCH] Script engine : implement eval(reader) --- .../src/dotty/tools/repl/ScriptEngine.scala | 17 +++++++++++++++-- tests/run-with-compiler/scripting.check | 1 + tests/run-with-compiler/scripting.scala | 1 + 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/compiler/src/dotty/tools/repl/ScriptEngine.scala b/compiler/src/dotty/tools/repl/ScriptEngine.scala index 786279ff5a07..ac10be7f4574 100644 --- a/compiler/src/dotty/tools/repl/ScriptEngine.scala +++ b/compiler/src/dotty/tools/repl/ScriptEngine.scala @@ -1,7 +1,7 @@ package dotty.tools package repl -import java.io.Reader +import java.io.{Reader, StringWriter} import javax.script.{AbstractScriptEngine, Bindings, ScriptContext, ScriptEngine => JScriptEngine, ScriptEngineFactory, ScriptException, SimpleBindings} import dotc.core.StdNames.str @@ -44,7 +44,20 @@ class ScriptEngine extends AbstractScriptEngine { } @throws[ScriptException] - def eval(reader: Reader, context: ScriptContext): Object = throw new UnsupportedOperationException + def eval(reader: Reader, context: ScriptContext): Object = eval(stringFromReader(reader), context) + + private val buffer = new Array[Char](8192) + + def stringFromReader(in: Reader) = { + val out = new StringWriter + var n = in.read(buffer) + while (n > -1) { + out.write(buffer, 0, n) + n = in.read(buffer) + } + in.close + out.toString + } } object ScriptEngine { diff --git a/tests/run-with-compiler/scripting.check b/tests/run-with-compiler/scripting.check index daaac9e30302..bda709ecfb47 100644 --- a/tests/run-with-compiler/scripting.check +++ b/tests/run-with-compiler/scripting.check @@ -1,2 +1,3 @@ 42 42 +42 diff --git a/tests/run-with-compiler/scripting.scala b/tests/run-with-compiler/scripting.scala index 27d13cba601c..b42fd714e05a 100644 --- a/tests/run-with-compiler/scripting.scala +++ b/tests/run-with-compiler/scripting.scala @@ -4,5 +4,6 @@ object Test { val e = m.getEngineByName("scala") println(e.eval("42")) println(e.eval("Some(42)").asInstanceOf[Option[Int]].get) + println(e.eval(new java.io.StringReader("42"))) } }