haikuwebkit/JSTests/modules/export-default-function-nam...

9 lines
314 B
JavaScript
Raw Permalink Normal View History

[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): Canonical link: https://commits.webkit.org/179232@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@204842 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2016-08-23 18:14:30 +00:00
import func from "./export-default-function-name-in-function-declaration.js"
import { shouldBe } from "./resources/assert.js";
export default function () { }
// https://tc39.github.io/ecma262/#sec-exports-runtime-semantics-evaluation
shouldBe(func.name, 'default');
shouldBe(func.toString(), `function () { }`);