Improve JSR-223 bindings implementation to allow use of external Bindings class such as SimpleBindings
This commit is contained in:
@@ -707,6 +707,7 @@ and LuaForge:
|
|||||||
<tr valign="top"><td> <b>2.0.1</b></td><td><ul>
|
<tr valign="top"><td> <b>2.0.1</b></td><td><ul>
|
||||||
<li>Improve correctness of singleton construction related to static initialization
|
<li>Improve correctness of singleton construction related to static initialization
|
||||||
<li>Fix nan-related error in constant folding logic that was failing on some JVMs
|
<li>Fix nan-related error in constant folding logic that was failing on some JVMs
|
||||||
|
<li>JSR-223 fixes: add META-INF/services entry in jse jar, improve bindings implementation
|
||||||
</ul></td></tr>
|
</ul></td></tr>
|
||||||
<tr valign="top"><td> <b>2.0.2</b></td><td><ul>
|
<tr valign="top"><td> <b>2.0.2</b></td><td><ul>
|
||||||
<li>Not yet released
|
<li>Not yet released
|
||||||
|
|||||||
@@ -25,10 +25,6 @@ import java.io.IOException;
|
|||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import javax.script.Bindings;
|
import javax.script.Bindings;
|
||||||
import javax.script.Compilable;
|
import javax.script.Compilable;
|
||||||
@@ -37,6 +33,7 @@ import javax.script.ScriptContext;
|
|||||||
import javax.script.ScriptEngine;
|
import javax.script.ScriptEngine;
|
||||||
import javax.script.ScriptEngineFactory;
|
import javax.script.ScriptEngineFactory;
|
||||||
import javax.script.ScriptException;
|
import javax.script.ScriptException;
|
||||||
|
import javax.script.SimpleBindings;
|
||||||
import javax.script.SimpleScriptContext;
|
import javax.script.SimpleScriptContext;
|
||||||
|
|
||||||
import org.luaj.vm2.LoadState;
|
import org.luaj.vm2.LoadState;
|
||||||
@@ -44,8 +41,11 @@ import org.luaj.vm2.Lua;
|
|||||||
import org.luaj.vm2.LuaClosure;
|
import org.luaj.vm2.LuaClosure;
|
||||||
import org.luaj.vm2.LuaError;
|
import org.luaj.vm2.LuaError;
|
||||||
import org.luaj.vm2.LuaFunction;
|
import org.luaj.vm2.LuaFunction;
|
||||||
|
import org.luaj.vm2.LuaTable;
|
||||||
import org.luaj.vm2.LuaValue;
|
import org.luaj.vm2.LuaValue;
|
||||||
import org.luaj.vm2.Prototype;
|
import org.luaj.vm2.Prototype;
|
||||||
|
import org.luaj.vm2.lib.ThreeArgFunction;
|
||||||
|
import org.luaj.vm2.lib.TwoArgFunction;
|
||||||
import org.luaj.vm2.lib.jse.JsePlatform;
|
import org.luaj.vm2.lib.jse.JsePlatform;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -121,12 +121,12 @@ public class LuaScriptEngine implements ScriptEngine, Compilable {
|
|||||||
|
|
||||||
public void put(String key, Object value) {
|
public void put(String key, Object value) {
|
||||||
Bindings b = getBindings(ScriptContext.ENGINE_SCOPE);
|
Bindings b = getBindings(ScriptContext.ENGINE_SCOPE);
|
||||||
((LuaBindings)b).put(key, value);
|
b.put(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object get(String key) {
|
public Object get(String key) {
|
||||||
Bindings b = getBindings(ScriptContext.ENGINE_SCOPE);
|
Bindings b = getBindings(ScriptContext.ENGINE_SCOPE);
|
||||||
return ((LuaBindings)b).get(key);
|
return b.get(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Bindings getBindings(int scope) {
|
public Bindings getBindings(int scope) {
|
||||||
@@ -138,7 +138,7 @@ public class LuaScriptEngine implements ScriptEngine, Compilable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Bindings createBindings() {
|
public Bindings createBindings() {
|
||||||
return new LuaBindings( _G );
|
return new SimpleBindings();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ScriptContext getContext() {
|
public ScriptContext getContext() {
|
||||||
@@ -168,8 +168,8 @@ public class LuaScriptEngine implements ScriptEngine, Compilable {
|
|||||||
return new CompiledScript() {
|
return new CompiledScript() {
|
||||||
public Object eval(ScriptContext context) throws ScriptException {
|
public Object eval(ScriptContext context) throws ScriptException {
|
||||||
Bindings b = context.getBindings(ScriptContext.ENGINE_SCOPE);
|
Bindings b = context.getBindings(ScriptContext.ENGINE_SCOPE);
|
||||||
LuaBindings lb = (LuaBindings) b;
|
LuaValue env = newBindingsBackedEnv(b);
|
||||||
LuaClosure c = new LuaClosure( p, lb.env );
|
LuaClosure c = new LuaClosure( p, env );
|
||||||
return c.invoke(LuaValue.NONE);
|
return c.invoke(LuaValue.NONE);
|
||||||
}
|
}
|
||||||
public ScriptEngine getEngine() {
|
public ScriptEngine getEngine() {
|
||||||
@@ -181,14 +181,14 @@ public class LuaScriptEngine implements ScriptEngine, Compilable {
|
|||||||
return new CompiledScript() {
|
return new CompiledScript() {
|
||||||
public Object eval(ScriptContext context) throws ScriptException {
|
public Object eval(ScriptContext context) throws ScriptException {
|
||||||
Bindings b = context.getBindings(ScriptContext.ENGINE_SCOPE);
|
Bindings b = context.getBindings(ScriptContext.ENGINE_SCOPE);
|
||||||
LuaBindings lb = (LuaBindings) b;
|
LuaValue env = newBindingsBackedEnv(b);
|
||||||
LuaFunction lf;
|
LuaFunction lf;
|
||||||
try {
|
try {
|
||||||
lf = (LuaFunction) c.newInstance();
|
lf = (LuaFunction) c.newInstance();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new ScriptException("instantiation failed: "+e.toString());
|
throw new ScriptException("instantiation failed: "+e.toString());
|
||||||
}
|
}
|
||||||
lf.setfenv(lb.env);
|
lf.setfenv(env);
|
||||||
return lf.invoke(LuaValue.NONE);
|
return lf.invoke(LuaValue.NONE);
|
||||||
}
|
}
|
||||||
public ScriptEngine getEngine() {
|
public ScriptEngine getEngine() {
|
||||||
@@ -206,30 +206,59 @@ public class LuaScriptEngine implements ScriptEngine, Compilable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------ lua bindings -----
|
// ------ lua bindings ----- private static final class BindingsBackedEnv extends LuaTable {
|
||||||
private static final class LuaBindings implements Bindings {
|
public LuaTable newBindingsBackedEnv(Bindings b) {
|
||||||
private LuaValue env;
|
LuaTable t = new LuaTable();
|
||||||
private LuaValue mt;
|
LuaTable mt = new LuaTable(0,2);
|
||||||
private LuaBindings( LuaValue _G ) {
|
mt.set(LuaValue.INDEX, new BindindsIndexFunction(b));
|
||||||
mt = LuaValue.tableOf();
|
mt.set(LuaValue.NEWINDEX, new BindingsNewindexFunction(b));
|
||||||
mt.set( LuaValue.INDEX, _G );
|
t.setmetatable(mt);
|
||||||
clear();
|
return t;
|
||||||
}
|
}
|
||||||
public void clear() {
|
|
||||||
env = LuaValue.tableOf();
|
private static final class BindingsNewindexFunction extends ThreeArgFunction {
|
||||||
env.setmetatable(mt);
|
private final Bindings b;
|
||||||
|
|
||||||
|
private BindingsNewindexFunction(Bindings b) {
|
||||||
|
this.b = b;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public LuaValue call(LuaValue tbl, LuaValue key, LuaValue val) {
|
||||||
|
String k = key.tojstring();
|
||||||
|
if ( val.isnil() )
|
||||||
|
b.remove(k);
|
||||||
|
else
|
||||||
|
b.put(k, toJava(val));
|
||||||
|
return NIL;
|
||||||
|
}
|
||||||
|
|
||||||
private Object toJava(LuaValue v) {
|
private Object toJava(LuaValue v) {
|
||||||
switch ( v.type() ) {
|
switch ( v.type() ) {
|
||||||
case LuaValue.TNIL: return null;
|
case LuaValue.TNIL: return null;
|
||||||
case LuaValue.TSTRING: return v.tojstring();
|
case LuaValue.TSTRING: return v.tojstring();
|
||||||
case LuaValue.TUSERDATA: return v.checkuserdata(Object.class);
|
case LuaValue.TUSERDATA: return v.checkuserdata(Object.class);
|
||||||
case LuaValue.TNUMBER: return v.isinttype()? new Integer(v.toint()): new Double(v.todouble());
|
case LuaValue.TNUMBER: return v.isinttype()? (Object) new Integer(v.toint()): (Object) new Double(v.todouble());
|
||||||
default:
|
default:
|
||||||
throw new java.lang.UnsupportedOperationException(
|
throw new java.lang.UnsupportedOperationException(
|
||||||
"LuaBindings cannot convert lua type '"+v.typename()+"' to Java");
|
"LuaBindings cannot convert lua type '"+v.typename()+"' to Java");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private final class BindindsIndexFunction extends TwoArgFunction {
|
||||||
|
private final Bindings b;
|
||||||
|
|
||||||
|
private BindindsIndexFunction(Bindings b) {
|
||||||
|
this.b = b;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LuaValue call(LuaValue tbl, LuaValue key) {
|
||||||
|
String k = key.tojstring();
|
||||||
|
if ( b.containsKey(k) )
|
||||||
|
return toLua(b.get(k));
|
||||||
|
return _G.get(key);
|
||||||
|
}
|
||||||
|
|
||||||
private LuaValue toLua(Object javaValue) {
|
private LuaValue toLua(Object javaValue) {
|
||||||
if ( javaValue instanceof Number ) {
|
if ( javaValue instanceof Number ) {
|
||||||
return LuaValue.valueOf(((Number)javaValue).doubleValue());
|
return LuaValue.valueOf(((Number)javaValue).doubleValue());
|
||||||
@@ -241,54 +270,6 @@ public class LuaScriptEngine implements ScriptEngine, Compilable {
|
|||||||
return LuaValue.userdataOf(javaValue);
|
return LuaValue.userdataOf(javaValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public boolean containsKey(Object key) {
|
|
||||||
return ! env.get(toLua(key)).isnil();
|
|
||||||
}
|
|
||||||
public Object get(Object key) {
|
|
||||||
return toJava(env.get(toLua(key)));
|
|
||||||
}
|
|
||||||
public Object put(String name, Object value) {
|
|
||||||
LuaValue key = toLua(name);
|
|
||||||
Object result = toJava(env.get(key));
|
|
||||||
env.set(key, toLua(value));
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
public void putAll(Map<? extends String, ? extends Object> toMerge) {
|
|
||||||
for ( Iterator it=toMerge.entrySet().iterator(); it.hasNext(); ) {
|
|
||||||
Map.Entry<String, Object> e = (Map.Entry<String, Object>) it.next();
|
|
||||||
put( e.getKey(), e.getValue() );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public Object remove(Object javakey) {
|
|
||||||
LuaValue key = toLua(javakey);
|
|
||||||
Object result = toJava(env.get(key));
|
|
||||||
env.set(key, LuaValue.NIL);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
public boolean containsValue(Object value) {
|
|
||||||
throw new java.lang.UnsupportedOperationException(
|
|
||||||
"containsValue() not supported for LuaBindings");
|
|
||||||
}
|
|
||||||
public Set<java.util.Map.Entry<String, Object>> entrySet() {
|
|
||||||
throw new java.lang.UnsupportedOperationException(
|
|
||||||
"entrySet() not supported for LuaBindings");
|
|
||||||
}
|
|
||||||
public boolean isEmpty() {
|
|
||||||
throw new java.lang.UnsupportedOperationException(
|
|
||||||
"isEmpty() not supported for LuaBindings");
|
|
||||||
}
|
|
||||||
public Set<String> keySet() {
|
|
||||||
throw new java.lang.UnsupportedOperationException(
|
|
||||||
"keySet() not supported for LuaBindings");
|
|
||||||
}
|
|
||||||
public int size() {
|
|
||||||
throw new java.lang.UnsupportedOperationException(
|
|
||||||
"size() not supported for LuaBindings");
|
|
||||||
}
|
|
||||||
public Collection<Object> values() {
|
|
||||||
throw new java.lang.UnsupportedOperationException(
|
|
||||||
"values() not supported for LuaBindings");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------ convert char stream to byte stream for lua compiler -----
|
// ------ convert char stream to byte stream for lua compiler -----
|
||||||
|
|||||||
Reference in New Issue
Block a user