1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-21 16:05:07 +00:00

LibJS: Parse computed MemberExpressions

MemberExpression comes in two flavors:

    computed: a[b]
non-computed: a.b

We can now parse both of the types. :^)
This commit is contained in:
Andreas Kling 2020-03-20 20:51:03 +01:00
parent 86642add2f
commit a3d2e07446
3 changed files with 26 additions and 12 deletions

View file

@ -480,7 +480,7 @@ Value AssignmentExpression::execute(Interpreter& interpreter) const
commit = [&](Value value) {
auto object = static_cast<const MemberExpression&>(*m_lhs).object().execute(interpreter).to_object(interpreter.heap());
ASSERT(object.is_object());
auto property_name = static_cast<const Identifier&>(static_cast<const MemberExpression&>(*m_lhs).property()).string();
auto property_name = static_cast<const MemberExpression&>(*m_lhs).computed_property_name(interpreter);
object.as_object()->put(property_name, value);
};
} else {
@ -638,24 +638,26 @@ Value ObjectExpression::execute(Interpreter& interpreter) const
void MemberExpression::dump(int indent) const
{
ASTNode::dump(indent);
print_indent(indent);
printf("%s (computed=%s)\n", class_name(), is_computed() ? "true" : "false");
m_object->dump(indent + 1);
m_property->dump(indent + 1);
}
String MemberExpression::computed_property_name(Interpreter& interpreter) const
{
if (!is_computed()) {
ASSERT(m_property->is_identifier());
return static_cast<const Identifier&>(*m_property).string();
}
return m_property->execute(interpreter).to_string();
}
Value MemberExpression::execute(Interpreter& interpreter) const
{
auto object_result = m_object->execute(interpreter).to_object(interpreter.heap());
ASSERT(object_result.is_object());
String property_name;
if (m_property->is_identifier()) {
property_name = static_cast<const Identifier&>(*m_property).string();
} else {
ASSERT_NOT_REACHED();
}
return object_result.as_object()->get(property_name);
return object_result.as_object()->get(computed_property_name(interpreter));
}
Value StringLiteral::execute(Interpreter& interpreter) const