1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 13:17:35 +00:00

LibJS: Implement basic support for the "new" keyword

NewExpression mostly piggybacks on the existing CallExpression. The big
difference is that "new" creates a new Object and passes it as |this|
to the callee.
This commit is contained in:
Andreas Kling 2020-03-28 16:33:52 +01:00
parent fecbef4ffe
commit 0593ce406b
5 changed files with 67 additions and 9 deletions

View file

@ -54,6 +54,7 @@ public:
virtual bool is_member_expression() const { return false; }
virtual bool is_scope_node() const { return false; }
virtual bool is_variable_declaration() const { return false; }
virtual bool is_new_expression() const { return false; }
protected:
ASTNode() {}
@ -470,7 +471,7 @@ private:
class CallExpression : public Expression {
public:
explicit CallExpression(NonnullRefPtr<Expression> callee, NonnullRefPtrVector<Expression> arguments = {})
CallExpression(NonnullRefPtr<Expression> callee, NonnullRefPtrVector<Expression> arguments = {})
: m_callee(move(callee))
, m_arguments(move(arguments))
{
@ -486,6 +487,18 @@ private:
const NonnullRefPtrVector<Expression> m_arguments;
};
class NewExpression final : public CallExpression {
public:
NewExpression(NonnullRefPtr<Expression> callee, NonnullRefPtrVector<Expression> arguments = {})
: CallExpression(move(callee), move(arguments))
{
}
private:
virtual const char * class_name() const override { return "NewExpression"; }
virtual bool is_new_expression() const override { return true; }
};
enum class AssignmentOp {
Assignment,
AdditionAssignment,