Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(302)

Unified Diff: src/parser.cc

Issue 1127063003: [es6] implement default parameters via desugaring (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/parser.cc
diff --git a/src/parser.cc b/src/parser.cc
index feed1e4b12fcef812309dfe4c4a7870fbcd41a13..57a44d3aa629d02f88c1c6764635aa02f4a4a2ba 100644
--- a/src/parser.cc
+++ b/src/parser.cc
@@ -1134,13 +1134,18 @@ FunctionLiteral* Parser::ParseLazy(Isolate* isolate, ParseInfo* info,
scope->set_start_position(shared_info->start_position());
FormalParameterErrorLocations error_locs;
bool has_rest = false;
+ bool has_initializers = false;
+ ZoneList<Expression*>* initializers =
+ new (zone()) ZoneList<Expression*>(0, zone());
if (Check(Token::LPAREN)) {
// '(' StrictFormalParameters ')'
- ParseFormalParameterList(scope, &error_locs, &has_rest, &ok);
+ ParseFormalParameterList(scope, &error_locs, initializers,
+ &has_initializers, &has_rest, &ok);
if (ok) ok = Check(Token::RPAREN);
} else {
// BindingIdentifier
- ParseFormalParameter(scope, &error_locs, has_rest, &ok);
+ ParseFormalParameter(scope, &error_locs, nullptr, nullptr, has_rest,
+ &ok);
}
if (ok) {
@@ -2064,6 +2069,11 @@ Variable* Parser::Declare(Declaration* declaration, bool resolve, bool* ok) {
}
+void Parser::ShadowParametersForExpressions(Scope* scope) {
+ scope->ShadowParametersForExpressions();
+}
+
+
// Language extension which is only enabled for source files loaded
// through the API's extension mechanism. A native function
// declaration is resolved by looking up the function through a
@@ -3522,6 +3532,97 @@ Statement* Parser::DesugarLexicalBindingsInForStatement(
}
+ZoneList<Statement*>* Parser::DesugarInitializeParameters(
+ Scope* scope, bool has_initializers, ZoneList<Expression*>* initializers) {
+ DCHECK(scope->is_function_scope());
+
arv (Not doing code reviews) 2015/05/06 20:32:38 How about adding a comment block explaining how th
+ if (has_initializers) {
+ ShadowParametersForExpressions(scope);
+ ZoneList<Statement*>* body = new (zone()) ZoneList<Statement*>(0, zone());
+ for (int i = 0; i < initializers->length(); ++i) {
+ Expression* initializer = initializers->at(i);
+
+ // Position of parameter VariableProxy, for hole-checking
+ int pos = scope->parameter_position(i);
+
+ // Lexically declare the initialized variable
+ static const VariableMode mode = LET;
+ VariableProxy* proxy =
+ NewUnresolved(scope->parameter(i)->raw_name(), mode);
+ VariableDeclaration* declaration = factory()->NewVariableDeclaration(
+ proxy, mode, scope, RelocInfo::kNoPosition);
+ bool ok = true;
+ proxy = factory()->NewVariableProxy(Declare(declaration, true, &ok), pos);
+ DCHECK(ok);
+ proxy->var()->set_maybe_assigned();
+
+ const AstRawString* fn_name = ast_value_factory()->empty_string();
+ const Runtime::Function* arguments =
+ Runtime::FunctionForId(Runtime::kInlineArguments);
arv (Not doing code reviews) 2015/05/06 20:32:38 I think Andreas wanted: function f(x = expr) {}
caitp (gmail) 2015/05/06 20:42:55 I don't think bounds-checking is needed --- at lea
+ ZoneList<Expression*>* arguments_i0 =
+ new (zone()) ZoneList<Expression*>(0, zone());
+ arguments_i0->Add(factory()->NewSmiLiteral(i, RelocInfo::kNoPosition),
+ zone());
+
+ // TODO(caitp): ensure proper TDZ behaviour --- need hole-check for
+ // all parameter bindings, including ones without initializers
+ if (initializer) {
+ // IS_UNDEFINED(%_Arguments(i)) ? <initializer> : %_Arguments(i);
+ ZoneList<Expression*>* arguments_i1 =
+ new (zone()) ZoneList<Expression*>(0, zone());
+ arguments_i1->Add(factory()->NewSmiLiteral(i, RelocInfo::kNoPosition),
+ zone());
+
+ Expression* arg_or_default = factory()->NewConditional(
+ // condition:
+ factory()->NewCompareOperation(
+ Token::EQ_STRICT,
+ factory()->NewCallRuntime(fn_name, arguments, arguments_i0,
+ RelocInfo::kNoPosition),
+ factory()->NewUndefinedLiteral(RelocInfo::kNoPosition),
+ RelocInfo::kNoPosition),
+ // if true:
+ initializer,
+ // if false:
+ factory()->NewCallRuntime(fn_name, arguments, arguments_i1,
+ RelocInfo::kNoPosition),
+ RelocInfo::kNoPosition);
+
+ Expression* assign = factory()->NewAssignment(
+ Token::INIT_LET, proxy, arg_or_default, RelocInfo::kNoPosition);
+
+ body->Add(
+ factory()->NewExpressionStatement(assign, RelocInfo::kNoPosition),
+ zone());
+ proxy->var()->set_initializer_position(initializer->position());
+ } else {
+ // let <name> = %_Arguments(i)
+ Expression* assign = factory()->NewAssignment(
+ Token::INIT_LET, proxy,
+ factory()->NewCallRuntime(fn_name, arguments, arguments_i0,
+ RelocInfo::kNoPosition),
+ RelocInfo::kNoPosition);
+ body->Add(
+ factory()->NewExpressionStatement(assign, RelocInfo::kNoPosition),
+ zone());
+ proxy->var()->set_initializer_position(pos);
+ }
+ }
+ return body;
+ } else {
arv (Not doing code reviews) 2015/05/06 20:32:38 no else after return
+ // If hasParameterExpressions is false, remove the unnecessary parameter
+ // block scopes.
+ ZoneList<Scope*>* scopes = scope->inner_scopes();
+ for (int i = 0; i < scopes->length(); ++i) {
+ Scope* scope = scopes->at(i);
+ DCHECK(scope->is_block_scope());
+ scope->FinalizeBlockScope();
+ }
+ return nullptr;
+ }
+}
+
+
Statement* Parser::ParseForStatement(ZoneList<const AstRawString*>* labels,
bool* ok) {
// ForStatement ::
@@ -3932,7 +4033,8 @@ void ParserTraits::DeclareArrowFunctionParameters(
parser_->scope_->RemoveUnresolved(expr->AsVariableProxy());
bool is_rest = false;
- bool is_duplicate = DeclareFormalParameter(scope, raw_name, is_rest);
+ int pos = expr->position();
+ bool is_duplicate = DeclareFormalParameter(scope, raw_name, is_rest, pos);
if (is_duplicate) {
// Arrow function parameter lists are parsed as StrictFormalParameters,
@@ -4061,8 +4163,12 @@ FunctionLiteral* Parser::ParseFunctionLiteral(
Expect(Token::LPAREN, CHECK_OK);
int start_position = scanner()->location().beg_pos;
scope_->set_start_position(start_position);
+ ZoneList<Expression*>* initializers =
+ new (zone()) ZoneList<Expression*>(0, zone());
+ bool has_initializers = false;
num_parameters =
- ParseFormalParameterList(scope, &error_locs, &has_rest, CHECK_OK);
+ ParseFormalParameterList(scope, &error_locs, initializers,
+ &has_initializers, &has_rest, CHECK_OK);
Expect(Token::RPAREN, CHECK_OK);
int formals_end_position = scanner()->location().end_pos;
@@ -4141,8 +4247,21 @@ FunctionLiteral* Parser::ParseFunctionLiteral(
SkipLazyFunctionBody(&materialized_literal_count,
&expected_property_count, CHECK_OK);
} else {
- body = ParseEagerFunctionBody(function_name, pos, fvar, fvar_init_op,
- kind, CHECK_OK);
+ body = DesugarInitializeParameters(scope, has_initializers, initializers);
+ if (has_initializers) {
+ // TODO(caitp): Function body scope must be a declaration scope
+ Scope* function_body_scope = NewScope(scope, BLOCK_SCOPE);
+ function_body_scope->set_start_position(scope->start_position());
+ function_body_scope->SetScopeName(function_name);
+ BlockState function_body_state(&scope_, function_body_scope);
+ ZoneList<Statement*>* inner_body = ParseEagerFunctionBody(
+ function_name, pos, fvar, fvar_init_op, kind, CHECK_OK);
+ scope->set_end_position(function_body_scope->end_position());
+ body->AddAll(*inner_body, zone());
+ } else {
+ body = ParseEagerFunctionBody(function_name, pos, fvar, fvar_init_op,
+ kind, CHECK_OK);
+ }
materialized_literal_count = function_state.materialized_literal_count();
expected_property_count = function_state.expected_property_count();
handler_count = function_state.handler_count();

Powered by Google App Engine
This is Rietveld 408576698