diff options
author | davidot <davidot@serenityos.org> | 2021-10-20 21:29:47 +0200 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-10-20 23:19:17 +0100 |
commit | 6b2accce317e1226feaf627ddd5fbca100eb7265 (patch) | |
tree | 3a3e8a589bdb17ba630109ec3d2b4268076a6b48 /Userland/Libraries/LibJS/Parser.cpp | |
parent | 1245512c508d88e2a025d9d4e58297f76afd34bb (diff) | |
download | serenity-6b2accce317e1226feaf627ddd5fbca100eb7265.zip |
LibJS: Add static initializers to classes
Diffstat (limited to 'Userland/Libraries/LibJS/Parser.cpp')
-rw-r--r-- | Userland/Libraries/LibJS/Parser.cpp | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Parser.cpp b/Userland/Libraries/LibJS/Parser.cpp index 06e866f284..03fecfd13b 100644 --- a/Userland/Libraries/LibJS/Parser.cpp +++ b/Userland/Libraries/LibJS/Parser.cpp @@ -97,6 +97,11 @@ public: return scope_pusher; } + static ScopePusher static_init_block_scope(Parser& parser, ScopeNode& node) + { + return ScopePusher(parser, &node, true); + } + void add_declaration(NonnullRefPtr<Declaration> declaration) { if (declaration->is_lexical_declaration()) { @@ -957,6 +962,24 @@ NonnullRefPtr<ClassExpression> Parser::parse_class_expression(bool expect_class_ break; } property_key = create_ast_node<StringLiteral>({ m_state.current_token.filename(), rule_start.position(), position() }, name); + } else if (match(TokenType::CurlyOpen) && is_static) { + auto static_start = push_start(); + consume(TokenType::CurlyOpen); + + auto static_init_block = create_ast_node<FunctionBody>({ m_state.current_token.filename(), rule_start.position(), position() }); + + TemporaryChange break_context_rollback(m_state.in_break_context, false); + TemporaryChange continue_context_rollback(m_state.in_continue_context, false); + TemporaryChange function_context_rollback(m_state.in_function_context, false); + TemporaryChange generator_function_context_rollback(m_state.in_generator_function_context, false); + TemporaryChange in_class_field_initializer_rollback(m_state.in_class_field_initializer, true); + + ScopePusher static_init_scope = ScopePusher::static_init_block_scope(*this, *static_init_block); + parse_statement_list(static_init_block); + + consume(TokenType::CurlyClose); + elements.append(create_ast_node<StaticInitializer>({ m_state.current_token.filename(), static_start.position(), position() }, move(static_init_block), static_init_scope.contains_direct_call_to_eval())); + continue; } else { expected("property key"); } |