1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 17:07:34 +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

@ -50,6 +50,7 @@ public:
bool is_string() const { return m_type == Type::String; }
bool is_object() const { return m_type == Type::Object; }
bool is_boolean() const { return m_type == Type::Boolean; }
bool is_cell() const { return is_string() || is_object(); }
explicit Value(bool value)
: m_type(Type::Boolean)
@ -75,6 +76,12 @@ public:
m_value.as_object = object;
}
explicit Value(PrimitiveString* string)
: m_type(Type::String)
{
m_value.as_string = string;
}
explicit Value(Type type)
: m_type(type)
{
@ -106,23 +113,38 @@ public:
return m_value.as_object;
}
const StringImpl* as_string() const
PrimitiveString* as_string()
{
ASSERT(is_string());
return m_value.as_string;
}
const PrimitiveString* as_string() const
{
ASSERT(is_string());
return m_value.as_string;
}
Cell* as_cell()
{
ASSERT(is_cell());
return m_value.as_cell;
}
String to_string() const;
bool to_boolean() const;
Value to_object(Heap&) const;
private:
Type m_type { Type::Undefined };
union {
bool as_bool;
double as_double;
StringImpl* as_string;
PrimitiveString* as_string;
Object* as_object;
Cell* as_cell;
} m_value;
};