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 991 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1002 DCHECK(!is_catch_scope()); | 1002 DCHECK(!is_catch_scope()); |
1003 DCHECK(!is_with_scope()); | 1003 DCHECK(!is_with_scope()); |
1004 DCHECK(is_declaration_scope() || | 1004 DCHECK(is_declaration_scope() || |
1005 (IsLexicalVariableMode(mode) && is_block_scope())); | 1005 (IsLexicalVariableMode(mode) && is_block_scope())); |
1006 | 1006 |
1007 VariableProxy* proxy = declaration->proxy(); | 1007 VariableProxy* proxy = declaration->proxy(); |
1008 DCHECK(proxy->raw_name() != NULL); | 1008 DCHECK(proxy->raw_name() != NULL); |
1009 const AstRawString* name = proxy->raw_name(); | 1009 const AstRawString* name = proxy->raw_name(); |
1010 bool is_function_declaration = declaration->IsFunctionDeclaration(); | 1010 bool is_function_declaration = declaration->IsFunctionDeclaration(); |
1011 | 1011 |
| 1012 // Pessimistically assume that top-level variables will be assigned. |
| 1013 // |
| 1014 // Top-level variables in a script can be accessed by other scripts or even |
| 1015 // become global properties. While this does not apply to top-level variables |
| 1016 // in a module (assuming they are not exported), we must still mark these as |
| 1017 // assigned because they might be accessed by a lazily parsed top-level |
| 1018 // function, which, for efficiency, we preparse without variable tracking. |
| 1019 if (is_script_scope() || is_module_scope()) { |
| 1020 if (mode != CONST) proxy->set_is_assigned(); |
| 1021 } |
| 1022 |
1012 Variable* var = nullptr; | 1023 Variable* var = nullptr; |
1013 if (is_eval_scope() && is_sloppy(language_mode()) && mode == VAR) { | 1024 if (is_eval_scope() && is_sloppy(language_mode()) && mode == VAR) { |
1014 // In a var binding in a sloppy direct eval, pollute the enclosing scope | 1025 // In a var binding in a sloppy direct eval, pollute the enclosing scope |
1015 // with this new binding by doing the following: | 1026 // with this new binding by doing the following: |
1016 // The proxy is bound to a lookup variable to force a dynamic declaration | 1027 // The proxy is bound to a lookup variable to force a dynamic declaration |
1017 // using the DeclareEvalVar or DeclareEvalFunction runtime functions. | 1028 // using the DeclareEvalVar or DeclareEvalFunction runtime functions. |
1018 var = new (zone()) | 1029 var = new (zone()) |
1019 Variable(this, name, mode, NORMAL_VARIABLE, init, kMaybeAssigned); | 1030 Variable(this, name, mode, NORMAL_VARIABLE, init, kMaybeAssigned); |
1020 var->AllocateTo(VariableLocation::LOOKUP, -1); | 1031 var->AllocateTo(VariableLocation::LOOKUP, -1); |
1021 } else { | 1032 } else { |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1145 DCHECK(!already_resolved_); | 1156 DCHECK(!already_resolved_); |
1146 DCHECK(!proxy->is_resolved()); | 1157 DCHECK(!proxy->is_resolved()); |
1147 proxy->set_next_unresolved(unresolved_); | 1158 proxy->set_next_unresolved(unresolved_); |
1148 unresolved_ = proxy; | 1159 unresolved_ = proxy; |
1149 } | 1160 } |
1150 | 1161 |
1151 Variable* DeclarationScope::DeclareDynamicGlobal(const AstRawString* name, | 1162 Variable* DeclarationScope::DeclareDynamicGlobal(const AstRawString* name, |
1152 VariableKind kind) { | 1163 VariableKind kind) { |
1153 DCHECK(is_script_scope()); | 1164 DCHECK(is_script_scope()); |
1154 return variables_.Declare(zone(), this, name, DYNAMIC_GLOBAL, kind); | 1165 return variables_.Declare(zone(), this, name, DYNAMIC_GLOBAL, kind); |
| 1166 // TODO(neis): Mark variable as maybe-assigned? |
1155 } | 1167 } |
1156 | 1168 |
1157 | 1169 |
1158 bool Scope::RemoveUnresolved(VariableProxy* var) { | 1170 bool Scope::RemoveUnresolved(VariableProxy* var) { |
1159 if (unresolved_ == var) { | 1171 if (unresolved_ == var) { |
1160 unresolved_ = var->next_unresolved(); | 1172 unresolved_ = var->next_unresolved(); |
1161 var->set_next_unresolved(nullptr); | 1173 var->set_next_unresolved(nullptr); |
1162 return true; | 1174 return true; |
1163 } | 1175 } |
1164 VariableProxy* current = unresolved_; | 1176 VariableProxy* current = unresolved_; |
(...skipping 1088 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2253 Variable* function = | 2265 Variable* function = |
2254 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; | 2266 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; |
2255 bool is_function_var_in_context = | 2267 bool is_function_var_in_context = |
2256 function != nullptr && function->IsContextSlot(); | 2268 function != nullptr && function->IsContextSlot(); |
2257 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - | 2269 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - |
2258 (is_function_var_in_context ? 1 : 0); | 2270 (is_function_var_in_context ? 1 : 0); |
2259 } | 2271 } |
2260 | 2272 |
2261 } // namespace internal | 2273 } // namespace internal |
2262 } // namespace v8 | 2274 } // namespace v8 |
OLD | NEW |