-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added better toString() for TypeInfo, added more generics tests, move…
…d JSOptionalParam outside JSObjectTypeInfo
- Loading branch information
1 parent
e3420d4
commit 763f9dd
Showing
15 changed files
with
286 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 32 additions & 2 deletions
34
src/main/java/dev/latvian/mods/rhino/type/JSFunctionTypeInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,41 @@ | ||
package dev.latvian.mods.rhino.type; | ||
|
||
import java.util.Map; | ||
import java.util.List; | ||
|
||
// (a: string) => void | ||
public record JSFunctionTypeInfo(Map<String, TypeInfo> fields, TypeInfo returnType) implements TypeInfo { | ||
public record JSFunctionTypeInfo(List<JSOptionalParam> params, TypeInfo returnType) implements TypeInfo { | ||
@Override | ||
public Class<?> asClass() { | ||
return TypeInfo.class; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return TypeStringContext.DEFAULT.toString(this); | ||
} | ||
|
||
@Override | ||
public void append(TypeStringContext ctx, StringBuilder sb) { | ||
sb.append('('); | ||
|
||
boolean first = true; | ||
|
||
for (var param : params) { | ||
if (first) { | ||
first = false; | ||
} else { | ||
sb.append(','); | ||
ctx.appendSpace(sb); | ||
} | ||
|
||
param.append(ctx, sb); | ||
} | ||
|
||
sb.append(')'); | ||
ctx.appendSpace(sb); | ||
sb.append('='); | ||
sb.append('>'); | ||
ctx.appendSpace(sb); | ||
ctx.append(sb, returnType); | ||
} | ||
} |
53 changes: 30 additions & 23 deletions
53
src/main/java/dev/latvian/mods/rhino/type/JSObjectTypeInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,48 @@ | ||
package dev.latvian.mods.rhino.type; | ||
|
||
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap; | ||
import java.util.List; | ||
|
||
import java.util.Map; | ||
// {a: string, b?: number} | ||
public record JSObjectTypeInfo(List<JSOptionalParam> fields) implements TypeInfo { | ||
public static JSObjectTypeInfo of(JSOptionalParam field) { | ||
return new JSObjectTypeInfo(List.of(field)); | ||
} | ||
|
||
// {a: string, b: number} | ||
public record JSObjectTypeInfo(Map<String, Field> fields) implements TypeInfo { | ||
public record Field(String name, TypeInfo type, boolean optional) { | ||
public Field(String name, TypeInfo type) { | ||
this(name, type, false); | ||
} | ||
public static JSObjectTypeInfo of(JSOptionalParam field1, JSOptionalParam field2) { | ||
return new JSObjectTypeInfo(List.of(field1, field2)); | ||
} | ||
|
||
public static JSObjectTypeInfo of(Field field) { | ||
return new JSObjectTypeInfo(Map.of(field.name, field)); | ||
public static JSObjectTypeInfo of(JSOptionalParam... fields) { | ||
return new JSObjectTypeInfo(List.of(fields)); | ||
} | ||
|
||
public static JSObjectTypeInfo of(Field field1, Field field2) { | ||
return new JSObjectTypeInfo(Map.of(field1.name, field1, field2.name, field2)); | ||
@Override | ||
public Class<?> asClass() { | ||
return TypeInfo.class; | ||
} | ||
|
||
public static JSObjectTypeInfo of(Field field1, Field field2, Field field3) { | ||
return new JSObjectTypeInfo(Map.of(field1.name, field1, field2.name, field2, field3.name, field3)); | ||
@Override | ||
public String toString() { | ||
return TypeStringContext.DEFAULT.toString(this); | ||
} | ||
|
||
public static JSObjectTypeInfo of(Field... fields) { | ||
var map = new Object2ObjectArrayMap<String, Field>(fields.length); | ||
@Override | ||
public void append(TypeStringContext ctx, StringBuilder sb) { | ||
sb.append('{'); | ||
|
||
boolean first = true; | ||
|
||
for (var field : fields) { | ||
map.put(field.name, field); | ||
if (first) { | ||
first = false; | ||
} else { | ||
sb.append(','); | ||
ctx.appendSpace(sb); | ||
} | ||
|
||
field.append(ctx, sb); | ||
} | ||
|
||
return new JSObjectTypeInfo(map); | ||
} | ||
|
||
@Override | ||
public Class<?> asClass() { | ||
return TypeInfo.class; | ||
sb.append('}'); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/dev/latvian/mods/rhino/type/JSOptionalParam.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package dev.latvian.mods.rhino.type; | ||
|
||
public record JSOptionalParam(String name, TypeInfo type, boolean optional) { | ||
public JSOptionalParam(String name, TypeInfo type) { | ||
this(name, type, false); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
var sb = new StringBuilder(); | ||
append(TypeStringContext.DEFAULT, sb); | ||
return sb.toString(); | ||
} | ||
|
||
public void append(TypeStringContext ctx, StringBuilder sb) { | ||
sb.append(name); | ||
|
||
if (optional) { | ||
sb.append('?'); | ||
} | ||
|
||
sb.append(':'); | ||
ctx.appendSpace(sb); | ||
ctx.append(sb, type); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/main/java/dev/latvian/mods/rhino/type/TypeStringContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package dev.latvian.mods.rhino.type; | ||
|
||
public interface TypeStringContext { | ||
TypeStringContext DEFAULT = new TypeStringContext() { | ||
}; | ||
|
||
default String toString(TypeInfo info) { | ||
var sb = new StringBuilder(); | ||
append(sb, info); | ||
return sb.toString(); | ||
} | ||
|
||
default void append(StringBuilder sb, TypeInfo type) { | ||
type.append(this, sb); | ||
} | ||
|
||
default void appendClassName(StringBuilder sb, ClassTypeInfo type) { | ||
sb.append(type.asClass().getName()); | ||
} | ||
|
||
default void appendSpace(StringBuilder sb) { | ||
sb.append(' '); | ||
} | ||
} |
Oops, something went wrong.