Commit 819c429c authored by adamk's avatar adamk Committed by Commit bot

[es6] Support Function name inference in variable declarations

This is behind the --harmony-function-name flag, currently disabled.

With the flag enabled, we now pass the relevant tests in
language/statements/*/fn-name-*.

BUG=v8:3699
LOG=y

Review URL: https://codereview.chromium.org/1518873004

Cr-Commit-Position: refs/heads/master@{#32817}
parent 5ceb4fec
......@@ -2531,6 +2531,7 @@ class FunctionLiteral final : public Expression {
Handle<String> name() const { return raw_name_->string(); }
const AstRawString* raw_name() const { return raw_name_; }
void set_raw_name(const AstRawString* name) { raw_name_ = name; }
Scope* scope() const { return scope_; }
ZoneList<Statement*>* body() const { return body_; }
void set_function_token_position(int pos) { function_token_position_ = pos; }
......@@ -2699,6 +2700,11 @@ class ClassLiteral final : public Expression {
Handle<String> name() const { return raw_name_->string(); }
const AstRawString* raw_name() const { return raw_name_; }
void set_raw_name(const AstRawString* name) {
DCHECK_NULL(raw_name_);
raw_name_ = name;
}
Scope* scope() const { return scope_; }
VariableProxy* class_variable_proxy() const { return class_variable_proxy_; }
Expression* extends() const { return extends_; }
......
......@@ -2112,6 +2112,7 @@ EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_completion)
EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_tolength)
EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_do_expressions)
EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_regexp_lookbehind)
EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_function_name)
EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(promise_extra)
......@@ -2649,6 +2650,7 @@ bool Genesis::InstallExperimentalNatives() {
static const char* harmony_do_expressions_natives[] = {nullptr};
static const char* harmony_regexp_subclass_natives[] = {nullptr};
static const char* harmony_regexp_lookbehind_natives[] = {nullptr};
static const char* harmony_function_name_natives[] = {nullptr};
static const char* promise_extra_natives[] = {"native promise-extra.js",
nullptr};
......
......@@ -202,6 +202,7 @@ DEFINE_IMPLICATION(es_staging, harmony_destructuring_assignment)
V(harmony_modules, "harmony modules") \
V(harmony_proxies, "harmony proxies") \
V(harmony_unicode_regexps, "harmony unicode regexps") \
V(harmony_function_name, "harmony Function name inference") \
V(harmony_reflect, "harmony Reflect API") \
V(harmony_sloppy_function, "harmony sloppy function block scoping") \
V(harmony_sharedarraybuffer, "harmony sharedarraybuffer") \
......
......@@ -116,7 +116,8 @@ class ParserBase : public Traits {
allow_harmony_destructuring_assignment_(false),
allow_strong_mode_(false),
allow_legacy_const_(true),
allow_harmony_do_expressions_(false) {}
allow_harmony_do_expressions_(false),
allow_harmony_function_name_(false) {}
#define ALLOW_ACCESSORS(name) \
bool allow_##name() const { return allow_##name##_; } \
......@@ -133,6 +134,7 @@ class ParserBase : public Traits {
ALLOW_ACCESSORS(strong_mode);
ALLOW_ACCESSORS(legacy_const);
ALLOW_ACCESSORS(harmony_do_expressions);
ALLOW_ACCESSORS(harmony_function_name);
#undef ALLOW_ACCESSORS
uintptr_t stack_limit() const { return stack_limit_; }
......@@ -920,6 +922,7 @@ class ParserBase : public Traits {
bool allow_strong_mode_;
bool allow_legacy_const_;
bool allow_harmony_do_expressions_;
bool allow_harmony_function_name_;
};
......
......@@ -933,6 +933,7 @@ Parser::Parser(ParseInfo* info)
set_allow_strong_mode(FLAG_strong_mode);
set_allow_legacy_const(FLAG_legacy_const);
set_allow_harmony_do_expressions(FLAG_harmony_do_expressions);
set_allow_harmony_function_name(FLAG_harmony_function_name);
for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount;
++feature) {
use_counts_[feature] = 0;
......@@ -2565,6 +2566,21 @@ void Parser::ParseVariableDeclarations(VariableDeclarationContext var_context,
fni_->RemoveLastFunction();
}
}
if (allow_harmony_function_name() && single_name) {
if (value->IsFunctionLiteral()) {
auto function_literal = value->AsFunctionLiteral();
if (function_literal->is_anonymous()) {
function_literal->set_raw_name(single_name);
}
} else if (value->IsClassLiteral()) {
auto class_literal = value->AsClassLiteral();
if (class_literal->raw_name() == nullptr) {
class_literal->set_raw_name(single_name);
}
}
}
// End position of the initializer is after the assignment expression.
initializer_position = scanner()->location().end_pos;
} else {
......@@ -4908,6 +4924,7 @@ PreParser::PreParseResult Parser::ParseLazyFunctionBodyWithPreParser(
SET_ALLOW(harmony_destructuring_bind);
SET_ALLOW(strong_mode);
SET_ALLOW(harmony_do_expressions);
SET_ALLOW(harmony_function_name);
#undef SET_ALLOW
}
PreParser::PreParseResult result = reusable_preparser_->PreParseLazyFunction(
......
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Flags: --harmony-function-name
(function testVariableDeclarationsFunction() {
'use strict';
var a = function(){};
assertEquals('a', a.name);
let b = () => {};
assertEquals('b', b.name);
const c = ((function(){}));
assertEquals('c', c.name);
var x = function(){}, y = () => {}, z = function withName() {};
assertEquals('x', x.name);
assertEquals('y', y.name);
assertEquals('withName', z.name);
})();
(function testVariableDeclarationsClass() {
'use strict';
var a = class {};
assertEquals('a', a.name);
let b = ((class {}));
assertEquals('b', b.name);
// Should not overwrite name property.
const c = class { static name() { } }
assertEquals('function', typeof c.name);
var x = class {}, y = class NamedClass {};
assertEquals('x', x.name);
assertEquals('NamedClass', y.name);
})();
......@@ -143,9 +143,6 @@
'language/computed-property-names/class/static/method-symbol': [FAIL, FAIL_SLOPPY],
'language/computed-property-names/class/static/method-string': [FAIL, FAIL_SLOPPY],
# https://code.google.com/p/v8/issues/detail?id=3673
'language/statements/class/definition/basics': [FAIL],
# https://code.google.com/p/v8/issues/detail?id=3566
'language/statements/for-of/body-dstr-assign-error': [FAIL],
'language/statements/for-of/body-put-error': [FAIL],
......@@ -251,6 +248,7 @@
'language/expressions/object/method-definition/fn-name-cover': [FAIL],
'language/expressions/object/method-definition/fn-name-fn': [FAIL],
'language/expressions/object/method-definition/fn-name-gen': [FAIL],
'language/statements/class/definition/basics': [FAIL],
'language/statements/class/definition/fn-name-accessor-get': [FAIL],
'language/statements/class/definition/fn-name-accessor-set': [FAIL],
'language/statements/class/definition/fn-name-gen-method': [FAIL],
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment