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

LibJS: Implement basic boolean coercion

We can't expect the conditionals in "if" and "while" statements to
always return a bool, so we need to know how to boolify a JS::Value.
This commit is contained in:
Andreas Kling 2020-03-10 08:46:20 +01:00
parent 386867da9f
commit dc0b091c31
3 changed files with 24 additions and 5 deletions

View file

@ -70,7 +70,7 @@ Value IfStatement::execute(Interpreter& interpreter) const
{
auto predicate_result = m_predicate->execute(interpreter);
if (predicate_result.as_bool())
if (predicate_result.to_boolean())
return interpreter.run(*m_consequent);
else
return interpreter.run(*m_alternate);
@ -79,7 +79,7 @@ Value IfStatement::execute(Interpreter& interpreter) const
Value WhileStatement::execute(Interpreter& interpreter) const
{
Value last_value = js_undefined();
while (m_predicate->execute(interpreter).as_bool()) {
while (m_predicate->execute(interpreter).to_boolean()) {
last_value = interpreter.run(*m_body);
}
@ -213,9 +213,8 @@ Value BinaryExpression::execute(Interpreter& interpreter) const
Value LogicalExpression::execute(Interpreter& interpreter) const
{
auto lhs_result = m_lhs->execute(interpreter).as_bool();
auto rhs_result = m_rhs->execute(interpreter).as_bool();
auto lhs_result = m_lhs->execute(interpreter).to_boolean();
auto rhs_result = m_rhs->execute(interpreter).to_boolean();
switch (m_op) {
case LogicalOp::And:
return Value(lhs_result && rhs_result);