From ff8128133738c9dc7b669b020385535a1826351f Mon Sep 17 00:00:00 2001 From: bigguy345 Date: Fri, 6 Mar 2026 05:09:36 +0200 Subject: [PATCH] All varargs i.e 'String...' handling is now completely functional --- dts-patches/ecmascript/Array.d.ts | 8 ++-- dts-patches/ecmascript/Math.d.ts | 4 +- dts-patches/ecmascript/String.d.ts | 2 +- .../script/autocomplete/AutocompleteItem.java | 6 ++- .../script/interpreter/ScriptDocument.java | 23 +++++++--- .../script/interpreter/field/FieldInfo.java | 5 +++ .../interpreter/hover/TokenHoverInfo.java | 9 ++++ .../interpreter/js_parser/JSMethodInfo.java | 6 ++- .../js_parser/TypeScriptDefinitionParser.java | 8 +++- .../interpreter/method/MethodCallInfo.java | 44 +++++++++++++++---- .../script/interpreter/method/MethodInfo.java | 17 +++++-- .../type/synthetic/SyntheticMethod.java | 11 +++-- 12 files changed, 110 insertions(+), 33 deletions(-) diff --git a/dts-patches/ecmascript/Array.d.ts b/dts-patches/ecmascript/Array.d.ts index ba57a405e..9a4e7d050 100644 --- a/dts-patches/ecmascript/Array.d.ts +++ b/dts-patches/ecmascript/Array.d.ts @@ -61,7 +61,7 @@ export interface Array { length: number; /** Combines two or more arrays and returns a new array without modifying the original */ - concat(...items: any[]): any[]; + concat(...items: any): any[]; /** Tests whether all elements in the array pass the provided test function */ every(callback: Java.java.util.function.Function): boolean; @@ -88,7 +88,7 @@ export interface Array { pop(): any; /** Adds one or more elements to the end of an array and returns the new length */ - push(...items: any[]): number; + push(...items: any): number; /** Applies a function against an accumulator and each element to reduce the array to a single value */ reduce(callback: Java.java.util.function.BiFunction, initialValue: any): any; @@ -112,11 +112,11 @@ export interface Array { sort(compareFn: Java.java.util.function.BiFunction): any[]; /** Changes the contents of an array by removing or replacing existing elements and/or adding new ones */ - splice(start: number, deleteCount: number, ...items: any[]): any[]; + splice(start: number, deleteCount: number, ...items: any): any[]; /** Returns a string representation of the array */ toString(): string; /** Adds one or more elements to the beginning of an array and returns the new length */ - unshift(...items: any[]): number; + unshift(...items: any): number; } diff --git a/dts-patches/ecmascript/Math.d.ts b/dts-patches/ecmascript/Math.d.ts index 51ab740ff..ff37ed0d7 100644 --- a/dts-patches/ecmascript/Math.d.ts +++ b/dts-patches/ecmascript/Math.d.ts @@ -103,10 +103,10 @@ export interface Math { log(x: number): number; /** Returns the largest of zero or more numbers */ - max(...values: number[]): number; + max(...values: number): number; /** Returns the smallest of zero or more numbers */ - min(...values: number[]): number; + min(...values: number): number; /** Returns base raised to the power of exponent */ pow(x: number, y: number): number; diff --git a/dts-patches/ecmascript/String.d.ts b/dts-patches/ecmascript/String.d.ts index 8a48033e2..549f136a4 100644 --- a/dts-patches/ecmascript/String.d.ts +++ b/dts-patches/ecmascript/String.d.ts @@ -62,7 +62,7 @@ export interface String { length: number; /** Creates a string from one or more Unicode character codes (e.g., String.fromCharCode(65, 66, 67) returns "ABC") */ - static fromCharCode(...codes: number[]): string; + static fromCharCode(...codes: number): string; /** Returns the Unicode code point (0–65535) of the character at the specified index. Unlike Java's charAt() + cast, this returns the numeric code directly. */ charCodeAt(pos: number): number; diff --git a/src/main/java/noppes/npcs/client/gui/util/script/autocomplete/AutocompleteItem.java b/src/main/java/noppes/npcs/client/gui/util/script/autocomplete/AutocompleteItem.java index 707a1e664..0d8c9f9ec 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/autocomplete/AutocompleteItem.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/autocomplete/AutocompleteItem.java @@ -121,6 +121,7 @@ public class AutocompleteItem implements Comparable { String paramType = param.getTypeInfo() != null ? getName(param.getTypeInfo()) : "?"; displayName.append(paramType); + if (param.isVarArg()) displayName.append("..."); if (param.getName() != null && !param.getName().isEmpty()) { displayName.append(" ").append(param.getName()); } @@ -269,6 +270,7 @@ public class AutocompleteItem implements Comparable { // Use resolved type with full generic display TypeInfo paramType = param.getResolvedType(contextType); String paramTypeName = paramType.isResolved() ? paramType.getDisplayName() : param.getType(); + if (param.isVarArg()) paramTypeName = paramTypeName + "..."; displayName.append(paramTypeName).append(" ").append(param.getName()); } displayName.append(")"); @@ -464,7 +466,9 @@ public class AutocompleteItem implements Comparable { FieldInfo param = method.getParameters().get(i); String paramType = param.getTypeInfo() != null ? getName(param.getTypeInfo()) : "?"; - sb.append(paramType).append(" ").append(param.getName()); + sb.append(paramType); + if (param.isVarArg()) sb.append("..."); + sb.append(" ").append(param.getName()); } sb.append(")"); diff --git a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/ScriptDocument.java b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/ScriptDocument.java index 907b740d4..91ee7b195 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/ScriptDocument.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/ScriptDocument.java @@ -1229,10 +1229,14 @@ public class ScriptDocument { while (m.find()) { String typeName = m.group(1); String paramName = m.group(2); + boolean isVarArg = typeName.endsWith("..."); + if (isVarArg) typeName = typeName.substring(0, typeName.length() - 3); TypeInfo typeInfo = resolveType(typeName); // Store the absolute position of the parameter name int paramNameStart = paramListStart + m.start(2); - params.add(FieldInfo.parameter(paramName, typeInfo, paramNameStart, null)); + FieldInfo fieldInfo = FieldInfo.parameter(paramName, typeInfo, paramNameStart, null); + fieldInfo.setVarArg(isVarArg); + params.add(fieldInfo); } return params; } @@ -2295,18 +2299,23 @@ public class ScriptDocument { // Typed: Type name paramName = parts[parts.length - 1]; String typeName = parts[parts.length - 2]; + boolean isVarArg = typeName.endsWith("..."); + if (isVarArg) typeName = typeName.substring(0, typeName.length() - 3); paramType = resolveType(typeName); + int nameOffset = text.indexOf(paramName, paramOffset); + if (nameOffset < 0) nameOffset = paramOffset; + FieldInfo paramInfo = FieldInfo.parameter(paramName, paramType, nameOffset, null); + paramInfo.setVarArg(isVarArg); + scope.addParameter(paramInfo); } else { // Untyped: just name (type will be inferred from expected FI) paramName = parts[0]; + int nameOffset = text.indexOf(paramName, paramOffset); + if (nameOffset < 0) nameOffset = paramOffset; + FieldInfo paramInfo = FieldInfo.parameter(paramName, paramType, nameOffset, null); + scope.addParameter(paramInfo); } - int nameOffset = text.indexOf(paramName, paramOffset); - if (nameOffset < 0) nameOffset = paramOffset; - - FieldInfo paramInfo = FieldInfo.parameter(paramName, paramType, nameOffset, null); - scope.addParameter(paramInfo); - offset = paramOffset + param.length(); } } diff --git a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/field/FieldInfo.java b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/field/FieldInfo.java index 65dbb165c..466f1d8c4 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/field/FieldInfo.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/field/FieldInfo.java @@ -66,6 +66,8 @@ public final class FieldInfo { // Enum constant specific info private EnumConstantInfo enumConstantInfo; + private boolean isVarArg = false; + // Type inference support (for JavaScript "any" typed variables) // When a variable is declared without type (var x;), it starts as "any" but can be // refined through assignments or JSDoc comments @@ -485,6 +487,9 @@ public final class FieldInfo { * Used when type inference determines a more specific type than the declared type. * @param inferredType The refined type based on assignments or JSDoc */ + public void setVarArg(boolean isVarArg) { this.isVarArg = isVarArg; } + public boolean isVarArg() { return isVarArg; } + public void setInferredType(TypeInfo inferredType) { this.inferredType = inferredType; } diff --git a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/hover/TokenHoverInfo.java b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/hover/TokenHoverInfo.java index c8f060803..c172ec9db 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/hover/TokenHoverInfo.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/hover/TokenHoverInfo.java @@ -1083,6 +1083,9 @@ public class TokenHoverInfo { TypeInfo paramType = param.getTypeInfo(); if (paramType != null) { addTypeSegments(paramType); + if (param.isVarArg()) { + addSegment("...", TokenType.DEFAULT.getHexColor()); + } addSegment(" ", TokenType.DEFAULT.getHexColor()); } addSegment(param.getName(), TokenType.PARAMETER.getHexColor()); @@ -1136,6 +1139,9 @@ public class TokenHoverInfo { int paramTypeColor = getColorForClass(paramTypes[i]); String paramTypeName = paramTypes[i].getSimpleName(); splitAndAddTypeName(paramTypeName, paramTypeColor); + if (method.isVarArgs() && i == paramTypes.length - 1) { + addSegment("...", TokenType.DEFAULT.getHexColor()); + } addSegment(" ", TokenType.DEFAULT.getHexColor()); // Try to get parameter name if available String paramName = params.length > i ? params[i].getName() : "arg" + i; @@ -1181,6 +1187,9 @@ public class TokenHoverInfo { TypeInfo paramType = param.getTypeInfo(); if (paramType != null) { addTypeSegments(paramType); + if (param.isVarArg()) { + addSegment("...", TokenType.DEFAULT.getHexColor()); + } addSegment(" ", TokenType.DEFAULT.getHexColor()); } addSegment(param.getName(), TokenType.PARAMETER.getHexColor()); diff --git a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/js_parser/JSMethodInfo.java b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/js_parser/JSMethodInfo.java index 6228f0c0f..4bca83dec 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/js_parser/JSMethodInfo.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/js_parser/JSMethodInfo.java @@ -142,12 +142,16 @@ public class JSMethodInfo { private final String type; private TypeInfo typeInfo; // Resolved TypeInfo (set during Phase 2) private JSMethodInfo containingMethod; // The type that contains the method with this parameter - + private boolean isVarArg = false; + public JSParameterInfo(String name, String type) { this.name = name; this.type = type; } + public void setVarArg(boolean isVarArg) { this.isVarArg = isVarArg; } + public boolean isVarArg() { return isVarArg; } + public void setContainingMethod(JSMethodInfo containingMethod) { this.containingMethod = containingMethod; } diff --git a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/js_parser/TypeScriptDefinitionParser.java b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/js_parser/TypeScriptDefinitionParser.java index f67eea480..ce9689e53 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/js_parser/TypeScriptDefinitionParser.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/js_parser/TypeScriptDefinitionParser.java @@ -630,12 +630,16 @@ public class TypeScriptDefinitionParser { part = part.trim(); if (part.isEmpty()) continue; - // Format: name: type or name?: type + // Format: name: type or name?: type or ...name: type int colonIndex = part.indexOf(':'); if (colonIndex > 0) { String name = part.substring(0, colonIndex).trim().replace("?", ""); String type = cleanType(part.substring(colonIndex + 1).trim()); - result.add(new JSMethodInfo.JSParameterInfo(name, type)); + boolean isVarArg = name.startsWith("..."); + if (isVarArg) name = name.substring(3).trim(); + JSMethodInfo.JSParameterInfo param = new JSMethodInfo.JSParameterInfo(name, type); + param.setVarArg(isVarArg); + result.add(param); } } diff --git a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/method/MethodCallInfo.java b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/method/MethodCallInfo.java index 7480ad466..bd73b497f 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/method/MethodCallInfo.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/method/MethodCallInfo.java @@ -339,8 +339,13 @@ public class MethodCallInfo { setError(ErrorType.UNRESOLVED_METHOD, "Cannot resolve constructor for '" + methodName + "'"); } - } else if (receiverType != null && arguments.size() == resolvedMethod.getParameterCount()) { - validateArgTypeError(); + } else if (receiverType != null) { + List cParams = resolvedMethod.getParameters(); + boolean ctorVarArg = !cParams.isEmpty() && cParams.get(cParams.size() - 1).isVarArg(); + boolean ctorCountOk = ctorVarArg + ? arguments.size() >= cParams.size() - 1 + : arguments.size() == resolvedMethod.getParameterCount(); + if (ctorCountOk) validateArgTypeError(); } return; } @@ -356,12 +361,21 @@ public class MethodCallInfo { List params = resolvedMethod.getParameters(); int expectedCount = params.size(); int actualCount = arguments.size(); + boolean isVarArgMethod = expectedCount > 0 && params.get(expectedCount - 1).isVarArg(); // Check arg count - if (actualCount != expectedCount) { - setError(ErrorType.WRONG_ARG_COUNT, - "Expected " + expectedCount + " argument(s) but got " + actualCount); - return; + if (isVarArgMethod) { + if (actualCount < expectedCount - 1) { + setError(ErrorType.WRONG_ARG_COUNT, + "Expected at least " + (expectedCount - 1) + " argument(s) but got " + actualCount); + return; + } + } else { + if (actualCount != expectedCount) { + setError(ErrorType.WRONG_ARG_COUNT, + "Expected " + expectedCount + " argument(s) but got " + actualCount); + return; + } } // Check each argument type @@ -379,7 +393,10 @@ public class MethodCallInfo { } public void validateArgTypeError() { - // Check each argument type + List params = resolvedMethod.getParameters(); + int paramCount = params.size(); + boolean isVarArgMethod = paramCount > 0 && params.get(paramCount - 1).isVarArg(); + for (int i = 0; i < arguments.size(); i++) { Argument arg = arguments.get(i); @@ -389,10 +406,19 @@ public class MethodCallInfo { continue; // Skip remaining checks for this argument } - FieldInfo para = resolvedMethod.getParameters().get(i); + // Varargs: all args at index >= (paramCount-1) map to the element type of the last param + FieldInfo para; + TypeInfo paramType; + if (isVarArgMethod && i >= paramCount - 1) { + para = params.get(paramCount - 1); + TypeInfo arrType = para.getTypeInfo(); + paramType = (arrType != null && arrType.isArray()) ? arrType.getElementType() : arrType; + } else { + para = params.get(i); + paramType = para.getTypeInfo(); + } TypeInfo argType = arg.getResolvedType(); - TypeInfo paramType = para.getTypeInfo(); if (argType != null && paramType != null) { if (!TypeChecker.isTypeCompatible(paramType, argType)) { setArgTypeError(i, "Expected " + paramType.getDisplayName() + diff --git a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/method/MethodInfo.java b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/method/MethodInfo.java index b06264d1f..a16943e4b 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/method/MethodInfo.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/method/MethodInfo.java @@ -206,7 +206,14 @@ public final class MethodInfo { paramName = jsName; } } - params.add(FieldInfo.reflectionParam(paramName, paramType)); + + FieldInfo fieldInfo = FieldInfo.reflectionParam(paramName, paramType); + boolean isVarArg = method.isVarArgs() && i == genericParamTypes.length - 1; + if (jsParams != null && i < jsParams.size()) { + isVarArg = isVarArg || jsParams.get(i).isVarArg(); + } + fieldInfo.setVarArg(isVarArg); + params.add(fieldInfo); } String documentation = null; @@ -242,7 +249,9 @@ public final class MethodInfo { if (paramType == null) { paramType = TypeInfo.fromClass(constructor.getParameterTypes()[i]); } - params.add(FieldInfo.reflectionParam("arg" + i, paramType)); + FieldInfo fieldInfo = FieldInfo.reflectionParam("arg" + i, paramType); + fieldInfo.setVarArg(constructor.isVarArgs() && i == genericParamTypes.length - 1); + params.add(fieldInfo); } return new MethodInfo(name, returnType, containingType, params, -1, -1, -1, -1, -1, true, true, modifiers, null, null); @@ -272,7 +281,9 @@ public final class MethodInfo { // Use the new getResolvedType method TypeInfo paramType = param.getResolvedType(containingType); - params.add(FieldInfo.reflectionParam(paramName, paramType)); + FieldInfo fieldInfo = FieldInfo.reflectionParam(paramName, paramType); + fieldInfo.setVarArg(param.isVarArg()); + params.add(fieldInfo); } // JS methods are always public (no access modifiers in .d.ts) diff --git a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/type/synthetic/SyntheticMethod.java b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/type/synthetic/SyntheticMethod.java index acae6098e..2a7c35efc 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/type/synthetic/SyntheticMethod.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/type/synthetic/SyntheticMethod.java @@ -62,11 +62,16 @@ public class SyntheticMethod { public MethodInfo toMethodInfo(TypeInfo containingType) { List paramInfos = new ArrayList<>(); for (SyntheticParameter param : parameters) { - TypeInfo paramType = TypeResolver.getInstance().resolve(param.typeName); + String typeName = param.typeName; + boolean isVarArg = typeName != null && typeName.endsWith("..."); + if (isVarArg) typeName = typeName.substring(0, typeName.length() - 3); + TypeInfo paramType = TypeResolver.getInstance().resolve(typeName); if (paramType == null) { - paramType = TypeInfo.unresolved(param.typeName, param.typeName); + paramType = TypeInfo.unresolved(typeName, typeName); } - paramInfos.add(FieldInfo.parameter(param.name, paramType, -1, null)); + FieldInfo fieldInfo = FieldInfo.parameter(param.name, paramType, -1, null); + fieldInfo.setVarArg(isVarArg); + paramInfos.add(fieldInfo); } TypeInfo returnTypeInfo = TypeResolver.getInstance().resolve(returnType);