Ignore:
Timestamp:
Jan 29, 2015, 2:59:19 PM (10 years ago)
Author:
[email protected]
Message:

Implement ES6 class syntax without inheritance support
https://bugs.webkit.org/show_bug.cgi?id=140918

Reviewed by Geoffrey Garen.

Source/JavaScriptCore:

Added the most basic support for ES6 class syntax. After this patch, we support basic class definition like:
class A {

constructor() { }
someMethod() { }

}

We'll add the support for "extends" keyword and automatically generating a constructor in follow up patches.
We also don't support block scoping of a class declaration.

We support both class declaration and class expression. A class expression is implemented by the newly added
ClassExprNode AST node. A class declaration is implemented by ClassDeclNode, which is a thin wrapper around
AssignResolveNode.

Tests: js/class-syntax-declaration.html

js/class-syntax-expression.html

  • bytecompiler/NodesCodegen.cpp:

(JSC::ObjectLiteralNode::emitBytecode): Create a new object instead of delegating the work to PropertyListNode.
Also fixed the 5-space indentation.
(JSC::PropertyListNode::emitBytecode): Don't create a new object now that ObjectLiteralNode does this.
(JSC::ClassDeclNode::emitBytecode): Added. Just let the AssignResolveNode node emit the byte code.
(JSC::ClassExprNode::emitBytecode): Create the class constructor and add static methods to the constructor by
emitting the byte code for PropertyListNode. Add instance methods to the class's prototype object the same way.

  • parser/ASTBuilder.h:

(JSC::ASTBuilder::createClassExpr): Added. Creates a ClassExprNode.
(JSC::ASTBuilder::createClassDeclStatement): Added. Creates a AssignResolveNode and wraps it by a ClassDeclNode.

  • parser/NodeConstructors.h:

(JSC::ClassDeclNode::ClassDeclNode): Added.
(JSC::ClassExprNode::ClassExprNode): Added.

  • parser/Nodes.h:

(JSC::ClassExprNode): Added.
(JSC::ClassDeclNode): Added.

  • parser/Parser.cpp:

(JSC::Parser<LexerType>::parseStatement): Added the support for class declaration.
(JSC::stringForFunctionMode): Return "method" for MethodMode.
(JSC::Parser<LexerType>::parseClassDeclaration): Added. Uses parseClass to create a class expression and wraps
it with ClassDeclNode as described above.
(JSC::Parser<LexerType>::parseClass): Parses a class expression.
(JSC::Parser<LexerType>::parseProperty):
(JSC::Parser<LexerType>::parseGetterSetter): Extracted from parseProperty to share the code between parseProperty
and parseClass.
(JSC::Parser<LexerType>::parsePrimaryExpression): Added the support for class expression.

  • parser/Parser.h:

(FunctionParseMode): Added MethodMode.

  • parser/SyntaxChecker.h:

(JSC::SyntaxChecker::createClassExpr): Added.
(JSC::SyntaxChecker::createClassDeclStatement): Added.

LayoutTests:

Added two tests for class declarations and class expressions.

  • TestExpectations:
  • js/class-syntax-declaration-expected.txt: Added.
  • js/class-syntax-declaration.html: Added.
  • js/class-syntax-expression-expected.txt: Added.
  • js/class-syntax-expression.html: Added.
  • js/script-tests/class-syntax-declaration.js: Added.
  • js/script-tests/class-syntax-expression.js: Added.
File:
1 edited

Legend:

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

    r178888 r179371  
    7474        ResolveEvalExpr, ResolveExpr, NumberExpr, StringExpr,
    7575        ThisExpr, NullExpr, BoolExpr, RegExpExpr, ObjectLiteralExpr,
    76         FunctionExpr, BracketExpr, DotExpr, CallExpr,
     76        FunctionExpr, ClassExpr, BracketExpr, DotExpr, CallExpr,
    7777        NewExpr, PreExpr, PostExpr, UnaryExpr, BinaryExpr,
    7878        ConditionalExpr, AssignmentExpr, TypeofExpr,
     
    114114    typedef int FormalParameterList;
    115115    typedef int FunctionBody;
     116#if ENABLE(ES6_CLASS_SYNTAX)
     117    typedef int ClassExpression;
     118#endif
    116119    typedef int Statement;
    117120    typedef int ClauseList;
     
    161164    ExpressionType createAssignResolve(const JSTokenLocation&, const Identifier&, ExpressionType, int, int, int) { return AssignmentExpr; }
    162165    ExpressionType createEmptyVarExpression(const JSTokenLocation&, const Identifier&) { return AssignmentExpr; }
     166#if ENABLE(ES6_CLASS_SYNTAX)
     167    ClassExpression createClassExpr(const JSTokenLocation&, const Identifier&, ExpressionType, ExpressionType, PropertyList, PropertyList) { return ClassExpr; }
     168#endif
    163169    ExpressionType createFunctionExpr(const JSTokenLocation&, const ParserFunctionInfo<SyntaxChecker>&, int) { return FunctionExpr; }
    164170    int createFunctionBody(const JSTokenLocation&, const JSTokenLocation&, int, int, bool) { return FunctionBodyResult; }
     
    196202    int createClauseList(int, int) { return ClauseListResult; }
    197203    int createFuncDeclStatement(const JSTokenLocation&, const ParserFunctionInfo<SyntaxChecker>&, int) { return StatementResult; }
    198     int createClassDeclStatement(const JSTokenLocation&, const Identifier&, ExpressionType, ExpressionType, PropertyList, PropertyList, int, int) { return StatementResult; }
     204#if ENABLE(ES6_CLASS_SYNTAX)
     205    int createClassDeclStatement(const JSTokenLocation&, ClassExpression,
     206        const JSTextPosition&, const JSTextPosition&, int, int) { return StatementResult; }
     207#endif
    199208    int createBlockStatement(const JSTokenLocation&, int, int, int) { return StatementResult; }
    200209    int createExprStatement(const JSTokenLocation&, int, int, int) { return StatementResult; }
Note: See TracChangeset for help on using the changeset viewer.