diff --git a/src/main/java/dev/latvian/mods/rhino/JavaMembers.java b/src/main/java/dev/latvian/mods/rhino/JavaMembers.java index 435c9b2f..cc4a90f5 100644 --- a/src/main/java/dev/latvian/mods/rhino/JavaMembers.java +++ b/src/main/java/dev/latvian/mods/rhino/JavaMembers.java @@ -24,6 +24,7 @@ import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Set; @@ -580,7 +581,7 @@ private void reflect(Scriptable scope, boolean includeProtected, Context cx) { char ch0 = nameComponent.charAt(0); if (Character.isUpperCase(ch0)) { if (nameComponent.length() == 1) { - beanPropertyName = nameComponent.toLowerCase(); + beanPropertyName = nameComponent.toLowerCase(Locale.ROOT); } else { char ch1 = nameComponent.charAt(1); if (!Character.isUpperCase(ch1)) { diff --git a/src/main/java/dev/latvian/mods/rhino/NativeString.java b/src/main/java/dev/latvian/mods/rhino/NativeString.java index e75e8f21..e00e3547 100644 --- a/src/main/java/dev/latvian/mods/rhino/NativeString.java +++ b/src/main/java/dev/latvian/mods/rhino/NativeString.java @@ -1004,11 +1004,11 @@ public Object execIdCall(IdFunctionObject f, Context cx, Scriptable scope, Scrip } case Id_toLocaleLowerCase: { String thisStr = ScriptRuntime.toString(cx, ScriptRuntimeES6.requireObjectCoercible(cx, thisObj, f)); - return thisStr.toLowerCase(); + return thisStr.toLowerCase(Locale.ROOT); } case Id_toLocaleUpperCase: { String thisStr = ScriptRuntime.toString(cx, ScriptRuntimeES6.requireObjectCoercible(cx, thisObj, f)); - return thisStr.toUpperCase(); + return thisStr.toUpperCase(Locale.ROOT); } case Id_trim: { String str = ScriptRuntime.toString(cx, ScriptRuntimeES6.requireObjectCoercible(cx, thisObj, f)); diff --git a/src/main/java/dev/latvian/mods/rhino/ScriptableObject.java b/src/main/java/dev/latvian/mods/rhino/ScriptableObject.java index 25d16ba0..591274d5 100644 --- a/src/main/java/dev/latvian/mods/rhino/ScriptableObject.java +++ b/src/main/java/dev/latvian/mods/rhino/ScriptableObject.java @@ -30,6 +30,7 @@ import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; +import java.util.Locale; import java.util.Map; /** @@ -757,7 +758,7 @@ private static String getPropertyName(String methodName, String prefix, Annotati propName = methodName.substring(3); if (Character.isUpperCase(propName.charAt(0))) { if (propName.length() == 1) { - propName = propName.toLowerCase(); + propName = propName.toLowerCase(Locale.ROOT); } else if (!Character.isUpperCase(propName.charAt(1))) { propName = Character.toLowerCase(propName.charAt(0)) + propName.substring(1); } diff --git a/src/main/java/dev/latvian/mods/rhino/ast/VariableDeclaration.java b/src/main/java/dev/latvian/mods/rhino/ast/VariableDeclaration.java index cb78b52f..42d188f5 100644 --- a/src/main/java/dev/latvian/mods/rhino/ast/VariableDeclaration.java +++ b/src/main/java/dev/latvian/mods/rhino/ast/VariableDeclaration.java @@ -11,6 +11,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.Locale; /** * A list of one or more var, const or let declarations. @@ -129,6 +130,6 @@ public void setIsStatement(boolean isStatement) { } private String declTypeName() { - return Token.typeToName(type).toLowerCase(); + return Token.typeToName(type).toLowerCase(Locale.ROOT); } } diff --git a/src/main/java/dev/latvian/mods/rhino/type/EnumTypeInfo.java b/src/main/java/dev/latvian/mods/rhino/type/EnumTypeInfo.java index cb972298..6df2f9d5 100644 --- a/src/main/java/dev/latvian/mods/rhino/type/EnumTypeInfo.java +++ b/src/main/java/dev/latvian/mods/rhino/type/EnumTypeInfo.java @@ -4,8 +4,10 @@ import dev.latvian.mods.rhino.util.RemappedEnumConstant; import dev.latvian.mods.rhino.util.wrap.TypeWrapperFactory; +import java.util.HashMap; import java.util.IdentityHashMap; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.stream.Collectors; @@ -25,6 +27,7 @@ public static String getName(Object e) { } private List constants; + private Map constantMap; EnumTypeInfo(Class type) { super(type); @@ -48,21 +51,41 @@ public Object wrap(Context cx, Object from, TypeInfo target) { return null; } - for (var entry : enumConstants()) { + var constants = enumConstants(); + + if (constantMap == null) { + constantMap = new HashMap<>(constants.size()); + + for (var entry : constants) { + var name = getName(entry); + constantMap.put(name.toLowerCase(Locale.ROOT), entry); + constantMap.put(name, entry); + } + } + + var lookup = constantMap.get(s); + + if (lookup != null) { + return lookup; + } + + for (var entry : constants) { if (getName(entry).equalsIgnoreCase(s)) { return entry; } } - throw new IllegalArgumentException("'" + s + "' is not a valid enum constant! Valid values are: " + enumConstants().stream().map(EnumTypeInfo::getName).map(s1 -> "'" + s1 + "'").collect(Collectors.joining(", "))); + throw new IllegalArgumentException("'" + s + "' is not a valid enum constant! Valid values are: " + constants.stream().map(EnumTypeInfo::getName).map(s1 -> "'" + s1 + "'").collect(Collectors.joining(", "))); } else if (from instanceof Number) { int index = ((Number) from).intValue(); - if (index < 0 || index >= enumConstants().size()) { - throw new IllegalArgumentException(index + " is not a valid enum index! Valid values are: 0 - " + (enumConstants().size() - 1)); + var constants = enumConstants(); + + if (index < 0 || index >= constants.size()) { + throw new IllegalArgumentException(index + " is not a valid enum index! Valid values are: 0 - " + (constants.size() - 1)); } - return enumConstants().get(index); + return constants.get(index); } return from; diff --git a/src/main/java/dev/latvian/mods/rhino/util/DefaultValueTypeHint.java b/src/main/java/dev/latvian/mods/rhino/util/DefaultValueTypeHint.java index 2f92da70..3893af5b 100644 --- a/src/main/java/dev/latvian/mods/rhino/util/DefaultValueTypeHint.java +++ b/src/main/java/dev/latvian/mods/rhino/util/DefaultValueTypeHint.java @@ -1,5 +1,7 @@ package dev.latvian.mods.rhino.util; +import java.util.Locale; + public enum DefaultValueTypeHint { STRING, NUMBER, @@ -10,7 +12,7 @@ public enum DefaultValueTypeHint { public final String name; DefaultValueTypeHint() { - name = name().toLowerCase(); + name = name().toLowerCase(Locale.ROOT); } public String toString() {