1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:18:11 +00:00

LibJS: Add short circuit logical evaluation

When evaluating logical binop expressions, the rhs must not be
evaluated if the lhs leads to the whole expression not being
truthy.
This commit is contained in:
Stephan Unverwerth 2020-04-03 19:11:31 +02:00 committed by Andreas Kling
parent a2b0cc8f08
commit 520311eb8b
2 changed files with 28 additions and 4 deletions

View file

@ -282,17 +282,26 @@ Value LogicalExpression::execute(Interpreter& interpreter) const
auto lhs_result = m_lhs->execute(interpreter);
if (interpreter.exception())
return {};
auto rhs_result = m_rhs->execute(interpreter);
if (interpreter.exception())
return {};
switch (m_op) {
case LogicalOp::And:
if (lhs_result.to_boolean())
if (lhs_result.to_boolean()) {
auto rhs_result = m_rhs->execute(interpreter);
if (interpreter.exception())
return {};
return Value(rhs_result);
}
return Value(lhs_result);
case LogicalOp::Or:
if (lhs_result.to_boolean())
return Value(lhs_result);
auto rhs_result = m_rhs->execute(interpreter);
if (interpreter.exception())
return {};
return Value(rhs_result);
}