Ignore:
Timestamp:
Aug 12, 2015, 1:38:45 PM (10 years ago)
Author:
Yusuke Suzuki
Message:

[ES6] Add ES6 Modules preparsing phase to collect the dependencies
https://bugs.webkit.org/show_bug.cgi?id=147353

Reviewed by Geoffrey Garen.

This patch implements ModuleRecord and ModuleAnalyzer.
ModuleAnalyzer analyzes the produced AST from the parser.
By collaborating with the parser, ModuleAnalyzer collects the information
that is necessary to request the loading for the dependent modules and
construct module's environment and namespace object before executing the actual
module body.

In the parser, we annotate which variable is imported binding and which variable
is exported from the current module. This information is leveraged in the ModuleAnalyzer
to categorize the export entries.

To preparse the modules in the parser, we just add the new flag ModuleParseMode
instead of introducing a new TreeContext type. This is because only 2 users use the
parseModuleSourceElements; preparser and actual compiler. Adding the flag is simple
enough to switch the context to the SyntaxChecker when parsing the non-module related
statement in the preparsing phase.

To demonstrate the module analyzer, we added the new option dumpModuleRecord option
into the JSC shell. By specifying this, the result of analysis is dumped when the module
is parsed and analyzed.

(JSC::ASTBuilder::createExportDefaultDeclaration):

  • parser/ModuleAnalyzer.cpp: Added.

(JSC::ModuleAnalyzer::ModuleAnalyzer):
(JSC::ModuleAnalyzer::exportedBinding):
(JSC::ModuleAnalyzer::declareExportAlias):
(JSC::ModuleAnalyzer::exportVariable):
(JSC::ModuleAnalyzer::analyze):

  • parser/ModuleAnalyzer.h: Added.

(JSC::ModuleAnalyzer::vm):
(JSC::ModuleAnalyzer::moduleRecord):

  • parser/ModuleRecord.cpp: Added.

(JSC::printableName):
(JSC::ModuleRecord::dump):

  • parser/ModuleRecord.h: Added.

(JSC::ModuleRecord::ImportEntry::isNamespace):
(JSC::ModuleRecord::create):
(JSC::ModuleRecord::appendRequestedModule):
(JSC::ModuleRecord::addImportEntry):
(JSC::ModuleRecord::addExportEntry):
(JSC::ModuleRecord::addStarExportEntry):

  • parser/NodeConstructors.h:

(JSC::ModuleDeclarationNode::ModuleDeclarationNode):
(JSC::ImportDeclarationNode::ImportDeclarationNode):
(JSC::ExportAllDeclarationNode::ExportAllDeclarationNode):
(JSC::ExportDefaultDeclarationNode::ExportDefaultDeclarationNode):
(JSC::ExportLocalDeclarationNode::ExportLocalDeclarationNode):
(JSC::ExportNamedDeclarationNode::ExportNamedDeclarationNode):

  • parser/Nodes.h:

(JSC::ExportDefaultDeclarationNode::localName):

  • parser/NodesAnalyzeModule.cpp: Added.

(JSC::ScopeNode::analyzeModule):
(JSC::SourceElements::analyzeModule):
(JSC::ImportDeclarationNode::analyzeModule):
(JSC::ExportAllDeclarationNode::analyzeModule):
(JSC::ExportDefaultDeclarationNode::analyzeModule):
(JSC::ExportLocalDeclarationNode::analyzeModule):
(JSC::ExportNamedDeclarationNode::analyzeModule):

  • parser/Parser.cpp:

(JSC::Parser<LexerType>::parseInner):
(JSC::Parser<LexerType>::parseModuleSourceElements):
(JSC::Parser<LexerType>::parseVariableDeclarationList):
(JSC::Parser<LexerType>::createBindingPattern):
(JSC::Parser<LexerType>::parseFunctionDeclaration):
(JSC::Parser<LexerType>::parseClassDeclaration):
(JSC::Parser<LexerType>::parseImportClauseItem):
(JSC::Parser<LexerType>::parseExportSpecifier):
(JSC::Parser<LexerType>::parseExportDeclaration):

  • parser/Parser.h:

(JSC::Scope::lexicalVariables):
(JSC::Scope::declareLexicalVariable):
(JSC::Parser::declareVariable):
(JSC::Parser::exportName):
(JSC::Parser<LexerType>::parse):
(JSC::parse):

  • parser/ParserModes.h:
  • parser/SyntaxChecker.h:

(JSC::SyntaxChecker::createExportDefaultDeclaration):

  • parser/VariableEnvironment.cpp:

(JSC::VariableEnvironment::markVariableAsImported):
(JSC::VariableEnvironment::markVariableAsExported):

  • parser/VariableEnvironment.h:

(JSC::VariableEnvironmentEntry::isExported):
(JSC::VariableEnvironmentEntry::isImported):
(JSC::VariableEnvironmentEntry::setIsExported):
(JSC::VariableEnvironmentEntry::setIsImported):

  • runtime/CommonIdentifiers.h:
  • runtime/Completion.cpp:

(JSC::checkModuleSyntax):

  • runtime/Options.h:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/parser/Nodes.h

    r188219 r188355  
    5050    class JSScope;
    5151    class ScopeNode;
     52    class ModuleAnalyzer;
    5253
    5354    enum Operator {
     
    12771278
    12781279        void emitBytecode(BytecodeGenerator&, RegisterID* destination);
     1280        void analyzeModule(ModuleAnalyzer&);
    12791281
    12801282    private:
     
    15751577        void setClosedVariables(Vector<RefPtr<UniquedStringImpl>>&&) { }
    15761578
     1579        void analyzeModule(ModuleAnalyzer&);
     1580
    15771581    protected:
    15781582        int m_startLineNumber;
     
    16741678    };
    16751679
    1676     class ImportDeclarationNode : public StatementNode {
     1680    class ModuleDeclarationNode : public StatementNode {
     1681    public:
     1682        virtual void analyzeModule(ModuleAnalyzer&) = 0;
     1683
     1684    protected:
     1685        ModuleDeclarationNode(const JSTokenLocation&);
     1686    };
     1687
     1688    class ImportDeclarationNode : public ModuleDeclarationNode {
    16771689    public:
    16781690        ImportDeclarationNode(const JSTokenLocation&, ImportSpecifierListNode*, ModuleSpecifierNode*);
     
    16831695    private:
    16841696        virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0) override;
     1697        virtual void analyzeModule(ModuleAnalyzer&) override;
    16851698
    16861699        ImportSpecifierListNode* m_specifierList;
     
    16881701    };
    16891702
    1690     class ExportAllDeclarationNode : public StatementNode {
     1703    class ExportAllDeclarationNode : public ModuleDeclarationNode {
    16911704    public:
    16921705        ExportAllDeclarationNode(const JSTokenLocation&, ModuleSpecifierNode*);
     
    16961709    private:
    16971710        virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0) override;
     1711        virtual void analyzeModule(ModuleAnalyzer&) override;
     1712
    16981713        ModuleSpecifierNode* m_moduleSpecifier;
    16991714    };
    17001715
    1701     class ExportDefaultDeclarationNode : public StatementNode {
    1702     public:
    1703         ExportDefaultDeclarationNode(const JSTokenLocation&, StatementNode*);
     1716    class ExportDefaultDeclarationNode : public ModuleDeclarationNode {
     1717    public:
     1718        ExportDefaultDeclarationNode(const JSTokenLocation&, StatementNode*, const Identifier& localName);
    17041719
    17051720        const StatementNode& declaration() const { return *m_declaration; }
    1706 
    1707     private:
    1708         virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0) override;
     1721        const Identifier& localName() const { return m_localName; }
     1722
     1723    private:
     1724        virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0) override;
     1725        virtual void analyzeModule(ModuleAnalyzer&) override;
    17091726        StatementNode* m_declaration;
    1710     };
    1711 
    1712     class ExportLocalDeclarationNode : public StatementNode {
     1727        const Identifier& m_localName;
     1728    };
     1729
     1730    class ExportLocalDeclarationNode : public ModuleDeclarationNode {
    17131731    public:
    17141732        ExportLocalDeclarationNode(const JSTokenLocation&, StatementNode*);
     
    17181736    private:
    17191737        virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0) override;
     1738        virtual void analyzeModule(ModuleAnalyzer&) override;
    17201739        StatementNode* m_declaration;
    17211740    };
     
    17471766    };
    17481767
    1749     class ExportNamedDeclarationNode : public StatementNode {
     1768    class ExportNamedDeclarationNode : public ModuleDeclarationNode {
    17501769    public:
    17511770        ExportNamedDeclarationNode(const JSTokenLocation&, ExportSpecifierListNode*, ModuleSpecifierNode*);
     
    17561775    private:
    17571776        virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0) override;
     1777        virtual void analyzeModule(ModuleAnalyzer&) override;
    17581778        ExportSpecifierListNode* m_specifierList;
    17591779        ModuleSpecifierNode* m_moduleSpecifier { nullptr };
Note: See TracChangeset for help on using the changeset viewer.