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/v8.h" | 5 #include "src/v8.h" |
6 | 6 |
7 #include "src/accessors.h" | 7 #include "src/accessors.h" |
8 #include "src/bootstrapper.h" | 8 #include "src/bootstrapper.h" |
9 #include "src/messages.h" | 9 #include "src/messages.h" |
10 #include "src/parser.h" | 10 #include "src/parser.h" |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 Variable::NORMAL, | 143 Variable::NORMAL, |
144 kCreatedInitialized); | 144 kCreatedInitialized); |
145 AllocateHeapSlot(variable); | 145 AllocateHeapSlot(variable); |
146 } | 146 } |
147 | 147 |
148 | 148 |
149 void Scope::SetDefaults(ScopeType scope_type, Scope* outer_scope, | 149 void Scope::SetDefaults(ScopeType scope_type, Scope* outer_scope, |
150 Handle<ScopeInfo> scope_info, | 150 Handle<ScopeInfo> scope_info, |
151 FunctionKind function_kind) { | 151 FunctionKind function_kind) { |
152 outer_scope_ = outer_scope; | 152 outer_scope_ = outer_scope; |
| 153 function_body_ = nullptr; |
153 scope_type_ = scope_type; | 154 scope_type_ = scope_type; |
154 function_kind_ = function_kind; | 155 function_kind_ = function_kind; |
155 block_scope_is_class_scope_ = false; | 156 block_scope_is_class_scope_ = false; |
156 scope_name_ = ast_value_factory_->empty_string(); | 157 scope_name_ = ast_value_factory_->empty_string(); |
157 dynamics_ = NULL; | 158 dynamics_ = NULL; |
158 receiver_ = NULL; | 159 receiver_ = NULL; |
159 new_target_ = nullptr; | 160 new_target_ = nullptr; |
160 function_ = NULL; | 161 function_ = NULL; |
161 arguments_ = NULL; | 162 arguments_ = NULL; |
162 illegal_redecl_ = NULL; | 163 illegal_redecl_ = NULL; |
(...skipping 24 matching lines...) Expand all Loading... |
187 rest_index_ = -1; | 188 rest_index_ = -1; |
188 scope_info_ = scope_info; | 189 scope_info_ = scope_info; |
189 start_position_ = RelocInfo::kNoPosition; | 190 start_position_ = RelocInfo::kNoPosition; |
190 end_position_ = RelocInfo::kNoPosition; | 191 end_position_ = RelocInfo::kNoPosition; |
191 if (!scope_info.is_null()) { | 192 if (!scope_info.is_null()) { |
192 scope_calls_eval_ = scope_info->CallsEval(); | 193 scope_calls_eval_ = scope_info->CallsEval(); |
193 language_mode_ = scope_info->language_mode(); | 194 language_mode_ = scope_info->language_mode(); |
194 block_scope_is_class_scope_ = scope_info->block_scope_is_class_scope(); | 195 block_scope_is_class_scope_ = scope_info->block_scope_is_class_scope(); |
195 function_kind_ = scope_info->function_kind(); | 196 function_kind_ = scope_info->function_kind(); |
196 } | 197 } |
| 198 if (scope_type == FUNCTION_BODY_SCOPE) { |
| 199 DCHECK_NOT_NULL(outer_scope); |
| 200 DCHECK(outer_scope->is_function_scope()); |
| 201 DCHECK_NULL(outer_scope->function_body_); |
| 202 outer_scope->function_body_ = this; |
| 203 } |
197 } | 204 } |
198 | 205 |
199 | 206 |
200 Scope* Scope::DeserializeScopeChain(Isolate* isolate, Zone* zone, | 207 Scope* Scope::DeserializeScopeChain(Isolate* isolate, Zone* zone, |
201 Context* context, Scope* script_scope) { | 208 Context* context, Scope* script_scope) { |
202 // Reconstruct the outer scope chain from a closure's context chain. | 209 // Reconstruct the outer scope chain from a closure's context chain. |
203 Scope* current_scope = NULL; | 210 Scope* current_scope = NULL; |
204 Scope* innermost_scope = NULL; | 211 Scope* innermost_scope = NULL; |
205 bool contains_with = false; | 212 bool contains_with = false; |
206 while (!context->IsNativeContext()) { | 213 while (!context->IsNativeContext()) { |
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
536 name, | 543 name, |
537 TEMPORARY, | 544 TEMPORARY, |
538 Variable::NORMAL, | 545 Variable::NORMAL, |
539 kCreatedInitialized); | 546 kCreatedInitialized); |
540 temps_.Add(var, zone()); | 547 temps_.Add(var, zone()); |
541 return var; | 548 return var; |
542 } | 549 } |
543 | 550 |
544 | 551 |
545 void Scope::AddDeclaration(Declaration* declaration) { | 552 void Scope::AddDeclaration(Declaration* declaration) { |
546 decls_.Add(declaration, zone()); | 553 Scope* target = this; |
| 554 if (is_function_body_scope() && declaration->mode() == VAR) { |
| 555 // Add `var` declarations to outer scope, to make allocation simpler |
| 556 target = outer_scope(); |
| 557 } |
| 558 target->decls_.Add(declaration, zone()); |
547 } | 559 } |
548 | 560 |
549 | 561 |
550 void Scope::SetIllegalRedeclaration(Expression* expression) { | 562 void Scope::SetIllegalRedeclaration(Expression* expression) { |
551 // Record only the first illegal redeclaration. | 563 // Record only the first illegal redeclaration. |
552 if (!HasIllegalRedeclaration()) { | 564 if (!HasIllegalRedeclaration()) { |
553 illegal_redecl_ = expression; | 565 illegal_redecl_ = expression; |
554 } | 566 } |
555 DCHECK(HasIllegalRedeclaration()); | 567 DCHECK(HasIllegalRedeclaration()); |
556 } | 568 } |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
812 static const char* Header(ScopeType scope_type) { | 824 static const char* Header(ScopeType scope_type) { |
813 switch (scope_type) { | 825 switch (scope_type) { |
814 case EVAL_SCOPE: return "eval"; | 826 case EVAL_SCOPE: return "eval"; |
815 case FUNCTION_SCOPE: return "function"; | 827 case FUNCTION_SCOPE: return "function"; |
816 case MODULE_SCOPE: return "module"; | 828 case MODULE_SCOPE: return "module"; |
817 case SCRIPT_SCOPE: return "global"; | 829 case SCRIPT_SCOPE: return "global"; |
818 case CATCH_SCOPE: return "catch"; | 830 case CATCH_SCOPE: return "catch"; |
819 case BLOCK_SCOPE: return "block"; | 831 case BLOCK_SCOPE: return "block"; |
820 case WITH_SCOPE: return "with"; | 832 case WITH_SCOPE: return "with"; |
821 case ARROW_SCOPE: return "arrow"; | 833 case ARROW_SCOPE: return "arrow"; |
| 834 case FUNCTION_BODY_SCOPE: |
| 835 return "function body"; |
822 } | 836 } |
823 UNREACHABLE(); | 837 UNREACHABLE(); |
824 return NULL; | 838 return NULL; |
825 } | 839 } |
826 | 840 |
827 | 841 |
828 static void Indent(int n, const char* str) { | 842 static void Indent(int n, const char* str) { |
829 PrintF("%*s%s", n, "", str); | 843 PrintF("%*s%s", n, "", str); |
830 } | 844 } |
831 | 845 |
(...skipping 517 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1349 if (params_[i]->name().is_identical_to( | 1363 if (params_[i]->name().is_identical_to( |
1350 isolate->factory()->arguments_string())) { | 1364 isolate->factory()->arguments_string())) { |
1351 return true; | 1365 return true; |
1352 } | 1366 } |
1353 } | 1367 } |
1354 return false; | 1368 return false; |
1355 } | 1369 } |
1356 | 1370 |
1357 | 1371 |
1358 void Scope::AllocateStackSlot(Variable* var) { | 1372 void Scope::AllocateStackSlot(Variable* var) { |
1359 if (is_block_scope()) { | 1373 if (is_function_body_scope()) { |
| 1374 // Function body variables need to be allocated in the "real" function |
| 1375 // scope, but can't be resolved from it |
| 1376 outer_scope()->AllocateStackSlot(var); |
| 1377 } else if (is_block_scope()) { |
1360 DeclarationScope()->AllocateStackSlot(var); | 1378 DeclarationScope()->AllocateStackSlot(var); |
1361 } else { | 1379 } else { |
1362 var->AllocateTo(Variable::LOCAL, num_stack_slots_++); | 1380 var->AllocateTo(Variable::LOCAL, num_stack_slots_++); |
1363 } | 1381 } |
1364 } | 1382 } |
1365 | 1383 |
1366 | 1384 |
1367 void Scope::AllocateHeapSlot(Variable* var) { | 1385 void Scope::AllocateHeapSlot(Variable* var) { |
1368 var->AllocateTo(Variable::CONTEXT, num_heap_slots_++); | 1386 var->AllocateTo(Variable::CONTEXT, num_heap_slots_++); |
1369 } | 1387 } |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1543 (function_ != NULL && function_->proxy()->var()->IsStackLocal() ? 1 : 0); | 1561 (function_ != NULL && function_->proxy()->var()->IsStackLocal() ? 1 : 0); |
1544 } | 1562 } |
1545 | 1563 |
1546 | 1564 |
1547 int Scope::ContextLocalCount() const { | 1565 int Scope::ContextLocalCount() const { |
1548 if (num_heap_slots() == 0) return 0; | 1566 if (num_heap_slots() == 0) return 0; |
1549 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - | 1567 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - |
1550 (function_ != NULL && function_->proxy()->var()->IsContextSlot() ? 1 : 0); | 1568 (function_ != NULL && function_->proxy()->var()->IsContextSlot() ? 1 : 0); |
1551 } | 1569 } |
1552 } } // namespace v8::internal | 1570 } } // namespace v8::internal |
OLD | NEW |