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

LibJS: Track whether a program has a top level await statement

This commit is contained in:
davidot 2022-01-18 18:55:19 +01:00 committed by Linus Groh
parent aca427fc8c
commit 99edf5b25a
2 changed files with 21 additions and 0 deletions

View file

@ -426,6 +426,9 @@ public:
NonnullRefPtrVector<ImportStatement> const& imports() const { return m_imports; } NonnullRefPtrVector<ImportStatement> const& imports() const { return m_imports; }
NonnullRefPtrVector<ExportStatement> const& exports() const { return m_exports; } NonnullRefPtrVector<ExportStatement> const& exports() const { return m_exports; }
bool has_top_level_await() const { return m_has_top_level_await; }
void set_has_top_level_await() { m_has_top_level_await = true; }
ThrowCompletionOr<void> global_declaration_instantiation(Interpreter& interpreter, GlobalObject& global_object, GlobalEnvironment& global_environment) const; ThrowCompletionOr<void> global_declaration_instantiation(Interpreter& interpreter, GlobalObject& global_object, GlobalEnvironment& global_environment) const;
private: private:
@ -436,6 +439,7 @@ private:
NonnullRefPtrVector<ImportStatement> m_imports; NonnullRefPtrVector<ImportStatement> m_imports;
NonnullRefPtrVector<ExportStatement> m_exports; NonnullRefPtrVector<ExportStatement> m_exports;
bool m_has_top_level_await { false };
}; };
class BlockStatement final : public ScopeNode { class BlockStatement final : public ScopeNode {

View file

@ -237,6 +237,16 @@ public:
m_identifier_and_argument_index_associations.ensure(index).append(move(identifier)); m_identifier_and_argument_index_associations.ensure(index).append(move(identifier));
} }
void set_contains_await_expression()
{
m_contains_await_expression = true;
}
bool contains_await_expression() const
{
return m_contains_await_expression;
}
private: private:
void throw_identifier_declared(FlyString const& name, NonnullRefPtr<Declaration> const& declaration) void throw_identifier_declared(FlyString const& name, NonnullRefPtr<Declaration> const& declaration)
{ {
@ -263,6 +273,7 @@ private:
bool m_contains_access_to_arguments_object { false }; bool m_contains_access_to_arguments_object { false };
bool m_contains_direct_call_to_eval { false }; bool m_contains_direct_call_to_eval { false };
bool m_contains_await_expression { false };
}; };
class OperatorPrecedenceTable { class OperatorPrecedenceTable {
@ -525,6 +536,10 @@ void Parser::parse_module(Program& program)
} }
} }
VERIFY(m_state.current_scope_pusher);
if (m_state.current_scope_pusher->contains_await_expression())
program.set_has_top_level_await();
for (auto& export_statement : program.exports()) { for (auto& export_statement : program.exports()) {
if (export_statement.has_statement()) if (export_statement.has_statement())
continue; continue;
@ -2342,6 +2357,8 @@ NonnullRefPtr<AwaitExpression> Parser::parse_await_expression()
auto associativity = operator_associativity(TokenType::Await); auto associativity = operator_associativity(TokenType::Await);
auto argument = parse_expression(precedence, associativity); auto argument = parse_expression(precedence, associativity);
m_state.current_scope_pusher->set_contains_await_expression();
return create_ast_node<AwaitExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, move(argument)); return create_ast_node<AwaitExpression>({ m_state.current_token.filename(), rule_start.position(), position() }, move(argument));
} }