LibJS: Implement nullish coalescing operator (??)

This commit is contained in:
Linus Groh 2020-04-18 00:49:11 +01:00 committed by Andreas Kling
parent 1806592d58
commit d14ddb6461
Notes: sideshowbarker 2024-07-19 07:31:09 +09:00
4 changed files with 60 additions and 12 deletions

View file

@ -338,20 +338,25 @@ Value LogicalExpression::execute(Interpreter& interpreter) const
auto rhs_result = m_rhs->execute(interpreter);
if (interpreter.exception())
return {};
return Value(rhs_result);
return rhs_result;
}
return Value(lhs_result);
case LogicalOp::Or:
return lhs_result;
case LogicalOp::Or: {
if (lhs_result.to_boolean())
return Value(lhs_result);
return lhs_result;
auto rhs_result = m_rhs->execute(interpreter);
if (interpreter.exception())
return {};
return Value(rhs_result);
return rhs_result;
}
case LogicalOp::NullishCoalescing:
if (lhs_result.is_null() || lhs_result.is_undefined()) {
auto rhs_result = m_rhs->execute(interpreter);
if (interpreter.exception())
return {};
return rhs_result;
}
return lhs_result;
}
ASSERT_NOT_REACHED();
@ -515,6 +520,9 @@ void LogicalExpression::dump(int indent) const
case LogicalOp::Or:
op_string = "||";
break;
case LogicalOp::NullishCoalescing:
op_string = "??";
break;
}
print_indent(indent);