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

Side by Side Diff: src/elements.cc

Issue 2735563002: Migrate %TypedArray%.prototype.fill to C++ (Closed)
Patch Set: format 80 cols 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
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 452 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
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
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, ElementsKind K = Kind>
2847 static inline uint8_t MaybeClamp(
Camillo Bruni 2017/03/14 09:30:35 ConvertNumberToUint8Clamped(Handle<Object> obj_val
rongjie 2017/03/14 10:08:48 Done.
2848 Handle<Object> obj_value, JSTypedArray* array,
2849 typename std::enable_if<K == UINT8_CLAMPED_ELEMENTS, T>::type = 0) {
2850 double value = 0.0;
2851 if (obj_value->IsSmi()) {
2852 value = Smi::cast(*obj_value)->value();
Camillo Bruni 2017/03/14 09:30:36 Special case directly on Smi and avoid slow double
rongjie 2017/03/14 10:08:48 Done.
2853 } else {
2854 DCHECK(obj_value->IsHeapNumber());
2855 value = HeapNumber::cast(*obj_value)->value();
2856 }
2857 value = std::min<double>(std::max<double>(0, value), 255);
2858 return static_cast<uint8_t>(value);
2859 }
2860
2861 template <typename T, ElementsKind K = Kind>
2862 static inline T MaybeClamp(
Camillo Bruni 2017/03/14 09:30:36 static ctype ConvertToNumber(Handle<Object> obj_va
Camillo Bruni 2017/03/14 09:31:21 On 2017/03/14 at 09:30:36, Camillo Bruni wrote: >
rongjie 2017/03/14 10:08:47 Done.
2863 Handle<Object> obj_value, JSTypedArray* array,
2864 typename std::enable_if<K != UINT8_CLAMPED_ELEMENTS, T>::type = 0) {
2865 T value = 0;
Camillo Bruni 2017/03/14 09:30:36 ctype value = 0;
rongjie 2017/03/14 10:08:48 Done.
2866 if (obj_value->IsSmi()) {
2867 value = static_cast<T>(Smi::cast(*obj_value)->value());
2868 } else {
2869 DCHECK(obj_value->IsHeapNumber());
2870 value = static_cast<T>(HeapNumber::cast(*obj_value)->value());
2871 }
2872 return value;
2873 }
2874
2875 static Object* FillImpl(Isolate* isolate, Handle<JSObject> receiver,
2876 Handle<Object> obj_value, uint32_t start, uint32_t end) {
2877 Handle<JSTypedArray> array = Handle<JSTypedArray>::cast(receiver);
2878 DCHECK(!array->WasNeutered());
2879
2880 if (!obj_value->IsNumber()) {
2881 return FillNumberSlowPath(isolate, array, obj_value, start, end);
2882 }
2883
2884 ctype value = MaybeClamp<ctype>(obj_value, *array);
Camillo Bruni 2017/03/14 09:30:35 I'm not a too big can of the the additional templa
rongjie 2017/03/14 10:08:47 Done. I previously tested similar solution but wi
2885
2886 // Ensure indexes are within array bounds
2887 DCHECK_LE(0, start);
2888 DCHECK_LE(start, end);
2889 DCHECK_LE(end, array->length_value());
2890
2891 DisallowHeapAllocation no_gc;
2892 BackingStore* elements = BackingStore::cast(receiver->elements());
2893 ctype* data = static_cast<ctype*>(elements->DataPtr());
2894 std::fill(data + start, data + end, value);
2895 return *array;
2896 }
2897
2818 static Maybe<bool> IncludesValueImpl(Isolate* isolate, 2898 static Maybe<bool> IncludesValueImpl(Isolate* isolate,
2819 Handle<JSObject> receiver, 2899 Handle<JSObject> receiver,
2820 Handle<Object> value, 2900 Handle<Object> value,
2821 uint32_t start_from, uint32_t length) { 2901 uint32_t start_from, uint32_t length) {
2822 DCHECK(JSObject::PrototypeHasNoElements(isolate, *receiver)); 2902 DCHECK(JSObject::PrototypeHasNoElements(isolate, *receiver));
2823 DisallowHeapAllocation no_gc; 2903 DisallowHeapAllocation no_gc;
2824 2904
2825 // TODO(caitp): return Just(false) here when implementing strict throwing on 2905 // TODO(caitp): return Just(false) here when implementing strict throwing on
2826 // neutered views. 2906 // neutered views.
2827 if (WasNeutered(*receiver)) { 2907 if (WasNeutered(*receiver)) {
(...skipping 1037 matching lines...) Expand 10 before | Expand all | Expand 10 after
3865 insertion_index += len; 3945 insertion_index += len;
3866 } 3946 }
3867 3947
3868 DCHECK_EQ(insertion_index, result_len); 3948 DCHECK_EQ(insertion_index, result_len);
3869 return result_array; 3949 return result_array;
3870 } 3950 }
3871 3951
3872 ElementsAccessor** ElementsAccessor::elements_accessors_ = NULL; 3952 ElementsAccessor** ElementsAccessor::elements_accessors_ = NULL;
3873 } // namespace internal 3953 } // namespace internal
3874 } // namespace v8 3954 } // namespace v8
OLDNEW
« no previous file with comments | « src/elements.h ('k') | src/js/array.js » ('j') | test/mjsunit/es6/typedarray-fill.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698