Skip to content

Commit

Permalink
Fix crash that happens with &&/|| in jbcsrc by consolidating logi…
Browse files Browse the repository at this point in the history
…c with visitConditionalOpNode.

PiperOrigin-RevId: 576924134
  • Loading branch information
nicholasyu-google authored and copybara-github committed Oct 26, 2023
1 parent d8948ea commit 67e187b
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions java/src/com/google/template/soy/jbcsrc/ExpressionCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -1090,13 +1090,8 @@ protected SoyExpression visitAndOpNode(AndOpNode node) {

@Override
protected SoyExpression visitAmpAmpOpNode(AmpAmpOpNode node) {
SoyExpression left = visit(node.getChild(0)).box();
SoyExpression right = visit(node.getChild(1)).box();
Branch condition = left.coerceToBoolean().compileToBranch();
return SoyExpression.forSoyValue(
node.getType(),
condition.ternary(
SoyRuntimeType.getBoxedType(node.getType()).runtimeType(), right, left));
return processConditionalOp(
node.getChild(0), node.getChild(1), node.getChild(0), node.getType());
}

@Override
Expand All @@ -1109,13 +1104,8 @@ protected SoyExpression visitOrOpNode(OrOpNode node) {

@Override
protected SoyExpression visitBarBarOpNode(BarBarOpNode node) {
SoyExpression left = visit(node.getChild(0)).box();
SoyExpression right = visit(node.getChild(1)).box();
Branch condition = left.coerceToBoolean().compileToBranch();
return SoyExpression.forSoyValue(
node.getType(),
condition.ternary(
SoyRuntimeType.getBoxedType(node.getType()).runtimeType(), left, right));
return processConditionalOp(
node.getChild(0), node.getChild(0), node.getChild(1), node.getType());
}

// Null coalescing operator
Expand Down Expand Up @@ -1151,9 +1141,18 @@ protected SoyExpression visitNullCoalescingOpNode(NullCoalescingOpNode node) {

@Override
protected SoyExpression visitConditionalOpNode(ConditionalOpNode node) {
Branch condition = visit(node.getChild(0)).compileToBranch();
SoyExpression trueBranch = visit(node.getChild(1));
SoyExpression falseBranch = visit(node.getChild(2));
return processConditionalOp(
node.getChild(0), node.getChild(1), node.getChild(2), node.getType());
}

private SoyExpression processConditionalOp(
ExprNode conditionNode,
ExprNode trueBranchNode,
ExprNode falseBranchNode,
SoyType nodeType) {
Branch condition = visit(conditionNode).compileToBranch();
SoyExpression trueBranch = visit(trueBranchNode);
SoyExpression falseBranch = visit(falseBranchNode);
// If types are == and they are both boxed (or both not boxed) then we can just use them
// directly.
// Otherwise we need to do boxing conversions.
Expand Down Expand Up @@ -1185,9 +1184,9 @@ protected SoyExpression visitConditionalOpNode(ConditionalOpNode node) {
SoyExpression boxedTrue = trueBranch.box();
return boxedTrue.withSource(condition.ternary(type, boxedTrue, falseBranch.box()));
}
Type boxedRuntimeType = SoyRuntimeType.getBoxedType(node.getType()).runtimeType();
Type boxedRuntimeType = SoyRuntimeType.getBoxedType(nodeType).runtimeType();
return SoyExpression.forSoyValue(
node.getType(), condition.ternary(boxedRuntimeType, trueBranch.box(), falseBranch.box()));
nodeType, condition.ternary(boxedRuntimeType, trueBranch.box(), falseBranch.box()));
}

@Override
Expand Down

0 comments on commit 67e187b

Please sign in to comment.