Coerce script engine eval() return values to Java.

This commit is contained in:
James Roseborough
2013-07-01 13:49:52 +00:00
parent 9a3f6161ce
commit 869f0e003c
4 changed files with 65 additions and 27 deletions

View File

@@ -847,6 +847,7 @@ Files are no longer hosted at LuaForge.
<tr valign="top"><td>&nbsp;&nbsp;<b>3.0-beta1</b></td><td><ul>
<li>Fix bug that didn't read package.path from environment.</li>
<li>Fix pluggable scripting engine lookup, simplify implementation, and add unit tests.</li>
<li>Coerce script engine eval() return values to Java.</li>
</ul></td></tr>
</table></td></tr></table>

View File

@@ -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) {

View File

@@ -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;
}
}
}

View File

@@ -21,6 +21,7 @@
******************************************************************************/
package org.luaj.vm2.script;
import java.io.ByteArrayInputStream;
import java.io.CharArrayReader;
import java.io.CharArrayWriter;
import java.io.Reader;
@@ -133,12 +134,12 @@ public class ScriptEngineTests extends TestSuite {
}
});
Object r = e.eval("return f('abc')");
assertEquals("abc123", r.toString());
assertEquals("abc123", r);
}
public void testCompiledScript() throws ScriptException {
CompiledScript cs = ((Compilable)e).compile("y = math.sqrt(x); return y");
b.put("x", 144);
assertEquals("12", cs.eval(b).toString());
assertEquals(12, cs.eval(b));
}
public void testBuggyLuaScript() {
try {
@@ -186,31 +187,31 @@ public class ScriptEngineTests extends TestSuite {
public void testBindingJavaInt() throws ScriptException {
CompiledScript cs = ((Compilable)e).compile("y = x; return 'x '..type(x)..' '..tostring(x)\n");
b.put("x", 111);
assertEquals("x number 111", cs.eval(b).toString());
assertEquals("x number 111", cs.eval(b));
assertEquals(111, b.get("y"));
}
public void testBindingJavaDouble() throws ScriptException {
CompiledScript cs = ((Compilable)e).compile("y = x; return 'x '..type(x)..' '..tostring(x)\n");
b.put("x", 125.125);
assertEquals("x number 125.125", cs.eval(b).toString());
assertEquals("x number 125.125", cs.eval(b));
assertEquals(125.125, b.get("y"));
}
public void testBindingJavaString() throws ScriptException {
CompiledScript cs = ((Compilable)e).compile("y = x; return 'x '..type(x)..' '..tostring(x)\n");
b.put("x", "foo");
assertEquals("x string foo", cs.eval(b).toString());
assertEquals("x string foo", cs.eval(b));
assertEquals("foo", b.get("y"));
}
public void testBindingJavaObject() throws ScriptException {
CompiledScript cs = ((Compilable)e).compile("y = x; return 'x '..type(x)..' '..tostring(x)\n");
b.put("x", new SomeUserClass());
assertEquals("x userdata some-user-value", cs.eval(b).toString());
assertEquals("x userdata some-user-value", cs.eval(b));
assertEquals(SomeUserClass.class, b.get("y").getClass());
}
public void testBindingJavaArray() throws ScriptException {
CompiledScript cs = ((Compilable)e).compile("y = x; return 'x '..type(x)..' '..x[1]..' '..x[2]\n");
CompiledScript cs = ((Compilable)e).compile("y = x; return 'x '..type(x)..' '..#x..' '..x[1]..' '..x[2]\n");
b.put("x", new int[] { 777, 888 });
assertEquals("x userdata 777 888", cs.eval(b).toString());
assertEquals("x userdata 2 777 888", cs.eval(b));
assertEquals(int[].class, b.get("y").getClass());
}
public void testBindingLuaFunction() throws ScriptException {
@@ -223,10 +224,19 @@ public class ScriptEngineTests extends TestSuite {
CompiledScript cs = ((Compilable)e).compile(
"x = x or luajava.newInstance('java.lang.String', 'test')\n" +
"return 'x ' .. type(x) .. ' ' .. tostring(x)\n");
assertEquals("x string test", cs.eval(b).toString());
assertEquals("x string test", cs.eval(b));
b.put("x", new SomeUserClass());
assertEquals("x userdata some-user-value", cs.eval(b).toString());
assertEquals("x userdata some-user-value", cs.eval(b));
}
public void testReturnMultipleValues() throws ScriptException {
CompiledScript cs = ((Compilable)e).compile("return 'foo', 'bar'\n");
Object o = cs.eval();
assertEquals(Object[].class, o.getClass());
Object[] array = (Object[]) o;
assertEquals(2, array.length);
assertEquals("foo", array[0]);
assertEquals("bar", array[1]);
}
}
public static class SomeUserClass {