| OLD | NEW |
| 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/elements.h" | 5 #include "src/elements.h" |
| 6 | 6 |
| 7 #include "src/arguments.h" | 7 #include "src/arguments.h" |
| 8 #include "src/conversions.h" | 8 #include "src/conversions.h" |
| 9 #include "src/factory.h" | 9 #include "src/factory.h" |
| 10 #include "src/messages.h" | 10 #include "src/messages.h" |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 #define ELEMENTS_TRAITS(Class, KindParam, Store) \ | 90 #define ELEMENTS_TRAITS(Class, KindParam, Store) \ |
| 91 template<> class ElementsKindTraits<KindParam> { \ | 91 template<> class ElementsKindTraits<KindParam> { \ |
| 92 public: /* NOLINT */ \ | 92 public: /* NOLINT */ \ |
| 93 static const ElementsKind Kind = KindParam; \ | 93 static const ElementsKind Kind = KindParam; \ |
| 94 typedef Store BackingStore; \ | 94 typedef Store BackingStore; \ |
| 95 }; | 95 }; |
| 96 ELEMENTS_LIST(ELEMENTS_TRAITS) | 96 ELEMENTS_LIST(ELEMENTS_TRAITS) |
| 97 #undef ELEMENTS_TRAITS | 97 #undef ELEMENTS_TRAITS |
| 98 | 98 |
| 99 | 99 |
| 100 static bool HasIndex(Handle<FixedArray> array, Handle<Object> index_handle) { | |
| 101 DisallowHeapAllocation no_gc; | |
| 102 Object* index = *index_handle; | |
| 103 int len0 = array->length(); | |
| 104 for (int i = 0; i < len0; i++) { | |
| 105 Object* element = array->get(i); | |
| 106 if (index->KeyEquals(element)) return true; | |
| 107 } | |
| 108 return false; | |
| 109 } | |
| 110 | |
| 111 | |
| 112 MUST_USE_RESULT | 100 MUST_USE_RESULT |
| 113 MaybeHandle<Object> ThrowArrayLengthRangeError(Isolate* isolate) { | 101 MaybeHandle<Object> ThrowArrayLengthRangeError(Isolate* isolate) { |
| 114 THROW_NEW_ERROR(isolate, NewRangeError(MessageTemplate::kInvalidArrayLength), | 102 THROW_NEW_ERROR(isolate, NewRangeError(MessageTemplate::kInvalidArrayLength), |
| 115 Object); | 103 Object); |
| 116 } | 104 } |
| 117 | 105 |
| 118 | 106 |
| 119 void CopyObjectToObjectElements(FixedArrayBase* from_base, | 107 void CopyObjectToObjectElements(FixedArrayBase* from_base, |
| 120 ElementsKind from_kind, uint32_t from_start, | 108 ElementsKind from_kind, uint32_t from_start, |
| 121 FixedArrayBase* to_base, ElementsKind to_kind, | 109 FixedArrayBase* to_base, ElementsKind to_kind, |
| (...skipping 756 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 878 // intentionally to avoid ArrayConcat() builtin performance degradation. | 866 // intentionally to avoid ArrayConcat() builtin performance degradation. |
| 879 // | 867 // |
| 880 // Details: The idea is that allocations actually happen only in case of | 868 // Details: The idea is that allocations actually happen only in case of |
| 881 // copying from object with fast double elements to object with object | 869 // copying from object with fast double elements to object with object |
| 882 // elements. In all the other cases there are no allocations performed and | 870 // elements. In all the other cases there are no allocations performed and |
| 883 // handle creation causes noticeable performance degradation of the builtin. | 871 // handle creation causes noticeable performance degradation of the builtin. |
| 884 ElementsAccessorSubclass::CopyElementsImpl( | 872 ElementsAccessorSubclass::CopyElementsImpl( |
| 885 from, from_start, *to, from_kind, to_start, packed_size, copy_size); | 873 from, from_start, *to, from_kind, to_start, packed_size, copy_size); |
| 886 } | 874 } |
| 887 | 875 |
| 888 virtual Handle<FixedArray> AddElementsToFixedArray( | 876 virtual void AddElementsToKeyAccumulator(Handle<JSObject> receiver, |
| 889 Handle<JSObject> receiver, Handle<FixedArray> to, | 877 KeyAccumulator* accumulator, |
| 890 FixedArray::KeyFilter filter) final { | 878 FixedArray::KeyFilter filter) final { |
| 891 Handle<FixedArrayBase> from(receiver->elements()); | 879 Handle<FixedArrayBase> from(receiver->elements()); |
| 892 | 880 uint32_t add_length = |
| 893 int len0 = to->length(); | 881 ElementsAccessorSubclass::GetCapacityImpl(*receiver, *from); |
| 894 #ifdef ENABLE_SLOW_DCHECKS | 882 if (add_length == 0) return; |
| 895 if (FLAG_enable_slow_asserts) { | 883 accumulator->PrepareForComparisons(add_length); |
| 896 for (int i = 0; i < len0; i++) { | 884 int prev_key_count = accumulator->GetLength(); |
| 897 DCHECK(!to->get(i)->IsTheHole()); | 885 for (uint32_t i = 0; i < add_length; i++) { |
| 886 if (!ElementsAccessorSubclass::HasEntryImpl(*from, i)) continue; |
| 887 Handle<Object> value = ElementsAccessorSubclass::GetImpl(from, i); |
| 888 DCHECK(!value->IsTheHole()); |
| 889 DCHECK(!value->IsAccessorPair()); |
| 890 DCHECK(!value->IsExecutableAccessorInfo()); |
| 891 if (filter == FixedArray::NON_SYMBOL_KEYS && value->IsSymbol()) { |
| 892 continue; |
| 898 } | 893 } |
| 894 accumulator->AddKey(value, prev_key_count); |
| 899 } | 895 } |
| 900 #endif | |
| 901 | |
| 902 // Optimize if 'other' is empty. | |
| 903 // We cannot optimize if 'this' is empty, as other may have holes. | |
| 904 uint32_t len1 = ElementsAccessorSubclass::GetCapacityImpl(*receiver, *from); | |
| 905 if (len1 == 0) return to; | |
| 906 | |
| 907 Isolate* isolate = from->GetIsolate(); | |
| 908 | |
| 909 // Compute how many elements are not in other. | |
| 910 uint32_t extra = 0; | |
| 911 for (uint32_t y = 0; y < len1; y++) { | |
| 912 if (ElementsAccessorSubclass::HasEntryImpl(*from, y)) { | |
| 913 Handle<Object> value = ElementsAccessorSubclass::GetImpl(from, y); | |
| 914 | |
| 915 DCHECK(!value->IsTheHole()); | |
| 916 DCHECK(!value->IsAccessorPair()); | |
| 917 DCHECK(!value->IsExecutableAccessorInfo()); | |
| 918 if (filter == FixedArray::NON_SYMBOL_KEYS && value->IsSymbol()) { | |
| 919 continue; | |
| 920 } | |
| 921 if (!HasIndex(to, value)) { | |
| 922 extra++; | |
| 923 } | |
| 924 } | |
| 925 } | |
| 926 | |
| 927 if (extra == 0) return to; | |
| 928 | |
| 929 // Allocate the result | |
| 930 Handle<FixedArray> result = isolate->factory()->NewFixedArray(len0 + extra); | |
| 931 | |
| 932 // Fill in the content | |
| 933 { | |
| 934 DisallowHeapAllocation no_gc; | |
| 935 WriteBarrierMode mode = result->GetWriteBarrierMode(no_gc); | |
| 936 for (int i = 0; i < len0; i++) { | |
| 937 Object* e = to->get(i); | |
| 938 DCHECK(e->IsString() || e->IsNumber()); | |
| 939 result->set(i, e, mode); | |
| 940 } | |
| 941 } | |
| 942 // Fill in the extra values. | |
| 943 uint32_t entry = 0; | |
| 944 for (uint32_t y = 0; y < len1; y++) { | |
| 945 if (ElementsAccessorSubclass::HasEntryImpl(*from, y)) { | |
| 946 Handle<Object> value = ElementsAccessorSubclass::GetImpl(from, y); | |
| 947 DCHECK(!value->IsAccessorPair()); | |
| 948 DCHECK(!value->IsExecutableAccessorInfo()); | |
| 949 if (filter == FixedArray::NON_SYMBOL_KEYS && value->IsSymbol()) { | |
| 950 continue; | |
| 951 } | |
| 952 if (!value->IsTheHole() && !HasIndex(to, value)) { | |
| 953 result->set(len0 + entry, *value); | |
| 954 entry++; | |
| 955 } | |
| 956 } | |
| 957 } | |
| 958 DCHECK(extra == entry); | |
| 959 return result; | |
| 960 } | 896 } |
| 961 | 897 |
| 962 static uint32_t GetCapacityImpl(JSObject* holder, | 898 static uint32_t GetCapacityImpl(JSObject* holder, |
| 963 FixedArrayBase* backing_store) { | 899 FixedArrayBase* backing_store) { |
| 964 return backing_store->length(); | 900 return backing_store->length(); |
| 965 } | 901 } |
| 966 | 902 |
| 967 uint32_t GetCapacity(JSObject* holder, FixedArrayBase* backing_store) final { | 903 uint32_t GetCapacity(JSObject* holder, FixedArrayBase* backing_store) final { |
| 968 return ElementsAccessorSubclass::GetCapacityImpl(holder, backing_store); | 904 return ElementsAccessorSubclass::GetCapacityImpl(holder, backing_store); |
| 969 } | 905 } |
| (...skipping 1467 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2437 } | 2373 } |
| 2438 } | 2374 } |
| 2439 | 2375 |
| 2440 DCHECK(j == result_len); | 2376 DCHECK(j == result_len); |
| 2441 return result_array; | 2377 return result_array; |
| 2442 } | 2378 } |
| 2443 | 2379 |
| 2444 ElementsAccessor** ElementsAccessor::elements_accessors_ = NULL; | 2380 ElementsAccessor** ElementsAccessor::elements_accessors_ = NULL; |
| 2445 } // namespace internal | 2381 } // namespace internal |
| 2446 } // namespace v8 | 2382 } // namespace v8 |
| OLD | NEW |