fixed bug: invoke overload method disorder
This commit is contained in:
@@ -1,24 +1,24 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2011 Luaj.org. All rights reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
******************************************************************************/
|
||||
* Copyright (c) 2011 Luaj.org. All rights reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
******************************************************************************/
|
||||
package org.luaj.vm2.libs.jse;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
@@ -35,10 +35,10 @@ import org.luaj.vm2.Varargs;
|
||||
/**
|
||||
* LuaValue that represents a Java method.
|
||||
* <p>
|
||||
* Can be invoked via call(LuaValue...) and related methods.
|
||||
* Can be invoked via call(LuaValue...) and related methods.
|
||||
* <p>
|
||||
* This class is not used directly.
|
||||
* It is returned by calls to calls to {@link JavaInstance#get(LuaValue key)}
|
||||
* This class is not used directly.
|
||||
* It is returned by calls to calls to {@link JavaInstance#get(LuaValue key)}
|
||||
* when a method is named.
|
||||
* @see CoerceJavaToLua
|
||||
* @see CoerceLuaToJava
|
||||
@@ -46,20 +46,20 @@ import org.luaj.vm2.Varargs;
|
||||
class JavaMethod extends JavaMember {
|
||||
|
||||
static final Map methods = Collections.synchronizedMap(new HashMap());
|
||||
|
||||
|
||||
static JavaMethod forMethod(Method m) {
|
||||
JavaMethod j = (JavaMethod) methods.get(m);
|
||||
if ( j == null )
|
||||
methods.put( m, j = new JavaMethod(m) );
|
||||
return j;
|
||||
}
|
||||
|
||||
|
||||
static LuaFunction forMethods(JavaMethod[] m) {
|
||||
return new Overload(m);
|
||||
}
|
||||
|
||||
|
||||
final Method method;
|
||||
|
||||
|
||||
private JavaMethod(Method m) {
|
||||
super( m.getParameterTypes(), m.getModifiers() );
|
||||
this.method = m;
|
||||
@@ -81,15 +81,15 @@ class JavaMethod extends JavaMember {
|
||||
public LuaValue call(LuaValue arg1, LuaValue arg2) {
|
||||
return invokeMethod(arg1.checkuserdata(), arg2);
|
||||
}
|
||||
|
||||
|
||||
public LuaValue call(LuaValue arg1, LuaValue arg2, LuaValue arg3) {
|
||||
return invokeMethod(arg1.checkuserdata(), LuaValue.varargsOf(arg2, arg3));
|
||||
}
|
||||
|
||||
|
||||
public Varargs invoke(Varargs args) {
|
||||
return invokeMethod(args.checkuserdata(1), args.subargs(2));
|
||||
}
|
||||
|
||||
|
||||
LuaValue invokeMethod(Object instance, Varargs args) {
|
||||
Object[] a = convertArgs(args);
|
||||
try {
|
||||
@@ -100,22 +100,33 @@ class JavaMethod extends JavaMember {
|
||||
return LuaValue.error("coercion error "+e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* LuaValue that represents an overloaded Java method.
|
||||
* <p>
|
||||
* On invocation, will pick the best method from the list, and invoke it.
|
||||
* <p>
|
||||
* This class is not used directly.
|
||||
* It is returned by calls to calls to {@link JavaInstance#get(LuaValue key)}
|
||||
* This class is not used directly.
|
||||
* It is returned by calls to calls to {@link JavaInstance#get(LuaValue key)}
|
||||
* when an overloaded method is named.
|
||||
*/
|
||||
static class Overload extends LuaFunction {
|
||||
|
||||
final JavaMethod[] methods;
|
||||
|
||||
|
||||
// no parammeter method index in overload method
|
||||
int noArgMethodIndex = -1;
|
||||
|
||||
Overload(JavaMethod[] methods) {
|
||||
this.methods = methods;
|
||||
JavaMethod m;
|
||||
for (int i=0; i<methods.length; i++) {
|
||||
m = methods[i];
|
||||
if (m.fixedargs.length == 0 && m.varargs == null) {
|
||||
noArgMethodIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public LuaValue call() {
|
||||
@@ -129,35 +140,40 @@ class JavaMethod extends JavaMember {
|
||||
public LuaValue call(LuaValue arg1, LuaValue arg2) {
|
||||
return invokeBestMethod(arg1.checkuserdata(), arg2);
|
||||
}
|
||||
|
||||
|
||||
public LuaValue call(LuaValue arg1, LuaValue arg2, LuaValue arg3) {
|
||||
return invokeBestMethod(arg1.checkuserdata(), LuaValue.varargsOf(arg2, arg3));
|
||||
}
|
||||
|
||||
|
||||
public Varargs invoke(Varargs args) {
|
||||
return invokeBestMethod(args.checkuserdata(1), args.subargs(2));
|
||||
}
|
||||
|
||||
private LuaValue invokeBestMethod(Object instance, Varargs args) {
|
||||
JavaMethod best = null;
|
||||
int score = CoerceLuaToJava.SCORE_UNCOERCIBLE;
|
||||
for ( int i=0; i<methods.length; i++ ) {
|
||||
int s = methods[i].score(args);
|
||||
if ( s < score ) {
|
||||
score = s;
|
||||
best = methods[i];
|
||||
if ( score == 0 )
|
||||
break;
|
||||
int n = args.narg();
|
||||
if (n == 0 && noArgMethodIndex != -1) {
|
||||
best = methods[noArgMethodIndex];
|
||||
} else {
|
||||
int score = CoerceLuaToJava.SCORE_UNCOERCIBLE;
|
||||
for ( int i=0; i<methods.length; i++ ) {
|
||||
int s = methods[i].score(args);
|
||||
if ( s < score ) {
|
||||
score = s;
|
||||
best = methods[i];
|
||||
if ( score == 0 )
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// any match?
|
||||
|
||||
// any match?
|
||||
if ( best == null )
|
||||
LuaValue.error("no coercible public method");
|
||||
|
||||
|
||||
// invoke it
|
||||
return best.invokeMethod(instance, args);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user