mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-20 04:09:57 +00:00
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
This commit is contained in:
1 parent
63f6c7c045
commit
182c00b142
2 files changed
+49
No files matched your search
+41
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user