Changeset 195178 in webkit


Ignore:
Timestamp:
Jan 16, 2016 4:04:37 PM (8 years ago)
Author:
commit-queue@webkit.org
Message:

[ES6] Arrow function syntax. Arrow function should support the destructuring parameters.
https://bugs.webkit.org/show_bug.cgi?id=146934

Patch by Skachkov Oleksandr <gskachkov@gmail.com> on 2016-01-16
Reviewed by Saam Barati.
Source/JavaScriptCore:

Added support of destructuring parameters, before arrow function expect only simple parameters,
e.g. (), (x), (x, y) or x in assigment expressio. To support destructuring parameters added
additional check that check for destructuring paramters if check does not pass for simple parameters.

  • parser/Parser.cpp:

(JSC::Parser<LexerType>::isArrowFunctionParameters):
(JSC::Parser<LexerType>::parseAssignmentExpression):

  • parser/Parser.h:

LayoutTests:

  • js/arrowfunction-syntax-errors-expected.txt:
  • js/arrowfunction-syntax-expected.txt:
  • js/script-tests/arrowfunction-syntax-errors.js:
  • js/script-tests/arrowfunction-syntax.js:
Location:
trunk
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r195175 r195178  
     12016-01-16  Skachkov Oleksandr  <gskachkov@gmail.com>
     2
     3        [ES6] Arrow function syntax. Arrow function should support the destructuring parameters.
     4        https://bugs.webkit.org/show_bug.cgi?id=146934
     5
     6        Reviewed by Saam Barati.
     7
     8        * js/arrowfunction-syntax-errors-expected.txt:
     9        * js/arrowfunction-syntax-expected.txt:
     10        * js/script-tests/arrowfunction-syntax-errors.js:
     11        * js/script-tests/arrowfunction-syntax.js:
     12
    1132016-01-16  Joseph Pecoraro  <pecoraro@apple.com>
    214
  • trunk/LayoutTests/js/arrowfunction-syntax-errors-expected.txt

    r187763 r195178  
    124124PASS var af3=(x, y)
    125125=>y+1 threw exception SyntaxError: Unexpected token '=>'.
     126PASS ([a, b] => a + b)(["a_", "b_"]) threw exception SyntaxError: Unexpected token '=>'. Expected ')' to end a compound expression..
     127PASS ({a, b} => a + b)({a:"a_", b:"b_"}) threw exception SyntaxError: Unexpected token '=>'. Expected ')' to end a compound expression..
     128PASS ({c:a,d:b} => a + b)({c:"a_", d:"b_"}) threw exception SyntaxError: Unexpected token '=>'. Expected ')' to end a compound expression..
     129PASS ({c:b,d:a} => a + b)({c:"a_", d:"b_"}) threw exception SyntaxError: Unexpected token '=>'. Expected ')' to end a compound expression..
     130PASS var arr1 = [a, b] => a + b; threw exception SyntaxError: Unexpected token '=>'. Expected ';' after variable declaration..
     131PASS var arr2 = {a, b} => a + b; threw exception SyntaxError: Unexpected token '=>'. Expected ';' after variable declaration..
     132PASS var arr3 = {c:a,d:b} => a + b; threw exception SyntaxError: Unexpected token '=>'. Expected ';' after variable declaration..
     133PASS var arr3 = {c:b,d:a} => a + b; threw exception SyntaxError: Unexpected token '=>'. Expected ';' after variable declaration..
    126134PASS successfullyParsed is true
    127135
  • trunk/LayoutTests/js/arrowfunction-syntax-expected.txt

    r185989 r195178  
    4141PASS (function funcSelfExecAE3(value) { var f = (x) => { x++; return x + 1; }; return f(value);})(123); is 125
    4242PASS (function funcSelfExecAE4(value) { var f = (x, y) => { x++; return x + y; }; return f(value, value * 2);})(123); is 370
     43PASS (([a, b]) => a + b)(["a_", "b_"]) is "a_b_"
     44PASS (({a, b}) => a + b)({a:"a_", b:"b_"}) is "a_b_"
     45PASS (({c:a, d:b}) => a + b)({c:"a_", d:"b_"}) is "a_b_"
     46PASS (({c:b, d:a}) => a + b)({c:"a_", d:"b_"}) is "b_a_"
     47PASS ((x, y, {c:b, d:a}) => x + y + a + b)("x_", "y_", {c:"a_", d:"b_"}) is "x_y_b_a_"
     48PASS (({c:b, d:a}, x, y) => x + y + a + b)({c:"a_", d:"b_"}, "x_", "y_") is "x_y_b_a_"
     49PASS ((x, y, {c:b, d:a}, [e, f]) => x + y + a + b + e + f)("x_", "y_", {c:"a_", d:"b_"}, ["e_", "f_"]) is "x_y_b_a_e_f_"
     50PASS ((x, y, {c:b, d:a}, [e, f], ...theArgs) => x + y + a + b + e + f + theArgs[0] + theArgs[1])("x_", "y_", {c:"a_", d:"b_"}, ["e_", "f_"], "g_", "h_") is "x_y_b_a_e_f_g_h_"
     51PASS arr1(["a_", "b_"]) is "a_b_"
     52PASS arr2({a:"a_", b:"b_"}) is "a_b_"
     53PASS arr3({c:"a_", d:"b_"}) is "a_b_"
     54PASS arr4({c:"a_", d:"b_"}) is "b_a_"
     55PASS arr5("x_", "y_", {c:"a_", d:"b_"}) is "x_y_b_a_"
     56PASS arr6({c:"a_", d:"b_"}, "x_", "y_") is "x_y_b_a_"
     57PASS arr7("x_", "y_", {c:"a_", d:"b_"}, ["e_", "f_"]) is "x_y_b_a_e_f_"
     58PASS arr8("x_", "y_", {c:"a_", d:"b_"}, ["e_", "f_"], "g_", "h_") is "x_y_b_a_e_f_g_h_"
    4359PASS successfullyParsed is true
    4460
  • trunk/LayoutTests/js/script-tests/arrowfunction-syntax-errors.js

    r186047 r195178  
    3939shouldThrow("var af3=(x, y)\n=>y+1");
    4040
     41shouldThrow('([a, b] => a + b)(["a_", "b_"])' );
     42shouldThrow('({a, b} => a + b)({a:"a_", b:"b_"})');
     43shouldThrow('({c:a,d:b} => a + b)({c:"a_", d:"b_"})');
     44shouldThrow('({c:b,d:a} => a + b)({c:"a_", d:"b_"})');
     45
     46shouldThrow('var arr1 = [a, b] => a + b;');
     47shouldThrow('var arr2 = {a, b} => a + b;');
     48shouldThrow('var arr3 = {c:a,d:b} => a + b;');
     49shouldThrow('var arr3 = {c:b,d:a} => a + b;');
     50
    4151var successfullyParsed = true;
  • trunk/LayoutTests/js/script-tests/arrowfunction-syntax.js

    r185989 r195178  
    8080shouldBe('(function funcSelfExecAE4(value) { var f = (x, y) => { x++; return x + y; }; return f(value, value * 2);})(123);', '370');
    8181
     82shouldBe('(([a, b]) => a + b)(["a_", "b_"])', '"a_b_"');
     83shouldBe('(({a, b}) => a + b)({a:"a_", b:"b_"})', '"a_b_"');
     84shouldBe('(({c:a, d:b}) => a + b)({c:"a_", d:"b_"})', '"a_b_"');
     85shouldBe('(({c:b, d:a}) => a + b)({c:"a_", d:"b_"})', '"b_a_"');
     86shouldBe('((x, y, {c:b, d:a}) => x + y + a + b)("x_", "y_", {c:"a_", d:"b_"})', '"x_y_b_a_"');
     87shouldBe('(({c:b, d:a}, x, y) => x + y + a + b)({c:"a_", d:"b_"}, "x_", "y_")', '"x_y_b_a_"');
     88shouldBe('((x, y, {c:b, d:a}, [e, f]) => x + y + a + b + e + f)("x_", "y_", {c:"a_", d:"b_"}, ["e_", "f_"])', '"x_y_b_a_e_f_"');
     89shouldBe('((x, y, {c:b, d:a}, [e, f], ...theArgs) => x + y + a + b + e + f + theArgs[0] + theArgs[1])("x_", "y_", {c:"a_", d:"b_"}, ["e_", "f_"], "g_", "h_")', '"x_y_b_a_e_f_g_h_"');
     90
     91var arr1 = ([a, b]) => a + b;
     92shouldBe('arr1(["a_", "b_"])', '"a_b_"');
     93
     94var arr2 = ({a, b}) => a + b;
     95shouldBe('arr2({a:"a_", b:"b_"})', '"a_b_"');
     96
     97var arr3 = ({c:a, d:b}) => a + b;
     98shouldBe('arr3({c:"a_", d:"b_"})', '"a_b_"');
     99
     100var arr4 = ({c:b, d:a}) => a + b;
     101shouldBe('arr4({c:"a_", d:"b_"})', '"b_a_"');
     102
     103var arr5 = (x, y, {c:b, d:a}) => x + y + a + b;
     104shouldBe('arr5("x_", "y_", {c:"a_", d:"b_"})', '"x_y_b_a_"');
     105
     106var arr6 = ({c:b, d:a}, x, y) => x + y + a + b;
     107shouldBe('arr6({c:"a_", d:"b_"}, "x_", "y_")', '"x_y_b_a_"');
     108
     109var arr7 = (x, y, {c:b, d:a}, [e, f]) => x + y + a + b + e + f;
     110shouldBe('arr7("x_", "y_", {c:"a_", d:"b_"}, ["e_", "f_"])', '"x_y_b_a_e_f_"');
     111
     112var arr8 = (x, y, {c:b, d:a}, [e, f], ...theArgs) => x + y + a + b + e + f + theArgs[0] + theArgs[1];
     113shouldBe('arr8("x_", "y_", {c:"a_", d:"b_"}, ["e_", "f_"], "g_", "h_")', '"x_y_b_a_e_f_g_h_"');
     114
    82115var successfullyParsed = true;
  • trunk/Source/JavaScriptCore/ChangeLog

    r195159 r195178  
     12016-01-16  Skachkov Oleksandr  <gskachkov@gmail.com>
     2
     3        [ES6] Arrow function syntax. Arrow function should support the destructuring parameters.
     4        https://bugs.webkit.org/show_bug.cgi?id=146934
     5
     6        Reviewed by Saam Barati.
     7       
     8        Added support of destructuring parameters, before arrow function expect only simple parameters,
     9        e.g. (), (x), (x, y) or x in assigment expressio. To support destructuring parameters added
     10        additional check that check for destructuring paramters if check does not pass for simple parameters.
     11
     12        * parser/Parser.cpp:
     13        (JSC::Parser<LexerType>::isArrowFunctionParameters):
     14        (JSC::Parser<LexerType>::parseAssignmentExpression):
     15        * parser/Parser.h:
     16
    1172016-01-15  Benjamin Poulain  <bpoulain@apple.com>
    218
  • trunk/Source/JavaScriptCore/parser/Parser.cpp

    r194881 r195178  
    372372    m_features = features;
    373373    m_numConstants = numConstants;
     374}
     375
     376template <typename LexerType>
     377bool Parser<LexerType>::isArrowFunctionParameters()
     378{
     379    bool isArrowFunction = false;
     380
     381    if (match(EOFTOK))
     382        return false;
     383   
     384    bool isOpenParen = match(OPENPAREN);
     385    bool isIdent = match(IDENT);
     386   
     387    if (!isOpenParen && !isIdent)
     388        return false;
     389
     390    SavePoint saveArrowFunctionPoint = createSavePoint();
     391       
     392    if (isIdent) {
     393        next();
     394        isArrowFunction = match(ARROWFUNCTION);
     395    } else {
     396        RELEASE_ASSERT(isOpenParen);
     397        next();
     398        if (match(CLOSEPAREN)) {
     399            next();
     400            isArrowFunction = match(ARROWFUNCTION);
     401        } else {
     402            SyntaxChecker syntaxChecker(const_cast<VM*>(m_vm), m_lexer.get());
     403            // We make fake scope, otherwise parseFormalParameters will add variable to current scope that lead to errors
     404            AutoPopScopeRef fakeScope(this, pushScope());
     405            fakeScope->setSourceParseMode(SourceParseMode::ArrowFunctionMode);
     406               
     407            unsigned parametersCount = 0;
     408            isArrowFunction = parseFormalParameters(syntaxChecker, syntaxChecker.createFormalParameterList(), parametersCount) && consume(CLOSEPAREN) && match(ARROWFUNCTION);
     409               
     410            popScope(fakeScope, syntaxChecker.NeedsFreeVariableInfo);
     411        }
     412    }
     413       
     414    restoreSavePoint(saveArrowFunctionPoint);
     415       
     416    return isArrowFunction;
    374417}
    375418
     
    28882931
    28892932#if ENABLE(ES6_ARROWFUNCTION_SYNTAX)
    2890     if (isArrowFunctionParamters())
     2933    if (isArrowFunctionParameters())
    28912934        return parseArrowFunctionExpression(context);
    28922935#endif
  • trunk/Source/JavaScriptCore/parser/Parser.h

    r194496 r195178  
    10281028        return match(SEMICOLON) || match(COMMA) || match(CLOSEPAREN) || match(CLOSEBRACE) || match(CLOSEBRACKET) || match(EOFTOK) || m_lexer->prevTerminator();
    10291029    }
    1030    
    1031     ALWAYS_INLINE bool isArrowFunctionParamters()
    1032     {
    1033         bool isArrowFunction = false;
    1034        
    1035         if (match(EOFTOK))
    1036             return isArrowFunction;
    1037        
    1038         SavePoint saveArrowFunctionPoint = createSavePoint();
    1039        
    1040         if (consume(OPENPAREN)) {
    1041             bool isArrowFunctionParamters = true;
    1042            
    1043             while (consume(IDENT)) {
    1044                 if (consume(COMMA)) {
    1045                     if (!match(IDENT)) {
    1046                         isArrowFunctionParamters = false;
    1047                         break;
    1048                     }
    1049                 } else
    1050                     break;
    1051             }
    1052            
    1053             if (isArrowFunctionParamters) {
    1054                 if (consume(CLOSEPAREN) && match(ARROWFUNCTION))
    1055                     isArrowFunction = true;
    1056             }
    1057         } else if (consume(IDENT) && match(ARROWFUNCTION))
    1058             isArrowFunction = true;
    1059 
    1060         restoreSavePoint(saveArrowFunctionPoint);
    1061        
    1062         return isArrowFunction;
    1063     }
    1064    
     1030
    10651031    ALWAYS_INLINE unsigned tokenStart()
    10661032    {
     
    12591225    template <class TreeBuilder> NEVER_INLINE bool parseFunctionInfo(TreeBuilder&, FunctionRequirements, SourceParseMode, bool nameIsInContainingScope, ConstructorKind, SuperBinding, int functionKeywordStart, ParserFunctionInfo<TreeBuilder>&, FunctionDefinitionType);
    12601226   
     1227    ALWAYS_INLINE bool isArrowFunctionParameters();
     1228   
    12611229    template <class TreeBuilder> NEVER_INLINE int parseFunctionParameters(TreeBuilder&, SourceParseMode, ParserFunctionInfo<TreeBuilder>&);
    12621230    template <class TreeBuilder> NEVER_INLINE typename TreeBuilder::FormalParameterList createGeneratorParameters(TreeBuilder&);
Note: See TracChangeset for help on using the changeset viewer.