Skip to content

Script engine : implement eval(reader) #9021

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions compiler/src/dotty/tools/repl/ScriptEngine.scala
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions tests/run-with-compiler/scripting.check
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
42
42
42
1 change: 1 addition & 0 deletions tests/run-with-compiler/scripting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
}
}