Skip to content

Commit

Permalink
Added RecordTypeInfo#setGlobalDefaultValue, few more JSTypeInfos
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Jul 2, 2024
1 parent 21d3510 commit 933a978
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/main/java/dev/latvian/mods/rhino/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
18 changes: 16 additions & 2 deletions src/main/java/dev/latvian/mods/rhino/type/RecordTypeInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object> {
private static final Map<Class<?>, Object> GLOBAL_DEFAULT_VALUES = new IdentityHashMap<>();

public static <T> void setGlobalDefaultValue(Class<T> 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) {
}

Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 933a978

Please sign in to comment.