Ignore:
Timestamp:
Aug 4, 2015, 2:26:49 PM (10 years ago)
Author:
Yusuke Suzuki
Message:

[ES6] Support Module Syntax
https://bugs.webkit.org/show_bug.cgi?id=147422

Reviewed by Saam Barati.

Source/JavaScriptCore:

This patch introduces ES6 Modules syntax parsing part.
In this patch, ASTBuilder just produces the corresponding nodes to the ES6 Modules syntax,
and this patch does not include the code generator part.

Modules require 2 phase parsing. In the first pass, we just analyze the dependent modules
and do not execute the body or construct the AST. And after analyzing all the dependent
modules, we will parse the dependent modules next.
After all analyzing part is done, we will start the second pass. In the second pass, we
will parse the module, produce the AST, and execute the body.
If we don't do so, we need to create all the ASTs in the module's dependent graph at first
because the given module can be executed after the all dependent modules are executed. It
means that we need to hold so many parser arenas. To avoid this, the first pass only extracts
the dependent modules' information.

In this patch, we don't add this analyzing part yet. This patch only implements the second pass.
This patch aims at just implementing the syntax parsing functionality correctly.
After this patch is landed, we will create the ModuleDependencyAnalyzer that inherits SyntaxChecker
to collect the dependent modules fast[1].

To test the parsing, we added the "checkModuleSyntax" function into jsc shell.
By using this, we can parse the given string as the module.

[1]: https://bugs.webkit.org/show_bug.cgi?id=147353

  • bytecompiler/NodesCodegen.cpp:

(JSC::ModuleProgramNode::emitBytecode):
(JSC::ImportDeclarationNode::emitBytecode):
(JSC::ExportAllDeclarationNode::emitBytecode):
(JSC::ExportDefaultDeclarationNode::emitBytecode):
(JSC::ExportLocalDeclarationNode::emitBytecode):
(JSC::ExportNamedDeclarationNode::emitBytecode):

  • jsc.cpp:

(GlobalObject::finishCreation):
(functionCheckModuleSyntax):

  • parser/ASTBuilder.h:

(JSC::ASTBuilder::createModuleSpecifier):
(JSC::ASTBuilder::createImportSpecifier):
(JSC::ASTBuilder::createImportSpecifierList):
(JSC::ASTBuilder::appendImportSpecifier):
(JSC::ASTBuilder::createImportDeclaration):
(JSC::ASTBuilder::createExportAllDeclaration):
(JSC::ASTBuilder::createExportDefaultDeclaration):
(JSC::ASTBuilder::createExportLocalDeclaration):
(JSC::ASTBuilder::createExportNamedDeclaration):
(JSC::ASTBuilder::createExportSpecifier):
(JSC::ASTBuilder::createExportSpecifierList):
(JSC::ASTBuilder::appendExportSpecifier):

  • parser/Keywords.table:
  • parser/NodeConstructors.h:

(JSC::ModuleSpecifierNode::ModuleSpecifierNode):
(JSC::ImportSpecifierNode::ImportSpecifierNode):
(JSC::ImportDeclarationNode::ImportDeclarationNode):
(JSC::ExportAllDeclarationNode::ExportAllDeclarationNode):
(JSC::ExportDefaultDeclarationNode::ExportDefaultDeclarationNode):
(JSC::ExportLocalDeclarationNode::ExportLocalDeclarationNode):
(JSC::ExportNamedDeclarationNode::ExportNamedDeclarationNode):
(JSC::ExportSpecifierNode::ExportSpecifierNode):

  • parser/Nodes.cpp:

(JSC::ModuleProgramNode::ModuleProgramNode):

  • parser/Nodes.h:

(JSC::ModuleProgramNode::startColumn):
(JSC::ModuleProgramNode::endColumn):
(JSC::ModuleSpecifierNode::moduleName):
(JSC::ImportSpecifierNode::importedName):
(JSC::ImportSpecifierNode::localName):
(JSC::ImportSpecifierListNode::specifiers):
(JSC::ImportSpecifierListNode::append):
(JSC::ImportDeclarationNode::specifierList):
(JSC::ImportDeclarationNode::moduleSpecifier):
(JSC::ExportAllDeclarationNode::moduleSpecifier):
(JSC::ExportDefaultDeclarationNode::declaration):
(JSC::ExportLocalDeclarationNode::declaration):
(JSC::ExportSpecifierNode::exportedName):
(JSC::ExportSpecifierNode::localName):
(JSC::ExportSpecifierListNode::specifiers):
(JSC::ExportSpecifierListNode::append):
(JSC::ExportNamedDeclarationNode::specifierList):
(JSC::ExportNamedDeclarationNode::moduleSpecifier):

  • parser/Parser.cpp:

(JSC::Parser<LexerType>::Parser):
(JSC::Parser<LexerType>::parseInner):
(JSC::Parser<LexerType>::parseModuleSourceElements):
(JSC::Parser<LexerType>::parseVariableDeclaration):
(JSC::Parser<LexerType>::parseVariableDeclarationList):
(JSC::Parser<LexerType>::createBindingPattern):
(JSC::Parser<LexerType>::tryParseDestructuringPatternExpression):
(JSC::Parser<LexerType>::parseDestructuringPattern):
(JSC::Parser<LexerType>::parseForStatement):
(JSC::Parser<LexerType>::parseFormalParameters):
(JSC::Parser<LexerType>::parseFunctionParameters):
(JSC::Parser<LexerType>::parseFunctionDeclaration):
(JSC::Parser<LexerType>::parseClassDeclaration):
(JSC::Parser<LexerType>::parseModuleSpecifier):
(JSC::Parser<LexerType>::parseImportClauseItem):
(JSC::Parser<LexerType>::parseImportDeclaration):
(JSC::Parser<LexerType>::parseExportSpecifier):
(JSC::Parser<LexerType>::parseExportDeclaration):
(JSC::Parser<LexerType>::parseMemberExpression):

  • parser/Parser.h:

(JSC::isIdentifierOrKeyword):
(JSC::ModuleScopeData::create):
(JSC::ModuleScopeData::exportedBindings):
(JSC::ModuleScopeData::exportName):
(JSC::ModuleScopeData::exportBinding):
(JSC::Scope::Scope):
(JSC::Scope::setIsModule):
(JSC::Scope::moduleScopeData):
(JSC::Parser::matchContextualKeyword):
(JSC::Parser::matchIdentifierOrKeyword):
(JSC::Parser::isofToken): Deleted.

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

(JSC::SyntaxChecker::createModuleSpecifier):
(JSC::SyntaxChecker::createImportSpecifier):
(JSC::SyntaxChecker::createImportSpecifierList):
(JSC::SyntaxChecker::appendImportSpecifier):
(JSC::SyntaxChecker::createImportDeclaration):
(JSC::SyntaxChecker::createExportAllDeclaration):
(JSC::SyntaxChecker::createExportDefaultDeclaration):
(JSC::SyntaxChecker::createExportLocalDeclaration):
(JSC::SyntaxChecker::createExportNamedDeclaration):
(JSC::SyntaxChecker::createExportSpecifier):
(JSC::SyntaxChecker::createExportSpecifierList):
(JSC::SyntaxChecker::appendExportSpecifier):

  • runtime/CommonIdentifiers.cpp:

(JSC::CommonIdentifiers::CommonIdentifiers):

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

(JSC::checkModuleSyntax):

  • runtime/Completion.h:
  • tests/stress/modules-syntax-error-with-names.js: Added.

(shouldThrow):

  • tests/stress/modules-syntax-error.js: Added.

(shouldThrow):
(checkModuleSyntaxError.checkModuleSyntaxError.checkModuleSyntaxError):

  • tests/stress/modules-syntax.js: Added.

(prototype.checkModuleSyntax):
(checkModuleSyntax):

  • tests/stress/tagged-templates-syntax.js:

LayoutTests:

'export' and 'import' are changed from FutureReservedWord to Keyword in ES6.
http://www.ecma-international.org/ecma-262/6.0/#sec-keywords
And restrict 'super' use under the Script / Module contexts.

  • js/dom/reserved-words-as-property-expected.txt:
  • sputnik/Conformance/07_Lexical_Conventions/7.5_Tokens/7.5.3_Future_Reserved_Words/S7.5.3_A1.10-expected.txt:
  • sputnik/Conformance/07_Lexical_Conventions/7.5_Tokens/7.5.3_Future_Reserved_Words/S7.5.3_A1.16-expected.txt:
  • sputnik/Conformance/07_Lexical_Conventions/7.5_Tokens/7.5.3_Future_Reserved_Words/S7.5.3_A1.27-expected.txt:
File:
1 edited

Legend:

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

    r187515 r187890  
    16231623    };
    16241624
     1625    class ModuleProgramNode : public ScopeNode {
     1626    public:
     1627        ModuleProgramNode(ParserArena&, const JSTokenLocation& start, const JSTokenLocation& end, unsigned startColumn, unsigned endColumn, SourceElements*, VariableEnvironment&, FunctionStack&, VariableEnvironment&, FunctionParameters*, const SourceCode&, CodeFeatures, int numConstants);
     1628
     1629        unsigned startColumn() const { return m_startColumn; }
     1630        unsigned endColumn() const { return m_endColumn; }
     1631
     1632        static const bool scopeIsFunction = false;
     1633
     1634    private:
     1635        virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0) override;
     1636        unsigned m_startColumn;
     1637        unsigned m_endColumn;
     1638    };
     1639
     1640    class ModuleSpecifierNode : public Node {
     1641    public:
     1642        ModuleSpecifierNode(const JSTokenLocation&, const Identifier& moduleName);
     1643
     1644        const Identifier& moduleName() { return m_moduleName; }
     1645
     1646    private:
     1647        const Identifier& m_moduleName;
     1648    };
     1649
     1650    class ImportSpecifierNode : public Node {
     1651    public:
     1652        ImportSpecifierNode(const JSTokenLocation&, const Identifier& importedName, const Identifier& localName);
     1653
     1654        const Identifier& importedName() { return m_importedName; }
     1655        const Identifier& localName() { return m_localName; }
     1656
     1657    private:
     1658        const Identifier& m_importedName;
     1659        const Identifier& m_localName;
     1660    };
     1661
     1662    class ImportSpecifierListNode : public ParserArenaDeletable {
     1663    public:
     1664        typedef Vector<ImportSpecifierNode*, 3> Specifiers;
     1665
     1666        const Specifiers& specifiers() const { return m_specifiers; }
     1667        void append(ImportSpecifierNode* specifier)
     1668        {
     1669            m_specifiers.append(specifier);
     1670        }
     1671
     1672    private:
     1673        Specifiers m_specifiers;
     1674    };
     1675
     1676    class ImportDeclarationNode : public StatementNode {
     1677    public:
     1678        ImportDeclarationNode(const JSTokenLocation&, ImportSpecifierListNode*, ModuleSpecifierNode*);
     1679
     1680        ImportSpecifierListNode* specifierList() const { return m_specifierList; }
     1681        ModuleSpecifierNode* moduleSpecifier() const { return m_moduleSpecifier; }
     1682
     1683    private:
     1684        virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0) override;
     1685
     1686        ImportSpecifierListNode* m_specifierList;
     1687        ModuleSpecifierNode* m_moduleSpecifier;
     1688    };
     1689
     1690    class ExportAllDeclarationNode : public StatementNode {
     1691    public:
     1692        ExportAllDeclarationNode(const JSTokenLocation&, ModuleSpecifierNode*);
     1693
     1694        ModuleSpecifierNode* moduleSpecifier() const { return m_moduleSpecifier; }
     1695
     1696    private:
     1697        virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0) override;
     1698        ModuleSpecifierNode* m_moduleSpecifier;
     1699    };
     1700
     1701    class ExportDefaultDeclarationNode : public StatementNode {
     1702    public:
     1703        ExportDefaultDeclarationNode(const JSTokenLocation&, StatementNode*);
     1704
     1705        const StatementNode& declaration() const { return *m_declaration; }
     1706
     1707    private:
     1708        virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0) override;
     1709        StatementNode* m_declaration;
     1710    };
     1711
     1712    class ExportLocalDeclarationNode : public StatementNode {
     1713    public:
     1714        ExportLocalDeclarationNode(const JSTokenLocation&, StatementNode*);
     1715
     1716        const StatementNode& declaration() const { return *m_declaration; }
     1717
     1718    private:
     1719        virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0) override;
     1720        StatementNode* m_declaration;
     1721    };
     1722
     1723    class ExportSpecifierNode : public Node {
     1724    public:
     1725        ExportSpecifierNode(const JSTokenLocation&, const Identifier& localName, const Identifier& exportedName);
     1726
     1727        const Identifier& exportedName() { return m_exportedName; }
     1728        const Identifier& localName() { return m_localName; }
     1729
     1730    private:
     1731        const Identifier& m_localName;
     1732        const Identifier& m_exportedName;
     1733    };
     1734
     1735    class ExportSpecifierListNode : public ParserArenaDeletable {
     1736    public:
     1737        typedef Vector<ExportSpecifierNode*, 3> Specifiers;
     1738
     1739        const Specifiers& specifiers() const { return m_specifiers; }
     1740        void append(ExportSpecifierNode* specifier)
     1741        {
     1742            m_specifiers.append(specifier);
     1743        }
     1744
     1745    private:
     1746        Specifiers m_specifiers;
     1747    };
     1748
     1749    class ExportNamedDeclarationNode : public StatementNode {
     1750    public:
     1751        ExportNamedDeclarationNode(const JSTokenLocation&, ExportSpecifierListNode*, ModuleSpecifierNode*);
     1752
     1753        ExportSpecifierListNode* specifierList() const { return m_specifierList; }
     1754        ModuleSpecifierNode* moduleSpecifier() const { return m_moduleSpecifier; }
     1755
     1756    private:
     1757        virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0) override;
     1758        ExportSpecifierListNode* m_specifierList;
     1759        ModuleSpecifierNode* m_moduleSpecifier { nullptr };
     1760    };
     1761
    16251762    class FunctionParameters : public ParserArenaDeletable {
    16261763    public:
Note: See TracChangeset for help on using the changeset viewer.