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

Side by Side Diff: src/elements.cc

Issue 2744283002: [typedarrays] Implement %TypedArray%.prototype.lastIndexOf in C++ (Closed)
Patch Set: Correct integral value check Created 3 years, 9 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/elements.h ('k') | src/js/array.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 // 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/isolate-inl.h" 10 #include "src/isolate-inl.h"
(...skipping 1201 matching lines...) Expand 10 before | Expand all | Expand 10 after
1212 return IndexOfValueSlowPath(isolate, receiver, value, start_from, length); 1212 return IndexOfValueSlowPath(isolate, receiver, value, start_from, length);
1213 } 1213 }
1214 1214
1215 Maybe<int64_t> IndexOfValue(Isolate* isolate, Handle<JSObject> receiver, 1215 Maybe<int64_t> IndexOfValue(Isolate* isolate, Handle<JSObject> receiver,
1216 Handle<Object> value, uint32_t start_from, 1216 Handle<Object> value, uint32_t start_from,
1217 uint32_t length) final { 1217 uint32_t length) final {
1218 return Subclass::IndexOfValueImpl(isolate, receiver, value, start_from, 1218 return Subclass::IndexOfValueImpl(isolate, receiver, value, start_from,
1219 length); 1219 length);
1220 } 1220 }
1221 1221
1222 static Maybe<int64_t> LastIndexOfValueImpl(Isolate* isolate,
1223 Handle<JSObject> receiver,
1224 Handle<Object> value,
1225 uint32_t start_from) {
1226 UNREACHABLE();
1227 return Just<int64_t>(-1);
1228 }
1229
1230 Maybe<int64_t> LastIndexOfValue(Isolate* isolate, Handle<JSObject> receiver,
1231 Handle<Object> value,
1232 uint32_t start_from) final {
1233 return Subclass::LastIndexOfValueImpl(isolate, receiver, value, start_from);
1234 }
1235
1222 static uint32_t GetIndexForEntryImpl(FixedArrayBase* backing_store, 1236 static uint32_t GetIndexForEntryImpl(FixedArrayBase* backing_store,
1223 uint32_t entry) { 1237 uint32_t entry) {
1224 return entry; 1238 return entry;
1225 } 1239 }
1226 1240
1227 static uint32_t GetEntryForIndexImpl(Isolate* isolate, JSObject* holder, 1241 static uint32_t GetEntryForIndexImpl(Isolate* isolate, JSObject* holder,
1228 FixedArrayBase* backing_store, 1242 FixedArrayBase* backing_store,
1229 uint32_t index, PropertyFilter filter) { 1243 uint32_t index, PropertyFilter filter) {
1230 uint32_t length = Subclass::GetMaxIndex(holder, backing_store); 1244 uint32_t length = Subclass::GetMaxIndex(holder, backing_store);
1231 if (IsHoleyElementsKind(kind())) { 1245 if (IsHoleyElementsKind(kind())) {
(...skipping 1676 matching lines...) Expand 10 before | Expand all | Expand 10 after
2908 if (static_cast<double>(typed_search_value) != search_value) { 2922 if (static_cast<double>(typed_search_value) != search_value) {
2909 return Just<int64_t>(-1); // Loss of precision. 2923 return Just<int64_t>(-1); // Loss of precision.
2910 } 2924 }
2911 2925
2912 for (uint32_t k = start_from; k < length; ++k) { 2926 for (uint32_t k = start_from; k < length; ++k) {
2913 ctype element_k = elements->get_scalar(k); 2927 ctype element_k = elements->get_scalar(k);
2914 if (element_k == typed_search_value) return Just<int64_t>(k); 2928 if (element_k == typed_search_value) return Just<int64_t>(k);
2915 } 2929 }
2916 return Just<int64_t>(-1); 2930 return Just<int64_t>(-1);
2917 } 2931 }
2932
2933 static Maybe<int64_t> LastIndexOfValueImpl(Isolate* isolate,
2934 Handle<JSObject> receiver,
2935 Handle<Object> value,
2936 uint32_t start_from) {
2937 DisallowHeapAllocation no_gc;
2938 DCHECK(!WasNeutered(*receiver));
2939
2940 if (!value->IsNumber()) return Just<int64_t>(-1);
2941 BackingStore* elements = BackingStore::cast(receiver->elements());
2942
2943 double search_value = value->Number();
2944
2945 if (!std::isfinite(search_value)) {
2946 if (std::is_integral<ctype>::value) {
2947 // Integral types cannot represent +Inf or NaN.
2948 return Just<int64_t>(-1);
2949 } else if (std::isnan(search_value)) {
2950 // Strict Equality Comparison of NaN is always false.
2951 return Just<int64_t>(-1);
2952 }
2953 } else if (search_value < std::numeric_limits<ctype>::lowest() ||
2954 search_value > std::numeric_limits<ctype>::max()) {
2955 // Return -1 if value can't be represented in this ElementsKind.
2956 return Just<int64_t>(-1);
2957 }
2958
2959 ctype typed_search_value = static_cast<ctype>(search_value);
2960 if (static_cast<double>(typed_search_value) != search_value) {
2961 return Just<int64_t>(-1); // Loss of precision.
2962 }
2963
2964 DCHECK_LT(start_from, elements->length());
2965
2966 uint32_t k = start_from;
2967 do {
2968 ctype element_k = elements->get_scalar(k);
2969 if (element_k == typed_search_value) return Just<int64_t>(k);
2970 } while (k-- != 0);
2971 return Just<int64_t>(-1);
2972 }
2918 }; 2973 };
2919 2974
2920 #define FIXED_ELEMENTS_ACCESSOR(Type, type, TYPE, ctype, size) \ 2975 #define FIXED_ELEMENTS_ACCESSOR(Type, type, TYPE, ctype, size) \
2921 typedef TypedElementsAccessor<TYPE##_ELEMENTS, ctype> \ 2976 typedef TypedElementsAccessor<TYPE##_ELEMENTS, ctype> \
2922 Fixed##Type##ElementsAccessor; 2977 Fixed##Type##ElementsAccessor;
2923 2978
2924 TYPED_ARRAYS(FIXED_ELEMENTS_ACCESSOR) 2979 TYPED_ARRAYS(FIXED_ELEMENTS_ACCESSOR)
2925 #undef FIXED_ELEMENTS_ACCESSOR 2980 #undef FIXED_ELEMENTS_ACCESSOR
2926 2981
2927 template <typename Subclass, typename ArgumentsAccessor, typename KindTraits> 2982 template <typename Subclass, typename ArgumentsAccessor, typename KindTraits>
(...skipping 935 matching lines...) Expand 10 before | Expand all | Expand 10 after
3863 insertion_index += len; 3918 insertion_index += len;
3864 } 3919 }
3865 3920
3866 DCHECK_EQ(insertion_index, result_len); 3921 DCHECK_EQ(insertion_index, result_len);
3867 return result_array; 3922 return result_array;
3868 } 3923 }
3869 3924
3870 ElementsAccessor** ElementsAccessor::elements_accessors_ = NULL; 3925 ElementsAccessor** ElementsAccessor::elements_accessors_ = NULL;
3871 } // namespace internal 3926 } // namespace internal
3872 } // namespace v8 3927 } // namespace v8
OLDNEW
« no previous file with comments | « src/elements.h ('k') | src/js/array.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698