mirror of
https://github.com/RGBCube/serenity
synced 2025-07-01 11:12:07 +00:00
LibJS: Implement nullish coalescing operator (??)
This commit is contained in:
parent
1806592d58
commit
d14ddb6461
4 changed files with 60 additions and 12 deletions
|
@ -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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue