From 929ef25efd402368a9f154b61aa96b4b01a02ce2 Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Tue, 14 Aug 2018 13:01:51 +0200 Subject: Fix module dependency handling The evaluation of a module can have side-effects by modifying the global object or objects in it. Therefore even a seemingly empty import such as import "./foo.js" needs to be listed in the module requests. It's also important that they are evaluated in the order of declaration. Therefore we collect all module requests separately - even those that don't have import variables to process. This patch also ensures that the export and import declarations are visited in the correct order, by unifying both AST nodes to be hooked into the statement list. The fact that we connect the module list items into a statement list is solely an artifact of re-using defineFunction() which takes a StatementList as body. Change-Id: I75dc357b2aecfc324d9a9fe66952eff1ec1dfd8a Reviewed-by: Lars Knoll --- src/qml/parser/qqmljsast.cpp | 32 +++++++++++--------------------- src/qml/parser/qqmljsast_p.h | 4 ++-- src/qml/parser/qqmljsastvisitor_p.h | 3 --- 3 files changed, 13 insertions(+), 26 deletions(-) (limited to 'src/qml/parser') diff --git a/src/qml/parser/qqmljsast.cpp b/src/qml/parser/qqmljsast.cpp index 0b1b97a41a..ca4317a9a6 100644 --- a/src/qml/parser/qqmljsast.cpp +++ b/src/qml/parser/qqmljsast.cpp @@ -1162,26 +1162,10 @@ void ExportDeclaration::accept0(Visitor *visitor) visitor->endVisit(this); } -void ModuleItemList::accept0(Visitor *visitor) -{ - if (visitor->visit(this)) { - for (ModuleItemList *it = this; it; it = it->next) { - if (it->item) { - // The statement list is concatenated together between module list - // items and stored in the ESModule, thus not visited from there. - if (it->item->kind == Kind_StatementList) - continue; - // Export declaration are also injected into the statement list for - // processing of declarations an variable statements, so don't visit - // it here to avoid double visits. - if (it->item->kind == Kind_ExportDeclaration) - continue; - } - accept(it->item, visitor); - } - } - - visitor->endVisit(this); +void ModuleItemList::accept0(Visitor *) +{ + // See ESModule::accept0 + Q_UNREACHABLE(); } StatementList *ModuleItemList::buildStatementList(MemoryPool *pool) const @@ -1192,6 +1176,8 @@ StatementList *ModuleItemList::buildStatementList(MemoryPool *pool) const if (!listItem) { if (AST::ExportDeclaration *exportDecl = AST::cast(item->item)) listItem = new (pool) AST::StatementList(exportDecl); + else if (AST::ImportDeclaration *importDecl = AST::cast(item->item)) + listItem = new (pool) AST::StatementList(importDecl); else continue; } @@ -1209,7 +1195,11 @@ StatementList *ModuleItemList::buildStatementList(MemoryPool *pool) const void ESModule::accept0(Visitor *visitor) { if (visitor->visit(this)) { - accept(body, visitor); + // We don't accept the ModuleItemList (body) as instead the statement list items + // as well as the import/export declarations are linked together in the statement + // list. That way they are processed in correct order and can be used with the codegen's + // defineFunction() that expects a statement list. + // accept(body, visitor); accept(statements, visitor); } diff --git a/src/qml/parser/qqmljsast_p.h b/src/qml/parser/qqmljsast_p.h index e344dd4a28..1429d784c6 100644 --- a/src/qml/parser/qqmljsast_p.h +++ b/src/qml/parser/qqmljsast_p.h @@ -2539,7 +2539,7 @@ public: QStringRef moduleSpecifier; }; -class QML_PARSER_EXPORT ImportDeclaration: public Node +class QML_PARSER_EXPORT ImportDeclaration: public Statement { public: QQMLJS_DECLARE_AST_NODE(ImportDeclaration) @@ -2755,7 +2755,7 @@ public: StatementList *buildStatementList(MemoryPool *pool) const; - void accept0(Visitor *visitor) override; + void accept0(Visitor *) override; SourceLocation firstSourceLocation() const override { return item->firstSourceLocation(); } diff --git a/src/qml/parser/qqmljsastvisitor_p.h b/src/qml/parser/qqmljsastvisitor_p.h index 3162bb1630..c925096de6 100644 --- a/src/qml/parser/qqmljsastvisitor_p.h +++ b/src/qml/parser/qqmljsastvisitor_p.h @@ -369,9 +369,6 @@ public: virtual bool visit(ModuleItem *) { return true; } virtual void endVisit(ModuleItem *) {} - virtual bool visit(ModuleItemList *) { return true; } - virtual void endVisit(ModuleItemList *) {} - virtual bool visit(ESModule *) { return true; } virtual void endVisit(ESModule *) {} -- cgit v1.2.3