fixed bug: invoke overload method disorder

This commit is contained in:
UnlegitDqrk
2026-03-01 12:59:23 +01:00
parent ba3d1d8ef9
commit 068451886d

View File

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