summaryrefslogtreecommitdiff
path: root/src/repl/scala/tools/nsc/interpreter/IMain.scala
diff options
context:
space:
mode:
authorRaphael Jolly <rjolly@users.sourceforge.net>2014-01-30 13:29:02 +0100
committerRaphael Jolly <rjolly@users.sourceforge.net>2014-01-31 10:06:36 +0100
commit9dfac45e808017982e04fb20567c70b76dee3ede (patch)
tree4be4bf4202d87973b8f852e7c45a811720ff5923 /src/repl/scala/tools/nsc/interpreter/IMain.scala
parent8f6f4032b5c026fd9301cebe28dde5bb7c8e264c (diff)
downloadscala-9dfac45e808017982e04fb20567c70b76dee3ede.tar.gz
scala-9dfac45e808017982e04fb20567c70b76dee3ede.tar.bz2
scala-9dfac45e808017982e04fb20567c70b76dee3ede.zip
SI-7933 REPL javax.script eval is cached result
The problem is that the repl underneath the script engine evaluates input to val res0..resN, so it is a one shot operation. To allow repetition, compile(script) now returns a CompiledScript object whose eval method can be called any number of times.
Diffstat (limited to 'src/repl/scala/tools/nsc/interpreter/IMain.scala')
-rw-r--r--src/repl/scala/tools/nsc/interpreter/IMain.scala27
1 files changed, 10 insertions, 17 deletions
diff --git a/src/repl/scala/tools/nsc/interpreter/IMain.scala b/src/repl/scala/tools/nsc/interpreter/IMain.scala
index 8fba1e538e..d261fc5be9 100644
--- a/src/repl/scala/tools/nsc/interpreter/IMain.scala
+++ b/src/repl/scala/tools/nsc/interpreter/IMain.scala
@@ -18,7 +18,7 @@ import scala.reflect.internal.util.{ BatchSourceFile, SourceFile }
import scala.tools.util.PathResolver
import scala.tools.nsc.io.AbstractFile
import scala.tools.nsc.typechecker.{ TypeStrings, StructuredTypeStrings }
-import scala.tools.nsc.util.{ ScalaClassLoader, stringFromWriter, StackTraceOps }
+import scala.tools.nsc.util.{ ScalaClassLoader, stringFromReader, stringFromWriter, StackTraceOps }
import scala.tools.nsc.util.Exceptional.unwrap
import javax.script.{AbstractScriptEngine, Bindings, ScriptContext, ScriptEngine, ScriptEngineFactory, ScriptException, CompiledScript, Compilable}
@@ -534,8 +534,7 @@ class IMain(@BeanProperty val factory: ScriptEngineFactory, initialSettings: Set
var code = ""
var bound = false
- @throws[ScriptException]
- def compile(script: String): CompiledScript = {
+ def compiled(script: String): CompiledScript = {
if (!bound) {
quietBind("engine" -> this.asInstanceOf[ScriptEngine])
bound = true
@@ -562,18 +561,6 @@ class IMain(@BeanProperty val factory: ScriptEngineFactory, initialSettings: Set
}
}
- @throws[ScriptException]
- def compile(reader: java.io.Reader): CompiledScript = {
- val writer = new java.io.StringWriter()
- var c = reader.read()
- while(c != -1) {
- writer.write(c)
- c = reader.read()
- }
- reader.close()
- compile(writer.toString())
- }
-
private class WrappedRequest(val req: Request) extends CompiledScript {
var recorded = false
@@ -1014,10 +1001,16 @@ class IMain(@BeanProperty val factory: ScriptEngineFactory, initialSettings: Set
}
@throws[ScriptException]
- def eval(script: String, context: ScriptContext): Object = compile(script).eval(context)
+ def compile(script: String): CompiledScript = eval("new javax.script.CompiledScript { def eval(context: javax.script.ScriptContext): Object = { " + script + " }.asInstanceOf[Object]; def getEngine: javax.script.ScriptEngine = engine }").asInstanceOf[CompiledScript]
+
+ @throws[ScriptException]
+ def compile(reader: java.io.Reader): CompiledScript = compile(stringFromReader(reader))
+
+ @throws[ScriptException]
+ def eval(script: String, context: ScriptContext): Object = compiled(script).eval(context)
@throws[ScriptException]
- def eval(reader: java.io.Reader, context: ScriptContext): Object = compile(reader).eval(context)
+ def eval(reader: java.io.Reader, context: ScriptContext): Object = eval(stringFromReader(reader), context)
override def finalize = close