Improve luajava method resolution
This commit is contained in:
@@ -81,6 +81,7 @@ public abstract class Varargs {
|
||||
public LuaString checkstring(int i) { return arg(i).checkstring(); }
|
||||
public LuaTable checktable(int i) { return arg(i).checktable(); }
|
||||
public LuaThread checkthread(int i) { return arg(i).checkthread(); }
|
||||
public Object checkuserdata(int i) { return arg(i).checkuserdata(); }
|
||||
public Object checkuserdata(int i,Class c) { return arg(i).checkuserdata(c); }
|
||||
public LuaValue checkvalue(int i) { return i<=narg()? arg(i): LuaValue.error("value expected"); }
|
||||
public LuaValue checknotnil(int i) { return arg(i).checknotnil(); }
|
||||
@@ -91,6 +92,17 @@ public abstract class Varargs {
|
||||
return i>narg() || arg(i).isnil();
|
||||
}
|
||||
|
||||
public boolean toboolean(int i) { return arg(i).toboolean(); }
|
||||
public byte tobyte(int i) { return arg(i).tobyte(); }
|
||||
public char tochar(int i) { return arg(i).tochar(); }
|
||||
public double todouble(int i) { return arg(i).todouble(); }
|
||||
public float tofloat(int i) { return arg(i).tofloat(); }
|
||||
public int toint(int i) { return arg(i).toint(); }
|
||||
public long tolong(int i) { return arg(i).tolong(); }
|
||||
public short toshort(int i) { return arg(i).toshort(); }
|
||||
public Object touserdata(int i) { return arg(i).touserdata(); }
|
||||
public Object touserdata(int i,Class c) { return arg(i).touserdata(c); }
|
||||
|
||||
public String toString() {
|
||||
Buffer sb = new Buffer();
|
||||
sb.append( "(" );
|
||||
|
||||
@@ -206,6 +206,7 @@ public class LuajavaLib extends OneArgFunction {
|
||||
if ( mt == null ) {
|
||||
mt = new LuaTable();
|
||||
mt.set( LuaValue.INDEX, new TwoArgFunction() {
|
||||
private Map methods;
|
||||
public LuaValue call(LuaValue table, LuaValue key) {
|
||||
Object instance = table.touserdata();
|
||||
if ( key.isinttype() ) {
|
||||
@@ -224,7 +225,16 @@ public class LuajavaLib extends OneArgFunction {
|
||||
} catch (NoSuchFieldException nsfe) {
|
||||
if ( clazz.isArray() && key.equals(LENGTH) )
|
||||
return LuaValue.valueOf( Array.getLength(instance) );
|
||||
return new LMethod(clazz,s);
|
||||
if ( methods == null )
|
||||
methods = new HashMap();
|
||||
LMethod m = (LMethod) methods.get(s);
|
||||
if ( m == null ) {
|
||||
m = new LMethod(clazz,s);
|
||||
// not safe - param list needs to
|
||||
// distinguish between more cases
|
||||
// methods.put(s, m);
|
||||
}
|
||||
return m;
|
||||
} catch (Exception e) {
|
||||
throw new LuaError(e);
|
||||
}
|
||||
@@ -273,7 +283,7 @@ public class LuajavaLib extends OneArgFunction {
|
||||
public Varargs invoke(Varargs args) {
|
||||
try {
|
||||
// find the method
|
||||
Object instance = args.checkuserdata(1,Object.class);
|
||||
Object instance = args.touserdata(1);
|
||||
ParamsList params = new ParamsList( args );
|
||||
Method meth = resolveMethod( clazz, s, params );
|
||||
|
||||
|
||||
@@ -166,20 +166,17 @@ public class LuaJavaCoercionTest extends TestCase {
|
||||
|
||||
// get sample field, call with one arguments
|
||||
LuaValue arg = CoerceJavaToLua.coerce(new Integer(123));
|
||||
method.call(v,arg);
|
||||
result = method.call(v);
|
||||
result = method.call(v,arg);
|
||||
assertEquals( "int-args 123", result.toString() );
|
||||
|
||||
// get sample field, call with array argument
|
||||
arg = CoerceJavaToLua.coerce(new int[]{345,678});
|
||||
method.call(v,arg);
|
||||
result = method.call(v);
|
||||
result = method.call(v,arg);
|
||||
assertEquals( "int-array-args 345,678", result.toString() );
|
||||
|
||||
// get sample field, call with two-d array argument
|
||||
arg = CoerceJavaToLua.coerce(new int[][]{{22,33},{44,55}});
|
||||
method.call(v,arg);
|
||||
result = method.call(v);
|
||||
result = method.call(v,arg);
|
||||
assertEquals( "int-array-array-args 22,33,44,55", result.toString() );
|
||||
}
|
||||
|
||||
@@ -198,10 +195,10 @@ public class LuaJavaCoercionTest extends TestCase {
|
||||
public void testExceptionMessage() {
|
||||
String script = "return pcall( luajava.bindClass( \""+SomeClass.class.getName()+"\").someMethod )";
|
||||
Varargs vresult = _G.get("loadstring").call(LuaValue.valueOf(script)).invoke(LuaValue.NONE);
|
||||
LuaValue message = vresult.arg1();
|
||||
LuaValue status = vresult.arg(2);
|
||||
LuaValue status = vresult.arg1();
|
||||
LuaValue message = vresult.arg(2);
|
||||
assertEquals( LuaValue.FALSE, status );
|
||||
assertEquals( "lua error: "+SomeException.class.getName()+": this is some message", message.toString() );
|
||||
assertEquals( "vm error: "+SomeException.class.getName()+": this is some message", message.toString() );
|
||||
}
|
||||
|
||||
public void testLuaErrorCause() {
|
||||
|
||||
Reference in New Issue
Block a user