1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-28 08:35:09 +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

@ -247,6 +247,8 @@ NonnullRefPtr<Expression> Parser::parse_primary_expression()
return parse_function_node<FunctionExpression>();
case TokenType::BracketOpen:
return parse_array_expression();
case TokenType::New:
return parse_new_expression();
default:
m_has_errors = true;
expected("primary expression (missing switch case)");
@ -443,6 +445,29 @@ NonnullRefPtr<CallExpression> Parser::parse_call_expression(NonnullRefPtr<Expres
return create_ast_node<CallExpression>(move(lhs), move(arguments));
}
NonnullRefPtr<NewExpression> Parser::parse_new_expression()
{
consume(TokenType::New);
// FIXME: Support full expressions as the callee as well.
auto callee = create_ast_node<Identifier>(consume(TokenType::Identifier).value());
NonnullRefPtrVector<Expression> arguments;
if (match(TokenType::ParenOpen)) {
consume(TokenType::ParenOpen);
while (match_expression()) {
arguments.append(parse_expression(0));
if (!match(TokenType::Comma))
break;
consume();
}
consume(TokenType::ParenClose);
}
return create_ast_node<NewExpression>(move(callee), move(arguments));
}
NonnullRefPtr<ReturnStatement> Parser::parse_return_statement()
{
consume(TokenType::Return);