OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 : FLAG_print_scopes) { | 301 : FLAG_print_scopes) { |
302 scope->Print(); | 302 scope->Print(); |
303 } | 303 } |
304 | 304 |
305 if (FLAG_harmony_modules && FLAG_print_interfaces && top->is_global_scope()) { | 305 if (FLAG_harmony_modules && FLAG_print_interfaces && top->is_global_scope()) { |
306 PrintF("global : "); | 306 PrintF("global : "); |
307 top->interface()->Print(); | 307 top->interface()->Print(); |
308 } | 308 } |
309 #endif | 309 #endif |
310 | 310 |
311 if (FLAG_harmony_scoping) { | |
312 VariableProxy* proxy = scope->CheckAssignmentToConst(); | |
313 if (proxy != NULL) { | |
314 // Found an assignment to const. Throw a syntax error. | |
315 MessageLocation location(info->script(), | |
316 proxy->position(), | |
317 proxy->position()); | |
318 Isolate* isolate = info->isolate(); | |
319 Factory* factory = isolate->factory(); | |
320 Handle<JSArray> array = factory->NewJSArray(0); | |
321 Handle<Object> result = | |
322 factory->NewSyntaxError("harmony_const_assign", array); | |
323 isolate->Throw(*result, &location); | |
324 return false; | |
325 } | |
326 } | |
327 | |
328 info->SetScope(scope); | 311 info->SetScope(scope); |
329 return true; | 312 return true; |
330 } | 313 } |
331 | 314 |
332 | 315 |
333 void Scope::Initialize() { | 316 void Scope::Initialize() { |
334 ASSERT(!already_resolved()); | 317 ASSERT(!already_resolved()); |
335 | 318 |
336 // Add this scope as a new inner scope of the outer scope. | 319 // Add this scope as a new inner scope of the outer scope. |
337 if (outer_scope_ != NULL) { | 320 if (outer_scope_ != NULL) { |
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
584 return decl; | 567 return decl; |
585 } | 568 } |
586 previous = current; | 569 previous = current; |
587 current = current->outer_scope_; | 570 current = current->outer_scope_; |
588 } while (!previous->is_declaration_scope()); | 571 } while (!previous->is_declaration_scope()); |
589 } | 572 } |
590 return NULL; | 573 return NULL; |
591 } | 574 } |
592 | 575 |
593 | 576 |
594 VariableProxy* Scope::CheckAssignmentToConst() { | |
595 // Check this scope. | |
596 if (is_extended_mode()) { | |
597 for (int i = 0; i < unresolved_.length(); i++) { | |
598 ASSERT(unresolved_[i]->var() != NULL); | |
599 if (unresolved_[i]->var()->is_const_mode() && | |
600 unresolved_[i]->IsLValue()) { | |
601 return unresolved_[i]; | |
602 } | |
603 } | |
604 } | |
605 | |
606 // Check inner scopes. | |
607 for (int i = 0; i < inner_scopes_.length(); i++) { | |
608 VariableProxy* proxy = inner_scopes_[i]->CheckAssignmentToConst(); | |
609 if (proxy != NULL) return proxy; | |
610 } | |
611 | |
612 // No assignments to const found. | |
613 return NULL; | |
614 } | |
615 | |
616 | |
617 class VarAndOrder { | 577 class VarAndOrder { |
618 public: | 578 public: |
619 VarAndOrder(Variable* var, int order) : var_(var), order_(order) { } | 579 VarAndOrder(Variable* var, int order) : var_(var), order_(order) { } |
620 Variable* var() const { return var_; } | 580 Variable* var() const { return var_; } |
621 int order() const { return order_; } | 581 int order() const { return order_; } |
622 static int Compare(const VarAndOrder* a, const VarAndOrder* b) { | 582 static int Compare(const VarAndOrder* a, const VarAndOrder* b) { |
623 return a->order_ - b->order_; | 583 return a->order_ - b->order_; |
624 } | 584 } |
625 | 585 |
626 private: | 586 private: |
(...skipping 468 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1095 break; | 1055 break; |
1096 | 1056 |
1097 case DYNAMIC_LOOKUP: | 1057 case DYNAMIC_LOOKUP: |
1098 // The variable could not be resolved statically. | 1058 // The variable could not be resolved statically. |
1099 var = NonLocal(proxy->name(), DYNAMIC); | 1059 var = NonLocal(proxy->name(), DYNAMIC); |
1100 break; | 1060 break; |
1101 } | 1061 } |
1102 | 1062 |
1103 ASSERT(var != NULL); | 1063 ASSERT(var != NULL); |
1104 | 1064 |
| 1065 if (FLAG_harmony_scoping && is_extended_mode() && |
| 1066 var->is_const_mode() && proxy->IsLValue()) { |
| 1067 // Assignment to const. Throw a syntax error. |
| 1068 MessageLocation location( |
| 1069 info->script(), proxy->position(), proxy->position()); |
| 1070 Isolate* isolate = Isolate::Current(); |
| 1071 Factory* factory = isolate->factory(); |
| 1072 Handle<JSArray> array = factory->NewJSArray(0); |
| 1073 Handle<Object> result = |
| 1074 factory->NewSyntaxError("harmony_const_assign", array); |
| 1075 isolate->Throw(*result, &location); |
| 1076 return false; |
| 1077 } |
| 1078 |
1105 if (FLAG_harmony_modules) { | 1079 if (FLAG_harmony_modules) { |
1106 bool ok; | 1080 bool ok; |
1107 #ifdef DEBUG | 1081 #ifdef DEBUG |
1108 if (FLAG_print_interface_details) | 1082 if (FLAG_print_interface_details) |
1109 PrintF("# Resolve %s:\n", var->name()->ToAsciiArray()); | 1083 PrintF("# Resolve %s:\n", var->name()->ToAsciiArray()); |
1110 #endif | 1084 #endif |
1111 proxy->interface()->Unify(var->interface(), zone(), &ok); | 1085 proxy->interface()->Unify(var->interface(), zone(), &ok); |
1112 if (!ok) { | 1086 if (!ok) { |
1113 #ifdef DEBUG | 1087 #ifdef DEBUG |
1114 if (FLAG_print_interfaces) { | 1088 if (FLAG_print_interfaces) { |
1115 PrintF("SCOPES TYPE ERROR\n"); | 1089 PrintF("SCOPES TYPE ERROR\n"); |
1116 PrintF("proxy: "); | 1090 PrintF("proxy: "); |
1117 proxy->interface()->Print(); | 1091 proxy->interface()->Print(); |
1118 PrintF("var: "); | 1092 PrintF("var: "); |
1119 var->interface()->Print(); | 1093 var->interface()->Print(); |
1120 } | 1094 } |
1121 #endif | 1095 #endif |
1122 | 1096 |
1123 // Inconsistent use of module. Throw a syntax error. | 1097 // Inconsistent use of module. Throw a syntax error. |
1124 // TODO(rossberg): generate more helpful error message. | 1098 // TODO(rossberg): generate more helpful error message. |
1125 MessageLocation location(info->script(), | 1099 MessageLocation location( |
1126 proxy->position(), | 1100 info->script(), proxy->position(), proxy->position()); |
1127 proxy->position()); | |
1128 Isolate* isolate = Isolate::Current(); | 1101 Isolate* isolate = Isolate::Current(); |
1129 Factory* factory = isolate->factory(); | 1102 Factory* factory = isolate->factory(); |
1130 Handle<JSArray> array = factory->NewJSArray(1); | 1103 Handle<JSArray> array = factory->NewJSArray(1); |
1131 USE(JSObject::SetElement(array, 0, var->name(), NONE, kStrictMode)); | 1104 USE(JSObject::SetElement(array, 0, var->name(), NONE, kStrictMode)); |
1132 Handle<Object> result = | 1105 Handle<Object> result = |
1133 factory->NewSyntaxError("module_type_error", array); | 1106 factory->NewSyntaxError("module_type_error", array); |
1134 isolate->Throw(*result, &location); | 1107 isolate->Throw(*result, &location); |
1135 return false; | 1108 return false; |
1136 } | 1109 } |
1137 } | 1110 } |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1407 } | 1380 } |
1408 | 1381 |
1409 | 1382 |
1410 int Scope::ContextLocalCount() const { | 1383 int Scope::ContextLocalCount() const { |
1411 if (num_heap_slots() == 0) return 0; | 1384 if (num_heap_slots() == 0) return 0; |
1412 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - | 1385 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - |
1413 (function_ != NULL && function_->proxy()->var()->IsContextSlot() ? 1 : 0); | 1386 (function_ != NULL && function_->proxy()->var()->IsContextSlot() ? 1 : 0); |
1414 } | 1387 } |
1415 | 1388 |
1416 } } // namespace v8::internal | 1389 } } // namespace v8::internal |
OLD | NEW |