| 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/bootstrapper.h" | 10 #include "src/bootstrapper.h" |
| (...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 | 356 |
| 357 Variable* result = variables_.Declare(zone(), this, name, mode, kind, | 357 Variable* result = variables_.Declare(zone(), this, name, mode, kind, |
| 358 init_flag, maybe_assigned_flag); | 358 init_flag, maybe_assigned_flag); |
| 359 result->AllocateTo(location, index); | 359 result->AllocateTo(location, index); |
| 360 } | 360 } |
| 361 | 361 |
| 362 // Internalize function proxy for this scope. | 362 // Internalize function proxy for this scope. |
| 363 if (scope_info_->HasFunctionName()) { | 363 if (scope_info_->HasFunctionName()) { |
| 364 Handle<String> name_handle(scope_info_->FunctionName(), isolate); | 364 Handle<String> name_handle(scope_info_->FunctionName(), isolate); |
| 365 const AstRawString* name = ast_value_factory->GetString(name_handle); | 365 const AstRawString* name = ast_value_factory->GetString(name_handle); |
| 366 VariableMode mode; | 366 int index = scope_info_->FunctionContextSlotIndex(*name_handle); |
| 367 int index = scope_info_->FunctionContextSlotIndex(*name_handle, &mode); | |
| 368 if (index >= 0) { | 367 if (index >= 0) { |
| 369 Variable* result = AsDeclarationScope()->DeclareFunctionVar(name); | 368 Variable* result = AsDeclarationScope()->DeclareFunctionVar(name); |
| 370 DCHECK_EQ(mode, result->mode()); | |
| 371 result->AllocateTo(VariableLocation::CONTEXT, index); | 369 result->AllocateTo(VariableLocation::CONTEXT, index); |
| 372 } | 370 } |
| 373 } | 371 } |
| 374 | 372 |
| 375 scope_info_ = Handle<ScopeInfo>::null(); | 373 scope_info_ = Handle<ScopeInfo>::null(); |
| 376 } | 374 } |
| 377 | 375 |
| 378 DeclarationScope* Scope::AsDeclarationScope() { | 376 DeclarationScope* Scope::AsDeclarationScope() { |
| 379 DCHECK(is_declaration_scope()); | 377 DCHECK(is_declaration_scope()); |
| 380 return static_cast<DeclarationScope*>(this); | 378 return static_cast<DeclarationScope*>(this); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 468 IsAccessorFunction(function_kind_)) { | 466 IsAccessorFunction(function_kind_)) { |
| 469 this_function_ = | 467 this_function_ = |
| 470 Declare(zone(), this, ast_value_factory->this_function_string(), CONST, | 468 Declare(zone(), this, ast_value_factory->this_function_string(), CONST, |
| 471 Variable::NORMAL, kCreatedInitialized); | 469 Variable::NORMAL, kCreatedInitialized); |
| 472 } | 470 } |
| 473 } | 471 } |
| 474 | 472 |
| 475 Variable* DeclarationScope::DeclareFunctionVar(const AstRawString* name) { | 473 Variable* DeclarationScope::DeclareFunctionVar(const AstRawString* name) { |
| 476 DCHECK(is_function_scope()); | 474 DCHECK(is_function_scope()); |
| 477 DCHECK_NULL(function_); | 475 DCHECK_NULL(function_); |
| 478 VariableMode mode = is_strict(language_mode()) ? CONST : CONST_LEGACY; | 476 Variable::Kind kind = is_sloppy(language_mode()) |
| 479 function_ = new (zone()) | 477 ? Variable::SLOPPY_FUNCTION_NAME |
| 480 Variable(this, name, mode, Variable::NORMAL, kCreatedInitialized); | 478 : Variable::NORMAL; |
| 479 function_ = |
| 480 new (zone()) Variable(this, name, CONST, kind, kCreatedInitialized); |
| 481 return function_; | 481 return function_; |
| 482 } | 482 } |
| 483 | 483 |
| 484 Scope* Scope::FinalizeBlockScope() { | 484 Scope* Scope::FinalizeBlockScope() { |
| 485 DCHECK(is_block_scope()); | 485 DCHECK(is_block_scope()); |
| 486 | 486 |
| 487 if (variables_.occupancy() > 0 || | 487 if (variables_.occupancy() > 0 || |
| 488 (is_declaration_scope() && calls_sloppy_eval())) { | 488 (is_declaration_scope() && calls_sloppy_eval())) { |
| 489 return this; | 489 return this; |
| 490 } | 490 } |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 631 maybe_assigned_flag); | 631 maybe_assigned_flag); |
| 632 var->AllocateTo(location, index); | 632 var->AllocateTo(location, index); |
| 633 return var; | 633 return var; |
| 634 } | 634 } |
| 635 | 635 |
| 636 Variable* DeclarationScope::LookupFunctionVar(const AstRawString* name) { | 636 Variable* DeclarationScope::LookupFunctionVar(const AstRawString* name) { |
| 637 if (function_ != nullptr && function_->raw_name() == name) { | 637 if (function_ != nullptr && function_->raw_name() == name) { |
| 638 return function_; | 638 return function_; |
| 639 } else if (!scope_info_.is_null()) { | 639 } else if (!scope_info_.is_null()) { |
| 640 // If we are backed by a scope info, try to lookup the variable there. | 640 // If we are backed by a scope info, try to lookup the variable there. |
| 641 VariableMode mode; | 641 int index = scope_info_->FunctionContextSlotIndex(*(name->string())); |
| 642 int index = scope_info_->FunctionContextSlotIndex(*(name->string()), &mode); | |
| 643 if (index < 0) return nullptr; | 642 if (index < 0) return nullptr; |
| 644 Variable* var = DeclareFunctionVar(name); | 643 Variable* var = DeclareFunctionVar(name); |
| 645 DCHECK_EQ(mode, var->mode()); | |
| 646 var->AllocateTo(VariableLocation::CONTEXT, index); | 644 var->AllocateTo(VariableLocation::CONTEXT, index); |
| 647 return var; | 645 return var; |
| 648 } else { | 646 } else { |
| 649 return nullptr; | 647 return nullptr; |
| 650 } | 648 } |
| 651 } | 649 } |
| 652 | 650 |
| 653 | 651 |
| 654 Variable* Scope::Lookup(const AstRawString* name) { | 652 Variable* Scope::Lookup(const AstRawString* name) { |
| 655 for (Scope* scope = this; | 653 for (Scope* scope = this; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 697 // allocated via NewTemporary(). | 695 // allocated via NewTemporary(). |
| 698 DCHECK(IsDeclaredVariableMode(mode)); | 696 DCHECK(IsDeclaredVariableMode(mode)); |
| 699 return Declare(zone(), this, name, mode, kind, init_flag, | 697 return Declare(zone(), this, name, mode, kind, init_flag, |
| 700 maybe_assigned_flag); | 698 maybe_assigned_flag); |
| 701 } | 699 } |
| 702 | 700 |
| 703 Variable* Scope::DeclareVariable( | 701 Variable* Scope::DeclareVariable( |
| 704 Declaration* declaration, VariableMode mode, InitializationFlag init, | 702 Declaration* declaration, VariableMode mode, InitializationFlag init, |
| 705 bool allow_harmony_restrictive_generators, | 703 bool allow_harmony_restrictive_generators, |
| 706 bool* sloppy_mode_block_scope_function_redefinition, bool* ok) { | 704 bool* sloppy_mode_block_scope_function_redefinition, bool* ok) { |
| 707 DCHECK(IsDeclaredVariableMode(mode) && mode != CONST_LEGACY); | 705 DCHECK(IsDeclaredVariableMode(mode)); |
| 708 DCHECK(!already_resolved_); | 706 DCHECK(!already_resolved_); |
| 709 | 707 |
| 710 if (mode == VAR && !is_declaration_scope()) { | 708 if (mode == VAR && !is_declaration_scope()) { |
| 711 return GetDeclarationScope()->DeclareVariable( | 709 return GetDeclarationScope()->DeclareVariable( |
| 712 declaration, mode, init, allow_harmony_restrictive_generators, | 710 declaration, mode, init, allow_harmony_restrictive_generators, |
| 713 sloppy_mode_block_scope_function_redefinition, ok); | 711 sloppy_mode_block_scope_function_redefinition, ok); |
| 714 } | 712 } |
| 715 DCHECK(!is_catch_scope()); | 713 DCHECK(!is_catch_scope()); |
| 716 DCHECK(!is_with_scope()); | 714 DCHECK(!is_with_scope()); |
| 717 DCHECK(is_declaration_scope() || | 715 DCHECK(is_declaration_scope() || |
| (...skipping 950 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1668 Variable* function = | 1666 Variable* function = |
| 1669 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; | 1667 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; |
| 1670 bool is_function_var_in_context = | 1668 bool is_function_var_in_context = |
| 1671 function != nullptr && function->IsContextSlot(); | 1669 function != nullptr && function->IsContextSlot(); |
| 1672 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - | 1670 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - |
| 1673 (is_function_var_in_context ? 1 : 0); | 1671 (is_function_var_in_context ? 1 : 0); |
| 1674 } | 1672 } |
| 1675 | 1673 |
| 1676 } // namespace internal | 1674 } // namespace internal |
| 1677 } // namespace v8 | 1675 } // namespace v8 |
| OLD | NEW |