OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 | 111 |
112 Scope::Scope(Scope* outer_scope, ScopeType type) | 112 Scope::Scope(Scope* outer_scope, ScopeType type) |
113 : isolate_(Isolate::Current()), | 113 : isolate_(Isolate::Current()), |
114 inner_scopes_(4), | 114 inner_scopes_(4), |
115 variables_(), | 115 variables_(), |
116 temps_(4), | 116 temps_(4), |
117 params_(4), | 117 params_(4), |
118 unresolved_(16), | 118 unresolved_(16), |
119 decls_(4), | 119 decls_(4), |
120 already_resolved_(false) { | 120 already_resolved_(false) { |
121 SetDefaults(type, outer_scope, Handle<SerializedScopeInfo>::null()); | 121 SetDefaults(type, outer_scope, Handle<ScopeInfo>::null()); |
122 // At some point we might want to provide outer scopes to | 122 // At some point we might want to provide outer scopes to |
123 // eval scopes (by walking the stack and reading the scope info). | 123 // eval scopes (by walking the stack and reading the scope info). |
124 // In that case, the ASSERT below needs to be adjusted. | 124 // In that case, the ASSERT below needs to be adjusted. |
125 ASSERT((type == GLOBAL_SCOPE || type == EVAL_SCOPE) == (outer_scope == NULL)); | 125 ASSERT((type == GLOBAL_SCOPE || type == EVAL_SCOPE) == (outer_scope == NULL)); |
126 ASSERT(!HasIllegalRedeclaration()); | 126 ASSERT(!HasIllegalRedeclaration()); |
127 } | 127 } |
128 | 128 |
129 | 129 |
130 Scope::Scope(Scope* inner_scope, | 130 Scope::Scope(Scope* inner_scope, |
131 ScopeType type, | 131 ScopeType type, |
132 Handle<SerializedScopeInfo> scope_info) | 132 Handle<ScopeInfo> scope_info) |
133 : isolate_(Isolate::Current()), | 133 : isolate_(Isolate::Current()), |
134 inner_scopes_(4), | 134 inner_scopes_(4), |
135 variables_(), | 135 variables_(), |
136 temps_(4), | 136 temps_(4), |
137 params_(4), | 137 params_(4), |
138 unresolved_(16), | 138 unresolved_(16), |
139 decls_(4), | 139 decls_(4), |
140 already_resolved_(true) { | 140 already_resolved_(true) { |
141 SetDefaults(type, NULL, scope_info); | 141 SetDefaults(type, NULL, scope_info); |
142 if (!scope_info.is_null() && scope_info->HasHeapAllocatedLocals()) { | 142 if (!scope_info.is_null()) { |
143 num_heap_slots_ = scope_info_->NumberOfContextSlots(); | 143 num_heap_slots_ = scope_info_->ContextLength(); |
144 } | 144 } |
145 AddInnerScope(inner_scope); | 145 AddInnerScope(inner_scope); |
146 } | 146 } |
147 | 147 |
148 | 148 |
149 Scope::Scope(Scope* inner_scope, Handle<String> catch_variable_name) | 149 Scope::Scope(Scope* inner_scope, Handle<String> catch_variable_name) |
150 : isolate_(Isolate::Current()), | 150 : isolate_(Isolate::Current()), |
151 inner_scopes_(1), | 151 inner_scopes_(1), |
152 variables_(), | 152 variables_(), |
153 temps_(0), | 153 temps_(0), |
154 params_(0), | 154 params_(0), |
155 unresolved_(0), | 155 unresolved_(0), |
156 decls_(0), | 156 decls_(0), |
157 already_resolved_(true) { | 157 already_resolved_(true) { |
158 SetDefaults(CATCH_SCOPE, NULL, Handle<SerializedScopeInfo>::null()); | 158 SetDefaults(CATCH_SCOPE, NULL, Handle<ScopeInfo>::null()); |
159 AddInnerScope(inner_scope); | 159 AddInnerScope(inner_scope); |
160 ++num_var_or_const_; | 160 ++num_var_or_const_; |
161 Variable* variable = variables_.Declare(this, | 161 Variable* variable = variables_.Declare(this, |
162 catch_variable_name, | 162 catch_variable_name, |
163 VAR, | 163 VAR, |
164 true, // Valid left-hand side. | 164 true, // Valid left-hand side. |
165 Variable::NORMAL); | 165 Variable::NORMAL); |
166 AllocateHeapSlot(variable); | 166 AllocateHeapSlot(variable); |
167 } | 167 } |
168 | 168 |
169 | 169 |
170 void Scope::SetDefaults(ScopeType type, | 170 void Scope::SetDefaults(ScopeType type, |
171 Scope* outer_scope, | 171 Scope* outer_scope, |
172 Handle<SerializedScopeInfo> scope_info) { | 172 Handle<ScopeInfo> scope_info) { |
173 outer_scope_ = outer_scope; | 173 outer_scope_ = outer_scope; |
174 type_ = type; | 174 type_ = type; |
175 scope_name_ = isolate_->factory()->empty_symbol(); | 175 scope_name_ = isolate_->factory()->empty_symbol(); |
176 dynamics_ = NULL; | 176 dynamics_ = NULL; |
177 receiver_ = NULL; | 177 receiver_ = NULL; |
178 function_ = NULL; | 178 function_ = NULL; |
179 arguments_ = NULL; | 179 arguments_ = NULL; |
180 illegal_redecl_ = NULL; | 180 illegal_redecl_ = NULL; |
181 scope_inside_with_ = false; | 181 scope_inside_with_ = false; |
182 scope_contains_with_ = false; | 182 scope_contains_with_ = false; |
(...skipping 16 matching lines...) Expand all Loading... |
199 Scope* Scope::DeserializeScopeChain(CompilationInfo* info, | 199 Scope* Scope::DeserializeScopeChain(CompilationInfo* info, |
200 Scope* global_scope) { | 200 Scope* global_scope) { |
201 // Reconstruct the outer scope chain from a closure's context chain. | 201 // Reconstruct the outer scope chain from a closure's context chain. |
202 ASSERT(!info->closure().is_null()); | 202 ASSERT(!info->closure().is_null()); |
203 Context* context = info->closure()->context(); | 203 Context* context = info->closure()->context(); |
204 Scope* current_scope = NULL; | 204 Scope* current_scope = NULL; |
205 Scope* innermost_scope = NULL; | 205 Scope* innermost_scope = NULL; |
206 bool contains_with = false; | 206 bool contains_with = false; |
207 while (!context->IsGlobalContext()) { | 207 while (!context->IsGlobalContext()) { |
208 if (context->IsWithContext()) { | 208 if (context->IsWithContext()) { |
209 Scope* with_scope = new Scope(current_scope, WITH_SCOPE, | 209 Scope* with_scope = new Scope(current_scope, |
210 Handle<SerializedScopeInfo>::null()); | 210 WITH_SCOPE, |
| 211 Handle<ScopeInfo>::null()); |
211 current_scope = with_scope; | 212 current_scope = with_scope; |
212 // All the inner scopes are inside a with. | 213 // All the inner scopes are inside a with. |
213 contains_with = true; | 214 contains_with = true; |
214 for (Scope* s = innermost_scope; s != NULL; s = s->outer_scope()) { | 215 for (Scope* s = innermost_scope; s != NULL; s = s->outer_scope()) { |
215 s->scope_inside_with_ = true; | 216 s->scope_inside_with_ = true; |
216 } | 217 } |
217 } else if (context->IsFunctionContext()) { | 218 } else if (context->IsFunctionContext()) { |
218 SerializedScopeInfo* scope_info = | 219 ScopeInfo* scope_info = context->closure()->shared()->scope_info(); |
219 context->closure()->shared()->scope_info(); | 220 current_scope = new Scope(current_scope, |
220 current_scope = new Scope(current_scope, FUNCTION_SCOPE, | 221 FUNCTION_SCOPE, |
221 Handle<SerializedScopeInfo>(scope_info)); | 222 Handle<ScopeInfo>(scope_info)); |
222 } else if (context->IsBlockContext()) { | 223 } else if (context->IsBlockContext()) { |
223 SerializedScopeInfo* scope_info = | 224 ScopeInfo* scope_info = ScopeInfo::cast(context->extension()); |
224 SerializedScopeInfo::cast(context->extension()); | 225 current_scope = new Scope(current_scope, |
225 current_scope = new Scope(current_scope, BLOCK_SCOPE, | 226 BLOCK_SCOPE, |
226 Handle<SerializedScopeInfo>(scope_info)); | 227 Handle<ScopeInfo>(scope_info)); |
227 } else { | 228 } else { |
228 ASSERT(context->IsCatchContext()); | 229 ASSERT(context->IsCatchContext()); |
229 String* name = String::cast(context->extension()); | 230 String* name = String::cast(context->extension()); |
230 current_scope = new Scope(current_scope, Handle<String>(name)); | 231 current_scope = new Scope(current_scope, Handle<String>(name)); |
231 } | 232 } |
232 if (contains_with) current_scope->RecordWithStatement(); | 233 if (contains_with) current_scope->RecordWithStatement(); |
233 if (innermost_scope == NULL) innermost_scope = current_scope; | 234 if (innermost_scope == NULL) innermost_scope = current_scope; |
234 | 235 |
235 // Forget about a with when we move to a context for a different function. | 236 // Forget about a with when we move to a context for a different function. |
236 if (context->previous()->closure() != context->closure()) { | 237 if (context->previous()->closure() != context->closure()) { |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 | 355 |
355 // Check context slot lookup. | 356 // Check context slot lookup. |
356 VariableMode mode; | 357 VariableMode mode; |
357 int index = scope_info_->ContextSlotIndex(*name, &mode); | 358 int index = scope_info_->ContextSlotIndex(*name, &mode); |
358 if (index < 0) { | 359 if (index < 0) { |
359 // Check parameters. | 360 // Check parameters. |
360 mode = VAR; | 361 mode = VAR; |
361 index = scope_info_->ParameterIndex(*name); | 362 index = scope_info_->ParameterIndex(*name); |
362 if (index < 0) { | 363 if (index < 0) { |
363 // Check the function name. | 364 // Check the function name. |
364 index = scope_info_->FunctionContextSlotIndex(*name, NULL); | 365 index = scope_info_->FunctionContextSlotIndex(*name, &mode); |
365 if (index < 0) return NULL; | 366 if (index < 0) return NULL; |
366 } | 367 } |
367 } | 368 } |
368 | 369 |
369 Variable* var = | 370 Variable* var = |
370 variables_.Declare(this, name, mode, true, Variable::NORMAL); | 371 variables_.Declare(this, name, mode, true, Variable::NORMAL); |
371 var->AllocateTo(Variable::CONTEXT, index); | 372 var->AllocateTo(Variable::CONTEXT, index); |
372 return var; | 373 return var; |
373 } | 374 } |
374 | 375 |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
497 return decl; | 498 return decl; |
498 } | 499 } |
499 previous = current; | 500 previous = current; |
500 current = current->outer_scope_; | 501 current = current->outer_scope_; |
501 } while (!previous->is_declaration_scope()); | 502 } while (!previous->is_declaration_scope()); |
502 } | 503 } |
503 return NULL; | 504 return NULL; |
504 } | 505 } |
505 | 506 |
506 | 507 |
507 template<class Allocator> | 508 void Scope::CollectUsedVariables(ZoneList<Variable*>* locals) { |
508 void Scope::CollectUsedVariables(List<Variable*, Allocator>* locals) { | |
509 // Collect variables in this scope. | 509 // Collect variables in this scope. |
510 // Note that the function_ variable - if present - is not | 510 // Note that the function_ variable - if present - is not |
511 // collected here but handled separately in ScopeInfo | 511 // collected here but handled separately in ScopeInfo |
512 // which is the current user of this function). | 512 // which is the current user of this function). |
513 for (int i = 0; i < temps_.length(); i++) { | 513 for (int i = 0; i < temps_.length(); i++) { |
514 Variable* var = temps_[i]; | 514 Variable* var = temps_[i]; |
515 if (var->is_used()) { | 515 if (var->is_used()) { |
516 locals->Add(var); | 516 locals->Add(var); |
517 } | 517 } |
518 } | 518 } |
519 for (VariableMap::Entry* p = variables_.Start(); | 519 for (VariableMap::Entry* p = variables_.Start(); |
520 p != NULL; | 520 p != NULL; |
521 p = variables_.Next(p)) { | 521 p = variables_.Next(p)) { |
522 Variable* var = reinterpret_cast<Variable*>(p->value); | 522 Variable* var = reinterpret_cast<Variable*>(p->value); |
523 if (var->is_used()) { | 523 if (var->is_used()) { |
524 locals->Add(var); | 524 locals->Add(var); |
525 } | 525 } |
526 } | 526 } |
527 } | 527 } |
528 | 528 |
529 | 529 |
530 // Make sure the method gets instantiated by the template system. | |
531 template void Scope::CollectUsedVariables( | |
532 List<Variable*, FreeStoreAllocationPolicy>* locals); | |
533 template void Scope::CollectUsedVariables( | |
534 List<Variable*, PreallocatedStorage>* locals); | |
535 template void Scope::CollectUsedVariables( | |
536 List<Variable*, ZoneListAllocationPolicy>* locals); | |
537 | |
538 | |
539 void Scope::AllocateVariables(Handle<Context> context) { | 530 void Scope::AllocateVariables(Handle<Context> context) { |
540 ASSERT(outer_scope_ == NULL); // eval or global scopes only | 531 ASSERT(outer_scope_ == NULL); // eval or global scopes only |
541 | 532 |
542 // 1) Propagate scope information. | 533 // 1) Propagate scope information. |
543 // If we are in an eval scope, we may have other outer scopes about | 534 // If we are in an eval scope, we may have other outer scopes about |
544 // which we don't know anything at this point. Thus we must be conservative | 535 // which we don't know anything at this point. Thus we must be conservative |
545 // and assume they may invoke eval themselves. Eventually we could capture | 536 // and assume they may invoke eval themselves. Eventually we could capture |
546 // this information in the ScopeInfo and then use it here (by traversing | 537 // this information in the ScopeInfo and then use it here (by traversing |
547 // the call chain stack, at compile time). | 538 // the call chain stack, at compile time). |
548 | 539 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
603 | 594 |
604 Scope* Scope::DeclarationScope() { | 595 Scope* Scope::DeclarationScope() { |
605 Scope* scope = this; | 596 Scope* scope = this; |
606 while (!scope->is_declaration_scope()) { | 597 while (!scope->is_declaration_scope()) { |
607 scope = scope->outer_scope(); | 598 scope = scope->outer_scope(); |
608 } | 599 } |
609 return scope; | 600 return scope; |
610 } | 601 } |
611 | 602 |
612 | 603 |
613 Handle<SerializedScopeInfo> Scope::GetSerializedScopeInfo() { | 604 Handle<ScopeInfo> Scope::GetScopeInfo() { |
614 if (scope_info_.is_null()) { | 605 if (scope_info_.is_null()) { |
615 scope_info_ = SerializedScopeInfo::Create(this); | 606 scope_info_ = ScopeInfo::Create(this); |
616 } | 607 } |
617 return scope_info_; | 608 return scope_info_; |
618 } | 609 } |
619 | 610 |
620 | 611 |
621 void Scope::GetNestedScopeChain( | 612 void Scope::GetNestedScopeChain( |
622 List<Handle<SerializedScopeInfo> >* chain, | 613 List<Handle<ScopeInfo> >* chain, |
623 int position) { | 614 int position) { |
624 chain->Add(Handle<SerializedScopeInfo>(GetSerializedScopeInfo())); | 615 chain->Add(Handle<ScopeInfo>(GetScopeInfo())); |
625 | 616 |
626 for (int i = 0; i < inner_scopes_.length(); i++) { | 617 for (int i = 0; i < inner_scopes_.length(); i++) { |
627 Scope* scope = inner_scopes_[i]; | 618 Scope* scope = inner_scopes_[i]; |
628 int beg_pos = scope->start_position(); | 619 int beg_pos = scope->start_position(); |
629 int end_pos = scope->end_position(); | 620 int end_pos = scope->end_position(); |
630 ASSERT(beg_pos >= 0 && end_pos >= 0); | 621 ASSERT(beg_pos >= 0 && end_pos >= 0); |
631 if (beg_pos <= position && position <= end_pos) { | 622 if (beg_pos <= position && position <= end_pos) { |
632 scope->GetNestedScopeChain(chain, position); | 623 scope->GetNestedScopeChain(chain, position); |
633 return; | 624 return; |
634 } | 625 } |
(...skipping 484 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1119 if (already_resolved()) return; | 1110 if (already_resolved()) return; |
1120 // The number of slots required for variables. | 1111 // The number of slots required for variables. |
1121 num_stack_slots_ = 0; | 1112 num_stack_slots_ = 0; |
1122 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS; | 1113 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS; |
1123 | 1114 |
1124 // Allocate variables for this scope. | 1115 // Allocate variables for this scope. |
1125 // Parameters must be allocated first, if any. | 1116 // Parameters must be allocated first, if any. |
1126 if (is_function_scope()) AllocateParameterLocals(); | 1117 if (is_function_scope()) AllocateParameterLocals(); |
1127 AllocateNonParameterLocals(); | 1118 AllocateNonParameterLocals(); |
1128 | 1119 |
1129 // Allocate context if necessary. | 1120 // Force allocation of a context for this scope if necessary. For a 'with' |
1130 bool must_have_local_context = false; | 1121 // scope and for a function scope that makes an 'eval' call we need a context, |
1131 if (scope_calls_eval_ || scope_contains_with_) { | 1122 // even if no local variables were statically allocated in the scope. |
1132 // The context for the eval() call or 'with' statement in this scope. | 1123 bool must_have_context = is_with_scope() || |
1133 // Unless we are in the global or an eval scope, we need a local | 1124 (is_function_scope() && calls_eval()); |
1134 // context even if we didn't statically allocate any locals in it, | |
1135 // and the compiler will access the context variable. If we are | |
1136 // not in an inner scope, the scope is provided from the outside. | |
1137 must_have_local_context = is_function_scope(); | |
1138 } | |
1139 | 1125 |
1140 // If we didn't allocate any locals in the local context, then we only | 1126 // If we didn't allocate any locals in the local context, then we only |
1141 // need the minimal number of slots if we must have a local context. | 1127 // need the minimal number of slots if we must have a context. |
1142 if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS && | 1128 if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS && !must_have_context) { |
1143 !must_have_local_context) { | |
1144 num_heap_slots_ = 0; | 1129 num_heap_slots_ = 0; |
1145 } | 1130 } |
1146 | 1131 |
1147 // Allocation done. | 1132 // Allocation done. |
1148 ASSERT(num_heap_slots_ == 0 || num_heap_slots_ >= Context::MIN_CONTEXT_SLOTS); | 1133 ASSERT(num_heap_slots_ == 0 || num_heap_slots_ >= Context::MIN_CONTEXT_SLOTS); |
1149 } | 1134 } |
1150 | 1135 |
1151 } } // namespace v8::internal | 1136 } } // namespace v8::internal |
OLD | NEW |