[ES6] Modules' `export default function/class` should be declaration
authorutatane.tea@gmail.com <utatane.tea@gmail.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 23 Aug 2016 18:14:30 +0000 (18:14 +0000)
committerutatane.tea@gmail.com <utatane.tea@gmail.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 23 Aug 2016 18:14:30 +0000 (18:14 +0000)
commit64d9de63319eaaa4cee2e598e42d50fccc8a4ed8
treeab5716e2f65b908e6dec9fd6e93e3ab3313017d9
parentbc888c759c631ca46a07154185efe5b60154fa37
[ES6] Modules' `export default function/class` should be declaration
https://bugs.webkit.org/show_bug.cgi?id=160499

Reviewed by Saam Barati.

JSTests:

Add several module tests. And flip the failed tests flags in test262.

* modules/export-default-function-name-in-assignment-expression.js: Added.
(export.default):
* modules/export-default-function-name-in-class-declaration.js: Added.
* modules/export-default-function-name-in-function-declaration.js: Added.
(export.default):
* modules/export-default-function-name-in-generator-declaration.js: Added.
(export.default):
* stress/method-name.js: Added.
(testSyntax):
(testSyntaxError):
(testSyntaxError.Hello.prototype.hello.hello):
(testSyntaxError.Hello):
(SyntaxError.Unexpected.identifier.string_appeared_here.Expected.an.opening.string_appeared_here.before.a.method.testSyntaxError.let.obj.hello.hello):
(testSyntaxError.Hello.prototype.get hello):
(testSyntaxError.Hello.prototype.set hello):
* test262.yaml:

Source/JavaScriptCore:

Previously, we parsed the following cases as FunctionExpression and ClassExpression.

    ```
    export default function () { }
    export default class { }
    ```

But, as per ES6 spec, the above `function ...` and `class ...` parts should be parsed
as function declaration and class declaration. This has big difference; the instantiation
of the function declarations are done in the function prologue.

In this patch, we correctly parse the above cases as declaration. To handle no-named
declarations, we add a new flag, DeclarationDefaultContext. This indicates [Default]
flag in the ES6 spec's BNF.

Furthermore, this patch also fixes the following name related bugs.

1. The bug related to "export default"'s function name. If the name is not provided (like the above case), the name of the function becomes
"default", not "*default*". This is special handling in ES6 spec. We handle this in JSFunction's reifyName.

2. `class Hello { hello hello() { } }` is accepted. We introduced FunctionRequirements::Unnamed and fix this bug.

* parser/ModuleScopeData.h:
(JSC::ModuleScopeData::exportBinding):
Exported names are already guranteed uniqueness by m_exportedNames. Not necessary to use set here. Use vector instead.

* parser/Parser.cpp:
(JSC::Parser<LexerType>::parseFunctionInfo):
If we pass FunctionRequirements::NoRequirements, we need to initialize functionInfo.name / classInfo.className
with the default fallback name. For example, in the above `export default` case, we initialize it with `*default*`.

(JSC::Parser<LexerType>::parseFunctionDeclaration):
(JSC::Parser<LexerType>::parseClassDeclaration):
(JSC::Parser<LexerType>::parseClass):
(JSC::Parser<LexerType>::parseExportDeclaration):
(JSC::Parser<LexerType>::parsePropertyMethod):
(JSC::Parser<LexerType>::parseGetterSetter):
(JSC::Parser<LexerType>::parseClassExpression):
(JSC::Parser<LexerType>::parseFunctionExpression):
(JSC::Parser<LexerType>::parsePrimaryExpression):
(JSC::Parser<LexerType>::parseArrowFunctionExpression):
* parser/Parser.h:
* runtime/JSFunction.cpp:
(JSC::JSFunction::reifyName):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@204842 268f45cc-cd09-0410-ab3c-d52691b4dbfc
16 files changed:
JSTests/ChangeLog
JSTests/modules/export-default-function-name-in-assignment-expression.js [new file with mode: 0644]
JSTests/modules/export-default-function-name-in-class-declaration.js [new file with mode: 0644]
JSTests/modules/export-default-function-name-in-function-declaration.js [new file with mode: 0644]
JSTests/modules/export-default-function-name-in-generator-declaration.js [new file with mode: 0644]
JSTests/stress/method-name.js [new file with mode: 0644]
JSTests/test262.yaml
Source/JavaScriptCore/ChangeLog
Source/JavaScriptCore/inspector/JSInjectedScriptHost.cpp
Source/JavaScriptCore/parser/ModuleScopeData.h
Source/JavaScriptCore/parser/Parser.cpp
Source/JavaScriptCore/parser/Parser.h
Source/JavaScriptCore/runtime/FunctionPrototype.cpp
Source/JavaScriptCore/runtime/JSFunction.cpp
Source/JavaScriptCore/runtime/JSFunction.h
Source/JavaScriptCore/runtime/LazyClassStructure.cpp