Convert most libraries to use standard loading.
This commit is contained in:
@@ -22,6 +22,7 @@
|
||||
package org.luaj.vm2.lib;
|
||||
|
||||
import org.luaj.vm2.LuaTable;
|
||||
import org.luaj.vm2.LuaValue;
|
||||
import org.luaj.vm2.lib.CoroutineLib;
|
||||
import org.luaj.vm2.lib.jse.JseBaseLib;
|
||||
import org.luaj.vm2.lib.jse.JseIoLib;
|
||||
@@ -39,15 +40,20 @@ public class JsePlatform {
|
||||
public static LuaTable standardGlobals() {
|
||||
LuaTable _G = new JseBaseLib();
|
||||
new org.luaj.vm2.lib.PackageLib(_G);
|
||||
_G.set( "io", new org.luaj.vm2.lib.jse.JseIoLib() );
|
||||
_G.set( "math", new org.luaj.vm2.lib.jse.JseMathLib() );
|
||||
_G.set( "os", new org.luaj.vm2.lib.jse.JseOsLib() );
|
||||
_G.set( "table", new org.luaj.vm2.lib.TableLib() );
|
||||
_G.set( "string", new org.luaj.vm2.lib.StringLib() );
|
||||
_G.set( "luajava", new org.luaj.vm2.lib.jse.LuajavaLib() );
|
||||
CoroutineLib.install(_G);
|
||||
DebugLib.install(_G);
|
||||
set(_G, "table", new org.luaj.vm2.lib.TableLib() );
|
||||
set(_G, "string", new org.luaj.vm2.lib.StringLib() );
|
||||
set(_G, "coroutine", new org.luaj.vm2.lib.CoroutineLib() );
|
||||
set(_G, "debug", new org.luaj.vm2.lib.DebugLib() );
|
||||
set(_G, "math", new org.luaj.vm2.lib.jse.JseMathLib() );
|
||||
set(_G, "io", new org.luaj.vm2.lib.jse.JseIoLib() );
|
||||
set(_G, "os", new org.luaj.vm2.lib.jse.JseOsLib() );
|
||||
set(_G, "luajava", new org.luaj.vm2.lib.jse.LuajavaLib() );
|
||||
return _G;
|
||||
}
|
||||
|
||||
|
||||
private static void set( LuaTable _G, String name, LuaValue chunk ) {
|
||||
chunk.setfenv(_G);
|
||||
LuaValue pkg = chunk.call(LuaValue.valueOf(name));
|
||||
_G.set( name, pkg );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,9 +21,9 @@
|
||||
******************************************************************************/
|
||||
package org.luaj.vm2.lib.jse;
|
||||
|
||||
import org.luaj.vm2.LuaTable;
|
||||
import org.luaj.vm2.LuaValue;
|
||||
import org.luaj.vm2.lib.LibFunction;
|
||||
import org.luaj.vm2.lib.OneArgFunction;
|
||||
import org.luaj.vm2.lib.TwoArgFunction;
|
||||
|
||||
/**
|
||||
@@ -34,31 +34,33 @@ import org.luaj.vm2.lib.TwoArgFunction;
|
||||
*/
|
||||
public class JseMathLib extends org.luaj.vm2.lib.MathLib {
|
||||
|
||||
public JseMathLib() {
|
||||
LibFunction.bind( this, new J2seMathFunc1().getClass(), new String[] {
|
||||
public JseMathLib() {}
|
||||
|
||||
protected LuaTable init() {
|
||||
LuaTable t = super.init();
|
||||
LibFunction.bind( t, this.getClass(), new String[] {
|
||||
"acos", "asin", "atan", "cosh",
|
||||
"exp", "log", "log10", "sinh",
|
||||
"tanh" } );
|
||||
LibFunction.bind( this, new J2seMathFunc2().getClass(), new String[] {
|
||||
LibFunction.bind( t, new J2seMathFunc2().getClass(), new String[] {
|
||||
"atan2", "pow", } );
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
public static class J2seMathFunc1 extends OneArgFunction {
|
||||
public LuaValue call(LuaValue arg) {
|
||||
switch ( opcode ) {
|
||||
case 0: return valueOf(Math.acos(arg.todouble()));
|
||||
case 1: return valueOf(Math.asin(arg.todouble()));
|
||||
case 2: return valueOf(Math.atan(arg.todouble()));
|
||||
case 3: return valueOf(Math.cosh(arg.todouble()));
|
||||
case 4: return valueOf(Math.exp(arg.todouble()));
|
||||
case 5: return valueOf(Math.log(arg.todouble()));
|
||||
case 6: return valueOf(Math.log10(arg.todouble()));
|
||||
case 7: return valueOf(Math.sinh(arg.todouble()));
|
||||
case 8: return valueOf(Math.tanh(arg.todouble()));
|
||||
}
|
||||
return NIL;
|
||||
|
||||
public LuaValue call(LuaValue arg) {
|
||||
switch ( opcode ) {
|
||||
case -1: return init();
|
||||
case 0: return valueOf(Math.acos(arg.todouble()));
|
||||
case 1: return valueOf(Math.asin(arg.todouble()));
|
||||
case 2: return valueOf(Math.atan(arg.todouble()));
|
||||
case 3: return valueOf(Math.cosh(arg.todouble()));
|
||||
case 4: return valueOf(Math.exp(arg.todouble()));
|
||||
case 5: return valueOf(Math.log(arg.todouble()));
|
||||
case 6: return valueOf(Math.log10(arg.todouble()));
|
||||
case 7: return valueOf(Math.sinh(arg.todouble()));
|
||||
case 8: return valueOf(Math.tanh(arg.todouble()));
|
||||
}
|
||||
return NIL;
|
||||
}
|
||||
|
||||
public static class J2seMathFunc2 extends TwoArgFunction {
|
||||
|
||||
@@ -47,15 +47,19 @@ import org.luaj.vm2.lib.ThreeArgFunction;
|
||||
import org.luaj.vm2.lib.TwoArgFunction;
|
||||
import org.luaj.vm2.lib.VarArgFunction;
|
||||
|
||||
public class LuajavaLib extends LuaTable {
|
||||
public class LuajavaLib extends VarArgFunction {
|
||||
|
||||
private static final int BINDCLASS = 0;
|
||||
private static final int NEWINSTANCE = 1;
|
||||
private static final int NEW = 2;
|
||||
private static final int CREATEPROXY = 3;
|
||||
private static final int LOADLIB = 4;
|
||||
private static final String LIBNAME = "luajava";
|
||||
|
||||
private static final int INIT = 0;
|
||||
private static final int BINDCLASS = 1;
|
||||
private static final int NEWINSTANCE = 2;
|
||||
private static final int NEW = 3;
|
||||
private static final int CREATEPROXY = 4;
|
||||
private static final int LOADLIB = 5;
|
||||
|
||||
private static final String[] NAMES = {
|
||||
LIBNAME,
|
||||
"bindClass",
|
||||
"newInstance",
|
||||
"new",
|
||||
@@ -69,89 +73,91 @@ public class LuajavaLib extends LuaTable {
|
||||
}
|
||||
|
||||
public LuajavaLib() {
|
||||
LibFunction.bind( this, LuajavaFuncV.class, NAMES );
|
||||
name = LIBNAME;
|
||||
opcode = INIT;
|
||||
}
|
||||
|
||||
// perform a lua call
|
||||
public static class LuajavaFuncV extends VarArgFunction {
|
||||
|
||||
public Varargs invoke(final Varargs args) {
|
||||
try {
|
||||
switch ( opcode ) {
|
||||
case BINDCLASS: {
|
||||
final Class clazz = Class.forName(args.checkString(1));
|
||||
return toUserdata( clazz, clazz );
|
||||
}
|
||||
case NEWINSTANCE:
|
||||
case NEW: {
|
||||
// get constructor
|
||||
final LuaValue c = args.checkvalue(1);
|
||||
final Class clazz = (opcode==NEWINSTANCE? Class.forName(c.toString()): (Class) c.checkuserdata(Class.class));
|
||||
final ParamsList params = new ParamsList( args );
|
||||
final Constructor con = resolveConstructor( clazz, params );
|
||||
|
||||
// coerce args, construct instance
|
||||
Object[] cargs = CoerceLuaToJava.coerceArgs( params.values, con.getParameterTypes() );
|
||||
Object o = con.newInstance( cargs );
|
||||
|
||||
// return result
|
||||
return toUserdata( o, clazz );
|
||||
}
|
||||
|
||||
case CREATEPROXY: {
|
||||
final int niface = args.narg()-1;
|
||||
if ( niface <= 0 )
|
||||
throw new LuaError("no interfaces");
|
||||
final LuaValue lobj = args.checktable(niface+1);
|
||||
|
||||
// get the interfaces
|
||||
final Class[] ifaces = new Class[niface];
|
||||
for ( int i=0; i<niface; i++ )
|
||||
ifaces[i] = Class.forName(args.checkString(i+1));
|
||||
|
||||
// create the invocation handler
|
||||
InvocationHandler handler = new InvocationHandler() {
|
||||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
|
||||
String name = method.getName();
|
||||
LuaValue func = lobj.get(name);
|
||||
if ( func.isnil() )
|
||||
return null;
|
||||
int n = args!=null? args.length: 0;
|
||||
LuaValue[] v = new LuaValue[n];
|
||||
for ( int i=0; i<n; i++ )
|
||||
v[i] = CoerceJavaToLua.coerce(args[i]);
|
||||
LuaValue result = func.invoke(v).arg1();
|
||||
return CoerceLuaToJava.coerceArg(result, method.getReturnType());
|
||||
}
|
||||
};
|
||||
|
||||
// create the proxy object
|
||||
Object proxy = Proxy.newProxyInstance(getClass().getClassLoader(), ifaces, handler);
|
||||
|
||||
// return the proxy
|
||||
return LuaValue.userdataOf( proxy );
|
||||
}
|
||||
case LOADLIB: {
|
||||
// get constructor
|
||||
String classname = args.checkString(1);
|
||||
String methodname = args.checkString(2);
|
||||
Class clazz = Class.forName(classname);
|
||||
Method method = clazz.getMethod(methodname, new Class[] {});
|
||||
Object result = method.invoke(clazz, new Object[] {});
|
||||
if ( result instanceof LuaValue ) {
|
||||
return (LuaValue) result;
|
||||
} else {
|
||||
return NIL;
|
||||
}
|
||||
}
|
||||
default:
|
||||
throw new LuaError("not yet supported: "+this);
|
||||
}
|
||||
} catch (LuaError e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new LuaError(e);
|
||||
public Varargs invoke(final Varargs args) {
|
||||
try {
|
||||
switch ( opcode ) {
|
||||
case INIT: {
|
||||
LuaTable t = new LuaTable(0,8);
|
||||
LibFunction.bind( t, this.getClass(), NAMES );
|
||||
return t;
|
||||
}
|
||||
case BINDCLASS: {
|
||||
final Class clazz = Class.forName(args.checkString(1));
|
||||
return toUserdata( clazz, clazz );
|
||||
}
|
||||
case NEWINSTANCE:
|
||||
case NEW: {
|
||||
// get constructor
|
||||
final LuaValue c = args.checkvalue(1);
|
||||
final Class clazz = (opcode==NEWINSTANCE? Class.forName(c.toString()): (Class) c.checkuserdata(Class.class));
|
||||
final ParamsList params = new ParamsList( args );
|
||||
final Constructor con = resolveConstructor( clazz, params );
|
||||
|
||||
// coerce args, construct instance
|
||||
Object[] cargs = CoerceLuaToJava.coerceArgs( params.values, con.getParameterTypes() );
|
||||
Object o = con.newInstance( cargs );
|
||||
|
||||
// return result
|
||||
return toUserdata( o, clazz );
|
||||
}
|
||||
|
||||
case CREATEPROXY: {
|
||||
final int niface = args.narg()-1;
|
||||
if ( niface <= 0 )
|
||||
throw new LuaError("no interfaces");
|
||||
final LuaValue lobj = args.checktable(niface+1);
|
||||
|
||||
// get the interfaces
|
||||
final Class[] ifaces = new Class[niface];
|
||||
for ( int i=0; i<niface; i++ )
|
||||
ifaces[i] = Class.forName(args.checkString(i+1));
|
||||
|
||||
// create the invocation handler
|
||||
InvocationHandler handler = new InvocationHandler() {
|
||||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
|
||||
String name = method.getName();
|
||||
LuaValue func = lobj.get(name);
|
||||
if ( func.isnil() )
|
||||
return null;
|
||||
int n = args!=null? args.length: 0;
|
||||
LuaValue[] v = new LuaValue[n];
|
||||
for ( int i=0; i<n; i++ )
|
||||
v[i] = CoerceJavaToLua.coerce(args[i]);
|
||||
LuaValue result = func.invoke(v).arg1();
|
||||
return CoerceLuaToJava.coerceArg(result, method.getReturnType());
|
||||
}
|
||||
};
|
||||
|
||||
// create the proxy object
|
||||
Object proxy = Proxy.newProxyInstance(getClass().getClassLoader(), ifaces, handler);
|
||||
|
||||
// return the proxy
|
||||
return LuaValue.userdataOf( proxy );
|
||||
}
|
||||
case LOADLIB: {
|
||||
// get constructor
|
||||
String classname = args.checkString(1);
|
||||
String methodname = args.checkString(2);
|
||||
Class clazz = Class.forName(classname);
|
||||
Method method = clazz.getMethod(methodname, new Class[] {});
|
||||
Object result = method.invoke(clazz, new Object[] {});
|
||||
if ( result instanceof LuaValue ) {
|
||||
return (LuaValue) result;
|
||||
} else {
|
||||
return NIL;
|
||||
}
|
||||
}
|
||||
default:
|
||||
throw new LuaError("not yet supported: "+this);
|
||||
}
|
||||
} catch (LuaError e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new LuaError(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user