OLD | NEW |
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/ast/ast.h" | 10 #include "src/ast/ast.h" |
(...skipping 582 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
593 IsAccessorFunction(function_kind_)) { | 593 IsAccessorFunction(function_kind_)) { |
594 this_function_ = | 594 this_function_ = |
595 Declare(zone(), this, ast_value_factory->this_function_string(), CONST, | 595 Declare(zone(), this, ast_value_factory->this_function_string(), CONST, |
596 NORMAL_VARIABLE, kCreatedInitialized); | 596 NORMAL_VARIABLE, kCreatedInitialized); |
597 } | 597 } |
598 } | 598 } |
599 | 599 |
600 Variable* DeclarationScope::DeclareFunctionVar(const AstRawString* name) { | 600 Variable* DeclarationScope::DeclareFunctionVar(const AstRawString* name) { |
601 DCHECK(is_function_scope()); | 601 DCHECK(is_function_scope()); |
602 DCHECK_NULL(function_); | 602 DCHECK_NULL(function_); |
| 603 DCHECK_NULL(variables_.Lookup(name)); |
603 VariableKind kind = is_sloppy(language_mode()) ? SLOPPY_FUNCTION_NAME_VARIABLE | 604 VariableKind kind = is_sloppy(language_mode()) ? SLOPPY_FUNCTION_NAME_VARIABLE |
604 : NORMAL_VARIABLE; | 605 : NORMAL_VARIABLE; |
605 function_ = | 606 function_ = |
606 new (zone()) Variable(this, name, CONST, kind, kCreatedInitialized); | 607 new (zone()) Variable(this, name, CONST, kind, kCreatedInitialized); |
| 608 if (calls_sloppy_eval()) { |
| 609 NonLocal(name, DYNAMIC); |
| 610 } else { |
| 611 variables_.Add(zone(), function_); |
| 612 } |
607 return function_; | 613 return function_; |
608 } | 614 } |
609 | 615 |
610 Scope* Scope::FinalizeBlockScope() { | 616 Scope* Scope::FinalizeBlockScope() { |
611 DCHECK(is_block_scope()); | 617 DCHECK(is_block_scope()); |
612 | 618 |
613 if (variables_.occupancy() > 0 || | 619 if (variables_.occupancy() > 0 || |
614 (is_declaration_scope() && calls_sloppy_eval())) { | 620 (is_declaration_scope() && calls_sloppy_eval())) { |
615 return this; | 621 return this; |
616 } | 622 } |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
735 MaybeAssignedFlag maybe_assigned_flag; | 741 MaybeAssignedFlag maybe_assigned_flag; |
736 | 742 |
737 VariableLocation location = VariableLocation::CONTEXT; | 743 VariableLocation location = VariableLocation::CONTEXT; |
738 int index = ScopeInfo::ContextSlotIndex(scope_info_, name_handle, &mode, | 744 int index = ScopeInfo::ContextSlotIndex(scope_info_, name_handle, &mode, |
739 &init_flag, &maybe_assigned_flag); | 745 &init_flag, &maybe_assigned_flag); |
740 if (index < 0 && scope_type() == MODULE_SCOPE) { | 746 if (index < 0 && scope_type() == MODULE_SCOPE) { |
741 location = VariableLocation::MODULE; | 747 location = VariableLocation::MODULE; |
742 index = scope_info_->ModuleIndex(name_handle, &mode, &init_flag, | 748 index = scope_info_->ModuleIndex(name_handle, &mode, &init_flag, |
743 &maybe_assigned_flag); | 749 &maybe_assigned_flag); |
744 } | 750 } |
745 if (index < 0) return nullptr; // Nowhere found. | 751 |
| 752 if (index < 0) { |
| 753 index = scope_info_->FunctionContextSlotIndex(*name_handle); |
| 754 if (index < 0) return nullptr; // Nowhere found. |
| 755 Variable* var = AsDeclarationScope()->DeclareFunctionVar(name); |
| 756 DCHECK_EQ(CONST, var->mode()); |
| 757 var->AllocateTo(VariableLocation::CONTEXT, index); |
| 758 return variables_.Lookup(name); |
| 759 } |
746 | 760 |
747 VariableKind kind = NORMAL_VARIABLE; | 761 VariableKind kind = NORMAL_VARIABLE; |
748 if (location == VariableLocation::CONTEXT && | 762 if (location == VariableLocation::CONTEXT && |
749 index == scope_info_->ReceiverContextSlotIndex()) { | 763 index == scope_info_->ReceiverContextSlotIndex()) { |
750 kind = THIS_VARIABLE; | 764 kind = THIS_VARIABLE; |
751 } | 765 } |
752 // TODO(marja, rossberg): Correctly declare FUNCTION, CLASS, NEW_TARGET, and | 766 // TODO(marja, rossberg): Correctly declare FUNCTION, CLASS, NEW_TARGET, and |
753 // ARGUMENTS bindings as their corresponding VariableKind. | 767 // ARGUMENTS bindings as their corresponding VariableKind. |
754 | 768 |
755 Variable* var = variables_.Declare(zone(), this, name, mode, kind, init_flag, | 769 Variable* var = variables_.Declare(zone(), this, name, mode, kind, init_flag, |
756 maybe_assigned_flag); | 770 maybe_assigned_flag); |
757 var->AllocateTo(location, index); | 771 var->AllocateTo(location, index); |
758 return var; | 772 return var; |
759 } | 773 } |
760 | 774 |
761 Variable* DeclarationScope::LookupFunctionVar(const AstRawString* name) { | |
762 if (function_ != nullptr && function_->raw_name() == name) { | |
763 return function_; | |
764 } else if (!scope_info_.is_null()) { | |
765 // If we are backed by a scope info, try to lookup the variable there. | |
766 int index = scope_info_->FunctionContextSlotIndex(*(name->string())); | |
767 if (index < 0) return nullptr; | |
768 Variable* var = DeclareFunctionVar(name); | |
769 var->AllocateTo(VariableLocation::CONTEXT, index); | |
770 return var; | |
771 } else { | |
772 return nullptr; | |
773 } | |
774 } | |
775 | |
776 | |
777 Variable* Scope::Lookup(const AstRawString* name) { | 775 Variable* Scope::Lookup(const AstRawString* name) { |
778 for (Scope* scope = this; | 776 for (Scope* scope = this; |
779 scope != NULL; | 777 scope != NULL; |
780 scope = scope->outer_scope()) { | 778 scope = scope->outer_scope()) { |
781 Variable* var = scope->LookupLocal(name); | 779 Variable* var = scope->LookupLocal(name); |
782 if (var != NULL) return var; | 780 if (var != NULL) return var; |
783 } | 781 } |
784 return NULL; | 782 return NULL; |
785 } | 783 } |
786 | 784 |
(...skipping 626 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1413 } | 1411 } |
1414 | 1412 |
1415 // Try to find the variable in this scope. | 1413 // Try to find the variable in this scope. |
1416 Variable* var = LookupLocal(proxy->raw_name()); | 1414 Variable* var = LookupLocal(proxy->raw_name()); |
1417 | 1415 |
1418 // We found a variable and we are done. (Even if there is an 'eval' in this | 1416 // We found a variable and we are done. (Even if there is an 'eval' in this |
1419 // scope which introduces the same variable again, the resulting variable | 1417 // scope which introduces the same variable again, the resulting variable |
1420 // remains the same.) | 1418 // remains the same.) |
1421 if (var != nullptr) return var; | 1419 if (var != nullptr) return var; |
1422 | 1420 |
1423 // We did not find a variable locally. Check against the function variable, if | |
1424 // any. | |
1425 if (is_function_scope()) { | |
1426 var = AsDeclarationScope()->LookupFunctionVar(proxy->raw_name()); | |
1427 if (var != nullptr) { | |
1428 if (calls_sloppy_eval()) return NonLocal(proxy->raw_name(), DYNAMIC); | |
1429 return var; | |
1430 } | |
1431 } | |
1432 | |
1433 if (outer_scope_ == outer_scope_end) { | 1421 if (outer_scope_ == outer_scope_end) { |
1434 if (!declare_free) return nullptr; | 1422 if (!declare_free) return nullptr; |
1435 DCHECK(is_script_scope()); | 1423 DCHECK(is_script_scope()); |
1436 // No binding has been found. Declare a variable on the global object. | 1424 // No binding has been found. Declare a variable on the global object. |
1437 return AsDeclarationScope()->DeclareDynamicGlobal(proxy->raw_name(), | 1425 return AsDeclarationScope()->DeclareDynamicGlobal(proxy->raw_name(), |
1438 NORMAL_VARIABLE); | 1426 NORMAL_VARIABLE); |
1439 } | 1427 } |
1440 | 1428 |
1441 DCHECK(!is_script_scope()); | 1429 DCHECK(!is_script_scope()); |
1442 | 1430 |
(...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1816 Variable* function = | 1804 Variable* function = |
1817 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; | 1805 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; |
1818 bool is_function_var_in_context = | 1806 bool is_function_var_in_context = |
1819 function != nullptr && function->IsContextSlot(); | 1807 function != nullptr && function->IsContextSlot(); |
1820 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - | 1808 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - |
1821 (is_function_var_in_context ? 1 : 0); | 1809 (is_function_var_in_context ? 1 : 0); |
1822 } | 1810 } |
1823 | 1811 |
1824 } // namespace internal | 1812 } // namespace internal |
1825 } // namespace v8 | 1813 } // namespace v8 |
OLD | NEW |