Permalink
Browse files

The script engine is given a better binding mechanism and reflexive a…

…ccess

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.
  • Loading branch information...
1 parent 6ec6f69 commit 6c48941f0961a17647a6c95022d13fef3ab1f956 @rjolly rjolly committed Mar 22, 2013
View
@@ -1103,6 +1103,13 @@ QUICK BUILD (QUICK)
<target name="quick.repl" depends="quick.pre-repl" unless="quick.repl.available">
<mkdir dir="${build-quick.dir}/classes/repl"/>
+ <javac
+ srcdir="${src.dir}/repl"
+ destdir="${build-quick.dir}/classes/repl"
+ includes="**/*.java"
+ target="1.6" source="1.5">
+ <compilerarg line="${javac.args} -XDignore.symbol.file"/>
+ </javac>
<scalacfork
destdir="${build-quick.dir}/classes/repl"
compilerpathref="quick.classpath"
@@ -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;
+ }
+ };
+ }
+}
@@ -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)

0 comments on commit 6c48941

Please sign in to comment.