Chromium Code Reviews| Index: src/parsing/parser.cc |
| diff --git a/src/parsing/parser.cc b/src/parsing/parser.cc |
| index 389068162dd7882d3072ef3d88ca1ef0fb971092..b4fd65dfd24941213cff8aa98213976f7b00f0bd 100644 |
| --- a/src/parsing/parser.cc |
| +++ b/src/parsing/parser.cc |
| @@ -969,7 +969,7 @@ FunctionLiteral* Parser::DoParseProgram(ParseInfo* info) { |
| // pre-existing bindings should be made writable, enumerable and |
| // nonconfigurable if possible, whereas this code will leave attributes |
| // unchanged if the property already exists. |
| - InsertSloppyBlockFunctionVarBindings(scope, &ok); |
| + InsertSloppyBlockFunctionVarBindings(scope, false, &ok); |
| } |
| if (ok) { |
| CheckConflictingVarDeclarations(scope_, &ok); |
| @@ -4356,9 +4356,6 @@ FunctionLiteral* Parser::ParseFunctionLiteral( |
| CheckDecimalLiteralWithLeadingZero(use_counts_, scope->start_position(), |
| scope->end_position()); |
| } |
| - if (is_sloppy(language_mode)) { |
| - InsertSloppyBlockFunctionVarBindings(scope, CHECK_OK); |
| - } |
| CheckConflictingVarDeclarations(scope, CHECK_OK); |
| if (body) { |
| @@ -4807,6 +4804,10 @@ ZoneList<Statement*>* Parser::ParseEagerFunctionBody( |
| SetLanguageMode(scope_, inner_scope->language_mode()); |
| Block* init_block = BuildParameterInitializationBlock(parameters, CHECK_OK); |
| + if (is_sloppy(inner_scope->language_mode())) { |
| + InsertSloppyBlockFunctionVarBindings(inner_scope, true, CHECK_OK); |
| + } |
| + |
| if (IsAsyncFunction(kind)) { |
| init_block = BuildRejectPromiseOnException(init_block); |
| } |
| @@ -4822,6 +4823,10 @@ ZoneList<Statement*>* Parser::ParseEagerFunctionBody( |
| result->Add(init_block, zone()); |
| result->Add(inner_block, zone()); |
| + } else { |
| + if (is_sloppy(inner_scope->language_mode())) { |
| + InsertSloppyBlockFunctionVarBindings(inner_scope, false, CHECK_OK); |
| + } |
| } |
| if (function_type == FunctionLiteral::kNamedExpression) { |
| @@ -5108,37 +5113,68 @@ void Parser::InsertShadowingVarBindingInitializers(Block* inner_block) { |
| } |
| } |
| - |
| -void Parser::InsertSloppyBlockFunctionVarBindings(Scope* scope, bool* ok) { |
| +void Parser::InsertSloppyBlockFunctionVarBindings(Scope* scope, |
| + bool outer_is_param_scope, |
| + bool* ok) { |
| // For each variable which is used as a function declaration in a sloppy |
| // block, |
| DCHECK(scope->is_declaration_scope()); |
| SloppyBlockFunctionMap* map = scope->sloppy_block_function_map(); |
| for (ZoneHashMap::Entry* p = map->Start(); p != nullptr; p = map->Next(p)) { |
| AstRawString* name = static_cast<AstRawString*>(p->key); |
| - // If the variable wouldn't conflict with a lexical declaration, |
| - Variable* var = scope->LookupLocal(name); |
| - if (var == nullptr || !IsLexicalVariableMode(var->mode())) { |
| + |
| + bool var_created = false; |
| + |
| + // Write in assignments to var for each block-scoped function declaration |
| + auto delegates = static_cast<SloppyBlockFunctionMap::Vector*>(p->value); |
| + for (SloppyBlockFunctionStatement* delegate : *delegates) { |
| + // If the variable wouldn't conflict with a lexical declaration |
| + // or parameter, |
| + |
| + // Check if there's a conflict with a simple parameter |
| + if (scope->IsDeclaredParameter(name)) continue; |
| + |
| + Scope* outer_scope = scope->outer_scope(); |
| + |
| + // Check if there's a conflict with a complex parameter |
| + if (outer_is_param_scope && outer_scope->LookupLocal(name) != nullptr) { |
|
Dan Ehrenberg
2016/06/28 01:07:00
Why would this not prohibit hoisting in cases like
bakkot
2016/06/28 01:19:37
The relevant "outer scope" is that of the scope in
|
| + continue; |
| + } |
| + |
| + // Check if there's a conflict with a lexical declaration |
| + Scope* query_scope = delegate->scope()->outer_scope(); |
| + Variable* var = nullptr; |
| + bool should_hoist = true; |
| + do { |
| + var = query_scope->LookupLocal(name); |
| + if (var != nullptr && IsLexicalVariableMode(var->mode())) { |
| + should_hoist = false; |
| + break; |
| + } |
| + query_scope = query_scope->outer_scope(); |
| + } while (query_scope != outer_scope); |
| + |
| + if (!should_hoist) continue; |
| + |
| // Declare a var-style binding for the function in the outer scope |
| - VariableProxy* proxy = scope->NewUnresolved(factory(), name); |
| - Declaration* declaration = factory()->NewVariableDeclaration( |
| - proxy, VAR, scope, RelocInfo::kNoPosition); |
| - Declare(declaration, DeclarationDescriptor::NORMAL, true, ok, scope); |
| - DCHECK(ok); // Based on the preceding check, this should not fail |
| - if (!ok) return; |
| - |
| - // Write in assignments to var for each block-scoped function declaration |
| - auto delegates = static_cast<SloppyBlockFunctionMap::Vector*>(p->value); |
| - for (SloppyBlockFunctionStatement* delegate : *delegates) { |
| - // Read from the local lexical scope and write to the function scope |
| - VariableProxy* to = scope->NewUnresolved(factory(), name); |
| - VariableProxy* from = delegate->scope()->NewUnresolved(factory(), name); |
| - Expression* assignment = factory()->NewAssignment( |
| - Token::ASSIGN, to, from, RelocInfo::kNoPosition); |
| - Statement* statement = factory()->NewExpressionStatement( |
| - assignment, RelocInfo::kNoPosition); |
| - delegate->set_statement(statement); |
| + if (!var_created) { |
| + var_created = true; |
| + VariableProxy* proxy = scope->NewUnresolved(factory(), name); |
| + Declaration* declaration = factory()->NewVariableDeclaration( |
| + proxy, VAR, scope, RelocInfo::kNoPosition); |
| + Declare(declaration, DeclarationDescriptor::NORMAL, true, ok, scope); |
| + DCHECK(ok); // Based on the preceding check, this should not fail |
| + if (!ok) return; |
| } |
| + |
| + // Read from the local lexical scope and write to the function scope |
| + VariableProxy* to = scope->NewUnresolved(factory(), name); |
| + VariableProxy* from = delegate->scope()->NewUnresolved(factory(), name); |
| + Expression* assignment = factory()->NewAssignment(Token::ASSIGN, to, from, |
| + RelocInfo::kNoPosition); |
| + Statement* statement = |
| + factory()->NewExpressionStatement(assignment, RelocInfo::kNoPosition); |
| + delegate->set_statement(statement); |
| } |
| } |
| } |