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 LuaString checkstring(int i) { return arg(i).checkstring(); }
|
||||||
public LuaTable checktable(int i) { return arg(i).checktable(); }
|
public LuaTable checktable(int i) { return arg(i).checktable(); }
|
||||||
public LuaThread checkthread(int i) { return arg(i).checkthread(); }
|
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 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 checkvalue(int i) { return i<=narg()? arg(i): LuaValue.error("value expected"); }
|
||||||
public LuaValue checknotnil(int i) { return arg(i).checknotnil(); }
|
public LuaValue checknotnil(int i) { return arg(i).checknotnil(); }
|
||||||
@@ -91,6 +92,17 @@ public abstract class Varargs {
|
|||||||
return i>narg() || arg(i).isnil();
|
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() {
|
public String toString() {
|
||||||
Buffer sb = new Buffer();
|
Buffer sb = new Buffer();
|
||||||
sb.append( "(" );
|
sb.append( "(" );
|
||||||
|
|||||||
@@ -206,6 +206,7 @@ public class LuajavaLib extends OneArgFunction {
|
|||||||
if ( mt == null ) {
|
if ( mt == null ) {
|
||||||
mt = new LuaTable();
|
mt = new LuaTable();
|
||||||
mt.set( LuaValue.INDEX, new TwoArgFunction() {
|
mt.set( LuaValue.INDEX, new TwoArgFunction() {
|
||||||
|
private Map methods;
|
||||||
public LuaValue call(LuaValue table, LuaValue key) {
|
public LuaValue call(LuaValue table, LuaValue key) {
|
||||||
Object instance = table.touserdata();
|
Object instance = table.touserdata();
|
||||||
if ( key.isinttype() ) {
|
if ( key.isinttype() ) {
|
||||||
@@ -224,7 +225,16 @@ public class LuajavaLib extends OneArgFunction {
|
|||||||
} catch (NoSuchFieldException nsfe) {
|
} catch (NoSuchFieldException nsfe) {
|
||||||
if ( clazz.isArray() && key.equals(LENGTH) )
|
if ( clazz.isArray() && key.equals(LENGTH) )
|
||||||
return LuaValue.valueOf( Array.getLength(instance) );
|
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) {
|
} catch (Exception e) {
|
||||||
throw new LuaError(e);
|
throw new LuaError(e);
|
||||||
}
|
}
|
||||||
@@ -273,7 +283,7 @@ public class LuajavaLib extends OneArgFunction {
|
|||||||
public Varargs invoke(Varargs args) {
|
public Varargs invoke(Varargs args) {
|
||||||
try {
|
try {
|
||||||
// find the method
|
// find the method
|
||||||
Object instance = args.checkuserdata(1,Object.class);
|
Object instance = args.touserdata(1);
|
||||||
ParamsList params = new ParamsList( args );
|
ParamsList params = new ParamsList( args );
|
||||||
Method meth = resolveMethod( clazz, s, params );
|
Method meth = resolveMethod( clazz, s, params );
|
||||||
|
|
||||||
|
|||||||
@@ -166,20 +166,17 @@ public class LuaJavaCoercionTest extends TestCase {
|
|||||||
|
|
||||||
// get sample field, call with one arguments
|
// get sample field, call with one arguments
|
||||||
LuaValue arg = CoerceJavaToLua.coerce(new Integer(123));
|
LuaValue arg = CoerceJavaToLua.coerce(new Integer(123));
|
||||||
method.call(v,arg);
|
result = method.call(v,arg);
|
||||||
result = method.call(v);
|
|
||||||
assertEquals( "int-args 123", result.toString() );
|
assertEquals( "int-args 123", result.toString() );
|
||||||
|
|
||||||
// get sample field, call with array argument
|
// get sample field, call with array argument
|
||||||
arg = CoerceJavaToLua.coerce(new int[]{345,678});
|
arg = CoerceJavaToLua.coerce(new int[]{345,678});
|
||||||
method.call(v,arg);
|
result = method.call(v,arg);
|
||||||
result = method.call(v);
|
|
||||||
assertEquals( "int-array-args 345,678", result.toString() );
|
assertEquals( "int-array-args 345,678", result.toString() );
|
||||||
|
|
||||||
// get sample field, call with two-d array argument
|
// get sample field, call with two-d array argument
|
||||||
arg = CoerceJavaToLua.coerce(new int[][]{{22,33},{44,55}});
|
arg = CoerceJavaToLua.coerce(new int[][]{{22,33},{44,55}});
|
||||||
method.call(v,arg);
|
result = method.call(v,arg);
|
||||||
result = method.call(v);
|
|
||||||
assertEquals( "int-array-array-args 22,33,44,55", result.toString() );
|
assertEquals( "int-array-array-args 22,33,44,55", result.toString() );
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -198,10 +195,10 @@ public class LuaJavaCoercionTest extends TestCase {
|
|||||||
public void testExceptionMessage() {
|
public void testExceptionMessage() {
|
||||||
String script = "return pcall( luajava.bindClass( \""+SomeClass.class.getName()+"\").someMethod )";
|
String script = "return pcall( luajava.bindClass( \""+SomeClass.class.getName()+"\").someMethod )";
|
||||||
Varargs vresult = _G.get("loadstring").call(LuaValue.valueOf(script)).invoke(LuaValue.NONE);
|
Varargs vresult = _G.get("loadstring").call(LuaValue.valueOf(script)).invoke(LuaValue.NONE);
|
||||||
LuaValue message = vresult.arg1();
|
LuaValue status = vresult.arg1();
|
||||||
LuaValue status = vresult.arg(2);
|
LuaValue message = vresult.arg(2);
|
||||||
assertEquals( LuaValue.FALSE, status );
|
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() {
|
public void testLuaErrorCause() {
|
||||||
|
|||||||
Reference in New Issue
Block a user