Improve jsr-223 bindings, allow ".lua" or "lua" as extensions, find classes when added as vm extension, enhance javadoc, add sources jar.
This commit is contained in:
18
README.html
18
README.html
@@ -714,19 +714,23 @@ and LuaForge:
|
|||||||
<h2>Main Changes by Version</h2>
|
<h2>Main Changes by Version</h2>
|
||||||
<table cellspacing="10"><tr><td><table cellspacing="4">
|
<table cellspacing="10"><tr><td><table cellspacing="4">
|
||||||
<tr valign="top"><td> <b>2.0</b></td><td><ul>
|
<tr valign="top"><td> <b>2.0</b></td><td><ul>
|
||||||
<li>Initial release of 2.0 version
|
<li>Initial release of 2.0 version </li>
|
||||||
</ul></td></tr>
|
</ul></td></tr>
|
||||||
<tr valign="top"><td> <b>2.0.1</b></td><td><ul>
|
<tr valign="top"><td> <b>2.0.1</b></td><td><ul>
|
||||||
<li>Improve correctness of singleton construction related to static initialization
|
<li>Improve correctness of singleton construction related to static initialization </li>
|
||||||
<li>Fix nan-related error in constant folding logic that was failing on some JVMs
|
<li>Fix nan-related error in constant folding logic that was failing on some JVMs </li>
|
||||||
<li>JSR-223 fixes: add META-INF/services entry in jse jar, improve bindings implementation
|
<li>JSR-223 fixes: add META-INF/services entry in jse jar, improve bindings implementation </li>
|
||||||
</ul></td></tr>
|
</ul></td></tr>
|
||||||
<tr valign="top"><td> <b>2.0.2</b></td><td><ul>
|
<tr valign="top"><td> <b>2.0.2</b></td><td><ul>
|
||||||
<li>JSR-223 bindings change: non Java-primitives will now be passed as LuaValue
|
<li>JSR-223 bindings change: non Java-primitives will now be passed as LuaValue </li>
|
||||||
<li>Fix selection logic when picking from functions with same name
|
<li>JSR-223 enhancement: allow both ".lua" and "lua" as extensions in getScriptEngine() </li>
|
||||||
|
<li>Improve selection logic when binding to overloaded functions using luajava</li>
|
||||||
|
<li>Enhance javadoc, put it <a href="http://luaj.sourceforge.net/api/2.0/index.html">online</a>
|
||||||
|
and <a href="docs/api/index.html">in distribution</a>
|
||||||
|
</li>
|
||||||
|
<li>Add lib/luaj-sources-2.0.2.jar for easier integration into an IDE such as Netbeans </li>
|
||||||
</ul></td></tr>
|
</ul></td></tr>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
<h2>Known Issues</h2>
|
<h2>Known Issues</h2>
|
||||||
<ul>
|
<ul>
|
||||||
<li>debug code may not be completely removed by some obfuscators
|
<li>debug code may not be completely removed by some obfuscators
|
||||||
|
|||||||
@@ -155,7 +155,7 @@ public class BaseLib extends OneArgFunction implements ResourceFinder {
|
|||||||
return c.getResourceAsStream(filename.startsWith("/")? filename: "/"+filename);
|
return c.getResourceAsStream(filename.startsWith("/")? filename: "/"+filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final class BaseLib2 extends TwoArgFunction {
|
static final class BaseLib2 extends TwoArgFunction {
|
||||||
public LuaValue call(LuaValue arg1, LuaValue arg2) {
|
public LuaValue call(LuaValue arg1, LuaValue arg2) {
|
||||||
switch ( opcode ) {
|
switch ( opcode ) {
|
||||||
case 0: // "collectgarbage", // ( opt [,arg] ) -> value
|
case 0: // "collectgarbage", // ( opt [,arg] ) -> value
|
||||||
@@ -202,7 +202,7 @@ public class BaseLib extends OneArgFunction implements ResourceFinder {
|
|||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final class BaseLibV extends VarArgFunction {
|
static final class BaseLibV extends VarArgFunction {
|
||||||
public BaseLib baselib;
|
public BaseLib baselib;
|
||||||
public Varargs invoke(Varargs args) {
|
public Varargs invoke(Varargs args) {
|
||||||
switch ( opcode ) {
|
switch ( opcode ) {
|
||||||
|
|||||||
@@ -247,7 +247,7 @@ public class IoLib extends OneArgFunction {
|
|||||||
((IoLibV) t.get(k[i])).iolib = this;
|
((IoLibV) t.get(k[i])).iolib = this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final class IoLibV extends VarArgFunction {
|
static final class IoLibV extends VarArgFunction {
|
||||||
public IoLib iolib;
|
public IoLib iolib;
|
||||||
public IoLibV() {
|
public IoLibV() {
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -104,7 +104,7 @@ public class MathLib extends OneArgFunction {
|
|||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final class MathLib1 extends OneArgFunction {
|
static final class MathLib1 extends OneArgFunction {
|
||||||
public LuaValue call(LuaValue arg) {
|
public LuaValue call(LuaValue arg) {
|
||||||
switch ( opcode ) {
|
switch ( opcode ) {
|
||||||
case 0: return valueOf(Math.abs(arg.checkdouble()));
|
case 0: return valueOf(Math.abs(arg.checkdouble()));
|
||||||
@@ -122,7 +122,7 @@ public class MathLib extends OneArgFunction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final class MathLib2 extends TwoArgFunction {
|
static final class MathLib2 extends TwoArgFunction {
|
||||||
protected MathLib mathlib;
|
protected MathLib mathlib;
|
||||||
public LuaValue call(LuaValue arg1, LuaValue arg2) {
|
public LuaValue call(LuaValue arg1, LuaValue arg2) {
|
||||||
switch ( opcode ) {
|
switch ( opcode ) {
|
||||||
@@ -189,7 +189,7 @@ public class MathLib extends OneArgFunction {
|
|||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final class MathLibV extends VarArgFunction {
|
static final class MathLibV extends VarArgFunction {
|
||||||
protected MathLib mathlib;
|
protected MathLib mathlib;
|
||||||
public Varargs invoke(Varargs args) {
|
public Varargs invoke(Varargs args) {
|
||||||
switch ( opcode ) {
|
switch ( opcode ) {
|
||||||
|
|||||||
@@ -126,7 +126,7 @@ public class PackageLib extends OneArgFunction {
|
|||||||
return env;
|
return env;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final class PkgLib1 extends OneArgFunction {
|
static final class PkgLib1 extends OneArgFunction {
|
||||||
PackageLib lib;
|
PackageLib lib;
|
||||||
public PkgLib1(LuaValue env,String name, int opcode, PackageLib lib) {
|
public PkgLib1(LuaValue env,String name, int opcode, PackageLib lib) {
|
||||||
this.env = env;
|
this.env = env;
|
||||||
@@ -151,7 +151,7 @@ public class PackageLib extends OneArgFunction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final class PkgLibV extends VarArgFunction {
|
static final class PkgLibV extends VarArgFunction {
|
||||||
PackageLib lib;
|
PackageLib lib;
|
||||||
public PkgLibV(LuaValue env,String name, int opcode, PackageLib lib) {
|
public PkgLibV(LuaValue env,String name, int opcode, PackageLib lib) {
|
||||||
this.env = env;
|
this.env = env;
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ public class StringLib extends OneArgFunction {
|
|||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final class StringLib1 extends OneArgFunction {
|
static final class StringLib1 extends OneArgFunction {
|
||||||
public LuaValue call(LuaValue arg) {
|
public LuaValue call(LuaValue arg) {
|
||||||
switch ( opcode ) {
|
switch ( opcode ) {
|
||||||
case 0: return dump(arg); // dump (function)
|
case 0: return dump(arg); // dump (function)
|
||||||
@@ -95,7 +95,7 @@ public class StringLib extends OneArgFunction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final class StringLibV extends VarArgFunction {
|
static final class StringLibV extends VarArgFunction {
|
||||||
public Varargs invoke(Varargs args) {
|
public Varargs invoke(Varargs args) {
|
||||||
switch ( opcode ) {
|
switch ( opcode ) {
|
||||||
case 0: return StringLib.byte_( args );
|
case 0: return StringLib.byte_( args );
|
||||||
@@ -319,7 +319,7 @@ public class StringLib extends OneArgFunction {
|
|||||||
|
|
||||||
private static final String FLAGS = "-+ #0";
|
private static final String FLAGS = "-+ #0";
|
||||||
|
|
||||||
private static class FormatDesc {
|
static class FormatDesc {
|
||||||
|
|
||||||
private boolean leftAdjust;
|
private boolean leftAdjust;
|
||||||
private boolean zeroPad;
|
private boolean zeroPad;
|
||||||
@@ -810,7 +810,7 @@ public class StringLib extends OneArgFunction {
|
|||||||
CHAR_TABLE['\f'] |= MASK_SPACE;
|
CHAR_TABLE['\f'] |= MASK_SPACE;
|
||||||
};
|
};
|
||||||
|
|
||||||
private static class MatchState {
|
static class MatchState {
|
||||||
final LuaString s;
|
final LuaString s;
|
||||||
final LuaString p;
|
final LuaString p;
|
||||||
final Varargs args;
|
final Varargs args;
|
||||||
|
|||||||
@@ -83,7 +83,7 @@ public class TableLib extends OneArgFunction {
|
|||||||
return NIL;
|
return NIL;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final class TableLibV extends VarArgFunction {
|
static final class TableLibV extends VarArgFunction {
|
||||||
public Varargs invoke(Varargs args) {
|
public Varargs invoke(Varargs args) {
|
||||||
switch ( opcode ) {
|
switch ( opcode ) {
|
||||||
case 0: { // "remove" (table [, pos]) -> removed-ele
|
case 0: { // "remove" (table [, pos]) -> removed-ele
|
||||||
|
|||||||
@@ -123,14 +123,14 @@ public class LuajavaLib extends VarArgFunction {
|
|||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
case BINDCLASS: {
|
case BINDCLASS: {
|
||||||
final Class clazz = Class.forName(args.checkjstring(1));
|
final Class clazz = classForName(args.checkjstring(1));
|
||||||
return toUserdata( clazz, clazz );
|
return toUserdata( clazz, clazz );
|
||||||
}
|
}
|
||||||
case NEWINSTANCE:
|
case NEWINSTANCE:
|
||||||
case NEW: {
|
case NEW: {
|
||||||
// get constructor
|
// get constructor
|
||||||
final LuaValue c = args.checkvalue(1);
|
final LuaValue c = args.checkvalue(1);
|
||||||
final Class clazz = (opcode==NEWINSTANCE? Class.forName(c.tojstring()): (Class) c.checkuserdata(Class.class));
|
final Class clazz = (opcode==NEWINSTANCE? classForName(c.tojstring()): (Class) c.checkuserdata(Class.class));
|
||||||
final Varargs consargs = args.subargs(2);
|
final Varargs consargs = args.subargs(2);
|
||||||
final long paramssig = LuajavaLib.paramsSignatureOf( consargs );
|
final long paramssig = LuajavaLib.paramsSignatureOf( consargs );
|
||||||
final Constructor con = resolveConstructor( clazz, paramssig );
|
final Constructor con = resolveConstructor( clazz, paramssig );
|
||||||
@@ -153,7 +153,7 @@ public class LuajavaLib extends VarArgFunction {
|
|||||||
// get the interfaces
|
// get the interfaces
|
||||||
final Class[] ifaces = new Class[niface];
|
final Class[] ifaces = new Class[niface];
|
||||||
for ( int i=0; i<niface; i++ )
|
for ( int i=0; i<niface; i++ )
|
||||||
ifaces[i] = Class.forName(args.checkjstring(i+1));
|
ifaces[i] = classForName(args.checkjstring(i+1));
|
||||||
|
|
||||||
// create the invocation handler
|
// create the invocation handler
|
||||||
InvocationHandler handler = new InvocationHandler() {
|
InvocationHandler handler = new InvocationHandler() {
|
||||||
@@ -193,7 +193,7 @@ public class LuajavaLib extends VarArgFunction {
|
|||||||
// get constructor
|
// get constructor
|
||||||
String classname = args.checkjstring(1);
|
String classname = args.checkjstring(1);
|
||||||
String methodname = args.checkjstring(2);
|
String methodname = args.checkjstring(2);
|
||||||
Class clazz = Class.forName(classname);
|
Class clazz = classForName(classname);
|
||||||
Method method = clazz.getMethod(methodname, new Class[] {});
|
Method method = clazz.getMethod(methodname, new Class[] {});
|
||||||
Object result = method.invoke(clazz, new Object[] {});
|
Object result = method.invoke(clazz, new Object[] {});
|
||||||
if ( result instanceof LuaValue ) {
|
if ( result instanceof LuaValue ) {
|
||||||
@@ -214,6 +214,11 @@ public class LuajavaLib extends VarArgFunction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// load classes using app loader to allow luaj to be used as an extension
|
||||||
|
protected Class classForName(String name) throws ClassNotFoundException {
|
||||||
|
return Class.forName(name, true, ClassLoader.getSystemClassLoader());
|
||||||
|
}
|
||||||
|
|
||||||
// params signature is
|
// params signature is
|
||||||
// - low 6-bits are number of parameters
|
// - low 6-bits are number of parameters
|
||||||
// - each of next 9 6-bit fields encode a parameter type:
|
// - each of next 9 6-bit fields encode a parameter type:
|
||||||
|
|||||||
@@ -22,7 +22,6 @@
|
|||||||
package org.luaj.vm2.script;
|
package org.luaj.vm2.script;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.script.ScriptEngine;
|
import javax.script.ScriptEngine;
|
||||||
@@ -33,7 +32,10 @@ import javax.script.ScriptEngineFactory;
|
|||||||
*/
|
*/
|
||||||
public class LuaScriptEngineFactory implements ScriptEngineFactory {
|
public class LuaScriptEngineFactory implements ScriptEngineFactory {
|
||||||
|
|
||||||
private static final String FILEEXT = ".lua";
|
private static final String [] EXTENSIONS = {
|
||||||
|
"lua",
|
||||||
|
".lua",
|
||||||
|
};
|
||||||
|
|
||||||
private static final String [] MIMETYPES = {
|
private static final String [] MIMETYPES = {
|
||||||
"text/plain",
|
"text/plain",
|
||||||
@@ -54,7 +56,7 @@ public class LuaScriptEngineFactory implements ScriptEngineFactory {
|
|||||||
|
|
||||||
|
|
||||||
public LuaScriptEngineFactory() {
|
public LuaScriptEngineFactory() {
|
||||||
extensions = Collections.nCopies(1, FILEEXT);
|
extensions = Arrays.asList(EXTENSIONS);
|
||||||
mimeTypes = Arrays.asList(MIMETYPES);
|
mimeTypes = Arrays.asList(MIMETYPES);
|
||||||
names = Arrays.asList(NAMES);
|
names = Arrays.asList(NAMES);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -267,30 +267,82 @@ public class LuaJavaCoercionTest extends TestCase {
|
|||||||
assertEquals( "152415787532388367501905199875019052100", sc );
|
assertEquals( "152415787532388367501905199875019052100", sc );
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class A {
|
public static class A {
|
||||||
public String set( int foo ) {
|
}
|
||||||
return "from set(int) "+foo;
|
public static class B extends A{
|
||||||
}
|
public String set( Object x ) { return "set(Object) "; }
|
||||||
public String set( String foo ) {
|
public String set( String x ) { return "set(String) "+x; }
|
||||||
return "from set(String) "+foo;
|
public String set( A x ) { return "set(A) "; }
|
||||||
}
|
public String set( B x ) { return "set(B) "; }
|
||||||
public String get() {
|
public String set( C x ) { return "set(C) "; }
|
||||||
return "bar";
|
public String set( byte x ) { return "set(byte) "+x; }
|
||||||
}
|
public String set( char x ) { return "set(char) "+x; }
|
||||||
|
public String set( short x ) { return "set(short) "+x; }
|
||||||
|
public String set( int x ) { return "set(int) "+x; }
|
||||||
|
public String set( long x ) { return "set(long) "+x; }
|
||||||
|
public String set( float x ) { return "set(float) "+x; }
|
||||||
|
public String set( double x ) { return "set(double) "+x; }
|
||||||
|
|
||||||
|
public String setr( double x ) { return "setr(double) "+x; }
|
||||||
|
public String setr( float x ) { return "setr(float) "+x; }
|
||||||
|
public String setr( long x ) { return "setr(long) "+x; }
|
||||||
|
public String setr( int x ) { return "setr(int) "+x; }
|
||||||
|
public String setr( short x ) { return "setr(short) "+x; }
|
||||||
|
public String setr( char x ) { return "setr(char) "+x; }
|
||||||
|
public String setr( byte x ) { return "setr(byte) "+x; }
|
||||||
|
public String setr( C x ) { return "setr(C) "; }
|
||||||
|
public String setr( B x ) { return "setr(B) "; }
|
||||||
|
public String setr( A x ) { return "setr(A) "; }
|
||||||
|
public String setr( String x ) { return "setr(String) "+x; }
|
||||||
|
public String setr( Object x ) { return "setr(Object) "; }
|
||||||
|
|
||||||
|
public Object getObject() { return new Object(); }
|
||||||
|
public String getString() { return "abc"; }
|
||||||
|
public A getA() { return new A(); }
|
||||||
|
public B getB() { return new B(); }
|
||||||
|
public C getC() { return new C(); }
|
||||||
|
public byte getbyte() { return 1; }
|
||||||
|
public char getchar() { return 2; }
|
||||||
|
public short getshort() { return 3; }
|
||||||
|
public int getint() { return 4; }
|
||||||
|
public long getlong() { return 5; }
|
||||||
|
public float getfloat() { return 6.5f; }
|
||||||
|
public double getdouble() { return 7.5; }
|
||||||
|
}
|
||||||
|
public static class C extends B {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testOverloadedJavaMethods() {
|
public void testOverloadedJavaMethodObject() { doOverloadedMethodTest( "Object", "" ); }
|
||||||
|
public void testOverloadedJavaMethodString() { doOverloadedMethodTest( "String", "abc" ); }
|
||||||
|
public void testOverloadedJavaMethodA() { doOverloadedMethodTest( "A", "" ); }
|
||||||
|
public void testOverloadedJavaMethodB() { doOverloadedMethodTest( "B", "" ); }
|
||||||
|
public void testOverloadedJavaMethodC() { doOverloadedMethodTest( "C", "" ); }
|
||||||
|
public void testOverloadedJavaMethodByte() { doOverloadedMethodTest( "byte", "1" ); }
|
||||||
|
public void testOverloadedJavaMethodChar() { doOverloadedMethodTest( "char", "2" ); }
|
||||||
|
public void testOverloadedJavaMethodShort() { doOverloadedMethodTest( "short", "3" ); }
|
||||||
|
public void testOverloadedJavaMethodInt() { doOverloadedMethodTest( "int", "4" ); }
|
||||||
|
public void testOverloadedJavaMethodLong() { doOverloadedMethodTest( "long", "5" ); }
|
||||||
|
public void testOverloadedJavaMethodFloat() { doOverloadedMethodTest( "float", "6.5" ); }
|
||||||
|
public void testOverloadedJavaMethodDouble() { doOverloadedMethodTest( "double", "7.5" ); }
|
||||||
|
|
||||||
|
private void doOverloadedMethodTest( String typename, String value ) {
|
||||||
String script =
|
String script =
|
||||||
"a = luajava.newInstance('"+A.class.getName()+"');\n" +
|
"local a = luajava.newInstance('"+B.class.getName()+"');\n" +
|
||||||
"return a:set(a:get())";
|
"local b = a:set(a:get"+typename+"())\n" +
|
||||||
|
"local c = a:setr(a:get"+typename+"())\n" +
|
||||||
|
"return b,c";
|
||||||
Varargs chunk = _G.get("loadstring").call(LuaValue.valueOf(script));
|
Varargs chunk = _G.get("loadstring").call(LuaValue.valueOf(script));
|
||||||
if ( ! chunk.arg1().toboolean() )
|
if ( ! chunk.arg1().toboolean() )
|
||||||
fail( chunk.arg(2).toString() );
|
fail( chunk.arg(2).toString() );
|
||||||
Varargs results = chunk.arg1().invoke();
|
Varargs results = chunk.arg1().invoke();
|
||||||
int nresults = results.narg();
|
int nresults = results.narg();
|
||||||
String sa = results.tojstring(1);
|
assertEquals( 2, nresults );
|
||||||
assertEquals( 1, nresults );
|
LuaValue b = results.arg(1);
|
||||||
assertEquals( "from set(String) bar", sa );
|
LuaValue c = results.arg(2);
|
||||||
|
String sb = b.tojstring();
|
||||||
|
String sc = c.tojstring();
|
||||||
|
assertEquals( "set("+typename+") "+value, sb );
|
||||||
|
assertEquals( "setr("+typename+") "+value, sc );
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user