Fix bug in luajava.createProxy() and demonstrate its use.
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
<classpathentry kind="src" path="src/script"/>
|
||||
<classpathentry kind="src" path="src/test/java"/>
|
||||
<classpathentry kind="src" path="src/sample"/>
|
||||
<classpathentry kind="src" path="src/test/res"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/3.8.1"/>
|
||||
<classpathentry kind="var" path="WTK_HOME/lib/cldcapi11.jar"/>
|
||||
|
||||
@@ -38,6 +38,8 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.luaj.vm.LFunction;
|
||||
import org.luaj.vm.LNil;
|
||||
import org.luaj.vm.LString;
|
||||
import org.luaj.vm.LTable;
|
||||
import org.luaj.vm.LUserData;
|
||||
import org.luaj.vm.LValue;
|
||||
@@ -126,7 +128,7 @@ public final class LuajavaLib extends LFunction {
|
||||
final int ninterfaces = Math.max(0,vm.gettop()-2);
|
||||
if ( ninterfaces <= 0 )
|
||||
throw new LuaErrorException("no interfaces");
|
||||
final LValue function = vm.tofunction(-1);
|
||||
final LValue lobj = vm.totable(-1);
|
||||
try {
|
||||
// get the interfaces
|
||||
final Class[] ifaces = new Class[ninterfaces];
|
||||
@@ -136,12 +138,18 @@ public final class LuajavaLib extends LFunction {
|
||||
// create the invocation handler
|
||||
InvocationHandler handler = new InvocationHandler() {
|
||||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
|
||||
vm.pushlvalue(function);
|
||||
int n = args.length;
|
||||
for ( int i=0; i<n; i++ )
|
||||
vm.pushlvalue( CoerceJavaToLua.coerce(args[i]) );
|
||||
vm.call(n, 1);
|
||||
return CoerceLuaToJava.coerceArg(vm.poplvalue(), method.getReturnType());
|
||||
vm.pushlvalue(lobj);
|
||||
vm.getfield( -1, LString.valueOf(method.getName()) );
|
||||
vm.remove( -2 );
|
||||
LValue result;
|
||||
if ( !vm.isnil( -1 ) ) {
|
||||
int n = args.length;
|
||||
for ( int i=0; i<n; i++ )
|
||||
vm.pushlvalue( CoerceJavaToLua.coerce(args[i]) );
|
||||
vm.call(n, 1);
|
||||
}
|
||||
result = vm.poplvalue();
|
||||
return CoerceLuaToJava.coerceArg(result, method.getReturnType());
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ package org.luaj.sample;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.luaj.compiler.LuaC;
|
||||
import org.luaj.lib.j2se.LuajavaLib;
|
||||
import org.luaj.platform.J2sePlatform;
|
||||
import org.luaj.vm.LClosure;
|
||||
@@ -45,12 +46,13 @@ public class LuajavaRunner {
|
||||
public static void main( String[] args ) throws IOException {
|
||||
|
||||
Platform.setInstance(new J2sePlatform());
|
||||
|
||||
LuaC.install();
|
||||
|
||||
// new lua state
|
||||
LuaState state = Platform.newLuaState();
|
||||
|
||||
// get script name
|
||||
String script = (args.length>0? args[0]: "/swingapp.luac");
|
||||
String script = (args.length>0? args[0]: "/swingapp.lua");
|
||||
System.out.println("loading '"+script+"'");
|
||||
|
||||
// load the file
|
||||
|
||||
@@ -18,3 +18,12 @@ jframe = luajava.bindClass( "javax.swing.JFrame" )
|
||||
frame:setDefaultCloseOperation(jframe.EXIT_ON_CLOSE)
|
||||
frame:pack()
|
||||
frame:setVisible(true)
|
||||
|
||||
local listener = luajava.createProxy("java.awt.event.MouseListener",
|
||||
{
|
||||
mouseClicked = function(me)
|
||||
print("clicked!", me)
|
||||
end
|
||||
})
|
||||
|
||||
frame:addMouseListener(listener)
|
||||
|
||||
@@ -1 +1 @@
|
||||
version: 0.80
|
||||
version: 0.81
|
||||
|
||||
Reference in New Issue
Block a user