Skip to content
This repository was archived by the owner on Feb 29, 2024. It is now read-only.

Commit 6f9d1cd

Browse files
Louis Yecopybara-github
authored andcommitted
Support short-circuiting binary expression.
PiperOrigin-RevId: 327017140 Change-Id: I79048fa64836d123d1a09550717bf99f370bfa99
1 parent 709e719 commit 6f9d1cd

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/agent/binary_expression_evaluator.cc

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,8 +468,22 @@ ErrorOr<JVariant> BinaryExpressionEvaluator::Evaluate(
468468
return arg1_value;
469469
}
470470

471-
// TODO: support short-circuit evaluation.
472-
// For example in this one: (true || exp), "exp" should never be evaluated.
471+
// Short-circuit the evaluation for "&&" or "||": don't evaluate arg2_ when
472+
// arg1_ can decide the value of the expression.
473+
if (type_ == BinaryJavaExpression::Type::conditional_and ||
474+
type_ == BinaryJavaExpression::Type::conditional_or) {
475+
jboolean arg1_value_boolean;
476+
if (!arg1_value.value().get<jboolean>(&arg1_value_boolean)) {
477+
return INTERNAL_ERROR_MESSAGE;
478+
}
479+
if ((type_ == BinaryJavaExpression::Type::conditional_and &&
480+
arg1_value_boolean == false) ||
481+
(type_ == BinaryJavaExpression::Type::conditional_or &&
482+
arg1_value_boolean == true)) {
483+
return JVariant::Boolean(arg1_value_boolean);
484+
}
485+
}
486+
473487
ErrorOr<JVariant> arg2_value = arg2_->Evaluate(evaluation_context);
474488
if (arg2_value.is_error()) {
475489
return arg2_value;

0 commit comments

Comments
 (0)