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

Side by Side Diff: src/scopes.cc

Issue 8688007: Statically check for assignments to const in harmony mode. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Addressed second round of comments. Created 9 years 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 | Annotate | Revision Log
« no previous file with comments | « src/scopes.h ('k') | test/mjsunit/harmony/block-const-assign.js » ('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 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 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 13 matching lines...) Expand all
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 #include "v8.h" 28 #include "v8.h"
29 29
30 #include "scopes.h" 30 #include "scopes.h"
31 31
32 #include "bootstrapper.h" 32 #include "bootstrapper.h"
33 #include "compiler.h" 33 #include "compiler.h"
34 #include "messages.h"
34 #include "scopeinfo.h" 35 #include "scopeinfo.h"
35 36
36 #include "allocation-inl.h" 37 #include "allocation-inl.h"
37 38
38 namespace v8 { 39 namespace v8 {
39 namespace internal { 40 namespace internal {
40 41
41 // ---------------------------------------------------------------------------- 42 // ----------------------------------------------------------------------------
42 // A Zone allocator for use with LocalsMap. 43 // A Zone allocator for use with LocalsMap.
43 44
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after
277 top->AllocateVariables(info->global_scope()); 278 top->AllocateVariables(info->global_scope());
278 279
279 #ifdef DEBUG 280 #ifdef DEBUG
280 if (info->isolate()->bootstrapper()->IsActive() 281 if (info->isolate()->bootstrapper()->IsActive()
281 ? FLAG_print_builtin_scopes 282 ? FLAG_print_builtin_scopes
282 : FLAG_print_scopes) { 283 : FLAG_print_scopes) {
283 scope->Print(); 284 scope->Print();
284 } 285 }
285 #endif 286 #endif
286 287
288 if (FLAG_harmony_scoping) {
289 VariableProxy* proxy = scope->CheckAssignmentToConst();
290 if (proxy != NULL) {
291 // Found an assignment to const. Throw a syntax error.
292 MessageLocation location(info->script(),
293 proxy->position(),
294 proxy->position());
295 Isolate* isolate = info->isolate();
296 Factory* factory = isolate->factory();
297 Handle<JSArray> array = factory->NewJSArray(0);
298 Handle<Object> result =
299 factory->NewSyntaxError("harmony_const_assign", array);
300 isolate->Throw(*result, &location);
301 return false;
302 }
303 }
304
287 info->SetScope(scope); 305 info->SetScope(scope);
288 return true; // Can not fail. 306 return true;
289 } 307 }
290 308
291 309
292 void Scope::Initialize() { 310 void Scope::Initialize() {
293 ASSERT(!already_resolved()); 311 ASSERT(!already_resolved());
294 312
295 // Add this scope as a new inner scope of the outer scope. 313 // Add this scope as a new inner scope of the outer scope.
296 if (outer_scope_ != NULL) { 314 if (outer_scope_ != NULL) {
297 outer_scope_->inner_scopes_.Add(this); 315 outer_scope_->inner_scopes_.Add(this);
298 scope_inside_with_ = outer_scope_->scope_inside_with_ || is_with_scope(); 316 scope_inside_with_ = outer_scope_->scope_inside_with_ || is_with_scope();
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after
547 return decl; 565 return decl;
548 } 566 }
549 previous = current; 567 previous = current;
550 current = current->outer_scope_; 568 current = current->outer_scope_;
551 } while (!previous->is_declaration_scope()); 569 } while (!previous->is_declaration_scope());
552 } 570 }
553 return NULL; 571 return NULL;
554 } 572 }
555 573
556 574
575 VariableProxy* Scope::CheckAssignmentToConst() {
576 // Check this scope.
577 if (is_extended_mode()) {
578 for (int i = 0; i < unresolved_.length(); i++) {
579 ASSERT(unresolved_[i]->var() != NULL);
580 if (unresolved_[i]->var()->is_const_mode() &&
581 unresolved_[i]->IsLValue()) {
582 return unresolved_[i];
583 }
584 }
585 }
586
587 // Check inner scopes.
588 for (int i = 0; i < inner_scopes_.length(); i++) {
589 VariableProxy* proxy = inner_scopes_[i]->CheckAssignmentToConst();
590 if (proxy != NULL) return proxy;
591 }
592
593 // No assignments to const found.
594 return NULL;
595 }
596
597
557 void Scope::CollectStackAndContextLocals(ZoneList<Variable*>* stack_locals, 598 void Scope::CollectStackAndContextLocals(ZoneList<Variable*>* stack_locals,
558 ZoneList<Variable*>* context_locals) { 599 ZoneList<Variable*>* context_locals) {
559 ASSERT(stack_locals != NULL); 600 ASSERT(stack_locals != NULL);
560 ASSERT(context_locals != NULL); 601 ASSERT(context_locals != NULL);
561 602
562 // Collect temporaries which are always allocated on the stack. 603 // Collect temporaries which are always allocated on the stack.
563 for (int i = 0; i < temps_.length(); i++) { 604 for (int i = 0; i < temps_.length(); i++) {
564 Variable* var = temps_[i]; 605 Variable* var = temps_[i];
565 if (var->is_used()) { 606 if (var->is_used()) {
566 ASSERT(var->IsStackLocal()); 607 ASSERT(var->IsStackLocal());
(...skipping 622 matching lines...) Expand 10 before | Expand all | Expand 10 after
1189 } 1230 }
1190 1231
1191 1232
1192 int Scope::ContextLocalCount() const { 1233 int Scope::ContextLocalCount() const {
1193 if (num_heap_slots() == 0) return 0; 1234 if (num_heap_slots() == 0) return 0;
1194 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - 1235 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS -
1195 (function_ != NULL && function_->var()->IsContextSlot() ? 1 : 0); 1236 (function_ != NULL && function_->var()->IsContextSlot() ? 1 : 0);
1196 } 1237 }
1197 1238
1198 } } // namespace v8::internal 1239 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/scopes.h ('k') | test/mjsunit/harmony/block-const-assign.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698