| 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/scopes.h" | 7 #include "src/scopes.h" |
| 8 | 8 |
| 9 #include "src/accessors.h" | 9 #include "src/accessors.h" |
| 10 #include "src/bootstrapper.h" | 10 #include "src/bootstrapper.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 VariableMap::VariableMap(Zone* zone) | 27 VariableMap::VariableMap(Zone* zone) |
| 28 : ZoneHashMap(ZoneHashMap::PointersMatch, 8, ZoneAllocationPolicy(zone)), | 28 : ZoneHashMap(ZoneHashMap::PointersMatch, 8, ZoneAllocationPolicy(zone)), |
| 29 zone_(zone) {} | 29 zone_(zone) {} |
| 30 VariableMap::~VariableMap() {} | 30 VariableMap::~VariableMap() {} |
| 31 | 31 |
| 32 | 32 |
| 33 Variable* VariableMap::Declare(Scope* scope, const AstRawString* name, | 33 Variable* VariableMap::Declare(Scope* scope, const AstRawString* name, |
| 34 VariableMode mode, bool is_valid_lhs, | 34 VariableMode mode, bool is_valid_lhs, |
| 35 Variable::Kind kind, | 35 Variable::Kind kind, |
| 36 InitializationFlag initialization_flag, | 36 InitializationFlag initialization_flag, |
| 37 MaybeAssignedFlag maybe_assigned_flag, | 37 MaybeAssignedFlag maybe_assigned_flag) { |
| 38 Interface* interface) { | |
| 39 // AstRawStrings are unambiguous, i.e., the same string is always represented | 38 // AstRawStrings are unambiguous, i.e., the same string is always represented |
| 40 // by the same AstRawString*. | 39 // by the same AstRawString*. |
| 41 // FIXME(marja): fix the type of Lookup. | 40 // FIXME(marja): fix the type of Lookup. |
| 42 Entry* p = ZoneHashMap::Lookup(const_cast<AstRawString*>(name), name->hash(), | 41 Entry* p = ZoneHashMap::Lookup(const_cast<AstRawString*>(name), name->hash(), |
| 43 true, ZoneAllocationPolicy(zone())); | 42 true, ZoneAllocationPolicy(zone())); |
| 44 if (p->value == NULL) { | 43 if (p->value == NULL) { |
| 45 // The variable has not been declared yet -> insert it. | 44 // The variable has not been declared yet -> insert it. |
| 46 DCHECK(p->key == name); | 45 DCHECK(p->key == name); |
| 47 p->value = new (zone()) | 46 p->value = new (zone()) Variable(scope, name, mode, is_valid_lhs, kind, |
| 48 Variable(scope, name, mode, is_valid_lhs, kind, initialization_flag, | 47 initialization_flag, maybe_assigned_flag); |
| 49 maybe_assigned_flag, interface); | |
| 50 } | 48 } |
| 51 return reinterpret_cast<Variable*>(p->value); | 49 return reinterpret_cast<Variable*>(p->value); |
| 52 } | 50 } |
| 53 | 51 |
| 54 | 52 |
| 55 Variable* VariableMap::Lookup(const AstRawString* name) { | 53 Variable* VariableMap::Lookup(const AstRawString* name) { |
| 56 Entry* p = ZoneHashMap::Lookup(const_cast<AstRawString*>(name), name->hash(), | 54 Entry* p = ZoneHashMap::Lookup(const_cast<AstRawString*>(name), name->hash(), |
| 57 false, ZoneAllocationPolicy(NULL)); | 55 false, ZoneAllocationPolicy(NULL)); |
| 58 if (p != NULL) { | 56 if (p != NULL) { |
| 59 DCHECK(reinterpret_cast<const AstRawString*>(p->key) == name); | 57 DCHECK(reinterpret_cast<const AstRawString*>(p->key) == name); |
| 60 DCHECK(p->value != NULL); | 58 DCHECK(p->value != NULL); |
| 61 return reinterpret_cast<Variable*>(p->value); | 59 return reinterpret_cast<Variable*>(p->value); |
| 62 } | 60 } |
| 63 return NULL; | 61 return NULL; |
| 64 } | 62 } |
| 65 | 63 |
| 66 | 64 |
| 67 // ---------------------------------------------------------------------------- | 65 // ---------------------------------------------------------------------------- |
| 68 // Implementation of Scope | 66 // Implementation of Scope |
| 69 | 67 |
| 70 Scope::Scope(Zone* zone, Scope* outer_scope, ScopeType scope_type, | 68 Scope::Scope(Zone* zone, Scope* outer_scope, ScopeType scope_type, |
| 71 AstValueFactory* ast_value_factory) | 69 AstValueFactory* ast_value_factory) |
| 72 : inner_scopes_(4, zone), | 70 : inner_scopes_(4, zone), |
| 73 variables_(zone), | 71 variables_(zone), |
| 74 internals_(4, zone), | 72 internals_(4, zone), |
| 75 temps_(4, zone), | 73 temps_(4, zone), |
| 76 params_(4, zone), | 74 params_(4, zone), |
| 77 unresolved_(16, zone), | 75 unresolved_(16, zone), |
| 78 decls_(4, zone), | 76 decls_(4, zone), |
| 79 interface_(scope_type == MODULE_SCOPE ? Interface::NewModule(zone) | 77 interface_(scope_type == MODULE_SCOPE ? Interface::New(zone) |
| 80 : NULL), | 78 : NULL), |
| 81 already_resolved_(false), | 79 already_resolved_(false), |
| 82 ast_value_factory_(ast_value_factory), | 80 ast_value_factory_(ast_value_factory), |
| 83 zone_(zone) { | 81 zone_(zone) { |
| 84 SetDefaults(scope_type, outer_scope, Handle<ScopeInfo>::null()); | 82 SetDefaults(scope_type, outer_scope, Handle<ScopeInfo>::null()); |
| 85 // The outermost scope must be a script scope. | 83 // The outermost scope must be a script scope. |
| 86 DCHECK(scope_type == SCRIPT_SCOPE || outer_scope != NULL); | 84 DCHECK(scope_type == SCRIPT_SCOPE || outer_scope != NULL); |
| 87 DCHECK(!HasIllegalRedeclaration()); | 85 DCHECK(!HasIllegalRedeclaration()); |
| 88 } | 86 } |
| 89 | 87 |
| (...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 462 rest_parameter_ = var; | 460 rest_parameter_ = var; |
| 463 rest_index_ = num_parameters(); | 461 rest_index_ = num_parameters(); |
| 464 } | 462 } |
| 465 params_.Add(var, zone()); | 463 params_.Add(var, zone()); |
| 466 return var; | 464 return var; |
| 467 } | 465 } |
| 468 | 466 |
| 469 | 467 |
| 470 Variable* Scope::DeclareLocal(const AstRawString* name, VariableMode mode, | 468 Variable* Scope::DeclareLocal(const AstRawString* name, VariableMode mode, |
| 471 InitializationFlag init_flag, | 469 InitializationFlag init_flag, |
| 472 MaybeAssignedFlag maybe_assigned_flag, | 470 MaybeAssignedFlag maybe_assigned_flag) { |
| 473 Interface* interface) { | |
| 474 DCHECK(!already_resolved()); | 471 DCHECK(!already_resolved()); |
| 475 // This function handles VAR, LET, and CONST modes. DYNAMIC variables are | 472 // This function handles VAR, LET, and CONST modes. DYNAMIC variables are |
| 476 // introduces during variable allocation, INTERNAL variables are allocated | 473 // introduces during variable allocation, INTERNAL variables are allocated |
| 477 // explicitly, and TEMPORARY variables are allocated via NewTemporary(). | 474 // explicitly, and TEMPORARY variables are allocated via NewTemporary(). |
| 478 DCHECK(IsDeclaredVariableMode(mode)); | 475 DCHECK(IsDeclaredVariableMode(mode)); |
| 479 ++num_var_or_const_; | 476 ++num_var_or_const_; |
| 480 return variables_.Declare(this, name, mode, true, Variable::NORMAL, init_flag, | 477 return variables_.Declare(this, name, mode, true, Variable::NORMAL, init_flag, |
| 481 maybe_assigned_flag, interface); | 478 maybe_assigned_flag); |
| 482 } | 479 } |
| 483 | 480 |
| 484 | 481 |
| 485 Variable* Scope::DeclareDynamicGlobal(const AstRawString* name) { | 482 Variable* Scope::DeclareDynamicGlobal(const AstRawString* name) { |
| 486 DCHECK(is_script_scope()); | 483 DCHECK(is_script_scope()); |
| 487 return variables_.Declare(this, | 484 return variables_.Declare(this, |
| 488 name, | 485 name, |
| 489 DYNAMIC_GLOBAL, | 486 DYNAMIC_GLOBAL, |
| 490 true, | 487 true, |
| 491 Variable::NORMAL, | 488 Variable::NORMAL, |
| (...skipping 596 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1088 | 1085 |
| 1089 case DYNAMIC_LOOKUP: | 1086 case DYNAMIC_LOOKUP: |
| 1090 // The variable could not be resolved statically. | 1087 // The variable could not be resolved statically. |
| 1091 var = NonLocal(proxy->raw_name(), DYNAMIC); | 1088 var = NonLocal(proxy->raw_name(), DYNAMIC); |
| 1092 break; | 1089 break; |
| 1093 } | 1090 } |
| 1094 | 1091 |
| 1095 DCHECK(var != NULL); | 1092 DCHECK(var != NULL); |
| 1096 if (proxy->is_assigned()) var->set_maybe_assigned(); | 1093 if (proxy->is_assigned()) var->set_maybe_assigned(); |
| 1097 | 1094 |
| 1098 if (FLAG_harmony_modules) { | |
| 1099 bool ok; | |
| 1100 #ifdef DEBUG | |
| 1101 if (FLAG_print_interface_details) { | |
| 1102 PrintF("# Resolve %.*s:\n", var->raw_name()->length(), | |
| 1103 var->raw_name()->raw_data()); | |
| 1104 } | |
| 1105 #endif | |
| 1106 proxy->interface()->Unify(var->interface(), zone(), &ok); | |
| 1107 if (!ok) { | |
| 1108 #ifdef DEBUG | |
| 1109 if (FLAG_print_interfaces) { | |
| 1110 PrintF("SCOPES TYPE ERROR\n"); | |
| 1111 PrintF("proxy: "); | |
| 1112 proxy->interface()->Print(); | |
| 1113 PrintF("var: "); | |
| 1114 var->interface()->Print(); | |
| 1115 } | |
| 1116 #endif | |
| 1117 | |
| 1118 // Inconsistent use of module. Throw a syntax error. | |
| 1119 // TODO(rossberg): generate more helpful error message. | |
| 1120 MessageLocation location( | |
| 1121 info->script(), proxy->position(), proxy->position()); | |
| 1122 Isolate* isolate = info->isolate(); | |
| 1123 Factory* factory = isolate->factory(); | |
| 1124 Handle<JSArray> array = factory->NewJSArray(1); | |
| 1125 JSObject::SetElement(array, 0, var->name(), NONE, STRICT).Assert(); | |
| 1126 Handle<Object> error; | |
| 1127 MaybeHandle<Object> maybe_error = | |
| 1128 factory->NewSyntaxError("module_type_error", array); | |
| 1129 if (maybe_error.ToHandle(&error)) isolate->Throw(*error, &location); | |
| 1130 return false; | |
| 1131 } | |
| 1132 } | |
| 1133 | |
| 1134 proxy->BindTo(var); | 1095 proxy->BindTo(var); |
| 1135 | 1096 |
| 1136 return true; | 1097 return true; |
| 1137 } | 1098 } |
| 1138 | 1099 |
| 1139 | 1100 |
| 1140 bool Scope::ResolveVariablesRecursively(CompilationInfo* info, | 1101 bool Scope::ResolveVariablesRecursively(CompilationInfo* info, |
| 1141 AstNodeFactory* factory) { | 1102 AstNodeFactory* factory) { |
| 1142 DCHECK(info->script_scope()->is_script_scope()); | 1103 DCHECK(info->script_scope()->is_script_scope()); |
| 1143 | 1104 |
| (...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1425 } | 1386 } |
| 1426 | 1387 |
| 1427 | 1388 |
| 1428 int Scope::ContextLocalCount() const { | 1389 int Scope::ContextLocalCount() const { |
| 1429 if (num_heap_slots() == 0) return 0; | 1390 if (num_heap_slots() == 0) return 0; |
| 1430 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - | 1391 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - |
| 1431 (function_ != NULL && function_->proxy()->var()->IsContextSlot() ? 1 : 0); | 1392 (function_ != NULL && function_->proxy()->var()->IsContextSlot() ? 1 : 0); |
| 1432 } | 1393 } |
| 1433 | 1394 |
| 1434 } } // namespace v8::internal | 1395 } } // namespace v8::internal |
| OLD | NEW |