1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 02:48:11 +00:00

LibJS: Don't save rule start positions along with the parser state

This fixes #4617.
Also fixes the small problem where some save states would be leaked.
This commit is contained in:
AnotherTest 2020-12-29 16:47:39 +03:30 committed by Andreas Kling
parent 6ffcd53479
commit 8ca0e8325a
2 changed files with 12 additions and 10 deletions

View file

@ -367,7 +367,6 @@ RefPtr<FunctionExpression> Parser::try_parse_arrow_function_expression(bool expe
ArmedScopeGuard state_rollback_guard = [&] { ArmedScopeGuard state_rollback_guard = [&] {
m_parser_state.m_var_scopes.take_last(); m_parser_state.m_var_scopes.take_last();
rule_start.disable();
load_state(); load_state();
}; };
@ -434,6 +433,7 @@ RefPtr<FunctionExpression> Parser::try_parse_arrow_function_expression(bool expe
if (!function_body_result.is_null()) { if (!function_body_result.is_null()) {
state_rollback_guard.disarm(); state_rollback_guard.disarm();
discard_saved_state();
auto body = function_body_result.release_nonnull(); auto body = function_body_result.release_nonnull();
return create_ast_node<FunctionExpression>({ rule_start.position(), position() }, "", move(body), move(parameters), function_length, m_parser_state.m_var_scopes.take_last(), is_strict, true); return create_ast_node<FunctionExpression>({ rule_start.position(), position() }, "", move(body), move(parameters), function_length, m_parser_state.m_var_scopes.take_last(), is_strict, true);
} }
@ -446,7 +446,6 @@ RefPtr<Statement> Parser::try_parse_labelled_statement()
save_state(); save_state();
auto rule_start = push_start(); auto rule_start = push_start();
ArmedScopeGuard state_rollback_guard = [&] { ArmedScopeGuard state_rollback_guard = [&] {
rule_start.disable();
load_state(); load_state();
}; };
@ -463,6 +462,7 @@ RefPtr<Statement> Parser::try_parse_labelled_statement()
statement->set_label(identifier); statement->set_label(identifier);
state_rollback_guard.disarm(); state_rollback_guard.disarm();
discard_saved_state();
return statement; return statement;
} }
@ -471,7 +471,6 @@ RefPtr<MetaProperty> Parser::try_parse_new_target_expression()
save_state(); save_state();
auto rule_start = push_start(); auto rule_start = push_start();
ArmedScopeGuard state_rollback_guard = [&] { ArmedScopeGuard state_rollback_guard = [&] {
rule_start.disable();
load_state(); load_state();
}; };
@ -485,6 +484,7 @@ RefPtr<MetaProperty> Parser::try_parse_new_target_expression()
return {}; return {};
state_rollback_guard.disarm(); state_rollback_guard.disarm();
discard_saved_state();
return create_ast_node<MetaProperty>({ rule_start.position(), position() }, MetaProperty::Type::NewTarget); return create_ast_node<MetaProperty>({ rule_start.position(), position() }, MetaProperty::Type::NewTarget);
} }
@ -2041,4 +2041,9 @@ void Parser::load_state()
m_parser_state = m_saved_state.take_last(); m_parser_state = m_saved_state.take_last();
} }
void Parser::discard_saved_state()
{
m_saved_state.take_last();
}
} }

View file

@ -169,6 +169,7 @@ private:
void consume_or_insert_semicolon(); void consume_or_insert_semicolon();
void save_state(); void save_state();
void load_state(); void load_state();
void discard_saved_state();
Position position() const; Position position() const;
struct RulePosition { struct RulePosition {
@ -180,25 +181,21 @@ private:
: m_parser(parser) : m_parser(parser)
, m_position(position) , m_position(position)
{ {
m_parser.m_parser_state.m_rule_starts.append(position); m_parser.m_rule_starts.append(position);
} }
~RulePosition() ~RulePosition()
{ {
if (!m_enabled) auto last = m_parser.m_rule_starts.take_last();
return;
auto last = m_parser.m_parser_state.m_rule_starts.take_last();
ASSERT(last.line == m_position.line); ASSERT(last.line == m_position.line);
ASSERT(last.column == m_position.column); ASSERT(last.column == m_position.column);
} }
const Position& position() const { return m_position; } const Position& position() const { return m_position; }
void disable() { m_enabled = false; }
private: private:
Parser& m_parser; Parser& m_parser;
Position m_position; Position m_position;
bool m_enabled { true };
}; };
[[nodiscard]] RulePosition push_start() { return { *this, position() }; } [[nodiscard]] RulePosition push_start() { return { *this, position() }; }
@ -210,7 +207,6 @@ private:
Vector<NonnullRefPtrVector<VariableDeclaration>> m_var_scopes; Vector<NonnullRefPtrVector<VariableDeclaration>> m_var_scopes;
Vector<NonnullRefPtrVector<VariableDeclaration>> m_let_scopes; Vector<NonnullRefPtrVector<VariableDeclaration>> m_let_scopes;
Vector<NonnullRefPtrVector<FunctionDeclaration>> m_function_scopes; Vector<NonnullRefPtrVector<FunctionDeclaration>> m_function_scopes;
Vector<Position> m_rule_starts;
HashTable<StringView> m_labels_in_scope; HashTable<StringView> m_labels_in_scope;
bool m_strict_mode { false }; bool m_strict_mode { false };
bool m_allow_super_property_lookup { false }; bool m_allow_super_property_lookup { false };
@ -224,6 +220,7 @@ private:
explicit ParserState(Lexer); explicit ParserState(Lexer);
}; };
Vector<Position> m_rule_starts;
ParserState m_parser_state; ParserState m_parser_state;
Vector<ParserState> m_saved_state; Vector<ParserState> m_saved_state;
}; };