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

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

Issue 2290753003: Allow lexically declared "arguments" in function scope in sloppy mode. (Closed)
Patch Set: update 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
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 418 matching lines...) Expand 10 before | Expand all | Expand 10 after
429 DCHECK(has_this_declaration()); 429 DCHECK(has_this_declaration());
430 430
431 bool subclass_constructor = IsSubclassConstructor(function_kind_); 431 bool subclass_constructor = IsSubclassConstructor(function_kind_);
432 Variable* var = Declare( 432 Variable* var = Declare(
433 zone(), this, ast_value_factory->this_string(), 433 zone(), this, ast_value_factory->this_string(),
434 subclass_constructor ? CONST : VAR, Variable::THIS, 434 subclass_constructor ? CONST : VAR, Variable::THIS,
435 subclass_constructor ? kNeedsInitialization : kCreatedInitialized); 435 subclass_constructor ? kNeedsInitialization : kCreatedInitialized);
436 receiver_ = var; 436 receiver_ = var;
437 } 437 }
438 438
439 void DeclarationScope::DeclareArguments(AstValueFactory* ast_value_factory) {
440 DCHECK(is_function_scope());
441 DCHECK(!is_arrow_scope());
442
443 // Check if there's lexically declared variable named arguments to avoid
444 // redeclaration.
445 // See https://tc39.github.io/ecma262/#sec-functiondeclarationinstantiation,
adamk 2016/09/01 17:36:26 The style we've mostly been going for here has bee
lpy 2016/09/01 20:00:55 Done.
446 // step 20.
447 Variable* arg_variable = LookupLocal(ast_value_factory->arguments_string());
448 if (arg_variable != nullptr && IsLexicalVariableMode(arg_variable->mode())) {
449 return;
450 }
451
452 // Declare 'arguments' variable which exists in all non arrow functions.
453 // Note that it might never be accessed, in which case it won't be
454 // allocated during variable allocation.
455 if (arg_variable == nullptr) {
456 arguments_ = Declare(zone(), this, ast_value_factory->arguments_string(),
457 VAR, Variable::ARGUMENTS, kCreatedInitialized);
458 } else {
459 arguments_ = arg_variable;
460 }
461 }
462
439 void DeclarationScope::DeclareDefaultFunctionVariables( 463 void DeclarationScope::DeclareDefaultFunctionVariables(
440 AstValueFactory* ast_value_factory) { 464 AstValueFactory* ast_value_factory) {
441 DCHECK(is_function_scope()); 465 DCHECK(is_function_scope());
442 DCHECK(!is_arrow_scope()); 466 DCHECK(!is_arrow_scope());
443 // Declare 'arguments' variable which exists in all non arrow functions.
444 // Note that it might never be accessed, in which case it won't be
445 // allocated during variable allocation.
446 arguments_ = Declare(zone(), this, ast_value_factory->arguments_string(), VAR,
447 Variable::ARGUMENTS, kCreatedInitialized);
448 467
449 new_target_ = Declare(zone(), this, ast_value_factory->new_target_string(), 468 new_target_ = Declare(zone(), this, ast_value_factory->new_target_string(),
450 CONST, Variable::NORMAL, kCreatedInitialized); 469 CONST, Variable::NORMAL, kCreatedInitialized);
451 470
452 if (IsConciseMethod(function_kind_) || IsClassConstructor(function_kind_) || 471 if (IsConciseMethod(function_kind_) || IsClassConstructor(function_kind_) ||
453 IsAccessorFunction(function_kind_)) { 472 IsAccessorFunction(function_kind_)) {
454 this_function_ = 473 this_function_ =
455 Declare(zone(), this, ast_value_factory->this_function_string(), CONST, 474 Declare(zone(), this, ast_value_factory->this_function_string(), CONST,
456 Variable::NORMAL, kCreatedInitialized); 475 Variable::NORMAL, kCreatedInitialized);
457 } 476 }
(...skipping 1014 matching lines...) Expand 10 before | Expand all | Expand 10 after
1472 1491
1473 void Scope::AllocateHeapSlot(Variable* var) { 1492 void Scope::AllocateHeapSlot(Variable* var) {
1474 var->AllocateTo(VariableLocation::CONTEXT, num_heap_slots_++); 1493 var->AllocateTo(VariableLocation::CONTEXT, num_heap_slots_++);
1475 } 1494 }
1476 1495
1477 void DeclarationScope::AllocateParameterLocals() { 1496 void DeclarationScope::AllocateParameterLocals() {
1478 DCHECK(is_function_scope()); 1497 DCHECK(is_function_scope());
1479 1498
1480 bool uses_sloppy_arguments = false; 1499 bool uses_sloppy_arguments = false;
1481 1500
1482 // Functions have 'arguments' declared implicitly in all non arrow functions. 1501 // Functions have 'arguments' declared implicitly in all non arrow functions.
adamk 2016/09/01 17:36:26 This comment is now wrong, I think it should just
lpy 2016/09/01 20:00:55 Done.
1483 if (arguments_ != nullptr) { 1502 if (arguments_ != nullptr) {
1484 // 'arguments' is used. Unless there is also a parameter called 1503 // 'arguments' is used. Unless there is also a parameter called
adamk 2016/09/01 17:36:26 Can you add a DCHECK(!is_arrow_scope()) right abov
lpy 2016/09/01 20:00:55 Done.
1485 // 'arguments', we must be conservative and allocate all parameters to 1504 // 'arguments', we must be conservative and allocate all parameters to
1486 // the context assuming they will be captured by the arguments object. 1505 // the context assuming they will be captured by the arguments object.
1487 // If we have a parameter named 'arguments', a (new) value is always 1506 // If we have a parameter named 'arguments', a (new) value is always
1488 // assigned to it via the function invocation. Then 'arguments' denotes 1507 // assigned to it via the function invocation. Then 'arguments' denotes
1489 // that specific parameter value and cannot be used to access the 1508 // that specific parameter value and cannot be used to access the
1490 // parameters, which is why we don't need to allocate an arguments 1509 // parameters, which is why we don't need to allocate an arguments
1491 // object in that case. 1510 // object in that case.
1492 if (MustAllocate(arguments_) && !has_arguments_parameter_) { 1511 if (MustAllocate(arguments_) && !has_arguments_parameter_) {
1493 // In strict mode 'arguments' does not alias formal parameters. 1512 // In strict mode 'arguments' does not alias formal parameters.
1494 // Therefore in strict mode we allocate parameters as if 'arguments' 1513 // Therefore in strict mode we allocate parameters as if 'arguments'
1495 // were not used. 1514 // were not used.
1496 // If the parameter list is not simple, arguments isn't sloppy either. 1515 // If the parameter list is not simple, arguments isn't sloppy either.
1497 uses_sloppy_arguments = 1516 uses_sloppy_arguments =
1498 is_sloppy(language_mode()) && has_simple_parameters(); 1517 is_sloppy(language_mode()) && has_simple_parameters();
1499 } else { 1518 } else {
1500 // 'arguments' is unused. Tell the code generator that it does not need to 1519 // 'arguments' is unused. Tell the code generator that it does not need to
1501 // allocate the arguments object by nulling out arguments_. 1520 // allocate the arguments object by nulling out arguments_.
1502 arguments_ = nullptr; 1521 arguments_ = nullptr;
1503 } 1522 }
1504
1505 } else {
1506 DCHECK(is_arrow_scope());
1507 } 1523 }
1508 1524
1509 // The same parameter may occur multiple times in the parameters_ list. 1525 // The same parameter may occur multiple times in the parameters_ list.
1510 // If it does, and if it is not copied into the context object, it must 1526 // If it does, and if it is not copied into the context object, it must
1511 // receive the highest parameter index for that parameter; thus iteration 1527 // receive the highest parameter index for that parameter; thus iteration
1512 // order is relevant! 1528 // order is relevant!
1513 for (int i = num_parameters() - 1; i >= 0; --i) { 1529 for (int i = num_parameters() - 1; i >= 0; --i) {
1514 Variable* var = params_[i]; 1530 Variable* var = params_[i];
1515 DCHECK(!has_rest_ || var != rest_parameter()); 1531 DCHECK(!has_rest_ || var != rest_parameter());
1516 DCHECK_EQ(this, var->scope()); 1532 DCHECK_EQ(this, var->scope());
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
1656 Variable* function = 1672 Variable* function =
1657 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; 1673 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr;
1658 bool is_function_var_in_context = 1674 bool is_function_var_in_context =
1659 function != nullptr && function->IsContextSlot(); 1675 function != nullptr && function->IsContextSlot();
1660 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - 1676 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS -
1661 (is_function_var_in_context ? 1 : 0); 1677 (is_function_var_in_context ? 1 : 0);
1662 } 1678 }
1663 1679
1664 } // namespace internal 1680 } // namespace internal
1665 } // namespace v8 1681 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698