diff options
author | Raphael Jolly <rjolly@users.sourceforge.net> | 2013-03-22 20:19:17 +0100 |
---|---|---|
committer | Raphael Jolly <rjolly@users.sourceforge.net> | 2013-03-22 20:19:17 +0100 |
commit | 6c48941f0961a17647a6c95022d13fef3ab1f956 (patch) | |
tree | e8f82aa4cd0daa010b10e42810d8b8dc957dd693 /src/repl/scala/tools/nsc/interpreter/IMain.scala | |
parent | 6ec6f69be2863056c1f10c56406e5a72f2e184cb (diff) | |
download | scala-6c48941f0961a17647a6c95022d13fef3ab1f956.tar.gz scala-6c48941f0961a17647a6c95022d13fef3ab1f956.tar.bz2 scala-6c48941f0961a17647a6c95022d13fef3ab1f956.zip |
The script engine is given a better binding mechanism and reflexive access
Better binding mecanism : formerly done through the default SimpleBindings
shipped with the API, it now goes through a custom IBindings class
which uses the bind method of the interpreter instead of simply
making the bindings available as a Map.
Reflexive access : the script engine is made available to itself
through a bound variable "engine" of type javax.script.ScriptEngine.
This will allow "variable injection" i.e. programmatic redefinition
of variables, among others.
Diffstat (limited to 'src/repl/scala/tools/nsc/interpreter/IMain.scala')
-rw-r--r-- | src/repl/scala/tools/nsc/interpreter/IMain.scala | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/src/repl/scala/tools/nsc/interpreter/IMain.scala b/src/repl/scala/tools/nsc/interpreter/IMain.scala index c92777c13e..d2b6cdd7f0 100644 --- a/src/repl/scala/tools/nsc/interpreter/IMain.scala +++ b/src/repl/scala/tools/nsc/interpreter/IMain.scala @@ -21,7 +21,7 @@ import scala.tools.nsc.util.Exceptional.unwrap import scala.collection.{ mutable, immutable } import scala.reflect.BeanProperty import scala.util.Properties.versionString -import javax.script.{AbstractScriptEngine, Bindings, ScriptContext, ScriptEngine, ScriptEngineFactory, ScriptException, SimpleBindings, CompiledScript, Compilable} +import javax.script.{AbstractScriptEngine, Bindings, ScriptContext, ScriptEngine, ScriptEngineFactory, ScriptException, CompiledScript, Compilable} import java.io.{ StringWriter, Reader } import java.util.Arrays import IMain._ @@ -62,9 +62,10 @@ import StdReplTags._ * @author Moez A. Abdel-Gawad * @author Lex Spoon */ -class IMain(@BeanProperty val factory: ScriptEngineFactory, initialSettings: Settings, protected val out: JPrintWriter) extends AbstractScriptEngine(new SimpleBindings) with Compilable with Imports { +class IMain(@BeanProperty val factory: ScriptEngineFactory, initialSettings: Settings, protected val out: JPrintWriter) extends AbstractScriptEngine with Compilable with Imports { imain => + setBindings(createBindings, ScriptContext.ENGINE_SCOPE) object replOutput extends ReplOutput(settings.Yreploutdir) { } @deprecated("Use replOutput.dir instead", "2.11.0") @@ -554,7 +555,7 @@ class IMain(@BeanProperty val factory: ScriptEngineFactory, initialSettings: Set @throws(classOf[ScriptException]) def compile(script: String): CompiledScript = { if (!bound) { - quietBind("bindings", getBindings(ScriptContext.ENGINE_SCOPE)) + quietBind("engine", this.asInstanceOf[ScriptEngine]) bound = true } val cat = code + script @@ -969,7 +970,18 @@ class IMain(@BeanProperty val factory: ScriptEngineFactory, initialSettings: Set override def toString = "Request(line=%s, %s trees)".format(line, trees.size) } - def createBindings: Bindings = new SimpleBindings + def createBindings: Bindings = new IBindings { + override def put(name: String, value: Object): Object = { + val n = name.indexOf(":") + val p: NamedParam = if (n < 0) (name, value) else { + val nme = name.substring(0, n).trim + val tpe = name.substring(n + 1).trim + NamedParamClass(nme, tpe, value) + } + if (!p.name.startsWith("javax.script")) bind(p) + null + } + } @throws(classOf[ScriptException]) def eval(script: String, context: ScriptContext): Object = compile(script).eval(context) |