Coerce script engine eval() return values to Java.
This commit is contained in:
@@ -23,8 +23,10 @@ package org.luaj.vm2.lib.jse;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
|
||||
import org.luaj.vm2.LuaTable;
|
||||
import org.luaj.vm2.LuaUserdata;
|
||||
import org.luaj.vm2.LuaValue;
|
||||
import org.luaj.vm2.lib.OneArgFunction;
|
||||
|
||||
/**
|
||||
* LuaValue that represents a Java instance of array type.
|
||||
@@ -41,8 +43,19 @@ class JavaArray extends LuaUserdata {
|
||||
|
||||
static final LuaValue LENGTH = valueOf("length");
|
||||
|
||||
static final LuaTable array_metatable;
|
||||
static {
|
||||
array_metatable = new LuaTable();
|
||||
array_metatable.rawset(LuaValue.LEN, new OneArgFunction() {
|
||||
public LuaValue call(LuaValue u) {
|
||||
return LuaValue.valueOf(Array.getLength(((LuaUserdata)u).m_instance));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
JavaArray(Object instance) {
|
||||
super(instance);
|
||||
setmetatable(array_metatable);
|
||||
}
|
||||
|
||||
public LuaValue get(LuaValue key) {
|
||||
|
||||
@@ -155,7 +155,7 @@ public class LuaScriptEngine extends AbstractScriptEngine implements ScriptEngin
|
||||
}
|
||||
f.initupvalue1(g);
|
||||
}
|
||||
return f.invoke(LuaValue.NONE);
|
||||
return toJava(f.invoke(LuaValue.NONE));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -215,23 +215,37 @@ public class LuaScriptEngine extends AbstractScriptEngine implements ScriptEngin
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
static private Object toJava(LuaValue luajValue) {
|
||||
switch ( luajValue.type() ) {
|
||||
case LuaValue.TNIL: return null;
|
||||
case LuaValue.TSTRING: return luajValue.tojstring();
|
||||
case LuaValue.TUSERDATA: return luajValue.checkuserdata(Object.class);
|
||||
case LuaValue.TNUMBER: return luajValue.isinttype()?
|
||||
(Object) new Integer(luajValue.toint()):
|
||||
(Object) new Double(luajValue.todouble());
|
||||
default: return luajValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static private LuaValue toLua(Object javaValue) {
|
||||
return javaValue == null? LuaValue.NIL:
|
||||
javaValue instanceof LuaValue? (LuaValue) javaValue:
|
||||
CoerceJavaToLua.coerce(javaValue);
|
||||
}
|
||||
|
||||
static private LuaValue toLua(Object javaValue) {
|
||||
return javaValue == null? LuaValue.NIL:
|
||||
javaValue instanceof LuaValue? (LuaValue) javaValue:
|
||||
CoerceJavaToLua.coerce(javaValue);
|
||||
static private Object toJava(LuaValue luajValue) {
|
||||
switch ( luajValue.type() ) {
|
||||
case LuaValue.TNIL: return null;
|
||||
case LuaValue.TSTRING: return luajValue.tojstring();
|
||||
case LuaValue.TUSERDATA: return luajValue.checkuserdata(Object.class);
|
||||
case LuaValue.TNUMBER: return luajValue.isinttype()?
|
||||
(Object) new Integer(luajValue.toint()):
|
||||
(Object) new Double(luajValue.todouble());
|
||||
default: return luajValue;
|
||||
}
|
||||
}
|
||||
|
||||
static private Object toJava(Varargs v) {
|
||||
final int n = v.narg();
|
||||
switch (n) {
|
||||
case 0: return null;
|
||||
case 1: return toJava(v.arg1());
|
||||
default:
|
||||
Object[] o = new Object[n];
|
||||
for (int i=0; i<n; ++i)
|
||||
o[i] = toJava(v.arg(i+1));
|
||||
return o;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user