Coerce script engine eval() return values to Java.
This commit is contained in:
@@ -847,6 +847,7 @@ Files are no longer hosted at LuaForge.
|
|||||||
<tr valign="top"><td> <b>3.0-beta1</b></td><td><ul>
|
<tr valign="top"><td> <b>3.0-beta1</b></td><td><ul>
|
||||||
<li>Fix bug that didn't read package.path from environment.</li>
|
<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>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>
|
</ul></td></tr>
|
||||||
</table></td></tr></table>
|
</table></td></tr></table>
|
||||||
|
|||||||
@@ -23,8 +23,10 @@ package org.luaj.vm2.lib.jse;
|
|||||||
|
|
||||||
import java.lang.reflect.Array;
|
import java.lang.reflect.Array;
|
||||||
|
|
||||||
|
import org.luaj.vm2.LuaTable;
|
||||||
import org.luaj.vm2.LuaUserdata;
|
import org.luaj.vm2.LuaUserdata;
|
||||||
import org.luaj.vm2.LuaValue;
|
import org.luaj.vm2.LuaValue;
|
||||||
|
import org.luaj.vm2.lib.OneArgFunction;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* LuaValue that represents a Java instance of array type.
|
* 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 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) {
|
JavaArray(Object instance) {
|
||||||
super(instance);
|
super(instance);
|
||||||
|
setmetatable(array_metatable);
|
||||||
}
|
}
|
||||||
|
|
||||||
public LuaValue get(LuaValue key) {
|
public LuaValue get(LuaValue key) {
|
||||||
|
|||||||
@@ -155,7 +155,7 @@ public class LuaScriptEngine extends AbstractScriptEngine implements ScriptEngin
|
|||||||
}
|
}
|
||||||
f.initupvalue1(g);
|
f.initupvalue1(g);
|
||||||
}
|
}
|
||||||
return f.invoke(LuaValue.NONE);
|
return toJava(f.invoke(LuaValue.NONE));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -215,6 +215,13 @@ public class LuaScriptEngine extends AbstractScriptEngine implements ScriptEngin
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
static private Object toJava(LuaValue luajValue) {
|
||||||
switch ( luajValue.type() ) {
|
switch ( luajValue.type() ) {
|
||||||
@@ -228,10 +235,17 @@ public class LuaScriptEngine extends AbstractScriptEngine implements ScriptEngin
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static private LuaValue toLua(Object javaValue) {
|
static private Object toJava(Varargs v) {
|
||||||
return javaValue == null? LuaValue.NIL:
|
final int n = v.narg();
|
||||||
javaValue instanceof LuaValue? (LuaValue) javaValue:
|
switch (n) {
|
||||||
CoerceJavaToLua.coerce(javaValue);
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,7 @@
|
|||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
package org.luaj.vm2.script;
|
package org.luaj.vm2.script;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.CharArrayReader;
|
import java.io.CharArrayReader;
|
||||||
import java.io.CharArrayWriter;
|
import java.io.CharArrayWriter;
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
@@ -133,12 +134,12 @@ public class ScriptEngineTests extends TestSuite {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
Object r = e.eval("return f('abc')");
|
Object r = e.eval("return f('abc')");
|
||||||
assertEquals("abc123", r.toString());
|
assertEquals("abc123", r);
|
||||||
}
|
}
|
||||||
public void testCompiledScript() throws ScriptException {
|
public void testCompiledScript() throws ScriptException {
|
||||||
CompiledScript cs = ((Compilable)e).compile("y = math.sqrt(x); return y");
|
CompiledScript cs = ((Compilable)e).compile("y = math.sqrt(x); return y");
|
||||||
b.put("x", 144);
|
b.put("x", 144);
|
||||||
assertEquals("12", cs.eval(b).toString());
|
assertEquals(12, cs.eval(b));
|
||||||
}
|
}
|
||||||
public void testBuggyLuaScript() {
|
public void testBuggyLuaScript() {
|
||||||
try {
|
try {
|
||||||
@@ -186,31 +187,31 @@ public class ScriptEngineTests extends TestSuite {
|
|||||||
public void testBindingJavaInt() throws ScriptException {
|
public void testBindingJavaInt() throws ScriptException {
|
||||||
CompiledScript cs = ((Compilable)e).compile("y = x; return 'x '..type(x)..' '..tostring(x)\n");
|
CompiledScript cs = ((Compilable)e).compile("y = x; return 'x '..type(x)..' '..tostring(x)\n");
|
||||||
b.put("x", 111);
|
b.put("x", 111);
|
||||||
assertEquals("x number 111", cs.eval(b).toString());
|
assertEquals("x number 111", cs.eval(b));
|
||||||
assertEquals(111, b.get("y"));
|
assertEquals(111, b.get("y"));
|
||||||
}
|
}
|
||||||
public void testBindingJavaDouble() throws ScriptException {
|
public void testBindingJavaDouble() throws ScriptException {
|
||||||
CompiledScript cs = ((Compilable)e).compile("y = x; return 'x '..type(x)..' '..tostring(x)\n");
|
CompiledScript cs = ((Compilable)e).compile("y = x; return 'x '..type(x)..' '..tostring(x)\n");
|
||||||
b.put("x", 125.125);
|
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"));
|
assertEquals(125.125, b.get("y"));
|
||||||
}
|
}
|
||||||
public void testBindingJavaString() throws ScriptException {
|
public void testBindingJavaString() throws ScriptException {
|
||||||
CompiledScript cs = ((Compilable)e).compile("y = x; return 'x '..type(x)..' '..tostring(x)\n");
|
CompiledScript cs = ((Compilable)e).compile("y = x; return 'x '..type(x)..' '..tostring(x)\n");
|
||||||
b.put("x", "foo");
|
b.put("x", "foo");
|
||||||
assertEquals("x string foo", cs.eval(b).toString());
|
assertEquals("x string foo", cs.eval(b));
|
||||||
assertEquals("foo", b.get("y"));
|
assertEquals("foo", b.get("y"));
|
||||||
}
|
}
|
||||||
public void testBindingJavaObject() throws ScriptException {
|
public void testBindingJavaObject() throws ScriptException {
|
||||||
CompiledScript cs = ((Compilable)e).compile("y = x; return 'x '..type(x)..' '..tostring(x)\n");
|
CompiledScript cs = ((Compilable)e).compile("y = x; return 'x '..type(x)..' '..tostring(x)\n");
|
||||||
b.put("x", new SomeUserClass());
|
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());
|
assertEquals(SomeUserClass.class, b.get("y").getClass());
|
||||||
}
|
}
|
||||||
public void testBindingJavaArray() throws ScriptException {
|
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 });
|
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());
|
assertEquals(int[].class, b.get("y").getClass());
|
||||||
}
|
}
|
||||||
public void testBindingLuaFunction() throws ScriptException {
|
public void testBindingLuaFunction() throws ScriptException {
|
||||||
@@ -223,9 +224,18 @@ public class ScriptEngineTests extends TestSuite {
|
|||||||
CompiledScript cs = ((Compilable)e).compile(
|
CompiledScript cs = ((Compilable)e).compile(
|
||||||
"x = x or luajava.newInstance('java.lang.String', 'test')\n" +
|
"x = x or luajava.newInstance('java.lang.String', 'test')\n" +
|
||||||
"return 'x ' .. type(x) .. ' ' .. tostring(x)\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());
|
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]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user