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 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 DCHECK(!already_resolved()); | 302 DCHECK(!already_resolved()); |
303 | 303 |
304 // Add this scope as a new inner scope of the outer scope. | 304 // Add this scope as a new inner scope of the outer scope. |
305 if (outer_scope_ != NULL) { | 305 if (outer_scope_ != NULL) { |
306 outer_scope_->inner_scopes_.Add(this, zone()); | 306 outer_scope_->inner_scopes_.Add(this, zone()); |
307 scope_inside_with_ = outer_scope_->scope_inside_with_ || is_with_scope(); | 307 scope_inside_with_ = outer_scope_->scope_inside_with_ || is_with_scope(); |
308 } else { | 308 } else { |
309 scope_inside_with_ = is_with_scope(); | 309 scope_inside_with_ = is_with_scope(); |
310 } | 310 } |
311 | 311 |
312 // Declare convenience variables and the receiver. | 312 // Declare convenience variables. |
| 313 // Declare and allocate receiver (even for the script scope, and even |
| 314 // if naccesses_ == 0). |
| 315 // NOTE: When loading parameters in the script scope, we must take |
| 316 // care not to access them as properties of the global object, but |
| 317 // instead load them directly from the stack. Currently, the only |
| 318 // such parameter is 'this' which is passed on the stack when |
| 319 // invoking scripts |
313 if (is_declaration_scope()) { | 320 if (is_declaration_scope()) { |
314 DCHECK(!subclass_constructor || is_function_scope()); | 321 DCHECK(!subclass_constructor || is_function_scope()); |
315 if (has_this_declaration()) { | 322 Variable* var = variables_.Declare( |
316 Variable* var = variables_.Declare( | 323 this, ast_value_factory_->this_string(), |
317 this, ast_value_factory_->this_string(), | 324 subclass_constructor ? CONST : VAR, Variable::THIS, |
318 subclass_constructor ? CONST : VAR, Variable::THIS, | 325 subclass_constructor ? kNeedsInitialization : kCreatedInitialized); |
319 subclass_constructor ? kNeedsInitialization : kCreatedInitialized); | 326 var->AllocateTo(Variable::PARAMETER, -1); |
320 receiver_ = var; | 327 receiver_ = var; |
321 } | |
322 | 328 |
323 if (subclass_constructor) { | 329 if (subclass_constructor) { |
324 new_target_ = | 330 new_target_ = |
325 variables_.Declare(this, ast_value_factory_->new_target_string(), | 331 variables_.Declare(this, ast_value_factory_->new_target_string(), |
326 CONST, Variable::NEW_TARGET, kCreatedInitialized); | 332 CONST, Variable::NEW_TARGET, kCreatedInitialized); |
327 new_target_->AllocateTo(Variable::PARAMETER, -2); | 333 new_target_->AllocateTo(Variable::PARAMETER, -2); |
328 new_target_->set_is_used(); | 334 new_target_->set_is_used(); |
329 } | 335 } |
| 336 } else { |
| 337 DCHECK(outer_scope() != NULL); |
| 338 receiver_ = outer_scope()->receiver(); |
330 } | 339 } |
331 | 340 |
332 if (is_function_scope() && !is_arrow_scope()) { | 341 if (is_function_scope() && !is_arrow_scope()) { |
333 // Declare 'arguments' variable which exists in all non arrow functions. | 342 // Declare 'arguments' variable which exists in all non arrow functions. |
334 // Note that it might never be accessed, in which case it won't be | 343 // Note that it might never be accessed, in which case it won't be |
335 // allocated during variable allocation. | 344 // allocated during variable allocation. |
336 variables_.Declare(this, | 345 variables_.Declare(this, |
337 ast_value_factory_->arguments_string(), | 346 ast_value_factory_->arguments_string(), |
338 VAR, | 347 VAR, |
339 Variable::ARGUMENTS, | 348 Variable::ARGUMENTS, |
(...skipping 693 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1033 *binding_kind = BOUND; | 1042 *binding_kind = BOUND; |
1034 } else if (outer_scope_ != NULL) { | 1043 } else if (outer_scope_ != NULL) { |
1035 var = outer_scope_->LookupRecursive(proxy, binding_kind, factory); | 1044 var = outer_scope_->LookupRecursive(proxy, binding_kind, factory); |
1036 if (*binding_kind == BOUND && (is_function_scope() || is_with_scope())) { | 1045 if (*binding_kind == BOUND && (is_function_scope() || is_with_scope())) { |
1037 var->ForceContextAllocation(); | 1046 var->ForceContextAllocation(); |
1038 } | 1047 } |
1039 } else { | 1048 } else { |
1040 DCHECK(is_script_scope()); | 1049 DCHECK(is_script_scope()); |
1041 } | 1050 } |
1042 | 1051 |
1043 if (is_with_scope() && (var == nullptr || !var->is_this())) { | 1052 if (is_with_scope()) { |
1044 DCHECK(!already_resolved()); | 1053 DCHECK(!already_resolved()); |
1045 // The current scope is a with scope, so the variable binding can not be | 1054 // The current scope is a with scope, so the variable binding can not be |
1046 // statically resolved. However, note that it was necessary to do a lookup | 1055 // statically resolved. However, note that it was necessary to do a lookup |
1047 // in the outer scope anyway, because if a binding exists in an outer scope, | 1056 // in the outer scope anyway, because if a binding exists in an outer scope, |
1048 // the associated variable has to be marked as potentially being accessed | 1057 // the associated variable has to be marked as potentially being accessed |
1049 // from inside of an inner with scope (the property may not be in the 'with' | 1058 // from inside of an inner with scope (the property may not be in the 'with' |
1050 // object). | 1059 // object). |
1051 if (var != NULL && proxy->is_assigned()) var->set_maybe_assigned(); | 1060 if (var != NULL && proxy->is_assigned()) var->set_maybe_assigned(); |
1052 *binding_kind = DYNAMIC_LOOKUP; | 1061 *binding_kind = DYNAMIC_LOOKUP; |
1053 return NULL; | 1062 return NULL; |
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1399 // order is relevant! | 1408 // order is relevant! |
1400 for (int i = params_.length() - 1; i >= 0; --i) { | 1409 for (int i = params_.length() - 1; i >= 0; --i) { |
1401 Variable* var = params_[i]; | 1410 Variable* var = params_[i]; |
1402 if (var == rest_parameter_) continue; | 1411 if (var == rest_parameter_) continue; |
1403 | 1412 |
1404 DCHECK(var->scope() == this); | 1413 DCHECK(var->scope() == this); |
1405 if (uses_sloppy_arguments || has_forced_context_allocation()) { | 1414 if (uses_sloppy_arguments || has_forced_context_allocation()) { |
1406 // Force context allocation of the parameter. | 1415 // Force context allocation of the parameter. |
1407 var->ForceContextAllocation(); | 1416 var->ForceContextAllocation(); |
1408 } | 1417 } |
1409 AllocateParameter(var, i); | |
1410 } | |
1411 } | |
1412 | 1418 |
1413 | 1419 if (MustAllocate(var)) { |
1414 void Scope::AllocateParameter(Variable* var, int index) { | 1420 if (MustAllocateInContext(var)) { |
1415 if (MustAllocate(var)) { | 1421 DCHECK(var->IsUnallocated() || var->IsContextSlot()); |
1416 if (MustAllocateInContext(var)) { | 1422 if (var->IsUnallocated()) { |
1417 DCHECK(var->IsUnallocated() || var->IsContextSlot()); | 1423 AllocateHeapSlot(var); |
1418 if (var->IsUnallocated()) { | 1424 } |
1419 AllocateHeapSlot(var); | 1425 } else { |
1420 } | 1426 DCHECK(var->IsUnallocated() || var->IsParameter()); |
1421 } else { | 1427 if (var->IsUnallocated()) { |
1422 DCHECK(var->IsUnallocated() || var->IsParameter()); | 1428 var->AllocateTo(Variable::PARAMETER, i); |
1423 if (var->IsUnallocated()) { | 1429 } |
1424 var->AllocateTo(Variable::PARAMETER, index); | |
1425 } | 1430 } |
1426 } | 1431 } |
1427 } | 1432 } |
1428 } | 1433 } |
1429 | 1434 |
1430 | 1435 |
1431 void Scope::AllocateReceiver() { | |
1432 DCHECK_NOT_NULL(receiver()); | |
1433 DCHECK_EQ(receiver()->scope(), this); | |
1434 | |
1435 if (has_forced_context_allocation()) { | |
1436 // Force context allocation of the receiver. | |
1437 receiver()->ForceContextAllocation(); | |
1438 } | |
1439 AllocateParameter(receiver(), -1); | |
1440 } | |
1441 | |
1442 | |
1443 void Scope::AllocateNonParameterLocal(Isolate* isolate, Variable* var) { | 1436 void Scope::AllocateNonParameterLocal(Isolate* isolate, Variable* var) { |
1444 DCHECK(var->scope() == this); | 1437 DCHECK(var->scope() == this); |
1445 DCHECK(!var->IsVariable(isolate->factory()->dot_result_string()) || | 1438 DCHECK(!var->IsVariable(isolate->factory()->dot_result_string()) || |
1446 !var->IsStackLocal()); | 1439 !var->IsStackLocal()); |
1447 if (var->IsUnallocated() && MustAllocate(var)) { | 1440 if (var->IsUnallocated() && MustAllocate(var)) { |
1448 if (MustAllocateInContext(var)) { | 1441 if (MustAllocateInContext(var)) { |
1449 AllocateHeapSlot(var); | 1442 AllocateHeapSlot(var); |
1450 } else { | 1443 } else { |
1451 AllocateStackSlot(var); | 1444 AllocateStackSlot(var); |
1452 } | 1445 } |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1502 | 1495 |
1503 // If scope is already resolved, we still need to allocate | 1496 // If scope is already resolved, we still need to allocate |
1504 // variables in inner scopes which might not had been resolved yet. | 1497 // variables in inner scopes which might not had been resolved yet. |
1505 if (already_resolved()) return; | 1498 if (already_resolved()) return; |
1506 // The number of slots required for variables. | 1499 // The number of slots required for variables. |
1507 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS; | 1500 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS; |
1508 | 1501 |
1509 // Allocate variables for this scope. | 1502 // Allocate variables for this scope. |
1510 // Parameters must be allocated first, if any. | 1503 // Parameters must be allocated first, if any. |
1511 if (is_function_scope()) AllocateParameterLocals(isolate); | 1504 if (is_function_scope()) AllocateParameterLocals(isolate); |
1512 if (has_this_declaration()) AllocateReceiver(); | |
1513 AllocateNonParameterLocals(isolate); | 1505 AllocateNonParameterLocals(isolate); |
1514 | 1506 |
1515 // Force allocation of a context for this scope if necessary. For a 'with' | 1507 // Force allocation of a context for this scope if necessary. For a 'with' |
1516 // scope and for a function scope that makes an 'eval' call we need a context, | 1508 // scope and for a function scope that makes an 'eval' call we need a context, |
1517 // even if no local variables were statically allocated in the scope. | 1509 // even if no local variables were statically allocated in the scope. |
1518 // Likewise for modules. | 1510 // Likewise for modules. |
1519 bool must_have_context = is_with_scope() || is_module_scope() || | 1511 bool must_have_context = is_with_scope() || is_module_scope() || |
1520 (is_function_scope() && calls_sloppy_eval()); | 1512 (is_function_scope() && calls_sloppy_eval()); |
1521 | 1513 |
1522 // If we didn't allocate any locals in the local context, then we only | 1514 // If we didn't allocate any locals in the local context, then we only |
(...skipping 28 matching lines...) Expand all Loading... |
1551 (function_ != NULL && function_->proxy()->var()->IsStackLocal() ? 1 : 0); | 1543 (function_ != NULL && function_->proxy()->var()->IsStackLocal() ? 1 : 0); |
1552 } | 1544 } |
1553 | 1545 |
1554 | 1546 |
1555 int Scope::ContextLocalCount() const { | 1547 int Scope::ContextLocalCount() const { |
1556 if (num_heap_slots() == 0) return 0; | 1548 if (num_heap_slots() == 0) return 0; |
1557 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - | 1549 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - |
1558 (function_ != NULL && function_->proxy()->var()->IsContextSlot() ? 1 : 0); | 1550 (function_ != NULL && function_->proxy()->var()->IsContextSlot() ? 1 : 0); |
1559 } | 1551 } |
1560 } } // namespace v8::internal | 1552 } } // namespace v8::internal |
OLD | NEW |