Skip to content

Commit 450c8d0

Browse files
committed
Clean up unboxing in bridge compiler.
1 parent 66a2986 commit 450c8d0

2 files changed

Lines changed: 11 additions & 22 deletions

File tree

src/main/java/org/byteskript/skript/compiler/BridgeCompiler.java

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -148,23 +148,14 @@ protected Class<?> getUnboxingType(Class<?> primitive) {
148148
}
149149

150150
protected void unbox(MethodVisitor visitor, Class<?> parameter) {
151+
if (!parameter.isPrimitive()) return;
151152
final String source = Type.getInternalName(OperatorHandler.class);
152-
if (parameter == byte.class)
153-
visitor.visitMethodInsn(184, source, "unboxB", "(Ljava/lang/Number;)B", false);
154-
if (parameter == short.class)
155-
visitor.visitMethodInsn(184, source, "unboxS", "(Ljava/lang/Number;)S", false);
156-
if (parameter == int.class)
157-
visitor.visitMethodInsn(184, source, "unboxI", "(Ljava/lang/Number;)I", false);
158-
if (parameter == long.class)
159-
visitor.visitMethodInsn(184, source, "unboxJ", "(Ljava/lang/Number;)J", false);
160-
if (parameter == float.class)
161-
visitor.visitMethodInsn(184, source, "unboxF", "(Ljava/lang/Number;)F", false);
162-
if (parameter == double.class)
163-
visitor.visitMethodInsn(184, source, "unboxD", "(Ljava/lang/Number;)D", false);
164-
if (parameter == boolean.class)
165-
visitor.visitMethodInsn(184, source, "unbox", "(Ljava/lang/Boolean;)Z", false);
166-
if (parameter == char.class)
167-
visitor.visitMethodInsn(184, source, "unbox", "(Ljava/lang/Character;)C", false);
153+
final Class<?> wrapper, prescriptive;
154+
prescriptive = this.getWrapperType(parameter);
155+
if (Number.class.isAssignableFrom(prescriptive)) wrapper = Number.class;
156+
else wrapper = prescriptive;
157+
final String code = Type.getDescriptor(parameter);
158+
visitor.visitMethodInsn(184, source, "unbox" + code, '(' + Type.getDescriptor(wrapper) + ')' + code, false);
168159
}
169160

170161
//region Utilities
@@ -197,10 +188,8 @@ protected Class<?> getWrapperType(Class<?> primitive) {
197188

198189
protected int wideIndexOffset(Class<?>[] params, Class<?> ret) {
199190
int i = 0;
200-
for (Class<?> param : params) {
201-
i += wideIndexOffset(param);
202-
}
203-
return Math.max(i, wideIndexOffset(ret));
191+
for (Class<?> param : params) i += this.wideIndexOffset(param);
192+
return Math.max(i, this.wideIndexOffset(ret));
204193
}
205194

206195
protected int wideIndexOffset(Class<?> thing) {

src/main/java/org/byteskript/skript/runtime/internal/OperatorHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ public static double unboxD(Number value) {
5959
return value.doubleValue();
6060
}
6161

62-
public static char unbox(Character value) {
62+
public static char unboxC(Character value) {
6363
if (value == null) return 0;
6464
return value;
6565
}
6666

67-
public static boolean unbox(Boolean value) {
67+
public static boolean unboxZ(Boolean value) {
6868
if (value == null) return false;
6969
return value;
7070
}

0 commit comments

Comments
 (0)