source: webkit/trunk/Source/JavaScriptCore/parser/ModuleScopeData.h

Last change on this file was 204842, checked in by Yusuke Suzuki, 9 years ago

[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..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..get hello):
(testSyntaxError.Hello..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 , 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 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 size: 2.5 KB
Line 
1/*
2* Copyright (C) 2015 Apple Inc. All rights reserved.
3* Copyright (C) 2016 Yusuke Suzuki <[email protected]>
4*
5* Redistribution and use in source and binary forms, with or without
6* modification, are permitted provided that the following conditions
7* are met:
8* 1. Redistributions of source code must retain the above copyright
9* notice, this list of conditions and the following disclaimer.
10* 2. Redistributions in binary form must reproduce the above copyright
11* notice, this list of conditions and the following disclaimer in the
12* documentation and/or other materials provided with the distribution.
13*
14* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
18* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*/
26
27#pragma once
28
29#include "Identifier.h"
30#include <wtf/RefPtr.h>
31
32namespace JSC {
33
34class ModuleScopeData : public RefCounted<ModuleScopeData> {
35WTF_MAKE_NONCOPYABLE(ModuleScopeData);
36WTF_MAKE_FAST_ALLOCATED;
37public:
38typedef HashMap<RefPtr<UniquedStringImpl>, Vector<RefPtr<UniquedStringImpl>>, IdentifierRepHash, HashTraits<RefPtr<UniquedStringImpl>>> IdentifierAliasMap;
39
40static Ref<ModuleScopeData> create() { return adoptRef(*new ModuleScopeData); }
41
42const IdentifierAliasMap& exportedBindings() const { return m_exportedBindings; }
43
44bool exportName(const Identifier& exportedName)
45{
46return m_exportedNames.add(exportedName.impl()).isNewEntry;
47}
48
49void exportBinding(const Identifier& localName, const Identifier& exportedName)
50{
51m_exportedBindings.add(localName.impl(), Vector<RefPtr<UniquedStringImpl>>()).iterator->value.append(exportedName.impl());
52}
53
54void exportBinding(const Identifier& localName)
55{
56exportBinding(localName, localName);
57}
58
59private:
60ModuleScopeData() = default;
61
62IdentifierSet m_exportedNames { };
63IdentifierAliasMap m_exportedBindings { };
64};
65
66} // namespace
Note: See TracBrowser for help on using the repository browser.