From 933a978146a8e5f5e78ac41d287e869fd02cada8 Mon Sep 17 00:00:00 2001 From: LatvianModder Date: Tue, 2 Jul 2024 17:46:32 +0300 Subject: [PATCH] Added RecordTypeInfo#setGlobalDefaultValue, few more JSTypeInfos --- .../java/dev/latvian/mods/rhino/Context.java | 2 +- .../rhino/type/JSBasicConstantTypeInfo.java | 24 +++++++++++++++++++ .../rhino/type/JSNumberConstantTypeInfo.java | 19 +++++++++++++++ .../rhino/type/JSStringConstantTypeInfo.java | 23 ++++++++++++++++++ .../mods/rhino/type/RecordTypeInfo.java | 18 ++++++++++++-- 5 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 src/main/java/dev/latvian/mods/rhino/type/JSBasicConstantTypeInfo.java create mode 100644 src/main/java/dev/latvian/mods/rhino/type/JSNumberConstantTypeInfo.java create mode 100644 src/main/java/dev/latvian/mods/rhino/type/JSStringConstantTypeInfo.java diff --git a/src/main/java/dev/latvian/mods/rhino/Context.java b/src/main/java/dev/latvian/mods/rhino/Context.java index 6d00670c..d98b924f 100644 --- a/src/main/java/dev/latvian/mods/rhino/Context.java +++ b/src/main/java/dev/latvian/mods/rhino/Context.java @@ -1321,7 +1321,7 @@ public Object mapOf(@Nullable Object from, TypeInfo kTarget, TypeInfo vTarget) { protected Object classOf(Object from) { if (from instanceof NativeJavaClass n) { - return n; + return n.getClassObject(); } else if (from instanceof Class c) { if (visibleToScripts(c.getName(), ClassVisibilityContext.ARGUMENT)) { return c; diff --git a/src/main/java/dev/latvian/mods/rhino/type/JSBasicConstantTypeInfo.java b/src/main/java/dev/latvian/mods/rhino/type/JSBasicConstantTypeInfo.java new file mode 100644 index 00000000..6f7ecc4f --- /dev/null +++ b/src/main/java/dev/latvian/mods/rhino/type/JSBasicConstantTypeInfo.java @@ -0,0 +1,24 @@ +package dev.latvian.mods.rhino.type; + +// null, undefined, true, false +public record JSBasicConstantTypeInfo(String value) implements TypeInfo { + public static final JSBasicConstantTypeInfo NULL = new JSBasicConstantTypeInfo("null"); + public static final JSBasicConstantTypeInfo UNDEFINED = new JSBasicConstantTypeInfo("undefined"); + public static final JSBasicConstantTypeInfo TRUE = new JSBasicConstantTypeInfo("true"); + public static final JSBasicConstantTypeInfo FALSE = new JSBasicConstantTypeInfo("false"); + + @Override + public Class asClass() { + return TypeInfo.class; + } + + @Override + public String toString() { + return value; + } + + @Override + public void append(TypeStringContext ctx, StringBuilder sb) { + sb.append(value); + } +} diff --git a/src/main/java/dev/latvian/mods/rhino/type/JSNumberConstantTypeInfo.java b/src/main/java/dev/latvian/mods/rhino/type/JSNumberConstantTypeInfo.java new file mode 100644 index 00000000..e0d44781 --- /dev/null +++ b/src/main/java/dev/latvian/mods/rhino/type/JSNumberConstantTypeInfo.java @@ -0,0 +1,19 @@ +package dev.latvian.mods.rhino.type; + +// 10, -402.01 +public record JSNumberConstantTypeInfo(Number number) implements TypeInfo { + @Override + public Class asClass() { + return TypeInfo.class; + } + + @Override + public String toString() { + return number.toString(); + } + + @Override + public void append(TypeStringContext ctx, StringBuilder sb) { + sb.append(number); + } +} diff --git a/src/main/java/dev/latvian/mods/rhino/type/JSStringConstantTypeInfo.java b/src/main/java/dev/latvian/mods/rhino/type/JSStringConstantTypeInfo.java new file mode 100644 index 00000000..2fcb9584 --- /dev/null +++ b/src/main/java/dev/latvian/mods/rhino/type/JSStringConstantTypeInfo.java @@ -0,0 +1,23 @@ +package dev.latvian.mods.rhino.type; + +import dev.latvian.mods.rhino.ScriptRuntime; + +// "abc" +public record JSStringConstantTypeInfo(String constant) implements TypeInfo { + public static final JSStringConstantTypeInfo EMPTY = new JSStringConstantTypeInfo(""); + + @Override + public Class asClass() { + return TypeInfo.class; + } + + @Override + public String toString() { + return ScriptRuntime.escapeAndWrapString(constant); + } + + @Override + public void append(TypeStringContext ctx, StringBuilder sb) { + sb.append(ScriptRuntime.escapeAndWrapString(constant)); + } +} diff --git a/src/main/java/dev/latvian/mods/rhino/type/RecordTypeInfo.java b/src/main/java/dev/latvian/mods/rhino/type/RecordTypeInfo.java index e7e97db3..1b655482 100644 --- a/src/main/java/dev/latvian/mods/rhino/type/RecordTypeInfo.java +++ b/src/main/java/dev/latvian/mods/rhino/type/RecordTypeInfo.java @@ -17,9 +17,23 @@ import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.Set; import java.util.function.Consumer; public class RecordTypeInfo extends ClassTypeInfo implements TypeWrapperFactory { + private static final Map, Object> GLOBAL_DEFAULT_VALUES = new IdentityHashMap<>(); + + public static void setGlobalDefaultValue(Class type, T value) { + GLOBAL_DEFAULT_VALUES.put(type, value); + } + + static { + setGlobalDefaultValue(Optional.class, Optional.empty()); + setGlobalDefaultValue(List.class, List.of()); + setGlobalDefaultValue(Set.class, Set.of()); + setGlobalDefaultValue(Map.class, Map.of()); + } + public record Component(int index, String name, TypeInfo type) { } @@ -51,8 +65,8 @@ public Data getData() { componentMap.put(c.name, c); defaultArguments[i] = c.type.createDefaultValue(); - if (c.type.is(TypeInfo.RAW_OPTIONAL)) { - defaultArguments[i] = Optional.empty(); + if (defaultArguments[i] == null) { + defaultArguments[i] = GLOBAL_DEFAULT_VALUES.getOrDefault(rc[i].getType(), null); } }