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

LibJS: Support "hello friends".length

The above snippet is a MemberExpression that necessitates the implicit
construction of a StringObject wrapper around a PrimitiveString.

We then do a property lookup (a "get") on the StringObject, where we
find the "length" property. This is pretty neat! :^)
This commit is contained in:
Andreas Kling 2020-03-11 18:58:19 +01:00
parent 6ec6fa1a02
commit 542108421e
6 changed files with 100 additions and 11 deletions

View file

@ -25,7 +25,10 @@
*/
#include <AK/String.h>
#include <LibJS/Heap.h>
#include <LibJS/Object.h>
#include <LibJS/PrimitiveString.h>
#include <LibJS/StringObject.h>
#include <LibJS/Value.h>
namespace JS {
@ -50,6 +53,9 @@ String Value::to_string() const
return String::format("{%s}", as_object()->class_name());
}
if (is_string())
return m_value.as_string->string();
ASSERT_NOT_REACHED();
}
@ -64,7 +70,7 @@ bool Value::to_boolean() const
case Type::Undefined:
return false;
case Type::String:
return String(as_string()).is_empty();
return !as_string()->string().is_empty();
case Type::Object:
return true;
default:
@ -72,6 +78,17 @@ bool Value::to_boolean() const
}
}
Value Value::to_object(Heap& heap) const
{
if (is_object())
return Value(as_object());
if (is_string())
return Value(heap.allocate<StringObject>(m_value.as_string));
ASSERT_NOT_REACHED();
}
Value greater_than(Value lhs, Value rhs)
{
ASSERT(lhs.is_number());