This repository was archived by the owner on Feb 29, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +16
-2
lines changed
Expand file tree Collapse file tree 1 file changed +16
-2
lines changed Original file line number Diff line number Diff 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;
You can’t perform that action at this time.
0 commit comments