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/isolate-inl.h" | 10 #include "src/isolate-inl.h" |
(...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
463 } cmp; | 463 } cmp; |
464 Object** start = | 464 Object** start = |
465 reinterpret_cast<Object**>(indices->GetFirstElementAddress()); | 465 reinterpret_cast<Object**>(indices->GetFirstElementAddress()); |
466 std::sort(start, start + sort_size, cmp); | 466 std::sort(start, start + sort_size, cmp); |
467 if (write_barrier_mode != SKIP_WRITE_BARRIER) { | 467 if (write_barrier_mode != SKIP_WRITE_BARRIER) { |
468 FIXED_ARRAY_ELEMENTS_WRITE_BARRIER(indices->GetIsolate()->heap(), *indices, | 468 FIXED_ARRAY_ELEMENTS_WRITE_BARRIER(indices->GetIsolate()->heap(), *indices, |
469 0, sort_size); | 469 0, sort_size); |
470 } | 470 } |
471 } | 471 } |
472 | 472 |
473 static Object* FillNumberSlowPath(Isolate* isolate, Handle<JSTypedArray> array, | |
474 Handle<Object> obj_value, | |
475 uint32_t start, uint32_t end) { | |
476 const char* kMethodName = "%TypedArray%.prototype.fill"; | |
477 Handle<Object> cast_value; | |
478 ElementsAccessor* elements = array->GetElementsAccessor(); | |
479 | |
480 for (uint32_t k = start; k < end; ++k) { | |
481 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | |
482 isolate, cast_value, Object::ToNumber(obj_value)); | |
483 // TODO(caitp,cbruni): throw on neutered array | |
484 if (V8_UNLIKELY(array->WasNeutered())) return *array; | |
485 elements->Set(array, k, *cast_value); | |
486 } | |
487 return *array; | |
488 } | |
489 | |
473 static Maybe<bool> IncludesValueSlowPath(Isolate* isolate, | 490 static Maybe<bool> IncludesValueSlowPath(Isolate* isolate, |
474 Handle<JSObject> receiver, | 491 Handle<JSObject> receiver, |
475 Handle<Object> value, | 492 Handle<Object> value, |
476 uint32_t start_from, uint32_t length) { | 493 uint32_t start_from, uint32_t length) { |
477 bool search_for_hole = value->IsUndefined(isolate); | 494 bool search_for_hole = value->IsUndefined(isolate); |
478 for (uint32_t k = start_from; k < length; ++k) { | 495 for (uint32_t k = start_from; k < length; ++k) { |
479 LookupIterator it(isolate, receiver, k); | 496 LookupIterator it(isolate, receiver, k); |
480 if (!it.IsFound()) { | 497 if (!it.IsFound()) { |
481 if (search_for_hole) return Just(true); | 498 if (search_for_hole) return Just(true); |
482 continue; | 499 continue; |
(...skipping 701 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1184 | 1201 |
1185 static uint32_t GetCapacityImpl(JSObject* holder, | 1202 static uint32_t GetCapacityImpl(JSObject* holder, |
1186 FixedArrayBase* backing_store) { | 1203 FixedArrayBase* backing_store) { |
1187 return backing_store->length(); | 1204 return backing_store->length(); |
1188 } | 1205 } |
1189 | 1206 |
1190 uint32_t GetCapacity(JSObject* holder, FixedArrayBase* backing_store) final { | 1207 uint32_t GetCapacity(JSObject* holder, FixedArrayBase* backing_store) final { |
1191 return Subclass::GetCapacityImpl(holder, backing_store); | 1208 return Subclass::GetCapacityImpl(holder, backing_store); |
1192 } | 1209 } |
1193 | 1210 |
1211 static Object* FillImpl(Isolate* isolate, Handle<JSObject> receiver, | |
1212 Handle<Object> obj_value, uint32_t start, uint32_t end) { | |
1213 UNREACHABLE(); | |
1214 return *receiver; | |
1215 } | |
1216 | |
1217 Object* Fill(Isolate* isolate, Handle<JSObject> receiver, | |
1218 Handle<Object> obj_value, uint32_t start, uint32_t end) { | |
1219 return Subclass::FillImpl(isolate, receiver, obj_value, start, end); | |
1220 } | |
1221 | |
1194 static Maybe<bool> IncludesValueImpl(Isolate* isolate, | 1222 static Maybe<bool> IncludesValueImpl(Isolate* isolate, |
1195 Handle<JSObject> receiver, | 1223 Handle<JSObject> receiver, |
1196 Handle<Object> value, | 1224 Handle<Object> value, |
1197 uint32_t start_from, uint32_t length) { | 1225 uint32_t start_from, uint32_t length) { |
1198 return IncludesValueSlowPath(isolate, receiver, value, start_from, length); | 1226 return IncludesValueSlowPath(isolate, receiver, value, start_from, length); |
1199 } | 1227 } |
1200 | 1228 |
1201 Maybe<bool> IncludesValue(Isolate* isolate, Handle<JSObject> receiver, | 1229 Maybe<bool> IncludesValue(Isolate* isolate, Handle<JSObject> receiver, |
1202 Handle<Object> value, uint32_t start_from, | 1230 Handle<Object> value, uint32_t start_from, |
1203 uint32_t length) final { | 1231 uint32_t length) final { |
(...skipping 1604 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2808 if (get_entries) { | 2836 if (get_entries) { |
2809 value = MakeEntryPair(isolate, index, value); | 2837 value = MakeEntryPair(isolate, index, value); |
2810 } | 2838 } |
2811 values_or_entries->set(count++, *value); | 2839 values_or_entries->set(count++, *value); |
2812 } | 2840 } |
2813 } | 2841 } |
2814 *nof_items = count; | 2842 *nof_items = count; |
2815 return Just(true); | 2843 return Just(true); |
2816 } | 2844 } |
2817 | 2845 |
2846 template <typename T> | |
2847 static inline uint8_t MaybeClamp( | |
2848 Handle<Object> obj_value, JSTypedArray* array, | |
2849 typename std::enable_if<std::is_same<T, uint8_t>::value, uint8_t>::type = | |
2850 0) { | |
2851 double value = 0.0; | |
2852 if (obj_value->IsSmi()) { | |
2853 value = Smi::cast(*obj_value)->value(); | |
2854 } else { | |
2855 DCHECK(obj_value->IsHeapNumber()); | |
2856 value = HeapNumber::cast(*obj_value)->value(); | |
2857 } | |
2858 if (array->type() == kExternalUint8ClampedArray) { | |
2859 value = std::min<double>(std::max<double>(0, value), 255); | |
2860 } | |
2861 return static_cast<uint8_t>(value); | |
2862 } | |
2863 | |
2864 template <typename T> | |
2865 static inline T MaybeClamp( | |
2866 Handle<Object> obj_value, JSTypedArray* array, | |
2867 typename std::enable_if<!std::is_same<T, uint8_t>::value, uint8_t>::type = | |
2868 0) { | |
2869 T value = 0; | |
2870 if (obj_value->IsSmi()) { | |
2871 value = static_cast<T>(Smi::cast(*obj_value)->value()); | |
2872 } else { | |
2873 DCHECK(obj_value->IsHeapNumber()); | |
2874 value = static_cast<T>(HeapNumber::cast(*obj_value)->value()); | |
2875 } | |
2876 return value; | |
2877 } | |
2878 | |
2879 static Object* FillImpl(Isolate* isolate, Handle<JSObject> receiver, | |
2880 Handle<Object> obj_value, uint32_t start, uint32_t end) { | |
2881 Handle<JSTypedArray> array = Handle<JSTypedArray>::cast(receiver); | |
Camillo Bruni
2017/03/13 15:58:14
Another nit, please add:
DHECK(!array->WasNeutere
rongjie
2017/03/14 00:18:07
Done.
| |
2882 | |
2883 if (!obj_value->IsNumber()) { | |
2884 return FillNumberSlowPath(isolate, array, obj_value, start, end); | |
2885 } | |
2886 | |
2887 ctype value = MaybeClamp<ctype>(obj_value, *array); | |
Camillo Bruni
2017/03/13 15:58:14
Sorry, didn't spot that before eather.
The compile
rongjie
2017/03/14 00:18:07
Use "Kind == UINT8_CLAMPED_ELEMENTS" done. This ma
| |
2888 | |
2889 // Ensure indexes are within array bounds | |
2890 DCHECK_LE(0, start); | |
2891 DCHECK_LE(start, end); | |
2892 DCHECK_LE(end, array->length_value()); | |
2893 | |
2894 DisallowHeapAllocation no_gc; | |
2895 BackingStore* elements = BackingStore::cast(receiver->elements()); | |
2896 ctype* data = static_cast<ctype*>(elements->DataPtr()); | |
2897 std::fill(data + start, data + end, value); | |
2898 return *array; | |
2899 } | |
2900 | |
2818 static Maybe<bool> IncludesValueImpl(Isolate* isolate, | 2901 static Maybe<bool> IncludesValueImpl(Isolate* isolate, |
2819 Handle<JSObject> receiver, | 2902 Handle<JSObject> receiver, |
2820 Handle<Object> value, | 2903 Handle<Object> value, |
2821 uint32_t start_from, uint32_t length) { | 2904 uint32_t start_from, uint32_t length) { |
2822 DCHECK(JSObject::PrototypeHasNoElements(isolate, *receiver)); | 2905 DCHECK(JSObject::PrototypeHasNoElements(isolate, *receiver)); |
2823 DisallowHeapAllocation no_gc; | 2906 DisallowHeapAllocation no_gc; |
2824 | 2907 |
2825 // TODO(caitp): return Just(false) here when implementing strict throwing on | 2908 // TODO(caitp): return Just(false) here when implementing strict throwing on |
2826 // neutered views. | 2909 // neutered views. |
2827 if (WasNeutered(*receiver)) { | 2910 if (WasNeutered(*receiver)) { |
(...skipping 1037 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3865 insertion_index += len; | 3948 insertion_index += len; |
3866 } | 3949 } |
3867 | 3950 |
3868 DCHECK_EQ(insertion_index, result_len); | 3951 DCHECK_EQ(insertion_index, result_len); |
3869 return result_array; | 3952 return result_array; |
3870 } | 3953 } |
3871 | 3954 |
3872 ElementsAccessor** ElementsAccessor::elements_accessors_ = NULL; | 3955 ElementsAccessor** ElementsAccessor::elements_accessors_ = NULL; |
3873 } // namespace internal | 3956 } // namespace internal |
3874 } // namespace v8 | 3957 } // namespace v8 |
OLD | NEW |