| 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 460 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 471 break; | 471 break; |
| 472 } | 472 } |
| 473 query_scope = query_scope->outer_scope(); | 473 query_scope = query_scope->outer_scope(); |
| 474 } while (query_scope != outer_scope); | 474 } while (query_scope != outer_scope); |
| 475 | 475 |
| 476 if (!should_hoist) continue; | 476 if (!should_hoist) continue; |
| 477 | 477 |
| 478 // Declare a var-style binding for the function in the outer scope | 478 // Declare a var-style binding for the function in the outer scope |
| 479 if (!var_created) { | 479 if (!var_created) { |
| 480 var_created = true; | 480 var_created = true; |
| 481 VariableProxy* proxy = NewUnresolved(factory, name); | 481 VariableProxy* proxy = factory->NewVariableProxy(name, NORMAL_VARIABLE); |
| 482 Declaration* declaration = | 482 Declaration* declaration = |
| 483 factory->NewVariableDeclaration(proxy, this, kNoSourcePosition); | 483 factory->NewVariableDeclaration(proxy, this, kNoSourcePosition); |
| 484 // Based on the preceding check, it doesn't matter what we pass as | 484 // Based on the preceding check, it doesn't matter what we pass as |
| 485 // allow_harmony_restrictive_generators and | 485 // allow_harmony_restrictive_generators and |
| 486 // sloppy_mode_block_scope_function_redefinition. | 486 // sloppy_mode_block_scope_function_redefinition. |
| 487 bool ok = true; | 487 bool ok = true; |
| 488 DeclareVariable(declaration, VAR, | 488 DeclareVariable(declaration, VAR, |
| 489 Variable::DefaultInitializationFlag(VAR), false, | 489 Variable::DefaultInitializationFlag(VAR), false, |
| 490 nullptr, &ok); | 490 nullptr, &ok); |
| 491 CHECK(ok); // Based on the preceding check, this should not fail | 491 CHECK(ok); // Based on the preceding check, this should not fail |
| (...skipping 991 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1483 Variable* invalidated = var; | 1483 Variable* invalidated = var; |
| 1484 var = NonLocal(proxy->raw_name(), DYNAMIC_LOCAL); | 1484 var = NonLocal(proxy->raw_name(), DYNAMIC_LOCAL); |
| 1485 var->set_local_if_not_shadowed(invalidated); | 1485 var->set_local_if_not_shadowed(invalidated); |
| 1486 } | 1486 } |
| 1487 | 1487 |
| 1488 return var; | 1488 return var; |
| 1489 } | 1489 } |
| 1490 | 1490 |
| 1491 void Scope::ResolveVariable(ParseInfo* info, VariableProxy* proxy) { | 1491 void Scope::ResolveVariable(ParseInfo* info, VariableProxy* proxy) { |
| 1492 DCHECK(info->script_scope()->is_script_scope()); | 1492 DCHECK(info->script_scope()->is_script_scope()); |
| 1493 | 1493 DCHECK(!proxy->is_resolved()); |
| 1494 // If the proxy is already resolved there's nothing to do | |
| 1495 // (functions and consts may be resolved by the parser). | |
| 1496 if (proxy->is_resolved()) return; | |
| 1497 | |
| 1498 // Otherwise, try to resolve the variable. | |
| 1499 Variable* var = LookupRecursive(proxy, nullptr); | 1494 Variable* var = LookupRecursive(proxy, nullptr); |
| 1500 | |
| 1501 ResolveTo(info, proxy, var); | 1495 ResolveTo(info, proxy, var); |
| 1502 } | 1496 } |
| 1503 | 1497 |
| 1504 void Scope::ResolveTo(ParseInfo* info, VariableProxy* proxy, Variable* var) { | 1498 void Scope::ResolveTo(ParseInfo* info, VariableProxy* proxy, Variable* var) { |
| 1505 #ifdef DEBUG | 1499 #ifdef DEBUG |
| 1506 if (info->script_is_native()) { | 1500 if (info->script_is_native()) { |
| 1507 // To avoid polluting the global object in native scripts | 1501 // To avoid polluting the global object in native scripts |
| 1508 // - Variables must not be allocated to the global scope. | 1502 // - Variables must not be allocated to the global scope. |
| 1509 CHECK_NOT_NULL(outer_scope()); | 1503 CHECK_NOT_NULL(outer_scope()); |
| 1510 // - Variables must be bound locally or unallocated. | 1504 // - Variables must be bound locally or unallocated. |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1542 scope->ResolveVariablesRecursively(info); | 1536 scope->ResolveVariablesRecursively(info); |
| 1543 } | 1537 } |
| 1544 } | 1538 } |
| 1545 | 1539 |
| 1546 VariableProxy* Scope::FetchFreeVariables(DeclarationScope* max_outer_scope, | 1540 VariableProxy* Scope::FetchFreeVariables(DeclarationScope* max_outer_scope, |
| 1547 ParseInfo* info, | 1541 ParseInfo* info, |
| 1548 VariableProxy* stack) { | 1542 VariableProxy* stack) { |
| 1549 for (VariableProxy *proxy = unresolved_, *next = nullptr; proxy != nullptr; | 1543 for (VariableProxy *proxy = unresolved_, *next = nullptr; proxy != nullptr; |
| 1550 proxy = next) { | 1544 proxy = next) { |
| 1551 next = proxy->next_unresolved(); | 1545 next = proxy->next_unresolved(); |
| 1552 if (proxy->is_resolved()) continue; | 1546 DCHECK(!proxy->is_resolved()); |
| 1553 Variable* var = LookupRecursive(proxy, max_outer_scope->outer_scope()); | 1547 Variable* var = LookupRecursive(proxy, max_outer_scope->outer_scope()); |
| 1554 if (var == nullptr) { | 1548 if (var == nullptr) { |
| 1555 proxy->set_next_unresolved(stack); | 1549 proxy->set_next_unresolved(stack); |
| 1556 stack = proxy; | 1550 stack = proxy; |
| 1557 } else if (info != nullptr) { | 1551 } else if (info != nullptr) { |
| 1558 ResolveTo(info, proxy, var); | 1552 ResolveTo(info, proxy, var); |
| 1559 } | 1553 } |
| 1560 } | 1554 } |
| 1561 | 1555 |
| 1562 // Clear unresolved_ as it's in an inconsistent state. | 1556 // Clear unresolved_ as it's in an inconsistent state. |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1812 Variable* function = | 1806 Variable* function = |
| 1813 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; | 1807 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; |
| 1814 bool is_function_var_in_context = | 1808 bool is_function_var_in_context = |
| 1815 function != nullptr && function->IsContextSlot(); | 1809 function != nullptr && function->IsContextSlot(); |
| 1816 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - | 1810 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - |
| 1817 (is_function_var_in_context ? 1 : 0); | 1811 (is_function_var_in_context ? 1 : 0); |
| 1818 } | 1812 } |
| 1819 | 1813 |
| 1820 } // namespace internal | 1814 } // namespace internal |
| 1821 } // namespace v8 | 1815 } // namespace v8 |
| OLD | NEW |