OLD | NEW |
---|---|
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 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 <iomanip> | 5 #include <iomanip> |
6 #include <sstream> | 6 #include <sstream> |
7 | 7 |
8 #include "src/v8.h" | 8 #include "src/v8.h" |
9 | 9 |
10 #include "src/accessors.h" | 10 #include "src/accessors.h" |
(...skipping 1870 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1881 void Map::ConnectElementsTransition(Handle<Map> parent, Handle<Map> child) { | 1881 void Map::ConnectElementsTransition(Handle<Map> parent, Handle<Map> child) { |
1882 Isolate* isolate = parent->GetIsolate(); | 1882 Isolate* isolate = parent->GetIsolate(); |
1883 Handle<Name> name = isolate->factory()->elements_transition_symbol(); | 1883 Handle<Name> name = isolate->factory()->elements_transition_symbol(); |
1884 ConnectTransition(parent, child, name, SPECIAL_TRANSITION); | 1884 ConnectTransition(parent, child, name, SPECIAL_TRANSITION); |
1885 } | 1885 } |
1886 | 1886 |
1887 | 1887 |
1888 void JSObject::MigrateToMap(Handle<JSObject> object, Handle<Map> new_map, | 1888 void JSObject::MigrateToMap(Handle<JSObject> object, Handle<Map> new_map, |
1889 int expected_additional_properties) { | 1889 int expected_additional_properties) { |
1890 if (object->map() == *new_map) return; | 1890 if (object->map() == *new_map) return; |
1891 // If this object is a prototype (the callee will check), invalidate any | |
1892 // prototype chains involving it. | |
1893 InvalidatePrototypeChains(object->map()); | |
1891 Handle<Map> old_map(object->map()); | 1894 Handle<Map> old_map(object->map()); |
1892 if (object->HasFastProperties()) { | 1895 if (object->HasFastProperties()) { |
1893 if (!new_map->is_dictionary_map()) { | 1896 if (!new_map->is_dictionary_map()) { |
1894 MigrateFastToFast(object, new_map); | 1897 MigrateFastToFast(object, new_map); |
1895 if (old_map->is_prototype_map()) { | 1898 if (old_map->is_prototype_map()) { |
1896 // Clear out the old descriptor array to avoid problems to sharing | 1899 // Clear out the old descriptor array to avoid problems to sharing |
1897 // the descriptor array without using an explicit. | 1900 // the descriptor array without using an explicit. |
1898 old_map->InitializeDescriptors( | 1901 old_map->InitializeDescriptors( |
1899 old_map->GetHeap()->empty_descriptor_array(), | 1902 old_map->GetHeap()->empty_descriptor_array(), |
1900 LayoutDescriptor::FastPointerLayout()); | 1903 LayoutDescriptor::FastPointerLayout()); |
1901 // Ensure that no transition was inserted for prototype migrations. | 1904 // Ensure that no transition was inserted for prototype migrations. |
1902 DCHECK_EQ(0, TransitionArray::NumberOfTransitions( | 1905 DCHECK_EQ(0, TransitionArray::NumberOfTransitions( |
1903 old_map->raw_transitions())); | 1906 old_map->raw_transitions())); |
1904 DCHECK(new_map->GetBackPointer()->IsUndefined()); | 1907 DCHECK(new_map->GetBackPointer()->IsUndefined()); |
1905 } | 1908 } |
1906 } else { | 1909 } else { |
1907 MigrateFastToSlow(object, new_map, expected_additional_properties); | 1910 MigrateFastToSlow(object, new_map, expected_additional_properties); |
1908 } | 1911 } |
1909 } else { | 1912 } else { |
1910 // For slow-to-fast migrations JSObject::MigrateSlowToFast() | 1913 // For slow-to-fast migrations JSObject::MigrateSlowToFast() |
1911 // must be used instead. | 1914 // must be used instead. |
1912 CHECK(new_map->is_dictionary_map()); | 1915 CHECK(new_map->is_dictionary_map()); |
1913 | 1916 |
1914 // Slow-to-slow migration is trivial. | 1917 // Slow-to-slow migration is trivial. |
1915 object->set_map(*new_map); | 1918 object->set_map(*new_map); |
1916 } | 1919 } |
1917 if (old_map->is_prototype_map()) { | 1920 if (old_map->is_prototype_map()) { |
1921 DCHECK(new_map->is_prototype_map()); | |
1922 DCHECK(object->map() == *new_map); | |
1918 new_map->set_prototype_info(old_map->prototype_info()); | 1923 new_map->set_prototype_info(old_map->prototype_info()); |
1919 old_map->set_prototype_info(Smi::FromInt(0)); | 1924 old_map->set_prototype_info(Smi::FromInt(0)); |
1920 } | 1925 } |
1921 } | 1926 } |
1922 | 1927 |
1923 | 1928 |
1924 // To migrate a fast instance to a fast map: | 1929 // To migrate a fast instance to a fast map: |
1925 // - First check whether the instance needs to be rewritten. If not, simply | 1930 // - First check whether the instance needs to be rewritten. If not, simply |
1926 // change the map. | 1931 // change the map. |
1927 // - Otherwise, allocate a fixed array large enough to hold all fields, in | 1932 // - Otherwise, allocate a fixed array large enough to hold all fields, in |
(...skipping 2749 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
4677 number_of_fields += 1; | 4682 number_of_fields += 1; |
4678 } | 4683 } |
4679 } | 4684 } |
4680 | 4685 |
4681 int inobject_props = object->map()->inobject_properties(); | 4686 int inobject_props = object->map()->inobject_properties(); |
4682 | 4687 |
4683 // Allocate new map. | 4688 // Allocate new map. |
4684 Handle<Map> new_map = Map::CopyDropDescriptors(handle(object->map())); | 4689 Handle<Map> new_map = Map::CopyDropDescriptors(handle(object->map())); |
4685 new_map->set_dictionary_map(false); | 4690 new_map->set_dictionary_map(false); |
4686 | 4691 |
4692 if (object->map()->is_prototype_map()) { | |
4693 DCHECK(new_map->is_prototype_map()); | |
4694 new_map->set_prototype_info(object->map()->prototype_info()); | |
4695 object->map()->set_prototype_info(Smi::FromInt(0)); | |
4696 } | |
4697 | |
4687 #if TRACE_MAPS | 4698 #if TRACE_MAPS |
4688 if (FLAG_trace_maps) { | 4699 if (FLAG_trace_maps) { |
4689 PrintF("[TraceMaps: SlowToFast from= %p to= %p reason= %s ]\n", | 4700 PrintF("[TraceMaps: SlowToFast from= %p to= %p reason= %s ]\n", |
4690 reinterpret_cast<void*>(object->map()), | 4701 reinterpret_cast<void*>(object->map()), |
4691 reinterpret_cast<void*>(*new_map), reason); | 4702 reinterpret_cast<void*>(*new_map), reason); |
4692 } | 4703 } |
4693 #endif | 4704 #endif |
4694 | 4705 |
4695 if (instance_descriptor_length == 0) { | 4706 if (instance_descriptor_length == 0) { |
4696 DisallowHeapAllocation no_gc; | 4707 DisallowHeapAllocation no_gc; |
(...skipping 2165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
6862 #ifdef VERIFY_HEAP | 6873 #ifdef VERIFY_HEAP |
6863 if (FLAG_verify_heap) new_map->DictionaryMapVerify(); | 6874 if (FLAG_verify_heap) new_map->DictionaryMapVerify(); |
6864 #endif | 6875 #endif |
6865 #ifdef ENABLE_SLOW_DCHECKS | 6876 #ifdef ENABLE_SLOW_DCHECKS |
6866 if (FLAG_enable_slow_asserts) { | 6877 if (FLAG_enable_slow_asserts) { |
6867 // The cached map should match newly created normalized map bit-by-bit, | 6878 // The cached map should match newly created normalized map bit-by-bit, |
6868 // except for the code cache, which can contain some ics which can be | 6879 // except for the code cache, which can contain some ics which can be |
6869 // applied to the shared map, dependent code and weak cell cache. | 6880 // applied to the shared map, dependent code and weak cell cache. |
6870 Handle<Map> fresh = Map::CopyNormalized(fast_map, mode); | 6881 Handle<Map> fresh = Map::CopyNormalized(fast_map, mode); |
6871 | 6882 |
6872 DCHECK(memcmp(fresh->address(), | 6883 if (new_map->is_prototype_map()) { |
6873 new_map->address(), | 6884 // For prototype maps, the PrototypeInfo is not copied. |
6874 Map::kCodeCacheOffset) == 0); | 6885 DCHECK(memcmp(fresh->address(), new_map->address(), |
6886 kTransitionsOrPrototypeInfoOffset) == 0); | |
6887 DCHECK(fresh->raw_transitions() == Smi::FromInt(0)); | |
6888 STATIC_ASSERT(kDescriptorsOffset == | |
6889 kTransitionsOrPrototypeInfoOffset + kPointerSize); | |
6890 DCHECK(memcmp(HeapObject::RawField(*fresh, kDescriptorsOffset), | |
6891 HeapObject::RawField(*new_map, kDescriptorsOffset), | |
6892 kCodeCacheOffset - kDescriptorsOffset) == 0); | |
6893 } else { | |
6894 DCHECK(memcmp(fresh->address(), new_map->address(), | |
6895 Map::kCodeCacheOffset) == 0); | |
6896 } | |
6875 STATIC_ASSERT(Map::kDependentCodeOffset == | 6897 STATIC_ASSERT(Map::kDependentCodeOffset == |
6876 Map::kCodeCacheOffset + kPointerSize); | 6898 Map::kCodeCacheOffset + kPointerSize); |
6877 STATIC_ASSERT(Map::kWeakCellCacheOffset == | 6899 STATIC_ASSERT(Map::kWeakCellCacheOffset == |
6878 Map::kDependentCodeOffset + kPointerSize); | 6900 Map::kDependentCodeOffset + kPointerSize); |
6879 int offset = Map::kWeakCellCacheOffset + kPointerSize; | 6901 int offset = Map::kWeakCellCacheOffset + kPointerSize; |
6880 DCHECK(memcmp(fresh->address() + offset, | 6902 DCHECK(memcmp(fresh->address() + offset, |
6881 new_map->address() + offset, | 6903 new_map->address() + offset, |
6882 Map::kSize - offset) == 0); | 6904 Map::kSize - offset) == 0); |
6883 } | 6905 } |
6884 #endif | 6906 #endif |
(...skipping 1310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
8195 SearchForDuplicates search_for_duplicates) { | 8217 SearchForDuplicates search_for_duplicates) { |
8196 Handle<WeakFixedArray> array = | 8218 Handle<WeakFixedArray> array = |
8197 (maybe_array.is_null() || !maybe_array->IsWeakFixedArray()) | 8219 (maybe_array.is_null() || !maybe_array->IsWeakFixedArray()) |
8198 ? Allocate(value->GetIsolate(), 1, Handle<WeakFixedArray>::null()) | 8220 ? Allocate(value->GetIsolate(), 1, Handle<WeakFixedArray>::null()) |
8199 : Handle<WeakFixedArray>::cast(maybe_array); | 8221 : Handle<WeakFixedArray>::cast(maybe_array); |
8200 | 8222 |
8201 if (search_for_duplicates == kAddIfNotFound) { | 8223 if (search_for_duplicates == kAddIfNotFound) { |
8202 for (int i = 0; i < array->Length(); ++i) { | 8224 for (int i = 0; i < array->Length(); ++i) { |
8203 if (array->Get(i) == *value) return array; | 8225 if (array->Get(i) == *value) return array; |
8204 } | 8226 } |
8227 #if 0 // Enable this if you want to check your search_for_duplicates flags. | |
8205 } else { | 8228 } else { |
8206 #ifdef DEBUG | |
8207 for (int i = 0; i < array->Length(); ++i) { | 8229 for (int i = 0; i < array->Length(); ++i) { |
8208 DCHECK_NE(*value, array->Get(i)); | 8230 DCHECK_NE(*value, array->Get(i)); |
8209 } | 8231 } |
8210 #endif | 8232 #endif |
8211 } | 8233 } |
8212 | 8234 |
8213 // Try to store the new entry if there's room. Optimize for consecutive | 8235 // Try to store the new entry if there's room. Optimize for consecutive |
8214 // accesses. | 8236 // accesses. |
8215 int first_index = array->last_used_index(); | 8237 int first_index = array->last_used_index(); |
8216 for (int i = first_index;;) { | 8238 for (int i = first_index;;) { |
(...skipping 1760 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
9977 Object* maybe_proto_info = prototype->map()->prototype_info(); | 9999 Object* maybe_proto_info = prototype->map()->prototype_info(); |
9978 if (!maybe_proto_info->IsPrototypeInfo()) return; | 10000 if (!maybe_proto_info->IsPrototypeInfo()) return; |
9979 Handle<PrototypeInfo> proto_info(PrototypeInfo::cast(maybe_proto_info), | 10001 Handle<PrototypeInfo> proto_info(PrototypeInfo::cast(maybe_proto_info), |
9980 isolate); | 10002 isolate); |
9981 Object* maybe_registry = proto_info->prototype_users(); | 10003 Object* maybe_registry = proto_info->prototype_users(); |
9982 if (!maybe_registry->IsWeakFixedArray()) return; | 10004 if (!maybe_registry->IsWeakFixedArray()) return; |
9983 WeakFixedArray::cast(maybe_registry)->Remove(user); | 10005 WeakFixedArray::cast(maybe_registry)->Remove(user); |
9984 } | 10006 } |
9985 | 10007 |
9986 | 10008 |
10009 static void InvalidatePrototypeChainsInternal(Map* map) { | |
10010 if (!map->is_prototype_map()) return; | |
10011 Object* maybe_proto_info = map->prototype_info(); | |
10012 if (!maybe_proto_info->IsPrototypeInfo()) return; | |
10013 PrototypeInfo* proto_info = PrototypeInfo::cast(maybe_proto_info); | |
10014 Object* maybe_cell = proto_info->validity_cell(); | |
10015 if (maybe_cell->IsCell()) { | |
10016 // Just set the value; the cell will be replaced lazily. | |
10017 Cell* cell = Cell::cast(maybe_cell); | |
10018 cell->set_value(Smi::FromInt(Map::kPrototypeChainInvalid)); | |
10019 } | |
10020 | |
10021 Object* maybe_array = proto_info->prototype_users(); | |
10022 if (!maybe_array->IsWeakFixedArray()) return; | |
10023 | |
10024 WeakFixedArray* users = WeakFixedArray::cast(maybe_array); | |
10025 for (int i = 0; i < users->Length(); ++i) { | |
10026 Object* maybe_user = users->Get(i); | |
10027 if (maybe_user->IsSmi()) continue; | |
10028 | |
10029 // For now, only maps register themselves as users. | |
10030 Map* user = Map::cast(maybe_user); | |
10031 // Walk the prototype chain (backwards, towards leaf objects) if necessary. | |
10032 InvalidatePrototypeChainsInternal(user); | |
10033 } | |
10034 } | |
10035 | |
10036 | |
9987 // static | 10037 // static |
10038 void JSObject::InvalidatePrototypeChains(Map* map) { | |
10039 if (!FLAG_eliminate_prototype_chain_checks) return; | |
10040 DisallowHeapAllocation no_gc; | |
10041 if (map->IsJSGlobalProxyMap()) { | |
10042 PrototypeIterator iter(map); | |
10043 map = JSObject::cast(iter.GetCurrent())->map(); | |
10044 } | |
10045 InvalidatePrototypeChainsInternal(map); | |
10046 } | |
10047 | |
10048 | |
10049 // static | |
10050 Handle<Cell> Map::GetOrCreatePrototypeChainValidityCell(Handle<Map> map, | |
10051 Isolate* isolate) { | |
10052 Handle<Object> maybe_prototype(map->prototype(), isolate); | |
10053 if (!maybe_prototype->IsJSObject()) return Handle<Cell>::null(); | |
10054 Handle<JSObject> prototype = Handle<JSObject>::cast(maybe_prototype); | |
10055 if (prototype->IsJSGlobalProxy()) { | |
10056 prototype = handle(JSObject::cast(prototype->map()->prototype()), isolate); | |
Toon Verwaest
2015/04/07 09:28:38
PrototypeIterator
Jakob Kummerow
2015/04/14 09:41:57
Done.
| |
10057 } | |
10058 PrototypeInfo* proto_info = | |
10059 PrototypeInfo::cast(prototype->map()->prototype_info()); | |
10060 Object* maybe_cell = proto_info->validity_cell(); | |
10061 // Return existing cell if it's still valid. | |
10062 if (maybe_cell->IsCell()) { | |
10063 Handle<Cell> cell(Cell::cast(maybe_cell), isolate); | |
10064 if (cell->value() == Smi::FromInt(Map::kPrototypeChainValid)) { | |
10065 return handle(Cell::cast(maybe_cell), isolate); | |
10066 } | |
10067 } | |
10068 // Otherwise create a new cell. | |
10069 Handle<Cell> cell = isolate->factory()->NewCell( | |
10070 handle(Smi::FromInt(Map::kPrototypeChainValid), isolate)); | |
10071 proto_info->set_validity_cell(*cell); | |
10072 return cell; | |
10073 } | |
10074 | |
10075 | |
9988 void Map::SetPrototype(Handle<Map> map, Handle<Object> prototype, | 10076 void Map::SetPrototype(Handle<Map> map, Handle<Object> prototype, |
9989 PrototypeOptimizationMode proto_mode) { | 10077 PrototypeOptimizationMode proto_mode) { |
9990 if (map->prototype()->IsJSObject() && FLAG_track_prototype_users) { | 10078 if (map->prototype()->IsJSObject() && FLAG_track_prototype_users) { |
9991 Handle<JSObject> old_prototype(JSObject::cast(map->prototype())); | 10079 Handle<JSObject> old_prototype(JSObject::cast(map->prototype())); |
9992 JSObject::UnregisterPrototypeUser(old_prototype, map); | 10080 JSObject::UnregisterPrototypeUser(old_prototype, map); |
9993 } | 10081 } |
9994 if (prototype->IsJSObject()) { | 10082 if (prototype->IsJSObject()) { |
9995 Handle<JSObject> prototype_jsobj = Handle<JSObject>::cast(prototype); | 10083 Handle<JSObject> prototype_jsobj = Handle<JSObject>::cast(prototype); |
10084 JSObject::OptimizeAsPrototype(prototype_jsobj, proto_mode); | |
9996 if (ShouldRegisterAsPrototypeUser(map, prototype_jsobj)) { | 10085 if (ShouldRegisterAsPrototypeUser(map, prototype_jsobj)) { |
9997 JSObject::RegisterPrototypeUser(prototype_jsobj, map); | 10086 JSObject::RegisterPrototypeUser(prototype_jsobj, map); |
9998 } | 10087 } |
9999 JSObject::OptimizeAsPrototype(prototype_jsobj, proto_mode); | |
10000 } | 10088 } |
10001 WriteBarrierMode wb_mode = | 10089 WriteBarrierMode wb_mode = |
10002 prototype->IsNull() ? SKIP_WRITE_BARRIER : UPDATE_WRITE_BARRIER; | 10090 prototype->IsNull() ? SKIP_WRITE_BARRIER : UPDATE_WRITE_BARRIER; |
10003 map->set_prototype(*prototype, wb_mode); | 10091 map->set_prototype(*prototype, wb_mode); |
10004 } | 10092 } |
10005 | 10093 |
10006 | 10094 |
10007 // static | 10095 // static |
10008 bool Map::ShouldRegisterAsPrototypeUser(Handle<Map> map, | 10096 bool Map::ShouldRegisterAsPrototypeUser(Handle<Map> map, |
10009 Handle<JSObject> prototype) { | 10097 Handle<JSObject> prototype) { |
(...skipping 7111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
17121 CompilationInfo* info) { | 17209 CompilationInfo* info) { |
17122 Handle<DependentCode> codes = DependentCode::InsertCompilationInfo( | 17210 Handle<DependentCode> codes = DependentCode::InsertCompilationInfo( |
17123 handle(cell->dependent_code(), info->isolate()), | 17211 handle(cell->dependent_code(), info->isolate()), |
17124 DependentCode::kPropertyCellChangedGroup, info->object_wrapper()); | 17212 DependentCode::kPropertyCellChangedGroup, info->object_wrapper()); |
17125 if (*codes != cell->dependent_code()) cell->set_dependent_code(*codes); | 17213 if (*codes != cell->dependent_code()) cell->set_dependent_code(*codes); |
17126 info->dependencies(DependentCode::kPropertyCellChangedGroup)->Add( | 17214 info->dependencies(DependentCode::kPropertyCellChangedGroup)->Add( |
17127 cell, info->zone()); | 17215 cell, info->zone()); |
17128 } | 17216 } |
17129 | 17217 |
17130 } } // namespace v8::internal | 17218 } } // namespace v8::internal |
OLD | NEW |