Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Final ASMAPI cleanup, NumberType enum change, and new insertInsnList method #54

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rename NumberType, widen usage with castNumber
  • Loading branch information
Jonathing committed Oct 15, 2024
commit 1695e6cd8df645d9e10bd736a0a7f74c1b985286
27 changes: 21 additions & 6 deletions src/main/java/net/minecraftforge/coremod/api/ASMAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,17 @@ public static MethodInsnNode buildMethodCall(final String ownerName, final Strin
}

/**
* Signifies the type of number constant for a {@link LdcNumberType}.
* Signifies the type of number constant for a {@link NumberType}.
*/
public enum LdcNumberType {
public enum NumberType {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this renamed/does it break compatibility? How old is this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was renamed because it is no longer exclusive to LdcInsnNode. There aren't any versions of Forge that have bumped CoreMods recently to include LdcNumberType, so it doesn't break compatibility.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, then if you're done i can pull this

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it's ready

INTEGER(Number::intValue),
FLOAT(Number::floatValue),
LONG(Number::longValue),
DOUBLE(Number::doubleValue);

private final Function<Number, Object> mapper;

LdcNumberType(Function<Number, Object> mapper) {
NumberType(Function<Number, Object> mapper) {
this.mapper = mapper;
}

Expand All @@ -112,14 +112,29 @@ public Object map(Number number) {
}

/**
* Builds a new {@link LdcInsnNode} with the given number value and type.
* Casts a given number to a given specific {@link NumberType}. This helps elliviate the problems that comes with JavaScript's
* ambiguous number system.
* <p>
* The result is returned as an {@link Object} so it can be used as a value in various instructions that require
* values.
*
* @param value The number to cast
* @param type The type of number to cast to
* @return The casted number
*/
public static Object castNumber(final Number value, final NumberType type) {
return type.map(value);
}

/**
* Builds a new {@link LdcInsnNode} with the given number value and {@link NumberType}.
*
* @param value The number value
* @param type The type of the number
* @return The built LDC node
*/
public static LdcInsnNode buildNumberLdcInsnNode(final Number value, final LdcNumberType type) {
return new LdcInsnNode(type.map(value));
public static LdcInsnNode buildNumberLdcInsnNode(final Number value, final NumberType type) {
return new LdcInsnNode(castNumber(value, type));
}

/**
Expand Down