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

Side by Side Diff: src/ast/ast.cc

Issue 2445333002: Ensure slow properties for simple {__proto__:null} literals. (Closed)
Patch Set: fixing compilation issue Created 3 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/ast/ast.h ('k') | src/ast/compile-time-value.h » ('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/ast/ast.h" 5 #include "src/ast/ast.h"
6 6
7 #include <cmath> // For isfinite. 7 #include <cmath> // For isfinite.
8 8
9 #include "src/ast/compile-time-value.h" 9 #include "src/ast/compile-time-value.h"
10 #include "src/ast/prettyprinter.h" 10 #include "src/ast/prettyprinter.h"
(...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after
483 property->SetSlot(spec->AddStoreICSlot(language_mode)); 483 property->SetSlot(spec->AddStoreICSlot(language_mode));
484 } 484 }
485 break; 485 break;
486 } 486 }
487 } 487 }
488 488
489 for (; property_index < properties()->length(); property_index++) { 489 for (; property_index < properties()->length(); property_index++) {
490 ObjectLiteral::Property* property = properties()->at(property_index); 490 ObjectLiteral::Property* property = properties()->at(property_index);
491 491
492 Expression* value = property->value(); 492 Expression* value = property->value();
493 if (property->kind() != ObjectLiteral::Property::PROTOTYPE) { 493 if (!property->IsPrototype()) {
494 if (FunctionLiteral::NeedsHomeObject(value)) { 494 if (FunctionLiteral::NeedsHomeObject(value)) {
495 property->SetSlot(spec->AddStoreICSlot(language_mode)); 495 property->SetSlot(spec->AddStoreICSlot(language_mode));
496 } 496 }
497 } 497 }
498 property->SetStoreDataPropertySlot( 498 property->SetStoreDataPropertySlot(
499 spec->AddStoreDataPropertyInLiteralICSlot()); 499 spec->AddStoreDataPropertyInLiteralICSlot());
500 } 500 }
501 } 501 }
502 502
503 503
504 void ObjectLiteral::CalculateEmitStore(Zone* zone) { 504 void ObjectLiteral::CalculateEmitStore(Zone* zone) {
505 const auto GETTER = ObjectLiteral::Property::GETTER; 505 const auto GETTER = ObjectLiteral::Property::GETTER;
506 const auto SETTER = ObjectLiteral::Property::SETTER; 506 const auto SETTER = ObjectLiteral::Property::SETTER;
507 507
508 ZoneAllocationPolicy allocator(zone); 508 ZoneAllocationPolicy allocator(zone);
509 509
510 CustomMatcherZoneHashMap table( 510 CustomMatcherZoneHashMap table(
511 Literal::Match, ZoneHashMap::kDefaultHashMapCapacity, allocator); 511 Literal::Match, ZoneHashMap::kDefaultHashMapCapacity, allocator);
512 for (int i = properties()->length() - 1; i >= 0; i--) { 512 for (int i = properties()->length() - 1; i >= 0; i--) {
513 ObjectLiteral::Property* property = properties()->at(i); 513 ObjectLiteral::Property* property = properties()->at(i);
514 if (property->is_computed_name()) continue; 514 if (property->is_computed_name()) continue;
515 if (property->kind() == ObjectLiteral::Property::PROTOTYPE) continue; 515 if (property->IsPrototype()) continue;
516 Literal* literal = property->key()->AsLiteral(); 516 Literal* literal = property->key()->AsLiteral();
517 DCHECK(!literal->IsNullLiteral()); 517 DCHECK(!literal->IsNullLiteral());
518 518
519 // If there is an existing entry do not emit a store unless the previous 519 // If there is an existing entry do not emit a store unless the previous
520 // entry was also an accessor. 520 // entry was also an accessor.
521 uint32_t hash = literal->Hash(); 521 uint32_t hash = literal->Hash();
522 ZoneHashMap::Entry* entry = table.LookupOrInsert(literal, hash, allocator); 522 ZoneHashMap::Entry* entry = table.LookupOrInsert(literal, hash, allocator);
523 if (entry->value != NULL) { 523 if (entry->value != NULL) {
524 auto previous_kind = 524 auto previous_kind =
525 static_cast<ObjectLiteral::Property*>(entry->value)->kind(); 525 static_cast<ObjectLiteral::Property*>(entry->value)->kind();
526 if (!((property->kind() == GETTER && previous_kind == SETTER) || 526 if (!((property->kind() == GETTER && previous_kind == SETTER) ||
527 (property->kind() == SETTER && previous_kind == GETTER))) { 527 (property->kind() == SETTER && previous_kind == GETTER))) {
528 property->set_emit_store(false); 528 property->set_emit_store(false);
529 } 529 }
530 } 530 }
531 entry->value = property; 531 entry->value = property;
532 } 532 }
533 } 533 }
534 534
535 535 void ObjectLiteral::InitFlagsForPendingNullPrototype(int i) {
536 bool ObjectLiteral::IsBoilerplateProperty(ObjectLiteral::Property* property) { 536 // We still check for __proto__:null after computed property names.
537 return property != NULL && 537 for (; i < properties()->length(); i++) {
538 property->kind() != ObjectLiteral::Property::PROTOTYPE; 538 if (properties()->at(i)->IsNullPrototype()) {
539 set_has_null_protoype(true);
540 break;
541 }
542 }
539 } 543 }
540 544
541 void ObjectLiteral::InitDepthAndFlags() { 545 void ObjectLiteral::InitDepthAndFlags() {
542 if (depth_ > 0) return; 546 if (is_initialized()) return;
543
544 int position = 0;
545 // Accumulate the value in local variables and store it at the end.
546 bool is_simple = true; 547 bool is_simple = true;
548 bool has_seen_prototype = false;
547 int depth_acc = 1; 549 int depth_acc = 1;
550 uint32_t nof_properties = 0;
551 uint32_t elements = 0;
548 uint32_t max_element_index = 0; 552 uint32_t max_element_index = 0;
549 uint32_t elements = 0;
550 for (int i = 0; i < properties()->length(); i++) { 553 for (int i = 0; i < properties()->length(); i++) {
551 ObjectLiteral::Property* property = properties()->at(i); 554 ObjectLiteral::Property* property = properties()->at(i);
552 if (!IsBoilerplateProperty(property)) { 555 if (property->IsPrototype()) {
556 has_seen_prototype = true;
557 // __proto__:null has no side-effects and is set directly on the
558 // boilerplate.
559 if (property->IsNullPrototype()) {
560 set_has_null_protoype(true);
561 continue;
562 }
563 DCHECK(!has_null_prototype());
553 is_simple = false; 564 is_simple = false;
554 continue; 565 continue;
555 } 566 }
556 567 if (nof_properties == boilerplate_properties_) {
557 if (static_cast<uint32_t>(position) == boilerplate_properties_ * 2) {
558 DCHECK(property->is_computed_name()); 568 DCHECK(property->is_computed_name());
559 is_simple = false; 569 is_simple = false;
570 if (!has_seen_prototype) InitFlagsForPendingNullPrototype(i);
560 break; 571 break;
561 } 572 }
562 DCHECK(!property->is_computed_name()); 573 DCHECK(!property->is_computed_name());
563 574
564 MaterializedLiteral* m_literal = property->value()->AsMaterializedLiteral(); 575 MaterializedLiteral* m_literal = property->value()->AsMaterializedLiteral();
565 if (m_literal != NULL) { 576 if (m_literal != NULL) {
566 m_literal->InitDepthAndFlags(); 577 m_literal->InitDepthAndFlags();
567 if (m_literal->depth() >= depth_acc) depth_acc = m_literal->depth() + 1; 578 if (m_literal->depth() >= depth_acc) depth_acc = m_literal->depth() + 1;
568 } 579 }
569 580
(...skipping 19 matching lines...) Expand all
589 // literal with fast elements will be a waste of space. 600 // literal with fast elements will be a waste of space.
590 uint32_t element_index = 0; 601 uint32_t element_index = 0;
591 if (key->IsString() && key->AsString()->AsArrayIndex(&element_index)) { 602 if (key->IsString() && key->AsString()->AsArrayIndex(&element_index)) {
592 max_element_index = Max(element_index, max_element_index); 603 max_element_index = Max(element_index, max_element_index);
593 elements++; 604 elements++;
594 } else if (key->ToUint32(&element_index) && element_index != kMaxUInt32) { 605 } else if (key->ToUint32(&element_index) && element_index != kMaxUInt32) {
595 max_element_index = Max(element_index, max_element_index); 606 max_element_index = Max(element_index, max_element_index);
596 elements++; 607 elements++;
597 } 608 }
598 609
599 // Increment the position for the key and the value. 610 nof_properties++;
600 position += 2;
601 } 611 }
602 612
603 bit_field_ = FastElementsField::update( 613 set_fast_elements((max_element_index <= 32) ||
604 bit_field_, 614 ((2 * elements) >= max_element_index));
605 (max_element_index <= 32) || ((2 * elements) >= max_element_index)); 615 set_has_elements(elements > 0);
606 bit_field_ = HasElementsField::update(bit_field_, elements > 0);
607
608 set_is_simple(is_simple); 616 set_is_simple(is_simple);
609 set_depth(depth_acc); 617 set_depth(depth_acc);
610 } 618 }
611 619
612 void ObjectLiteral::BuildConstantProperties(Isolate* isolate) { 620 void ObjectLiteral::BuildConstantProperties(Isolate* isolate) {
613 if (!constant_properties_.is_null()) return; 621 if (!constant_properties_.is_null()) return;
614 622
615 int index_keys = 0; 623 int index_keys = 0;
616 bool has_seen_proto = false; 624 bool has_seen_proto = false;
617 for (int i = 0; i < properties()->length(); i++) { 625 for (int i = 0; i < properties()->length(); i++) {
618 ObjectLiteral::Property* property = properties()->at(i); 626 ObjectLiteral::Property* property = properties()->at(i);
619 if (!IsBoilerplateProperty(property)) { 627 if (property->IsPrototype()) {
620 has_seen_proto = true; 628 has_seen_proto = true;
621 continue; 629 continue;
622 } 630 }
623 if (property->is_computed_name()) { 631 if (property->is_computed_name()) {
624 continue; 632 continue;
625 } 633 }
626 634
627 Handle<Object> key = property->key()->AsLiteral()->value(); 635 Handle<Object> key = property->key()->AsLiteral()->value();
628 636
629 uint32_t element_index = 0; 637 uint32_t element_index = 0;
630 if (key->ToArrayIndex(&element_index) || 638 if (key->ToArrayIndex(&element_index) ||
631 (key->IsString() && String::cast(*key)->AsArrayIndex(&element_index))) { 639 (key->IsString() && String::cast(*key)->AsArrayIndex(&element_index))) {
632 index_keys++; 640 index_keys++;
633 } 641 }
634 } 642 }
635 643
636 Handle<BoilerplateDescription> constant_properties = 644 Handle<BoilerplateDescription> constant_properties =
637 isolate->factory()->NewBoilerplateDescription(boilerplate_properties_, 645 isolate->factory()->NewBoilerplateDescription(boilerplate_properties_,
638 properties()->length(), 646 properties()->length(),
639 index_keys, has_seen_proto); 647 index_keys, has_seen_proto);
640 648
641 int position = 0; 649 int position = 0;
642 for (int i = 0; i < properties()->length(); i++) { 650 for (int i = 0; i < properties()->length(); i++) {
643 ObjectLiteral::Property* property = properties()->at(i); 651 ObjectLiteral::Property* property = properties()->at(i);
644 if (!IsBoilerplateProperty(property)) { 652 if (property->IsPrototype()) continue;
645 continue;
646 }
647 653
648 if (static_cast<uint32_t>(position) == boilerplate_properties_ * 2) { 654 if (static_cast<uint32_t>(position) == boilerplate_properties_ * 2) {
649 DCHECK(property->is_computed_name()); 655 DCHECK(property->is_computed_name());
650 break; 656 break;
651 } 657 }
652 DCHECK(!property->is_computed_name()); 658 DCHECK(!property->is_computed_name());
653 659
654 MaterializedLiteral* m_literal = property->value()->AsMaterializedLiteral(); 660 MaterializedLiteral* m_literal = property->value()->AsMaterializedLiteral();
655 if (m_literal != NULL) { 661 if (m_literal != NULL) {
656 m_literal->BuildConstants(isolate); 662 m_literal->BuildConstants(isolate);
(...skipping 29 matching lines...) Expand all
686 ConstructorBuiltins::kMaximumClonedShallowObjectProperties; 692 ConstructorBuiltins::kMaximumClonedShallowObjectProperties;
687 } 693 }
688 694
689 ElementsKind ArrayLiteral::constant_elements_kind() const { 695 ElementsKind ArrayLiteral::constant_elements_kind() const {
690 return static_cast<ElementsKind>(constant_elements()->elements_kind()); 696 return static_cast<ElementsKind>(constant_elements()->elements_kind());
691 } 697 }
692 698
693 void ArrayLiteral::InitDepthAndFlags() { 699 void ArrayLiteral::InitDepthAndFlags() {
694 DCHECK_LT(first_spread_index_, 0); 700 DCHECK_LT(first_spread_index_, 0);
695 701
696 if (depth_ > 0) return; 702 if (is_initialized()) return;
697 703
698 int constants_length = values()->length(); 704 int constants_length = values()->length();
699 705
700 // Fill in the literals. 706 // Fill in the literals.
701 bool is_simple = true; 707 bool is_simple = true;
702 int depth_acc = 1; 708 int depth_acc = 1;
703 int array_index = 0; 709 int array_index = 0;
704 for (; array_index < constants_length; array_index++) { 710 for (; array_index < constants_length; array_index++) {
705 Expression* element = values()->at(array_index); 711 Expression* element = values()->at(array_index);
706 DCHECK(!element->IsSpread()); 712 DCHECK(!element->IsSpread());
(...skipping 428 matching lines...) Expand 10 before | Expand all | Expand 10 after
1135 #ifdef DEBUG 1141 #ifdef DEBUG
1136 return is_jsruntime() ? NameForNativeContextIntrinsicIndex(context_index_) 1142 return is_jsruntime() ? NameForNativeContextIntrinsicIndex(context_index_)
1137 : function_->name; 1143 : function_->name;
1138 #else 1144 #else
1139 return is_jsruntime() ? "(context function)" : function_->name; 1145 return is_jsruntime() ? "(context function)" : function_->name;
1140 #endif // DEBUG 1146 #endif // DEBUG
1141 } 1147 }
1142 1148
1143 } // namespace internal 1149 } // namespace internal
1144 } // namespace v8 1150 } // namespace v8
OLDNEW
« no previous file with comments | « src/ast/ast.h ('k') | src/ast/compile-time-value.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698