summaryrefslogtreecommitdiff
path: root/src/repl
diff options
context:
space:
mode:
authorRaphael Jolly <rjolly@users.sourceforge.net>2013-03-22 20:19:17 +0100
committerRaphael Jolly <rjolly@users.sourceforge.net>2013-03-22 20:19:17 +0100
commit6c48941f0961a17647a6c95022d13fef3ab1f956 (patch)
treee8f82aa4cd0daa010b10e42810d8b8dc957dd693 /src/repl
parent6ec6f69be2863056c1f10c56406e5a72f2e184cb (diff)
downloadscala-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')
-rw-r--r--src/repl/scala/tools/nsc/interpreter/IBindings.java45
-rw-r--r--src/repl/scala/tools/nsc/interpreter/IMain.scala20
2 files changed, 61 insertions, 4 deletions
diff --git a/src/repl/scala/tools/nsc/interpreter/IBindings.java b/src/repl/scala/tools/nsc/interpreter/IBindings.java
new file mode 100644
index 0000000000..b4cee4b957
--- /dev/null
+++ b/src/repl/scala/tools/nsc/interpreter/IBindings.java
@@ -0,0 +1,45 @@
+/* NSC -- new Scala compiler
+ * Copyright 2005-2013 LAMP/EPFL
+ * @author Raphael Jolly
+ */
+
+package scala.tools.nsc.interpreter;
+
+import java.util.Map;
+import java.util.AbstractMap;
+import java.util.Set;
+import java.util.AbstractSet;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+import javax.script.Bindings;
+
+abstract class IBindings extends AbstractMap<String, Object> implements Bindings {
+ public Set<Map.Entry<String, Object>> entrySet() {
+ return new AbstractSet<Map.Entry<String, Object>>() {
+ public int size() {
+ return 0;
+ }
+
+ public Iterator<Map.Entry<String, Object>> iterator() {
+ return new Iterator<Map.Entry<String, Object>>() {
+ public boolean hasNext() {
+ return false;
+ }
+
+ public Map.Entry<String, Object> next() {
+ throw new NoSuchElementException();
+ }
+
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+ };
+ }
+
+ public boolean add(Map.Entry<String, Object> e) {
+ IBindings.this.put(e.getKey(), e.getValue());
+ return true;
+ }
+ };
+ }
+}
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)