From fb3d6546e50e3009b717bc1c1e0d5db8ff98bca3 Mon Sep 17 00:00:00 2001 From: bigguy345 Date: Sat, 7 Mar 2026 00:40:04 +0200 Subject: [PATCH] Fixed broken varargs overload selector logic --- .../interpreter/type/OverloadSelector.java | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/type/OverloadSelector.java b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/type/OverloadSelector.java index 4bd105aa8..6400a3f9d 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/type/OverloadSelector.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/type/OverloadSelector.java @@ -21,7 +21,7 @@ public class OverloadSelector { // Scoring constants private static final int ARITY_MISMATCH_BASE = 10000; - private static final int VARARGS_PENALTY = 1000; + private static final int VARARGS_PENALTY = 50; private static final int UNKNOWN_TYPE_PENALTY = 10; private static final int COMPATIBLE_TYPE_PENALTY = 5; private static final int INCOMPATIBLE_TYPE_PENALTY = 100; @@ -208,6 +208,12 @@ public class OverloadSelector { java.lang.reflect.Method javaMethod = method.getJavaMethod(); if (javaMethod != null) { isVarArgs = javaMethod.isVarArgs(); + } else { + // JS methods from .d.ts have no backing Java method; vararg info lives on the last FieldInfo + List checkParams = method.getParameters(); + if (!checkParams.isEmpty()) { + isVarArgs = checkParams.get(checkParams.size() - 1).isVarArg(); + } } // Determine arity applicability @@ -256,6 +262,23 @@ public class OverloadSelector { } } + // For vararg methods, also score extra args (index >= paramCount) against the element type + if (isVarArgs && argCount > paramCount && !params.isEmpty()) { + TypeInfo varargElemType = params.get(paramCount - 1).getTypeInfo(); + for (int i = paramCount; i < argCount; i++) { + TypeInfo argType = (argTypes != null && i < argTypes.length) ? argTypes[i] : null; + if (argType == null || varargElemType == null) { + score += UNKNOWN_TYPE_PENALTY; + } else if (varargElemType.equals(argType)) { + // exact match + } else if (TypeChecker.isTypeCompatible(varargElemType, argType)) { + score += COMPATIBLE_TYPE_PENALTY; + } else { + score += INCOMPATIBLE_TYPE_PENALTY; + } + } + } + // Penalize extra args that don't match params (for non-varargs) if (!isVarArgs && argCount > paramCount) { score += (argCount - paramCount) * INCOMPATIBLE_TYPE_PENALTY;