mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 07:30:19 +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:
parent
86642add2f
commit
a3d2e07446
Notes:
sideshowbarker
2024-07-19 08:12:42 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/a3d2e074466
3 changed files with 26 additions and 12 deletions
|
@ -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
|
||||
|
|
|
@ -601,24 +601,29 @@ private:
|
|||
|
||||
class MemberExpression final : public Expression {
|
||||
public:
|
||||
MemberExpression(NonnullRefPtr<Expression> object, NonnullRefPtr<Expression> property)
|
||||
MemberExpression(NonnullRefPtr<Expression> object, NonnullRefPtr<Expression> property, bool computed = false)
|
||||
: m_object(move(object))
|
||||
, m_property(move(property))
|
||||
, m_computed(computed)
|
||||
{
|
||||
}
|
||||
|
||||
virtual Value execute(Interpreter&) const override;
|
||||
virtual void dump(int indent) const override;
|
||||
|
||||
bool is_computed() const { return m_computed; }
|
||||
const Expression& object() const { return *m_object; }
|
||||
const Expression& property() const { return *m_property; }
|
||||
|
||||
String computed_property_name(Interpreter&) const;
|
||||
|
||||
private:
|
||||
virtual bool is_member_expression() const override { return true; }
|
||||
virtual const char* class_name() const override { return "MemberExpression"; }
|
||||
|
||||
NonnullRefPtr<Expression> m_object;
|
||||
NonnullRefPtr<Expression> m_property;
|
||||
bool m_computed { false };
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -369,6 +369,12 @@ NonnullRefPtr<Expression> Parser::parse_secondary_expression(NonnullRefPtr<Expre
|
|||
case TokenType::Period:
|
||||
consume();
|
||||
return create_ast_node<MemberExpression>(move(lhs), parse_expression(min_precedence, associativity));
|
||||
case TokenType::BracketOpen: {
|
||||
consume(TokenType::BracketOpen);
|
||||
auto expression = create_ast_node<MemberExpression>(move(lhs), parse_expression(min_precedence, associativity), true);
|
||||
consume(TokenType::BracketClose);
|
||||
return expression;
|
||||
}
|
||||
case TokenType::PlusPlus:
|
||||
consume();
|
||||
return create_ast_node<UpdateExpression>(UpdateOp::Increment, move(lhs));
|
||||
|
@ -587,6 +593,7 @@ bool Parser::match_secondary_expression() const
|
|||
|| type == TokenType::LessThanEquals
|
||||
|| type == TokenType::ParenOpen
|
||||
|| type == TokenType::Period
|
||||
|| type == TokenType::BracketOpen
|
||||
|| type == TokenType::PlusPlus
|
||||
|| type == TokenType::MinusMinus;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue