Ignore:
Timestamp:
Aug 23, 2016, 11:14:30 AM (9 years ago)
Author:
Yusuke Suzuki
Message:

[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.

  1. 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):

File:
1 edited

Legend:

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

    r203953 r204842  
    3636WTF_MAKE_FAST_ALLOCATED;
    3737public:
    38     typedef HashMap<RefPtr<UniquedStringImpl>, IdentifierSet, IdentifierRepHash, HashTraits<RefPtr<UniquedStringImpl>>> IdentifierAliasMap;
     38    typedef HashMap<RefPtr<UniquedStringImpl>, Vector<RefPtr<UniquedStringImpl>>, IdentifierRepHash, HashTraits<RefPtr<UniquedStringImpl>>> IdentifierAliasMap;
    3939
    4040    static Ref<ModuleScopeData> create() { return adoptRef(*new ModuleScopeData); }
     
    4949    void exportBinding(const Identifier& localName, const Identifier& exportedName)
    5050    {
    51         m_exportedBindings.add(localName.impl(), IdentifierSet()).iterator->value.add(exportedName.impl());
     51        m_exportedBindings.add(localName.impl(), Vector<RefPtr<UniquedStringImpl>>()).iterator->value.append(exportedName.impl());
    5252    }
    5353
Note: See TracChangeset for help on using the changeset viewer.