Chromium Code Reviews| Index: src/elements.cc |
| diff --git a/src/elements.cc b/src/elements.cc |
| index 7b7f6893d84b557224acb1232b7798eb39cbb461..866f7e178fe4d0797d079ab8548040ab501d47c0 100644 |
| --- a/src/elements.cc |
| +++ b/src/elements.cc |
| @@ -470,6 +470,22 @@ static void SortIndices( |
| } |
| } |
| +static Object* FillNumberSlowPath(Isolate* isolate, Handle<JSTypedArray> array, |
| + Handle<Object> obj_value, |
| + uint32_t start, uint32_t end) { |
| + Handle<Object> cast_value; |
| + ElementsAccessor* elements = array->GetElementsAccessor(); |
| + |
| + for (uint32_t k = start; k < end; ++k) { |
| + ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| + isolate, cast_value, Object::ToNumber(obj_value)); |
| + // TODO(caitp,cbruni): throw on neutered array |
| + if (V8_UNLIKELY(array->WasNeutered())) return *array; |
| + elements->Set(array, k, *cast_value); |
| + } |
| + return *array; |
| +} |
| + |
| static Maybe<bool> IncludesValueSlowPath(Isolate* isolate, |
| Handle<JSObject> receiver, |
| Handle<Object> value, |
| @@ -1191,6 +1207,18 @@ class ElementsAccessorBase : public ElementsAccessor { |
| return Subclass::GetCapacityImpl(holder, backing_store); |
| } |
| + static Object* FillImpl(Isolate* isolate, Handle<JSObject> receiver, |
| + Handle<Object> obj_value, uint32_t start, |
| + uint32_t end) { |
| + UNREACHABLE(); |
| + return *receiver; |
| + } |
| + |
| + Object* Fill(Isolate* isolate, Handle<JSObject> receiver, |
| + Handle<Object> obj_value, uint32_t start, uint32_t end) { |
| + return Subclass::FillImpl(isolate, receiver, obj_value, start, end); |
| + } |
| + |
| static Maybe<bool> IncludesValueImpl(Isolate* isolate, |
| Handle<JSObject> receiver, |
| Handle<Object> value, |
| @@ -2815,6 +2843,57 @@ class TypedElementsAccessor |
| return Just(true); |
| } |
| + static inline uint8_t ConvertNumberToUint8Clamped(Handle<Object> obj_value) { |
|
Camillo Bruni
2017/03/20 10:20:22
I just realized that we can simplify this further
rongjie
2017/03/20 12:05:24
Done.
|
| + if (obj_value->IsSmi()) { |
| + int value = Smi::cast(*obj_value)->value(); |
| + return FixedTypedArray<Uint8ClampedArrayTraits>::from_int(value); |
| + } else { |
| + DCHECK(obj_value->IsHeapNumber()); |
| + double value = HeapNumber::cast(*obj_value)->value(); |
| + return FixedTypedArray<Uint8ClampedArrayTraits>::from_double(value); |
| + } |
| + } |
| + |
| + static inline ctype ConvertToNumber(Handle<Object> obj_value) { |
| + ctype value = 0; |
| + if (obj_value->IsSmi()) { |
| + value = static_cast<ctype>(Smi::cast(*obj_value)->value()); |
|
Camillo Bruni
2017/03/20 10:20:22
value = BackingStore::from_int(value);
rongjie
2017/03/20 12:05:24
Done.
|
| + } else { |
| + DCHECK(obj_value->IsHeapNumber()); |
| + value = static_cast<ctype>(HeapNumber::cast(*obj_value)->value()); |
|
Camillo Bruni
2017/03/20 10:20:22
value = BackingStore::from_double(value);
rongjie
2017/03/20 12:05:24
Done.
|
| + } |
| + return value; |
| + } |
| + |
| + static Object* FillImpl(Isolate* isolate, Handle<JSObject> receiver, |
| + Handle<Object> obj_value, uint32_t start, |
| + uint32_t end) { |
| + Handle<JSTypedArray> array = Handle<JSTypedArray>::cast(receiver); |
| + DCHECK(!array->WasNeutered()); |
| + |
| + if (!obj_value->IsNumber()) { |
| + return FillNumberSlowPath(isolate, array, obj_value, start, end); |
| + } |
| + |
| + ctype value = 0; |
| + if (Kind == UINT8_CLAMPED_ELEMENTS) { |
|
Camillo Bruni
2017/03/20 10:20:22
Drop the UINT8_CLAMPED_ELEMENTS check and use the
rongjie
2017/03/20 12:05:24
Done.
|
| + value = ConvertNumberToUint8Clamped(obj_value); |
| + } else { |
| + value = ConvertToNumber(obj_value); |
| + } |
| + |
| + // Ensure indexes are within array bounds |
| + DCHECK_LE(0, start); |
| + DCHECK_LE(start, end); |
| + DCHECK_LE(end, array->length_value()); |
| + |
| + DisallowHeapAllocation no_gc; |
| + BackingStore* elements = BackingStore::cast(receiver->elements()); |
| + ctype* data = static_cast<ctype*>(elements->DataPtr()); |
| + std::fill(data + start, data + end, value); |
| + return *array; |
| + } |
| + |
| static Maybe<bool> IncludesValueImpl(Isolate* isolate, |
| Handle<JSObject> receiver, |
| Handle<Object> value, |