Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(34)

Side by Side Diff: src/scopes.cc

Issue 1136073002: Resolve references to "this" the same way as normal variables (Closed) Base URL: https://chromium.googlesource.com/v8/v8@master
Patch Set: "this" should never be looked up dynamically Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/scopes.h ('k') | src/variables.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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. 312 // Declare convenience variables and the receiver.
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
320 if (is_declaration_scope()) { 313 if (is_declaration_scope()) {
321 DCHECK(!subclass_constructor || is_function_scope()); 314 DCHECK(!subclass_constructor || is_function_scope());
322 Variable* var = variables_.Declare( 315 if (has_this_declaration()) {
323 this, ast_value_factory_->this_string(), 316 Variable* var = variables_.Declare(
324 subclass_constructor ? CONST : VAR, Variable::THIS, 317 this, ast_value_factory_->this_string(),
325 subclass_constructor ? kNeedsInitialization : kCreatedInitialized); 318 subclass_constructor ? CONST : VAR, Variable::THIS,
326 var->AllocateTo(Variable::PARAMETER, -1); 319 subclass_constructor ? kNeedsInitialization : kCreatedInitialized);
327 receiver_ = var; 320 receiver_ = var;
321 }
328 322
329 if (subclass_constructor) { 323 if (subclass_constructor) {
330 new_target_ = 324 new_target_ =
331 variables_.Declare(this, ast_value_factory_->new_target_string(), 325 variables_.Declare(this, ast_value_factory_->new_target_string(),
332 CONST, Variable::NEW_TARGET, kCreatedInitialized); 326 CONST, Variable::NEW_TARGET, kCreatedInitialized);
333 new_target_->AllocateTo(Variable::PARAMETER, -2); 327 new_target_->AllocateTo(Variable::PARAMETER, -2);
334 new_target_->set_is_used(); 328 new_target_->set_is_used();
335 } 329 }
336 } else {
337 DCHECK(outer_scope() != NULL);
338 receiver_ = outer_scope()->receiver();
339 } 330 }
340 331
341 if (is_function_scope() && !is_arrow_scope()) { 332 if (is_function_scope() && !is_arrow_scope()) {
342 // Declare 'arguments' variable which exists in all non arrow functions. 333 // Declare 'arguments' variable which exists in all non arrow functions.
343 // Note that it might never be accessed, in which case it won't be 334 // Note that it might never be accessed, in which case it won't be
344 // allocated during variable allocation. 335 // allocated during variable allocation.
345 variables_.Declare(this, 336 variables_.Declare(this,
346 ast_value_factory_->arguments_string(), 337 ast_value_factory_->arguments_string(),
347 VAR, 338 VAR,
348 Variable::ARGUMENTS, 339 Variable::ARGUMENTS,
(...skipping 693 matching lines...) Expand 10 before | Expand all | Expand 10 after
1042 *binding_kind = BOUND; 1033 *binding_kind = BOUND;
1043 } else if (outer_scope_ != NULL) { 1034 } else if (outer_scope_ != NULL) {
1044 var = outer_scope_->LookupRecursive(proxy, binding_kind, factory); 1035 var = outer_scope_->LookupRecursive(proxy, binding_kind, factory);
1045 if (*binding_kind == BOUND && (is_function_scope() || is_with_scope())) { 1036 if (*binding_kind == BOUND && (is_function_scope() || is_with_scope())) {
1046 var->ForceContextAllocation(); 1037 var->ForceContextAllocation();
1047 } 1038 }
1048 } else { 1039 } else {
1049 DCHECK(is_script_scope()); 1040 DCHECK(is_script_scope());
1050 } 1041 }
1051 1042
1052 if (is_with_scope()) { 1043 // "this" can't be shadowed by "eval"-introduced bindings or by "with" scopes.
1044 // TODO(wingo): There are other variables in this category; add them.
1045 bool name_can_be_shadowed = var == nullptr || !var->is_this();
1046
1047 if (is_with_scope() && name_can_be_shadowed) {
1053 DCHECK(!already_resolved()); 1048 DCHECK(!already_resolved());
1054 // The current scope is a with scope, so the variable binding can not be 1049 // The current scope is a with scope, so the variable binding can not be
1055 // statically resolved. However, note that it was necessary to do a lookup 1050 // statically resolved. However, note that it was necessary to do a lookup
1056 // in the outer scope anyway, because if a binding exists in an outer scope, 1051 // in the outer scope anyway, because if a binding exists in an outer scope,
1057 // the associated variable has to be marked as potentially being accessed 1052 // the associated variable has to be marked as potentially being accessed
1058 // from inside of an inner with scope (the property may not be in the 'with' 1053 // from inside of an inner with scope (the property may not be in the 'with'
1059 // object). 1054 // object).
1060 if (var != NULL && proxy->is_assigned()) var->set_maybe_assigned(); 1055 if (var != NULL && proxy->is_assigned()) var->set_maybe_assigned();
1061 *binding_kind = DYNAMIC_LOOKUP; 1056 *binding_kind = DYNAMIC_LOOKUP;
1062 return NULL; 1057 return NULL;
1063 } else if (calls_sloppy_eval()) { 1058 } else if (calls_sloppy_eval() && name_can_be_shadowed) {
1064 // A variable binding may have been found in an outer scope, but the current 1059 // A variable binding may have been found in an outer scope, but the current
1065 // scope makes a sloppy 'eval' call, so the found variable may not be 1060 // scope makes a sloppy 'eval' call, so the found variable may not be
1066 // the correct one (the 'eval' may introduce a binding with the same name). 1061 // the correct one (the 'eval' may introduce a binding with the same name).
1067 // In that case, change the lookup result to reflect this situation. 1062 // In that case, change the lookup result to reflect this situation.
1068 if (*binding_kind == BOUND) { 1063 if (*binding_kind == BOUND) {
1069 *binding_kind = BOUND_EVAL_SHADOWED; 1064 *binding_kind = BOUND_EVAL_SHADOWED;
1070 } else if (*binding_kind == UNBOUND) { 1065 } else if (*binding_kind == UNBOUND) {
1071 *binding_kind = UNBOUND_EVAL_SHADOWED; 1066 *binding_kind = UNBOUND_EVAL_SHADOWED;
1072 } 1067 }
1073 } 1068 }
(...skipping 334 matching lines...) Expand 10 before | Expand all | Expand 10 after
1408 // order is relevant! 1403 // order is relevant!
1409 for (int i = params_.length() - 1; i >= 0; --i) { 1404 for (int i = params_.length() - 1; i >= 0; --i) {
1410 Variable* var = params_[i]; 1405 Variable* var = params_[i];
1411 if (var == rest_parameter_) continue; 1406 if (var == rest_parameter_) continue;
1412 1407
1413 DCHECK(var->scope() == this); 1408 DCHECK(var->scope() == this);
1414 if (uses_sloppy_arguments || has_forced_context_allocation()) { 1409 if (uses_sloppy_arguments || has_forced_context_allocation()) {
1415 // Force context allocation of the parameter. 1410 // Force context allocation of the parameter.
1416 var->ForceContextAllocation(); 1411 var->ForceContextAllocation();
1417 } 1412 }
1413 AllocateParameter(var, i);
1414 }
1415 }
1418 1416
1419 if (MustAllocate(var)) { 1417
1420 if (MustAllocateInContext(var)) { 1418 void Scope::AllocateParameter(Variable* var, int index) {
1421 DCHECK(var->IsUnallocated() || var->IsContextSlot()); 1419 if (MustAllocate(var)) {
1422 if (var->IsUnallocated()) { 1420 if (MustAllocateInContext(var)) {
1423 AllocateHeapSlot(var); 1421 DCHECK(var->IsUnallocated() || var->IsContextSlot());
1424 } 1422 if (var->IsUnallocated()) {
1425 } else { 1423 AllocateHeapSlot(var);
1426 DCHECK(var->IsUnallocated() || var->IsParameter()); 1424 }
1427 if (var->IsUnallocated()) { 1425 } else {
1428 var->AllocateTo(Variable::PARAMETER, i); 1426 DCHECK(var->IsUnallocated() || var->IsParameter());
1429 } 1427 if (var->IsUnallocated()) {
1428 var->AllocateTo(Variable::PARAMETER, index);
1430 } 1429 }
1431 } 1430 }
1432 } 1431 }
1433 } 1432 }
1434 1433
1435 1434
1435 void Scope::AllocateReceiver() {
1436 DCHECK_NOT_NULL(receiver());
1437 DCHECK_EQ(receiver()->scope(), this);
1438
1439 if (has_forced_context_allocation()) {
1440 // Force context allocation of the receiver.
1441 receiver()->ForceContextAllocation();
1442 }
1443 AllocateParameter(receiver(), -1);
1444 }
1445
1446
1436 void Scope::AllocateNonParameterLocal(Isolate* isolate, Variable* var) { 1447 void Scope::AllocateNonParameterLocal(Isolate* isolate, Variable* var) {
1437 DCHECK(var->scope() == this); 1448 DCHECK(var->scope() == this);
1438 DCHECK(!var->IsVariable(isolate->factory()->dot_result_string()) || 1449 DCHECK(!var->IsVariable(isolate->factory()->dot_result_string()) ||
1439 !var->IsStackLocal()); 1450 !var->IsStackLocal());
1440 if (var->IsUnallocated() && MustAllocate(var)) { 1451 if (var->IsUnallocated() && MustAllocate(var)) {
1441 if (MustAllocateInContext(var)) { 1452 if (MustAllocateInContext(var)) {
1442 AllocateHeapSlot(var); 1453 AllocateHeapSlot(var);
1443 } else { 1454 } else {
1444 AllocateStackSlot(var); 1455 AllocateStackSlot(var);
1445 } 1456 }
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
1495 1506
1496 // If scope is already resolved, we still need to allocate 1507 // If scope is already resolved, we still need to allocate
1497 // variables in inner scopes which might not had been resolved yet. 1508 // variables in inner scopes which might not had been resolved yet.
1498 if (already_resolved()) return; 1509 if (already_resolved()) return;
1499 // The number of slots required for variables. 1510 // The number of slots required for variables.
1500 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS; 1511 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS;
1501 1512
1502 // Allocate variables for this scope. 1513 // Allocate variables for this scope.
1503 // Parameters must be allocated first, if any. 1514 // Parameters must be allocated first, if any.
1504 if (is_function_scope()) AllocateParameterLocals(isolate); 1515 if (is_function_scope()) AllocateParameterLocals(isolate);
1516 if (has_this_declaration()) AllocateReceiver();
1505 AllocateNonParameterLocals(isolate); 1517 AllocateNonParameterLocals(isolate);
1506 1518
1507 // Force allocation of a context for this scope if necessary. For a 'with' 1519 // Force allocation of a context for this scope if necessary. For a 'with'
1508 // scope and for a function scope that makes an 'eval' call we need a context, 1520 // scope and for a function scope that makes an 'eval' call we need a context,
1509 // even if no local variables were statically allocated in the scope. 1521 // even if no local variables were statically allocated in the scope.
1510 // Likewise for modules. 1522 // Likewise for modules.
1511 bool must_have_context = is_with_scope() || is_module_scope() || 1523 bool must_have_context = is_with_scope() || is_module_scope() ||
1512 (is_function_scope() && calls_sloppy_eval()); 1524 (is_function_scope() && calls_sloppy_eval());
1513 1525
1514 // If we didn't allocate any locals in the local context, then we only 1526 // If we didn't allocate any locals in the local context, then we only
(...skipping 28 matching lines...) Expand all
1543 (function_ != NULL && function_->proxy()->var()->IsStackLocal() ? 1 : 0); 1555 (function_ != NULL && function_->proxy()->var()->IsStackLocal() ? 1 : 0);
1544 } 1556 }
1545 1557
1546 1558
1547 int Scope::ContextLocalCount() const { 1559 int Scope::ContextLocalCount() const {
1548 if (num_heap_slots() == 0) return 0; 1560 if (num_heap_slots() == 0) return 0;
1549 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - 1561 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS -
1550 (function_ != NULL && function_->proxy()->var()->IsContextSlot() ? 1 : 0); 1562 (function_ != NULL && function_->proxy()->var()->IsContextSlot() ? 1 : 0);
1551 } 1563 }
1552 } } // namespace v8::internal 1564 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/scopes.h ('k') | src/variables.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698