Remove libs dependency from PackageLib.
This commit is contained in:
@@ -37,38 +37,38 @@ import org.luaj.vm2.compiler.LuaC;
|
||||
import org.luaj.vm2.lib.LibFunction;
|
||||
import org.luaj.vm2.lib.VarArgFunction;
|
||||
|
||||
/**
|
||||
* Subclass of {@link LibFunction} which implements the features of the luajava package.
|
||||
* <p>
|
||||
* Luajava is an approach to mixing lua and java using simple functions that bind
|
||||
* java classes and methods to lua dynamically. The API is documented on the
|
||||
/**
|
||||
* Subclass of {@link LibFunction} which implements the features of the luajava package.
|
||||
* <p>
|
||||
* Luajava is an approach to mixing lua and java using simple functions that bind
|
||||
* java classes and methods to lua dynamically. The API is documented on the
|
||||
* <a href="http://www.keplerproject.org/luajava/">luajava</a> documentation pages.
|
||||
*
|
||||
* <p>
|
||||
* Typically, this library is included as part of a call to
|
||||
* Typically, this library is included as part of a call to
|
||||
* {@link org.luaj.vm2.lib.jse.JsePlatform#standardGlobals()}
|
||||
* <pre> {@code
|
||||
* Globals globals = JsePlatform.standardGlobals();
|
||||
* System.out.println( globals.get("luajava").get("bindClass").call( LuaValue.valueOf("java.lang.System") ).invokeMethod("currentTimeMillis") );
|
||||
* } </pre>
|
||||
* <p>
|
||||
* To instantiate and use it directly,
|
||||
* To instantiate and use it directly,
|
||||
* link it into your globals table via {@link Globals#load} using code such as:
|
||||
* <pre> {@code
|
||||
* Globals globals = new Globals();
|
||||
* globals.load(new JseBaseLib());
|
||||
* globals.load(new PackageLib());
|
||||
* globals.load(new LuajavaLib());
|
||||
* globals.load(
|
||||
* globals.load(
|
||||
* "sys = luajava.bindClass('java.lang.System')\n"+
|
||||
* "print ( sys:currentTimeMillis() )\n", "main.lua" ).call();
|
||||
* "print ( sys:currentTimeMillis() )\n", "main.lua" ).call();
|
||||
* } </pre>
|
||||
* <p>
|
||||
*
|
||||
* The {@code luajava} library is available
|
||||
* The {@code luajava} library is available
|
||||
* on all JSE platforms via the call to {@link org.luaj.vm2.lib.jse.JsePlatform#standardGlobals()}
|
||||
* and the luajava api's are simply invoked from lua.
|
||||
* Because it makes extensive use of Java's reflection API, it is not available
|
||||
* and the luajava api's are simply invoked from lua.
|
||||
* Because it makes extensive use of Java's reflection API, it is not available
|
||||
* on JME, but can be used in Android applications.
|
||||
* <p>
|
||||
* This has been implemented to match as closely as possible the behavior in the corresponding library in C.
|
||||
@@ -91,10 +91,10 @@ public class LuajavaLib extends VarArgFunction {
|
||||
static final int LOADLIB = 5;
|
||||
|
||||
static final String[] NAMES = {
|
||||
"bindClass",
|
||||
"newInstance",
|
||||
"new",
|
||||
"createProxy",
|
||||
"bindClass",
|
||||
"newInstance",
|
||||
"new",
|
||||
"createProxy",
|
||||
"loadLib",
|
||||
};
|
||||
|
||||
@@ -112,7 +112,7 @@ public class LuajavaLib extends VarArgFunction {
|
||||
LuaTable t = new LuaTable();
|
||||
bind( t, this.getClass(), NAMES, BINDCLASS );
|
||||
env.set("luajava", t);
|
||||
env.get("package").get("loaded").set("luajava", t);
|
||||
if (!env.get("package").isnil()) env.get("package").get("loaded").set("luajava", t);
|
||||
return t;
|
||||
}
|
||||
case BINDCLASS: {
|
||||
@@ -122,13 +122,13 @@ public class LuajavaLib extends VarArgFunction {
|
||||
case NEWINSTANCE:
|
||||
case NEW: {
|
||||
// get constructor
|
||||
final LuaValue c = args.checkvalue(1);
|
||||
final LuaValue c = args.checkvalue(1);
|
||||
final Class clazz = (opcode==NEWINSTANCE? classForName(c.tojstring()): (Class) c.checkuserdata(Class.class));
|
||||
final Varargs consargs = args.subargs(2);
|
||||
return JavaClass.forClass(clazz).getConstructor().invoke(consargs);
|
||||
}
|
||||
|
||||
case CREATEPROXY: {
|
||||
case CREATEPROXY: {
|
||||
final int niface = args.narg()-1;
|
||||
if ( niface <= 0 )
|
||||
throw new LuaError("no interfaces");
|
||||
@@ -136,7 +136,7 @@ public class LuajavaLib extends VarArgFunction {
|
||||
|
||||
// get the interfaces
|
||||
final Class[] ifaces = new Class[niface];
|
||||
for ( int i=0; i<niface; i++ )
|
||||
for ( int i=0; i<niface; i++ )
|
||||
ifaces[i] = classForName(args.checkjstring(i+1));
|
||||
|
||||
// create the invocation handler
|
||||
@@ -191,16 +191,16 @@ public class LuajavaLib extends VarArgFunction {
|
||||
if ( func.isnil() )
|
||||
return null;
|
||||
boolean isvarargs = ((method.getModifiers() & METHOD_MODIFIERS_VARARGS) != 0);
|
||||
int n = args!=null? args.length: 0;
|
||||
int n = args!=null? args.length: 0;
|
||||
LuaValue[] v;
|
||||
if ( isvarargs ) {
|
||||
if ( isvarargs ) {
|
||||
Object o = args[--n];
|
||||
int m = Array.getLength( o );
|
||||
v = new LuaValue[n+m];
|
||||
for ( int i=0; i<n; i++ )
|
||||
v[i] = CoerceJavaToLua.coerce(args[i]);
|
||||
for ( int i=0; i<m; i++ )
|
||||
v[i+n] = CoerceJavaToLua.coerce(Array.get(o,i));
|
||||
v[i+n] = CoerceJavaToLua.coerce(Array.get(o,i));
|
||||
} else {
|
||||
v = new LuaValue[n];
|
||||
for ( int i=0; i<n; i++ )
|
||||
|
||||
Reference in New Issue
Block a user