From 182c00b142b0adb6b81ce4d29d3cc9f966afe185 Mon Sep 17 00:00:00 2001 From: bigguy345 Date: Tue, 24 Feb 2026 17:57:01 +0200 Subject: [PATCH] Fix JS 'number' and 'any' compatibility in expression type resolution i.e fixes 'i * offsetMulti' in 'skinOverlay.setOffset(0, 0, i * offsetMulti)' not resolving properly --- .../interpreter/expression/TypeRules.java | 41 +++++++++++++++++++ .../script/interpreter/type/TypeChecker.java | 8 ++++ 2 files changed, 49 insertions(+) diff --git a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/expression/TypeRules.java b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/expression/TypeRules.java index ad1428088..b7bd0dedf 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/expression/TypeRules.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/expression/TypeRules.java @@ -3,9 +3,36 @@ package noppes.npcs.client.gui.util.script.interpreter.expression; import noppes.npcs.client.gui.util.script.interpreter.type.TypeInfo; public class TypeRules { + + private static boolean isJsNumber(TypeInfo type) { + if (type == null || !type.isResolved()) return false; + String name = type.getSimpleName(); + String full = type.getFullName(); + return "number".equals(name) || "number".equals(full); + } + + private static boolean isNumericPrimitiveOrWrapper(TypeInfo type) { + if (type == null || type.getJavaClass() == null) return false; + Class cls = type.getJavaClass(); + return cls == byte.class || cls == Byte.class || + cls == short.class || cls == Short.class || + cls == int.class || cls == Integer.class || + cls == long.class || cls == Long.class || + cls == float.class || cls == Float.class || + cls == double.class || cls == Double.class || + cls == char.class || cls == Character.class; + } + + private static boolean isAny(TypeInfo type) { + if (type == null) return false; + if (type == TypeInfo.ANY) return true; + String full = type.getFullName(); + return "any".equals(full); + } public static boolean isNumeric(TypeInfo type) { if (type == null || !type.isResolved()) return false; + if (isJsNumber(type)) return true; String name = type.getSimpleName(); return "int".equals(name) || "long".equals(name) || "float".equals(name) || "double".equals(name) || "byte".equals(name) || "short".equals(name) || "char".equals(name); @@ -20,6 +47,7 @@ public class TypeRules { public static boolean isFloatingPoint(TypeInfo type) { if (type == null || !type.isResolved()) return false; + if (isJsNumber(type)) return true; String name = type.getSimpleName(); return "float".equals(name) || "double".equals(name); } @@ -37,6 +65,7 @@ public class TypeRules { } public static TypeInfo binaryNumericPromotion(TypeInfo left, TypeInfo right) { + if (isJsNumber(left) || isJsNumber(right)) return TypeInfo.NUMBER; if (left == null || right == null || !left.isResolved() || !right.isResolved()) { return null; } @@ -50,6 +79,7 @@ public class TypeRules { public static TypeInfo unaryNumericPromotion(TypeInfo type) { if (type == null || !type.isResolved()) return null; + if (isJsNumber(type)) return TypeInfo.NUMBER; String name = type.getSimpleName(); if ("double".equals(name) || "float".equals(name) || "long".equals(name)) return type; if ("byte".equals(name) || "short".equals(name) || "char".equals(name) || "int".equals(name)) { @@ -66,6 +96,9 @@ public class TypeRules { if (op == OperatorType.ADD && (isString(left) || isString(right))) { return validateAgainstExpectedType(TypeInfo.fromClass(String.class)); } + if (isAny(left) || isAny(right)) { + return validateAgainstExpectedType(TypeInfo.ANY); + } if (isNumeric(left) && isNumeric(right)) { TypeInfo promoted = binaryNumericPromotion(left, right); return validateAgainstExpectedType(promoted); @@ -76,6 +109,9 @@ public class TypeRules { if (op == OperatorType.EQUALS || op == OperatorType.NOT_EQUALS) { return TypeInfo.fromPrimitive("boolean"); } + if (isAny(left) || isAny(right)) { + return TypeInfo.fromPrimitive("boolean"); + } if (isNumeric(left) && isNumeric(right)) { return TypeInfo.fromPrimitive("boolean"); } @@ -167,6 +203,11 @@ public class TypeRules { if (!sourceType.isResolved()) return false; + if ((isJsNumber(sourceType) && isNumericPrimitiveOrWrapper(targetType)) || + (isJsNumber(targetType) && isNumericPrimitiveOrWrapper(sourceType))) { + return true; + } + // Exact match if (sourceType.equals(targetType)) return true; diff --git a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/type/TypeChecker.java b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/type/TypeChecker.java index 70c6b73f4..c92f384a3 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/type/TypeChecker.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/type/TypeChecker.java @@ -53,6 +53,12 @@ public final class TypeChecker { } // Primitive widening conversions + if ("number".equals(expectedName) && isNumericType(actualName)) { + return true; + } + if ("number".equals(actualName) && isNumericType(expectedName)) { + return true; + } if (isNumericType(expectedName) && isNumericType(actualName)) { return canWiden(actualName, expectedName); } @@ -91,6 +97,7 @@ public final class TypeChecker { */ public static boolean isNumericType(String typeName) { switch (typeName) { + case "number": case "byte": case "Byte": case "short": case "Short": case "int": case "Integer": @@ -125,6 +132,7 @@ public final class TypeChecker { case "int": return 3; case "long": return 4; case "float": return 5; + case "number": case "double": return 6; default: return 0; }