Skip to content

Commit

Permalink
Extract helper for unboxing primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
electrum committed Feb 9, 2015
1 parent 6ce854e commit 5f52308
Showing 1 changed file with 31 additions and 18 deletions.
Expand Up @@ -152,7 +152,8 @@ public static ByteCodeNode generateInvocation(CompilerContext context, FunctionI
MethodType methodType = binding.getType(); MethodType methodType = binding.getType();


Signature signature = function.getSignature(); Signature signature = function.getSignature();
Class<?> unboxedReturnType = Primitives.unwrap(methodType.returnType()); Class<?> returnType = methodType.returnType();
Class<?> unboxedReturnType = Primitives.unwrap(returnType);


LabelNode end = new LabelNode("end"); LabelNode end = new LabelNode("end");
Block block = new Block(context) Block block = new Block(context)
Expand Down Expand Up @@ -181,23 +182,35 @@ public static ByteCodeNode generateInvocation(CompilerContext context, FunctionI
block.append(invoke(context, binding, function.getSignature())); block.append(invoke(context, binding, function.getSignature()));


if (function.isNullable()) { if (function.isNullable()) {
if (unboxedReturnType.isPrimitive() && unboxedReturnType != void.class) { block.append(unboxPrimitiveIfNecessary(context, returnType));
LabelNode notNull = new LabelNode("notNull"); }
block.dup(methodType.returnType()) block.visitLabel(end);
.ifNotNullGoto(notNull)
.putVariable("wasNull", true) return block;
.comment("swap boxed null with unboxed default") }
.pop(methodType.returnType())
.pushJavaDefault(unboxedReturnType) public static Block unboxPrimitiveIfNecessary(CompilerContext context, Class<?> boxedType)
.gotoLabel(end) {
.visitLabel(notNull) Block block = new Block(context);
.append(unboxPrimitive(context, unboxedReturnType)); LabelNode end = new LabelNode("end");
} Class<?> unboxedType = Primitives.unwrap(boxedType);
else {
block.dup(methodType.returnType()) if (unboxedType.isPrimitive() && unboxedType != void.class) {
.ifNotNullGoto(end) LabelNode notNull = new LabelNode("notNull");
.putVariable("wasNull", true); block.dup(boxedType)
} .ifNotNullGoto(notNull)
.putVariable("wasNull", true)
.comment("swap boxed null with unboxed default")
.pop(boxedType)
.pushJavaDefault(unboxedType)
.gotoLabel(end)
.visitLabel(notNull)
.append(unboxPrimitive(context, unboxedType));
}
else {
block.dup(boxedType)
.ifNotNullGoto(end)
.putVariable("wasNull", true);
} }
block.visitLabel(end); block.visitLabel(end);


Expand Down

0 comments on commit 5f52308

Please sign in to comment.