| 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 761 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 772 | 772 |
| 773 Variable* Scope::LookupInScopeInfo(const AstRawString* name) { | 773 Variable* Scope::LookupInScopeInfo(const AstRawString* name) { |
| 774 Handle<String> name_handle = name->string(); | 774 Handle<String> name_handle = name->string(); |
| 775 // The Scope is backed up by ScopeInfo. This means it cannot operate in a | 775 // The Scope is backed up by ScopeInfo. This means it cannot operate in a |
| 776 // heap-independent mode, and all strings must be internalized immediately. So | 776 // heap-independent mode, and all strings must be internalized immediately. So |
| 777 // it's ok to get the Handle<String> here. | 777 // it's ok to get the Handle<String> here. |
| 778 // If we have a serialized scope info, we might find the variable there. | 778 // If we have a serialized scope info, we might find the variable there. |
| 779 // There should be no local slot with the given name. | 779 // There should be no local slot with the given name. |
| 780 DCHECK_LT(scope_info_->StackSlotIndex(*name_handle), 0); | 780 DCHECK_LT(scope_info_->StackSlotIndex(*name_handle), 0); |
| 781 | 781 |
| 782 bool found = false; |
| 783 |
| 784 VariableLocation location; |
| 785 int index; |
| 782 VariableMode mode; | 786 VariableMode mode; |
| 783 InitializationFlag init_flag; | 787 InitializationFlag init_flag; |
| 784 MaybeAssignedFlag maybe_assigned_flag; | 788 MaybeAssignedFlag maybe_assigned_flag; |
| 785 | 789 |
| 786 VariableLocation location = VariableLocation::CONTEXT; | 790 { |
| 787 int index = ScopeInfo::ContextSlotIndex(scope_info_, name_handle, &mode, | 791 location = VariableLocation::CONTEXT; |
| 788 &init_flag, &maybe_assigned_flag); | 792 index = ScopeInfo::ContextSlotIndex(scope_info_, name_handle, &mode, |
| 789 if (index < 0 && scope_type() == MODULE_SCOPE) { | 793 &init_flag, &maybe_assigned_flag); |
| 794 found = index >= 0; |
| 795 } |
| 796 |
| 797 if (!found && scope_type() == MODULE_SCOPE) { |
| 790 location = VariableLocation::MODULE; | 798 location = VariableLocation::MODULE; |
| 791 index = scope_info_->ModuleIndex(name_handle, &mode, &init_flag, | 799 index = scope_info_->ModuleIndex(name_handle, &mode, &init_flag, |
| 792 &maybe_assigned_flag); | 800 &maybe_assigned_flag); |
| 801 found = index != 0; |
| 793 } | 802 } |
| 794 | 803 |
| 795 if (index < 0) { | 804 if (!found) { |
| 796 index = scope_info_->FunctionContextSlotIndex(*name_handle); | 805 index = scope_info_->FunctionContextSlotIndex(*name_handle); |
| 797 if (index < 0) return nullptr; // Nowhere found. | 806 if (index < 0) return nullptr; // Nowhere found. |
| 798 Variable* var = AsDeclarationScope()->DeclareFunctionVar(name); | 807 Variable* var = AsDeclarationScope()->DeclareFunctionVar(name); |
| 799 DCHECK_EQ(CONST, var->mode()); | 808 DCHECK_EQ(CONST, var->mode()); |
| 800 var->AllocateTo(VariableLocation::CONTEXT, index); | 809 var->AllocateTo(VariableLocation::CONTEXT, index); |
| 801 return variables_.Lookup(name); | 810 return variables_.Lookup(name); |
| 802 } | 811 } |
| 803 | 812 |
| 804 VariableKind kind = NORMAL_VARIABLE; | 813 VariableKind kind = NORMAL_VARIABLE; |
| 805 if (location == VariableLocation::CONTEXT && | 814 if (location == VariableLocation::CONTEXT && |
| (...skipping 1123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1929 } | 1938 } |
| 1930 | 1939 |
| 1931 if (this_function_ != nullptr && !MustAllocate(this_function_)) { | 1940 if (this_function_ != nullptr && !MustAllocate(this_function_)) { |
| 1932 this_function_ = nullptr; | 1941 this_function_ = nullptr; |
| 1933 } | 1942 } |
| 1934 } | 1943 } |
| 1935 | 1944 |
| 1936 void ModuleScope::AllocateModuleVariables() { | 1945 void ModuleScope::AllocateModuleVariables() { |
| 1937 for (const auto& it : module()->regular_imports()) { | 1946 for (const auto& it : module()->regular_imports()) { |
| 1938 Variable* var = LookupLocal(it.first); | 1947 Variable* var = LookupLocal(it.first); |
| 1939 var->AllocateTo(VariableLocation::MODULE, Variable::kModuleImportIndex); | 1948 var->AllocateTo(VariableLocation::MODULE, it.second->cell_index); |
| 1949 DCHECK(!var->IsExport()); |
| 1940 } | 1950 } |
| 1941 | 1951 |
| 1942 for (const auto& it : module()->regular_exports()) { | 1952 for (const auto& it : module()->regular_exports()) { |
| 1943 Variable* var = LookupLocal(it.first); | 1953 Variable* var = LookupLocal(it.first); |
| 1944 var->AllocateTo(VariableLocation::MODULE, Variable::kModuleExportIndex); | 1954 var->AllocateTo(VariableLocation::MODULE, it.second->cell_index); |
| 1955 DCHECK(var->IsExport()); |
| 1945 } | 1956 } |
| 1946 } | 1957 } |
| 1947 | 1958 |
| 1948 void Scope::AllocateVariablesRecursively() { | 1959 void Scope::AllocateVariablesRecursively() { |
| 1949 DCHECK(!already_resolved_); | 1960 DCHECK(!already_resolved_); |
| 1950 DCHECK_EQ(0, num_stack_slots_); | 1961 DCHECK_EQ(0, num_stack_slots_); |
| 1951 // Don't allocate variables of preparsed scopes. | 1962 // Don't allocate variables of preparsed scopes. |
| 1952 if (is_declaration_scope() && AsDeclarationScope()->is_lazily_parsed()) { | 1963 if (is_declaration_scope() && AsDeclarationScope()->is_lazily_parsed()) { |
| 1953 return; | 1964 return; |
| 1954 } | 1965 } |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2036 Variable* function = | 2047 Variable* function = |
| 2037 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; | 2048 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; |
| 2038 bool is_function_var_in_context = | 2049 bool is_function_var_in_context = |
| 2039 function != nullptr && function->IsContextSlot(); | 2050 function != nullptr && function->IsContextSlot(); |
| 2040 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - | 2051 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - |
| 2041 (is_function_var_in_context ? 1 : 0); | 2052 (is_function_var_in_context ? 1 : 0); |
| 2042 } | 2053 } |
| 2043 | 2054 |
| 2044 } // namespace internal | 2055 } // namespace internal |
| 2045 } // namespace v8 | 2056 } // namespace v8 |
| OLD | NEW |