| 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 12 matching lines...) Expand all Loading... |
| 23 // use. Because a Variable holding a handle with the same location exists | 23 // use. Because a Variable holding a handle with the same location exists |
| 24 // this is ensured. | 24 // this is ensured. |
| 25 | 25 |
| 26 VariableMap::VariableMap(Zone* zone) | 26 VariableMap::VariableMap(Zone* zone) |
| 27 : ZoneHashMap(ZoneHashMap::PointersMatch, 8, ZoneAllocationPolicy(zone)), | 27 : ZoneHashMap(ZoneHashMap::PointersMatch, 8, ZoneAllocationPolicy(zone)), |
| 28 zone_(zone) {} | 28 zone_(zone) {} |
| 29 VariableMap::~VariableMap() {} | 29 VariableMap::~VariableMap() {} |
| 30 | 30 |
| 31 | 31 |
| 32 Variable* VariableMap::Declare(Scope* scope, const AstRawString* name, | 32 Variable* VariableMap::Declare(Scope* scope, const AstRawString* name, |
| 33 VariableMode mode, bool is_valid_lhs, | 33 VariableMode mode, Variable::Kind kind, |
| 34 Variable::Kind kind, | |
| 35 InitializationFlag initialization_flag, | 34 InitializationFlag initialization_flag, |
| 36 MaybeAssignedFlag maybe_assigned_flag) { | 35 MaybeAssignedFlag maybe_assigned_flag) { |
| 37 // AstRawStrings are unambiguous, i.e., the same string is always represented | 36 // AstRawStrings are unambiguous, i.e., the same string is always represented |
| 38 // by the same AstRawString*. | 37 // by the same AstRawString*. |
| 39 // FIXME(marja): fix the type of Lookup. | 38 // FIXME(marja): fix the type of Lookup. |
| 40 Entry* p = ZoneHashMap::Lookup(const_cast<AstRawString*>(name), name->hash(), | 39 Entry* p = ZoneHashMap::Lookup(const_cast<AstRawString*>(name), name->hash(), |
| 41 true, ZoneAllocationPolicy(zone())); | 40 true, ZoneAllocationPolicy(zone())); |
| 42 if (p->value == NULL) { | 41 if (p->value == NULL) { |
| 43 // The variable has not been declared yet -> insert it. | 42 // The variable has not been declared yet -> insert it. |
| 44 DCHECK(p->key == name); | 43 DCHECK(p->key == name); |
| 45 p->value = new (zone()) Variable(scope, name, mode, is_valid_lhs, kind, | 44 p->value = new (zone()) Variable(scope, name, mode, kind, |
| 46 initialization_flag, maybe_assigned_flag); | 45 initialization_flag, maybe_assigned_flag); |
| 47 } | 46 } |
| 48 return reinterpret_cast<Variable*>(p->value); | 47 return reinterpret_cast<Variable*>(p->value); |
| 49 } | 48 } |
| 50 | 49 |
| 51 | 50 |
| 52 Variable* VariableMap::Lookup(const AstRawString* name) { | 51 Variable* VariableMap::Lookup(const AstRawString* name) { |
| 53 Entry* p = ZoneHashMap::Lookup(const_cast<AstRawString*>(name), name->hash(), | 52 Entry* p = ZoneHashMap::Lookup(const_cast<AstRawString*>(name), name->hash(), |
| 54 false, ZoneAllocationPolicy(NULL)); | 53 false, ZoneAllocationPolicy(NULL)); |
| 55 if (p != NULL) { | 54 if (p != NULL) { |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 already_resolved_(true), | 123 already_resolved_(true), |
| 125 ast_value_factory_(value_factory), | 124 ast_value_factory_(value_factory), |
| 126 zone_(zone) { | 125 zone_(zone) { |
| 127 SetDefaults(CATCH_SCOPE, NULL, Handle<ScopeInfo>::null()); | 126 SetDefaults(CATCH_SCOPE, NULL, Handle<ScopeInfo>::null()); |
| 128 AddInnerScope(inner_scope); | 127 AddInnerScope(inner_scope); |
| 129 ++num_var_or_const_; | 128 ++num_var_or_const_; |
| 130 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS; | 129 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS; |
| 131 Variable* variable = variables_.Declare(this, | 130 Variable* variable = variables_.Declare(this, |
| 132 catch_variable_name, | 131 catch_variable_name, |
| 133 VAR, | 132 VAR, |
| 134 true, // Valid left-hand side. | |
| 135 Variable::NORMAL, | 133 Variable::NORMAL, |
| 136 kCreatedInitialized); | 134 kCreatedInitialized); |
| 137 AllocateHeapSlot(variable); | 135 AllocateHeapSlot(variable); |
| 138 } | 136 } |
| 139 | 137 |
| 140 | 138 |
| 141 void Scope::SetDefaults(ScopeType scope_type, Scope* outer_scope, | 139 void Scope::SetDefaults(ScopeType scope_type, Scope* outer_scope, |
| 142 Handle<ScopeInfo> scope_info, | 140 Handle<ScopeInfo> scope_info, |
| 143 FunctionKind function_kind) { | 141 FunctionKind function_kind) { |
| 144 outer_scope_ = outer_scope; | 142 outer_scope_ = outer_scope; |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 // if naccesses_ == 0). | 304 // if naccesses_ == 0). |
| 307 // NOTE: When loading parameters in the script scope, we must take | 305 // NOTE: When loading parameters in the script scope, we must take |
| 308 // care not to access them as properties of the global object, but | 306 // care not to access them as properties of the global object, but |
| 309 // instead load them directly from the stack. Currently, the only | 307 // instead load them directly from the stack. Currently, the only |
| 310 // such parameter is 'this' which is passed on the stack when | 308 // such parameter is 'this' which is passed on the stack when |
| 311 // invoking scripts | 309 // invoking scripts |
| 312 if (is_declaration_scope()) { | 310 if (is_declaration_scope()) { |
| 313 DCHECK(!subclass_constructor || is_function_scope()); | 311 DCHECK(!subclass_constructor || is_function_scope()); |
| 314 Variable* var = variables_.Declare( | 312 Variable* var = variables_.Declare( |
| 315 this, ast_value_factory_->this_string(), | 313 this, ast_value_factory_->this_string(), |
| 316 subclass_constructor ? CONST : VAR, false, Variable::THIS, | 314 subclass_constructor ? CONST : VAR, Variable::THIS, |
| 317 subclass_constructor ? kNeedsInitialization : kCreatedInitialized); | 315 subclass_constructor ? kNeedsInitialization : kCreatedInitialized); |
| 318 var->AllocateTo(Variable::PARAMETER, -1); | 316 var->AllocateTo(Variable::PARAMETER, -1); |
| 319 receiver_ = var; | 317 receiver_ = var; |
| 320 | 318 |
| 321 if (subclass_constructor) { | 319 if (subclass_constructor) { |
| 322 new_target_ = variables_.Declare( | 320 new_target_ = |
| 323 this, ast_value_factory_->new_target_string(), CONST, false, | 321 variables_.Declare(this, ast_value_factory_->new_target_string(), |
| 324 Variable::NEW_TARGET, kCreatedInitialized); | 322 CONST, Variable::NEW_TARGET, kCreatedInitialized); |
| 325 new_target_->AllocateTo(Variable::PARAMETER, -2); | 323 new_target_->AllocateTo(Variable::PARAMETER, -2); |
| 326 new_target_->set_is_used(); | 324 new_target_->set_is_used(); |
| 327 } | 325 } |
| 328 } else { | 326 } else { |
| 329 DCHECK(outer_scope() != NULL); | 327 DCHECK(outer_scope() != NULL); |
| 330 receiver_ = outer_scope()->receiver(); | 328 receiver_ = outer_scope()->receiver(); |
| 331 } | 329 } |
| 332 | 330 |
| 333 if (is_function_scope()) { | 331 if (is_function_scope()) { |
| 334 // Declare 'arguments' variable which exists in all functions. | 332 // Declare 'arguments' variable which exists in all functions. |
| 335 // Note that it might never be accessed, in which case it won't be | 333 // Note that it might never be accessed, in which case it won't be |
| 336 // allocated during variable allocation. | 334 // allocated during variable allocation. |
| 337 variables_.Declare(this, | 335 variables_.Declare(this, |
| 338 ast_value_factory_->arguments_string(), | 336 ast_value_factory_->arguments_string(), |
| 339 VAR, | 337 VAR, |
| 340 true, | |
| 341 Variable::ARGUMENTS, | 338 Variable::ARGUMENTS, |
| 342 kCreatedInitialized); | 339 kCreatedInitialized); |
| 343 } | 340 } |
| 344 } | 341 } |
| 345 | 342 |
| 346 | 343 |
| 347 Scope* Scope::FinalizeBlockScope() { | 344 Scope* Scope::FinalizeBlockScope() { |
| 348 DCHECK(is_block_scope()); | 345 DCHECK(is_block_scope()); |
| 349 DCHECK(internals_.is_empty()); | 346 DCHECK(internals_.is_empty()); |
| 350 DCHECK(temps_.is_empty()); | 347 DCHECK(temps_.is_empty()); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 406 | 403 |
| 407 mode = DYNAMIC; | 404 mode = DYNAMIC; |
| 408 location = Variable::LOOKUP; | 405 location = Variable::LOOKUP; |
| 409 init_flag = kCreatedInitialized; | 406 init_flag = kCreatedInitialized; |
| 410 // Be conservative and flag parameters as maybe assigned. Better information | 407 // Be conservative and flag parameters as maybe assigned. Better information |
| 411 // would require ScopeInfo to serialize the maybe_assigned bit also for | 408 // would require ScopeInfo to serialize the maybe_assigned bit also for |
| 412 // parameters. | 409 // parameters. |
| 413 maybe_assigned_flag = kMaybeAssigned; | 410 maybe_assigned_flag = kMaybeAssigned; |
| 414 } | 411 } |
| 415 | 412 |
| 416 Variable* var = variables_.Declare(this, name, mode, true, Variable::NORMAL, | 413 Variable* var = variables_.Declare(this, name, mode, Variable::NORMAL, |
| 417 init_flag, maybe_assigned_flag); | 414 init_flag, maybe_assigned_flag); |
| 418 var->AllocateTo(location, index); | 415 var->AllocateTo(location, index); |
| 419 return var; | 416 return var; |
| 420 } | 417 } |
| 421 | 418 |
| 422 | 419 |
| 423 Variable* Scope::LookupFunctionVar(const AstRawString* name, | 420 Variable* Scope::LookupFunctionVar(const AstRawString* name, |
| 424 AstNodeFactory* factory) { | 421 AstNodeFactory* factory) { |
| 425 if (function_ != NULL && function_->proxy()->raw_name() == name) { | 422 if (function_ != NULL && function_->proxy()->raw_name() == name) { |
| 426 return function_->proxy()->var(); | 423 return function_->proxy()->var(); |
| 427 } else if (!scope_info_.is_null()) { | 424 } else if (!scope_info_.is_null()) { |
| 428 // If we are backed by a scope info, try to lookup the variable there. | 425 // If we are backed by a scope info, try to lookup the variable there. |
| 429 VariableMode mode; | 426 VariableMode mode; |
| 430 int index = scope_info_->FunctionContextSlotIndex(*(name->string()), &mode); | 427 int index = scope_info_->FunctionContextSlotIndex(*(name->string()), &mode); |
| 431 if (index < 0) return NULL; | 428 if (index < 0) return NULL; |
| 432 Variable* var = new(zone()) Variable( | 429 Variable* var = new (zone()) |
| 433 this, name, mode, true /* is valid LHS */, | 430 Variable(this, name, mode, Variable::NORMAL, kCreatedInitialized); |
| 434 Variable::NORMAL, kCreatedInitialized); | |
| 435 VariableProxy* proxy = factory->NewVariableProxy(var); | 431 VariableProxy* proxy = factory->NewVariableProxy(var); |
| 436 VariableDeclaration* declaration = factory->NewVariableDeclaration( | 432 VariableDeclaration* declaration = factory->NewVariableDeclaration( |
| 437 proxy, mode, this, RelocInfo::kNoPosition); | 433 proxy, mode, this, RelocInfo::kNoPosition); |
| 438 DeclareFunctionVar(declaration); | 434 DeclareFunctionVar(declaration); |
| 439 var->AllocateTo(Variable::CONTEXT, index); | 435 var->AllocateTo(Variable::CONTEXT, index); |
| 440 return var; | 436 return var; |
| 441 } else { | 437 } else { |
| 442 return NULL; | 438 return NULL; |
| 443 } | 439 } |
| 444 } | 440 } |
| 445 | 441 |
| 446 | 442 |
| 447 Variable* Scope::Lookup(const AstRawString* name) { | 443 Variable* Scope::Lookup(const AstRawString* name) { |
| 448 for (Scope* scope = this; | 444 for (Scope* scope = this; |
| 449 scope != NULL; | 445 scope != NULL; |
| 450 scope = scope->outer_scope()) { | 446 scope = scope->outer_scope()) { |
| 451 Variable* var = scope->LookupLocal(name); | 447 Variable* var = scope->LookupLocal(name); |
| 452 if (var != NULL) return var; | 448 if (var != NULL) return var; |
| 453 } | 449 } |
| 454 return NULL; | 450 return NULL; |
| 455 } | 451 } |
| 456 | 452 |
| 457 | 453 |
| 458 Variable* Scope::DeclareParameter(const AstRawString* name, VariableMode mode, | 454 Variable* Scope::DeclareParameter(const AstRawString* name, VariableMode mode, |
| 459 bool is_rest) { | 455 bool is_rest) { |
| 460 DCHECK(!already_resolved()); | 456 DCHECK(!already_resolved()); |
| 461 DCHECK(is_function_scope()); | 457 DCHECK(is_function_scope()); |
| 462 Variable* var = variables_.Declare(this, name, mode, true, Variable::NORMAL, | 458 Variable* var = variables_.Declare(this, name, mode, Variable::NORMAL, |
| 463 kCreatedInitialized); | 459 kCreatedInitialized); |
| 464 if (is_rest) { | 460 if (is_rest) { |
| 465 DCHECK_NULL(rest_parameter_); | 461 DCHECK_NULL(rest_parameter_); |
| 466 rest_parameter_ = var; | 462 rest_parameter_ = var; |
| 467 rest_index_ = num_parameters(); | 463 rest_index_ = num_parameters(); |
| 468 } | 464 } |
| 469 params_.Add(var, zone()); | 465 params_.Add(var, zone()); |
| 470 return var; | 466 return var; |
| 471 } | 467 } |
| 472 | 468 |
| 473 | 469 |
| 474 Variable* Scope::DeclareLocal(const AstRawString* name, VariableMode mode, | 470 Variable* Scope::DeclareLocal(const AstRawString* name, VariableMode mode, |
| 475 InitializationFlag init_flag, Variable::Kind kind, | 471 InitializationFlag init_flag, Variable::Kind kind, |
| 476 MaybeAssignedFlag maybe_assigned_flag) { | 472 MaybeAssignedFlag maybe_assigned_flag) { |
| 477 DCHECK(!already_resolved()); | 473 DCHECK(!already_resolved()); |
| 478 // This function handles VAR, LET, and CONST modes. DYNAMIC variables are | 474 // This function handles VAR, LET, and CONST modes. DYNAMIC variables are |
| 479 // introduces during variable allocation, INTERNAL variables are allocated | 475 // introduces during variable allocation, INTERNAL variables are allocated |
| 480 // explicitly, and TEMPORARY variables are allocated via NewTemporary(). | 476 // explicitly, and TEMPORARY variables are allocated via NewTemporary(). |
| 481 DCHECK(IsDeclaredVariableMode(mode)); | 477 DCHECK(IsDeclaredVariableMode(mode)); |
| 482 ++num_var_or_const_; | 478 ++num_var_or_const_; |
| 483 return variables_.Declare(this, name, mode, true, kind, init_flag, | 479 return variables_.Declare(this, name, mode, kind, init_flag, |
| 484 maybe_assigned_flag); | 480 maybe_assigned_flag); |
| 485 } | 481 } |
| 486 | 482 |
| 487 | 483 |
| 488 Variable* Scope::DeclareDynamicGlobal(const AstRawString* name) { | 484 Variable* Scope::DeclareDynamicGlobal(const AstRawString* name) { |
| 489 DCHECK(is_script_scope()); | 485 DCHECK(is_script_scope()); |
| 490 return variables_.Declare(this, | 486 return variables_.Declare(this, |
| 491 name, | 487 name, |
| 492 DYNAMIC_GLOBAL, | 488 DYNAMIC_GLOBAL, |
| 493 true, | |
| 494 Variable::NORMAL, | 489 Variable::NORMAL, |
| 495 kCreatedInitialized); | 490 kCreatedInitialized); |
| 496 } | 491 } |
| 497 | 492 |
| 498 | 493 |
| 499 void Scope::RemoveUnresolved(VariableProxy* var) { | 494 void Scope::RemoveUnresolved(VariableProxy* var) { |
| 500 // Most likely (always?) any variable we want to remove | 495 // Most likely (always?) any variable we want to remove |
| 501 // was just added before, so we search backwards. | 496 // was just added before, so we search backwards. |
| 502 for (int i = unresolved_.length(); i-- > 0;) { | 497 for (int i = unresolved_.length(); i-- > 0;) { |
| 503 if (unresolved_[i] == var) { | 498 if (unresolved_[i] == var) { |
| 504 unresolved_.Remove(i); | 499 unresolved_.Remove(i); |
| 505 return; | 500 return; |
| 506 } | 501 } |
| 507 } | 502 } |
| 508 } | 503 } |
| 509 | 504 |
| 510 | 505 |
| 511 Variable* Scope::NewInternal(const AstRawString* name) { | 506 Variable* Scope::NewInternal(const AstRawString* name) { |
| 512 DCHECK(!already_resolved()); | 507 DCHECK(!already_resolved()); |
| 513 Variable* var = new(zone()) Variable(this, | 508 Variable* var = new(zone()) Variable(this, |
| 514 name, | 509 name, |
| 515 INTERNAL, | 510 INTERNAL, |
| 516 false, | |
| 517 Variable::NORMAL, | 511 Variable::NORMAL, |
| 518 kCreatedInitialized); | 512 kCreatedInitialized); |
| 519 internals_.Add(var, zone()); | 513 internals_.Add(var, zone()); |
| 520 return var; | 514 return var; |
| 521 } | 515 } |
| 522 | 516 |
| 523 | 517 |
| 524 Variable* Scope::NewTemporary(const AstRawString* name) { | 518 Variable* Scope::NewTemporary(const AstRawString* name) { |
| 525 DCHECK(!already_resolved()); | 519 DCHECK(!already_resolved()); |
| 526 Variable* var = new(zone()) Variable(this, | 520 Variable* var = new(zone()) Variable(this, |
| 527 name, | 521 name, |
| 528 TEMPORARY, | 522 TEMPORARY, |
| 529 true, | |
| 530 Variable::NORMAL, | 523 Variable::NORMAL, |
| 531 kCreatedInitialized); | 524 kCreatedInitialized); |
| 532 temps_.Add(var, zone()); | 525 temps_.Add(var, zone()); |
| 533 return var; | 526 return var; |
| 534 } | 527 } |
| 535 | 528 |
| 536 | 529 |
| 537 void Scope::AddDeclaration(Declaration* declaration) { | 530 void Scope::AddDeclaration(Declaration* declaration) { |
| 538 decls_.Add(declaration, zone()); | 531 decls_.Add(declaration, zone()); |
| 539 } | 532 } |
| (...skipping 440 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 980 if (dynamics_ == NULL) dynamics_ = new (zone()) DynamicScopePart(zone()); | 973 if (dynamics_ == NULL) dynamics_ = new (zone()) DynamicScopePart(zone()); |
| 981 VariableMap* map = dynamics_->GetMap(mode); | 974 VariableMap* map = dynamics_->GetMap(mode); |
| 982 Variable* var = map->Lookup(name); | 975 Variable* var = map->Lookup(name); |
| 983 if (var == NULL) { | 976 if (var == NULL) { |
| 984 // Declare a new non-local. | 977 // Declare a new non-local. |
| 985 InitializationFlag init_flag = (mode == VAR) | 978 InitializationFlag init_flag = (mode == VAR) |
| 986 ? kCreatedInitialized : kNeedsInitialization; | 979 ? kCreatedInitialized : kNeedsInitialization; |
| 987 var = map->Declare(NULL, | 980 var = map->Declare(NULL, |
| 988 name, | 981 name, |
| 989 mode, | 982 mode, |
| 990 true, | |
| 991 Variable::NORMAL, | 983 Variable::NORMAL, |
| 992 init_flag); | 984 init_flag); |
| 993 // Allocate it by giving it a dynamic lookup. | 985 // Allocate it by giving it a dynamic lookup. |
| 994 var->AllocateTo(Variable::LOOKUP, -1); | 986 var->AllocateTo(Variable::LOOKUP, -1); |
| 995 } | 987 } |
| 996 return var; | 988 return var; |
| 997 } | 989 } |
| 998 | 990 |
| 999 | 991 |
| 1000 Variable* Scope::LookupRecursive(VariableProxy* proxy, | 992 Variable* Scope::LookupRecursive(VariableProxy* proxy, |
| (...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1458 (function_ != NULL && function_->proxy()->var()->IsStackLocal() ? 1 : 0); | 1450 (function_ != NULL && function_->proxy()->var()->IsStackLocal() ? 1 : 0); |
| 1459 } | 1451 } |
| 1460 | 1452 |
| 1461 | 1453 |
| 1462 int Scope::ContextLocalCount() const { | 1454 int Scope::ContextLocalCount() const { |
| 1463 if (num_heap_slots() == 0) return 0; | 1455 if (num_heap_slots() == 0) return 0; |
| 1464 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - | 1456 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - |
| 1465 (function_ != NULL && function_->proxy()->var()->IsContextSlot() ? 1 : 0); | 1457 (function_ != NULL && function_->proxy()->var()->IsContextSlot() ? 1 : 0); |
| 1466 } | 1458 } |
| 1467 } } // namespace v8::internal | 1459 } } // namespace v8::internal |
| OLD | NEW |