mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 10:38:11 +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:
parent
386867da9f
commit
dc0b091c31
3 changed files with 24 additions and 5 deletions
|
@ -53,6 +53,25 @@ String Value::to_string() const
|
|||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
bool Value::to_boolean() const
|
||||
{
|
||||
switch (m_type) {
|
||||
case Type::Boolean:
|
||||
return m_value.as_bool;
|
||||
case Type::Number:
|
||||
return m_value.as_double == 0 || m_value.as_double == -0;
|
||||
case Type::Null:
|
||||
case Type::Undefined:
|
||||
return false;
|
||||
case Type::String:
|
||||
return String(as_string()).is_empty();
|
||||
case Type::Object:
|
||||
return true;
|
||||
default:
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
}
|
||||
|
||||
const LogStream& operator<<(const LogStream& stream, const Value& value)
|
||||
{
|
||||
return stream << value.to_string();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue