Pass user-supplied ScriptContext to script engine evaluation (fixes issue #21).

This commit is contained in:
James Roseborough
2014-03-09 16:45:57 +00:00
parent 7b97573ac8
commit 736218d1f2
3 changed files with 55 additions and 2 deletions

View File

@@ -920,6 +920,7 @@ Files are no longer hosted at LuaForge.
<li>Add sample code for Android Application that uses luaj.</li>
<li>Add sample code for Applet that uses luaj.</li>
<li>Fix balanced match for empty string (fixes issue #23).</li>
<li>Pass user-supplied ScriptContext to script engine evaluation (fixes issue #21).</li>
</ul></td></tr>
</table></td></tr></table>

View File

@@ -95,6 +95,21 @@ public class LuaScriptEngine extends AbstractScriptEngine implements ScriptEngin
}
}
@Override
public Object eval(Reader reader, Bindings bindings) throws ScriptException {
return ((LuajCompiledScript) compile(reader)).eval(context.globals, bindings);
}
@Override
public Object eval(String script, Bindings bindings) throws ScriptException {
return eval(new StringReader(script), bindings);
}
@Override
protected ScriptContext getScriptContext(Bindings nn) {
throw new IllegalStateException("LuajScriptEngine should not be allocating contexts.");
}
@Override
public Bindings createBindings() {
return new SimpleBindings();
@@ -109,7 +124,7 @@ public class LuaScriptEngine extends AbstractScriptEngine implements ScriptEngin
@Override
public Object eval(Reader reader, ScriptContext context)
throws ScriptException {
return compile(reader).eval();
return compile(reader).eval(context);
}
@Override
@@ -142,7 +157,7 @@ public class LuaScriptEngine extends AbstractScriptEngine implements ScriptEngin
return eval(((LuajContext) context).globals, context.getBindings(ScriptContext.ENGINE_SCOPE));
}
private Object eval(Globals g, Bindings b) throws ScriptException {
Object eval(Globals g, Bindings b) throws ScriptException {
g.setmetatable(new BindingsMetatable(b));
LuaFunction f = function;
if (f.isclosure())

View File

@@ -28,6 +28,7 @@ import java.io.Reader;
import javax.script.Bindings;
import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
@@ -49,6 +50,7 @@ public class ScriptEngineTests extends TestSuite {
suite.addTest( new TestSuite( SimpleBindingsTest.class, "Simple Bindings" ) );
suite.addTest( new TestSuite( DefaultBindingsTest.class, "Default Bindings" ) );
suite.addTest( new TestSuite( LuaJCBindingsTest.class, "LuaJC Bindings" ) );
suite.addTest( new TestSuite( UserContextTest.class, "User Context" ) );
return suite;
}
@@ -245,4 +247,39 @@ public class ScriptEngineTests extends TestSuite {
return "some-user-value";
}
}
public static class UserContextTest extends TestCase {
protected ScriptEngine e;
protected Bindings b;
protected ScriptContext c;
public void setUp() {
this.e = new ScriptEngineManager().getEngineByName("luaj");
this.c = new LuajContext();
this.b = c.getBindings(ScriptContext.ENGINE_SCOPE);
}
public void testUncompiledScript() throws ScriptException {
b.put("x", 144);
assertEquals(12, e.eval("z = math.sqrt(x); return z", b));
assertEquals(12, b.get("z"));
assertEquals(null, e.getBindings(ScriptContext.ENGINE_SCOPE).get("z"));
assertEquals(null, e.getBindings(ScriptContext.GLOBAL_SCOPE).get("z"));
b.put("x", 25);
assertEquals(5, e.eval("z = math.sqrt(x); return z", c));
assertEquals(5, b.get("z"));
assertEquals(null, e.getBindings(ScriptContext.ENGINE_SCOPE).get("z"));
assertEquals(null, e.getBindings(ScriptContext.GLOBAL_SCOPE).get("z"));
}
public void testCompiledScript() throws ScriptException {
CompiledScript cs = ((Compilable)e).compile("z = math.sqrt(x); return z");
b.put("x", 144);
assertEquals(12, cs.eval(b));
assertEquals(12, b.get("z"));
b.put("x", 25);
assertEquals(5, cs.eval(c));
assertEquals(5, b.get("z"));
}
}
}