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

Side by Side Diff: src/ast/scopes.cc

Issue 2304853002: Revert of Allow lexically declared "arguments" in function scope in sloppy mode. (Closed)
Patch Set: Created 4 years, 3 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 unified diff | Download patch
« no previous file with comments | « src/ast/scopes.h ('k') | src/parsing/parser.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/ast/scopes.h" 5 #include "src/ast/scopes.h"
6 6
7 #include <set> 7 #include <set>
8 8
9 #include "src/accessors.h" 9 #include "src/accessors.h"
10 #include "src/bootstrapper.h" 10 #include "src/bootstrapper.h"
(...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after
485 DCHECK(has_this_declaration()); 485 DCHECK(has_this_declaration());
486 486
487 bool subclass_constructor = IsSubclassConstructor(function_kind_); 487 bool subclass_constructor = IsSubclassConstructor(function_kind_);
488 Variable* var = Declare( 488 Variable* var = Declare(
489 zone(), this, ast_value_factory->this_string(), 489 zone(), this, ast_value_factory->this_string(),
490 subclass_constructor ? CONST : VAR, Variable::THIS, 490 subclass_constructor ? CONST : VAR, Variable::THIS,
491 subclass_constructor ? kNeedsInitialization : kCreatedInitialized); 491 subclass_constructor ? kNeedsInitialization : kCreatedInitialized);
492 receiver_ = var; 492 receiver_ = var;
493 } 493 }
494 494
495 void DeclarationScope::DeclareArguments(AstValueFactory* ast_value_factory) {
496 DCHECK(is_function_scope());
497 DCHECK(!is_arrow_scope());
498
499 // Check if there's lexically declared variable named arguments to avoid
500 // redeclaration. See ES#sec-functiondeclarationinstantiation, step 20.
501 Variable* arg_variable = LookupLocal(ast_value_factory->arguments_string());
502 if (arg_variable != nullptr && IsLexicalVariableMode(arg_variable->mode())) {
503 return;
504 }
505
506 // Declare 'arguments' variable which exists in all non arrow functions.
507 // Note that it might never be accessed, in which case it won't be
508 // allocated during variable allocation.
509 if (arg_variable == nullptr) {
510 arguments_ = Declare(zone(), this, ast_value_factory->arguments_string(),
511 VAR, Variable::ARGUMENTS, kCreatedInitialized);
512 } else {
513 arguments_ = arg_variable;
514 }
515 }
516
517 void DeclarationScope::DeclareDefaultFunctionVariables( 495 void DeclarationScope::DeclareDefaultFunctionVariables(
518 AstValueFactory* ast_value_factory) { 496 AstValueFactory* ast_value_factory) {
519 DCHECK(is_function_scope()); 497 DCHECK(is_function_scope());
520 DCHECK(!is_arrow_scope()); 498 DCHECK(!is_arrow_scope());
499 // Declare 'arguments' variable which exists in all non arrow functions.
500 // Note that it might never be accessed, in which case it won't be
501 // allocated during variable allocation.
502 arguments_ = Declare(zone(), this, ast_value_factory->arguments_string(), VAR,
503 Variable::ARGUMENTS, kCreatedInitialized);
521 504
522 new_target_ = Declare(zone(), this, ast_value_factory->new_target_string(), 505 new_target_ = Declare(zone(), this, ast_value_factory->new_target_string(),
523 CONST, Variable::NORMAL, kCreatedInitialized); 506 CONST, Variable::NORMAL, kCreatedInitialized);
524 507
525 if (IsConciseMethod(function_kind_) || IsClassConstructor(function_kind_) || 508 if (IsConciseMethod(function_kind_) || IsClassConstructor(function_kind_) ||
526 IsAccessorFunction(function_kind_)) { 509 IsAccessorFunction(function_kind_)) {
527 this_function_ = 510 this_function_ =
528 Declare(zone(), this, ast_value_factory->this_function_string(), CONST, 511 Declare(zone(), this, ast_value_factory->this_function_string(), CONST,
529 Variable::NORMAL, kCreatedInitialized); 512 Variable::NORMAL, kCreatedInitialized);
530 } 513 }
(...skipping 987 matching lines...) Expand 10 before | Expand all | Expand 10 after
1518 1501
1519 void Scope::AllocateHeapSlot(Variable* var) { 1502 void Scope::AllocateHeapSlot(Variable* var) {
1520 var->AllocateTo(VariableLocation::CONTEXT, num_heap_slots_++); 1503 var->AllocateTo(VariableLocation::CONTEXT, num_heap_slots_++);
1521 } 1504 }
1522 1505
1523 void DeclarationScope::AllocateParameterLocals() { 1506 void DeclarationScope::AllocateParameterLocals() {
1524 DCHECK(is_function_scope()); 1507 DCHECK(is_function_scope());
1525 1508
1526 bool uses_sloppy_arguments = false; 1509 bool uses_sloppy_arguments = false;
1527 1510
1511 // Functions have 'arguments' declared implicitly in all non arrow functions.
1528 if (arguments_ != nullptr) { 1512 if (arguments_ != nullptr) {
1529 DCHECK(!is_arrow_scope());
1530 // 'arguments' is used. Unless there is also a parameter called 1513 // 'arguments' is used. Unless there is also a parameter called
1531 // 'arguments', we must be conservative and allocate all parameters to 1514 // 'arguments', we must be conservative and allocate all parameters to
1532 // the context assuming they will be captured by the arguments object. 1515 // the context assuming they will be captured by the arguments object.
1533 // If we have a parameter named 'arguments', a (new) value is always 1516 // If we have a parameter named 'arguments', a (new) value is always
1534 // assigned to it via the function invocation. Then 'arguments' denotes 1517 // assigned to it via the function invocation. Then 'arguments' denotes
1535 // that specific parameter value and cannot be used to access the 1518 // that specific parameter value and cannot be used to access the
1536 // parameters, which is why we don't need to allocate an arguments 1519 // parameters, which is why we don't need to allocate an arguments
1537 // object in that case. 1520 // object in that case.
1538 if (MustAllocate(arguments_) && !has_arguments_parameter_) { 1521 if (MustAllocate(arguments_) && !has_arguments_parameter_) {
1539 // In strict mode 'arguments' does not alias formal parameters. 1522 // In strict mode 'arguments' does not alias formal parameters.
1540 // Therefore in strict mode we allocate parameters as if 'arguments' 1523 // Therefore in strict mode we allocate parameters as if 'arguments'
1541 // were not used. 1524 // were not used.
1542 // If the parameter list is not simple, arguments isn't sloppy either. 1525 // If the parameter list is not simple, arguments isn't sloppy either.
1543 uses_sloppy_arguments = 1526 uses_sloppy_arguments =
1544 is_sloppy(language_mode()) && has_simple_parameters(); 1527 is_sloppy(language_mode()) && has_simple_parameters();
1545 } else { 1528 } else {
1546 // 'arguments' is unused. Tell the code generator that it does not need to 1529 // 'arguments' is unused. Tell the code generator that it does not need to
1547 // allocate the arguments object by nulling out arguments_. 1530 // allocate the arguments object by nulling out arguments_.
1548 arguments_ = nullptr; 1531 arguments_ = nullptr;
1549 } 1532 }
1533
1534 } else {
1535 DCHECK(is_arrow_scope());
1550 } 1536 }
1551 1537
1552 // The same parameter may occur multiple times in the parameters_ list. 1538 // The same parameter may occur multiple times in the parameters_ list.
1553 // If it does, and if it is not copied into the context object, it must 1539 // If it does, and if it is not copied into the context object, it must
1554 // receive the highest parameter index for that parameter; thus iteration 1540 // receive the highest parameter index for that parameter; thus iteration
1555 // order is relevant! 1541 // order is relevant!
1556 for (int i = num_parameters() - 1; i >= 0; --i) { 1542 for (int i = num_parameters() - 1; i >= 0; --i) {
1557 Variable* var = params_[i]; 1543 Variable* var = params_[i];
1558 DCHECK(!has_rest_ || var != rest_parameter()); 1544 DCHECK(!has_rest_ || var != rest_parameter());
1559 DCHECK_EQ(this, var->scope()); 1545 DCHECK_EQ(this, var->scope());
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
1710 Variable* function = 1696 Variable* function =
1711 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; 1697 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr;
1712 bool is_function_var_in_context = 1698 bool is_function_var_in_context =
1713 function != nullptr && function->IsContextSlot(); 1699 function != nullptr && function->IsContextSlot();
1714 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - 1700 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS -
1715 (is_function_var_in_context ? 1 : 0); 1701 (is_function_var_in_context ? 1 : 0);
1716 } 1702 }
1717 1703
1718 } // namespace internal 1704 } // namespace internal
1719 } // namespace v8 1705 } // namespace v8
OLDNEW
« no previous file with comments | « src/ast/scopes.h ('k') | src/parsing/parser.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698