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

Side by Side Diff: src/elements.cc

Issue 2735563002: Migrate %TypedArray%.prototype.fill to C++ (Closed)
Patch Set: Update test 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 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 static inline uint8_t ConvertNumberToUint8Clamped(Handle<Object> obj_value) {
2847 if (obj_value->IsSmi()) {
2848 int value = Smi::cast(*obj_value)->value();
2849 value = std::min<int>(std::max<int>(0, value), 255);
2850 return static_cast<uint8_t>(value);
Camillo Bruni 2017/03/14 13:31:07 Could you use FixedTypedArray<Uint8ClampedArrayTra
rongjie 2017/03/14 13:43:02 Done.
2851 } else {
2852 DCHECK(obj_value->IsHeapNumber());
2853 double value = 0.0;
2854 value = HeapNumber::cast(*obj_value)->value();
2855 value = std::rint(value);
2856 value = std::min<double>(std::max<double>(0, value), 255);
2857 return static_cast<uint8_t>(value);
Camillo Bruni 2017/03/14 13:31:07 Could you use FixedTypedArray<Uint8ClampedArrayTra
rongjie 2017/03/14 13:43:02 Done.
2858 }
2859 }
2860
2861 static inline ctype ConvertToNumber(Handle<Object> obj_value) {
2862 ctype value = 0;
2863 if (obj_value->IsSmi()) {
2864 value = static_cast<ctype>(Smi::cast(*obj_value)->value());
2865 } else {
2866 DCHECK(obj_value->IsHeapNumber());
2867 value = static_cast<ctype>(HeapNumber::cast(*obj_value)->value());
2868 }
2869 return value;
2870 }
2871
2872 static Object* FillImpl(Isolate* isolate, Handle<JSObject> receiver,
2873 Handle<Object> obj_value, uint32_t start, uint32_t end) {
2874 Handle<JSTypedArray> array = Handle<JSTypedArray>::cast(receiver);
2875 DCHECK(!array->WasNeutered());
2876
2877 if (!obj_value->IsNumber()) {
2878 return FillNumberSlowPath(isolate, array, obj_value, start, end);
2879 }
2880
2881 ctype value = 0;
2882 if (Kind == UINT8_CLAMPED_ELEMENTS) {
2883 value = ConvertNumberToUint8Clamped(obj_value);
2884 } else {
2885 value = ConvertToNumber(obj_value);
2886 }
2887
2888 // Ensure indexes are within array bounds
2889 DCHECK_LE(0, start);
2890 DCHECK_LE(start, end);
2891 DCHECK_LE(end, array->length_value());
2892
2893 DisallowHeapAllocation no_gc;
2894 BackingStore* elements = BackingStore::cast(receiver->elements());
2895 ctype* data = static_cast<ctype*>(elements->DataPtr());
2896 std::fill(data + start, data + end, value);
2897 return *array;
2898 }
2899
2818 static Maybe<bool> IncludesValueImpl(Isolate* isolate, 2900 static Maybe<bool> IncludesValueImpl(Isolate* isolate,
2819 Handle<JSObject> receiver, 2901 Handle<JSObject> receiver,
2820 Handle<Object> value, 2902 Handle<Object> value,
2821 uint32_t start_from, uint32_t length) { 2903 uint32_t start_from, uint32_t length) {
2822 DCHECK(JSObject::PrototypeHasNoElements(isolate, *receiver)); 2904 DCHECK(JSObject::PrototypeHasNoElements(isolate, *receiver));
2823 DisallowHeapAllocation no_gc; 2905 DisallowHeapAllocation no_gc;
2824 2906
2825 // TODO(caitp): return Just(false) here when implementing strict throwing on 2907 // TODO(caitp): return Just(false) here when implementing strict throwing on
2826 // neutered views. 2908 // neutered views.
2827 if (WasNeutered(*receiver)) { 2909 if (WasNeutered(*receiver)) {
(...skipping 1037 matching lines...) Expand 10 before | Expand all | Expand 10 after
3865 insertion_index += len; 3947 insertion_index += len;
3866 } 3948 }
3867 3949
3868 DCHECK_EQ(insertion_index, result_len); 3950 DCHECK_EQ(insertion_index, result_len);
3869 return result_array; 3951 return result_array;
3870 } 3952 }
3871 3953
3872 ElementsAccessor** ElementsAccessor::elements_accessors_ = NULL; 3954 ElementsAccessor** ElementsAccessor::elements_accessors_ = NULL;
3873 } // namespace internal 3955 } // namespace internal
3874 } // namespace v8 3956 } // 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