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

Side by Side Diff: src/objects.cc

Issue 10238005: Use map transitions when defining accessor properties. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Addressed comments Created 8 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 | Annotate | Revision Log
« no previous file with comments | « src/objects.h ('k') | test/mjsunit/accessor-map-sharing.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 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 4395 matching lines...) Expand 10 before | Expand all | Expand 10 after
4406 accessors->SetComponents(getter, setter); 4406 accessors->SetComponents(getter, setter);
4407 4407
4408 return SetElementCallback(index, accessors, attributes); 4408 return SetElementCallback(index, accessors, attributes);
4409 } 4409 }
4410 4410
4411 4411
4412 MaybeObject* JSObject::CreateAccessorPairFor(String* name) { 4412 MaybeObject* JSObject::CreateAccessorPairFor(String* name) {
4413 LookupResult result(GetHeap()->isolate()); 4413 LookupResult result(GetHeap()->isolate());
4414 LocalLookupRealNamedProperty(name, &result); 4414 LocalLookupRealNamedProperty(name, &result);
4415 if (result.IsProperty() && result.type() == CALLBACKS) { 4415 if (result.IsProperty() && result.type() == CALLBACKS) {
4416 ASSERT(!result.IsDontDelete()); 4416 // Note that the result can actually have IsDontDelete() == true when we
4417 // e.g. have to fall back to the slow case while adding a setter after
4418 // successfully reusing a map transition for a getter. Nevertheless, this is
4419 // OK, because the assertion only holds for the whole addition of both
4420 // accessors, not for the addition of each part.
Michael Starzinger 2012/05/03 12:23:34 It would really be nice to preserve this assertion
4417 Object* obj = result.GetCallbackObject(); 4421 Object* obj = result.GetCallbackObject();
4418 if (obj->IsAccessorPair()) { 4422 if (obj->IsAccessorPair()) {
4419 return AccessorPair::cast(obj)->CopyWithoutTransitions(); 4423 return AccessorPair::cast(obj)->CopyWithoutTransitions();
4420 } 4424 }
4421 } 4425 }
4422 return GetHeap()->AllocateAccessorPair(); 4426 return GetHeap()->AllocateAccessorPair();
4423 } 4427 }
4424 4428
4425 4429
4426 MaybeObject* JSObject::DefinePropertyAccessor(String* name, 4430 MaybeObject* JSObject::DefinePropertyAccessor(String* name,
4427 Object* getter, 4431 Object* getter,
4428 Object* setter, 4432 Object* setter,
4429 PropertyAttributes attributes) { 4433 PropertyAttributes attributes) {
4434 Heap* heap = GetHeap();
4435 bool only_attribute_changes = getter->IsNull() && setter->IsNull();
4436 if (HasFastProperties() && !only_attribute_changes) {
4437 MaybeObject* getterOk = heap->undefined_value();
4438 if (!getter->IsNull()) {
4439 getterOk = DefineFastAccessor(name, ACCESSOR_GETTER, getter, attributes);
4440 if (getterOk->IsFailure()) return getterOk;
4441 }
4442
4443 MaybeObject* setterOk = heap->undefined_value();
4444 if (getterOk != heap->null_value() && !setter->IsNull()) {
4445 setterOk = DefineFastAccessor(name, ACCESSOR_SETTER, setter, attributes);
4446 if (setterOk->IsFailure()) return setterOk;
4447 }
4448
4449 if (getterOk != heap->null_value() && setterOk != heap->null_value()) {
4450 return heap->undefined_value();
4451 }
4452 }
4453
4430 AccessorPair* accessors; 4454 AccessorPair* accessors;
4431 { MaybeObject* maybe_accessors = CreateAccessorPairFor(name); 4455 { MaybeObject* maybe_accessors = CreateAccessorPairFor(name);
4432 if (!maybe_accessors->To(&accessors)) return maybe_accessors; 4456 if (!maybe_accessors->To(&accessors)) return maybe_accessors;
4433 } 4457 }
4434 accessors->SetComponents(getter, setter); 4458 accessors->SetComponents(getter, setter);
4435 return SetPropertyCallback(name, accessors, attributes); 4459 return SetPropertyCallback(name, accessors, attributes);
4436 } 4460 }
4437 4461
4438 4462
4439 bool JSObject::CanSetCallback(String* name) { 4463 bool JSObject::CanSetCallback(String* name) {
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
4569 4593
4570 if (!CanSetCallback(name)) return isolate->heap()->undefined_value(); 4594 if (!CanSetCallback(name)) return isolate->heap()->undefined_value();
4571 4595
4572 uint32_t index = 0; 4596 uint32_t index = 0;
4573 return name->AsArrayIndex(&index) ? 4597 return name->AsArrayIndex(&index) ?
4574 DefineElementAccessor(index, getter, setter, attributes) : 4598 DefineElementAccessor(index, getter, setter, attributes) :
4575 DefinePropertyAccessor(name, getter, setter, attributes); 4599 DefinePropertyAccessor(name, getter, setter, attributes);
4576 } 4600 }
4577 4601
4578 4602
4603 static MaybeObject* CreateFreshAccessor(JSObject* obj,
4604 String* name,
4605 AccessorComponent component,
4606 Object* accessor,
4607 PropertyAttributes attributes) {
4608 // step 1: create a new getter/setter pair with only the accessor in it
4609 Heap* heap = obj->GetHeap();
4610 AccessorPair* accessors2;
4611 { MaybeObject* maybe_accessors2 = heap->AllocateAccessorPair();
4612 if (!maybe_accessors2->To(&accessors2)) return maybe_accessors2;
4613 }
4614 accessors2->set(component, accessor);
4615
4616 // step 2: create a copy of the descriptors, incl. the new getter/setter pair
4617 Map* map1 = obj->map();
4618 CallbacksDescriptor callbacks_descr2(name, accessors2, attributes);
4619 DescriptorArray* descriptors2;
4620 { MaybeObject* maybe_descriptors2 =
4621 map1->instance_descriptors()->CopyInsert(&callbacks_descr2,
4622 REMOVE_TRANSITIONS);
4623 if (!maybe_descriptors2->To(&descriptors2)) return maybe_descriptors2;
4624 }
4625
4626 // step 3: create a new map with the new descriptors
4627 Map* map2;
4628 { MaybeObject* maybe_map2 = map1->CopyDropDescriptors();
4629 if (!maybe_map2->To(&map2)) return maybe_map2;
4630 }
4631 map2->set_instance_descriptors(descriptors2);
4632
4633 // step 4: create a new getter/setter pair with a transition to the new map
4634 AccessorPair* accessors1;
4635 { MaybeObject* maybe_accessors1 = heap->AllocateAccessorPair();
4636 if (!maybe_accessors1->To(&accessors1)) return maybe_accessors1;
4637 }
4638 accessors1->set(component, map2);
4639
4640 // step 5: create a copy of the descriptors, incl. the new getter/setter pair
4641 // with the transition
4642 CallbacksDescriptor callbacks_descr1(name, accessors1, attributes);
4643 DescriptorArray* descriptors1;
4644 { MaybeObject* maybe_descriptors1 =
4645 map1->instance_descriptors()->CopyInsert(&callbacks_descr1,
4646 KEEP_TRANSITIONS);
4647 if (!maybe_descriptors1->To(&descriptors1)) return maybe_descriptors1;
4648 }
4649
4650 // step 6: everything went well so far, so we make our changes visible
4651 obj->set_map(map2);
4652 map1->set_instance_descriptors(descriptors1);
4653 return obj;
4654 }
4655
4656 static bool TransitionToSameAccessor(Object* map,
4657 String* name,
4658 AccessorComponent component,
4659 Object* accessor,
4660 PropertyAttributes attributes ) {
4661 DescriptorArray* descs = Map::cast(map)->instance_descriptors();
4662 int number = descs->SearchWithCache(name);
4663 ASSERT(number != DescriptorArray::kNotFound);
4664 Object* target_accessor =
4665 AccessorPair::cast(descs->GetCallbacksObject(number))->get(component);
4666 PropertyAttributes target_attributes = descs->GetDetails(number).attributes();
4667 return target_accessor == accessor && target_attributes == attributes;
4668 }
4669
4670
4671 static MaybeObject* NewCallbackTransition(JSObject* obj,
4672 String* name,
4673 AccessorComponent component,
4674 Object* accessor,
4675 PropertyAttributes attributes,
4676 AccessorPair* accessors2) {
4677 // step 1: copy the old getter/setter pair and set the new accessor
4678 AccessorPair* accessors3;
4679 { MaybeObject* maybe_accessors3 = accessors2->CopyWithoutTransitions();
4680 if (!maybe_accessors3->To(&accessors3)) return maybe_accessors3;
4681 }
4682 accessors3->set(component, accessor);
4683
4684 // step 2: create a copy of the descriptors, incl. the new getter/setter pair
4685 Map* map2 = obj->map();
4686 CallbacksDescriptor callbacks_descr3(name, accessors3, attributes);
4687 DescriptorArray* descriptors3;
4688 { MaybeObject* maybe_descriptors3 =
4689 map2->instance_descriptors()->CopyInsert(&callbacks_descr3,
4690 REMOVE_TRANSITIONS);
4691 if (!maybe_descriptors3->To(&descriptors3)) return maybe_descriptors3;
4692 }
4693
4694 // step 3: create a new map with the new descriptors
4695 Map* map3;
4696 { MaybeObject* maybe_map3 = map2->CopyDropDescriptors();
4697 if (!maybe_map3->To(&map3)) return maybe_map3;
4698 }
4699 map3->set_instance_descriptors(descriptors3);
4700
4701 // step 4: everything went well so far, so we make our changes visible
4702 obj->set_map(map3);
4703 accessors2->set(component, map3);
4704 return obj;
4705 }
4706
4707
4708 MaybeObject* JSObject::DefineFastAccessor(String* name,
4709 AccessorComponent component,
4710 Object* accessor,
4711 PropertyAttributes attributes) {
4712 ASSERT(accessor->IsSpecFunction() || accessor->IsUndefined());
4713 LookupResult result(GetIsolate());
4714 LocalLookup(name, &result);
4715
4716 // If we have a new property, create a fresh accessor plus a transition to it.
4717 if (!result.IsFound()) {
4718 return CreateFreshAccessor(this, name, component, accessor, attributes);
4719 }
4720
4721 // If the property is not a JavaScript accessor, fall back to the slow case.
4722 if (result.type() != CALLBACKS) return GetHeap()->null_value();
4723 Object* callback_value = result.GetValue();
4724 if (!callback_value->IsAccessorPair()) return GetHeap()->null_value();
4725 AccessorPair* accessors = AccessorPair::cast(callback_value);
4726
4727 // Follow a callback transition, if there is a fitting one.
4728 Object* entry = accessors->get(component);
4729 if (entry->IsMap() &&
4730 TransitionToSameAccessor(entry, name, component, accessor, attributes)) {
4731 set_map(Map::cast(entry));
4732 return this;
4733 }
4734
4735 // When we re-add the same accessor again, there is nothing to do.
4736 if (entry == accessor && result.GetAttributes() == attributes) return this;
4737
4738 // Only the other accessor has been set so far, create a new transition.
4739 if (entry->IsTheHole()) {
4740 return NewCallbackTransition(this,
4741 name,
4742 component,
4743 accessor,
4744 attributes,
4745 accessors);
4746 }
4747
4748 // Nothing from the above worked, so we have to fall back to the slow case.
4749 return GetHeap()->null_value();
4750 }
4751
4752
4579 MaybeObject* JSObject::DefineAccessor(AccessorInfo* info) { 4753 MaybeObject* JSObject::DefineAccessor(AccessorInfo* info) {
4580 Isolate* isolate = GetIsolate(); 4754 Isolate* isolate = GetIsolate();
4581 String* name = String::cast(info->name()); 4755 String* name = String::cast(info->name());
4582 // Check access rights if needed. 4756 // Check access rights if needed.
4583 if (IsAccessCheckNeeded() && 4757 if (IsAccessCheckNeeded() &&
4584 !isolate->MayNamedAccess(this, name, v8::ACCESS_SET)) { 4758 !isolate->MayNamedAccess(this, name, v8::ACCESS_SET)) {
4585 isolate->ReportFailedAccessCheck(this, v8::ACCESS_SET); 4759 isolate->ReportFailedAccessCheck(this, v8::ACCESS_SET);
4586 return isolate->heap()->undefined_value(); 4760 return isolate->heap()->undefined_value();
4587 } 4761 }
4588 4762
(...skipping 1343 matching lines...) Expand 10 before | Expand all | Expand 10 after
5932 { MaybeObject* maybe_copy = heap->AllocateAccessorPair(); 6106 { MaybeObject* maybe_copy = heap->AllocateAccessorPair();
5933 if (!maybe_copy->To(&copy)) return maybe_copy; 6107 if (!maybe_copy->To(&copy)) return maybe_copy;
5934 } 6108 }
5935 copy->set_getter(getter()->IsMap() ? heap->the_hole_value() : getter()); 6109 copy->set_getter(getter()->IsMap() ? heap->the_hole_value() : getter());
5936 copy->set_setter(setter()->IsMap() ? heap->the_hole_value() : setter()); 6110 copy->set_setter(setter()->IsMap() ? heap->the_hole_value() : setter());
5937 return copy; 6111 return copy;
5938 } 6112 }
5939 6113
5940 6114
5941 Object* AccessorPair::GetComponent(AccessorComponent component) { 6115 Object* AccessorPair::GetComponent(AccessorComponent component) {
5942 Object* accessor = (component == ACCESSOR_GETTER) ? getter() : setter(); 6116 Object* accessor = get(component);
5943 return accessor->IsTheHole() ? GetHeap()->undefined_value() : accessor; 6117 return accessor->IsTheHole() ? GetHeap()->undefined_value() : accessor;
5944 } 6118 }
5945 6119
5946 6120
5947 MaybeObject* DeoptimizationInputData::Allocate(int deopt_entry_count, 6121 MaybeObject* DeoptimizationInputData::Allocate(int deopt_entry_count,
5948 PretenureFlag pretenure) { 6122 PretenureFlag pretenure) {
5949 ASSERT(deopt_entry_count > 0); 6123 ASSERT(deopt_entry_count > 0);
5950 return HEAP->AllocateFixedArray(LengthFor(deopt_entry_count), 6124 return HEAP->AllocateFixedArray(LengthFor(deopt_entry_count),
5951 pretenure); 6125 pretenure);
5952 } 6126 }
5953 6127
(...skipping 7013 matching lines...) Expand 10 before | Expand all | Expand 10 after
12967 set_year(Smi::FromInt(year), SKIP_WRITE_BARRIER); 13141 set_year(Smi::FromInt(year), SKIP_WRITE_BARRIER);
12968 set_month(Smi::FromInt(month), SKIP_WRITE_BARRIER); 13142 set_month(Smi::FromInt(month), SKIP_WRITE_BARRIER);
12969 set_day(Smi::FromInt(day), SKIP_WRITE_BARRIER); 13143 set_day(Smi::FromInt(day), SKIP_WRITE_BARRIER);
12970 set_weekday(Smi::FromInt(weekday), SKIP_WRITE_BARRIER); 13144 set_weekday(Smi::FromInt(weekday), SKIP_WRITE_BARRIER);
12971 set_hour(Smi::FromInt(hour), SKIP_WRITE_BARRIER); 13145 set_hour(Smi::FromInt(hour), SKIP_WRITE_BARRIER);
12972 set_min(Smi::FromInt(min), SKIP_WRITE_BARRIER); 13146 set_min(Smi::FromInt(min), SKIP_WRITE_BARRIER);
12973 set_sec(Smi::FromInt(sec), SKIP_WRITE_BARRIER); 13147 set_sec(Smi::FromInt(sec), SKIP_WRITE_BARRIER);
12974 } 13148 }
12975 13149
12976 } } // namespace v8::internal 13150 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/objects.h ('k') | test/mjsunit/accessor-map-sharing.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698