Index: src/elements.cc |
diff --git a/src/elements.cc b/src/elements.cc |
index 7b7f6893d84b557224acb1232b7798eb39cbb461..2e1bc5fbb8c111e1bbbe8ae78488f206d9840ce2 100644 |
--- a/src/elements.cc |
+++ b/src/elements.cc |
@@ -470,6 +470,28 @@ static void SortIndices( |
} |
} |
+static Object* FillNumberSlowPath(Isolate* isolate, Handle<JSTypedArray> array, |
+ Handle<Object> obj_value, |
+ uint32_t start, uint32_t end) { |
+ const char* kMethodName = "%TypedArray%.prototype.fill"; |
+ 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 and just return *array |
Camillo Bruni
2017/03/13 09:02:30
drop "and just return *array"
rongjie
2017/03/13 10:46:40
Done.
|
+ if (V8_UNLIKELY(array->WasNeutered())) { |
+ THROW_NEW_ERROR_RETURN_FAILURE( |
+ isolate, NewTypeError(MessageTemplate::kDetachedOperation, |
+ isolate->factory()->NewStringFromAsciiChecked( |
+ kMethodName))); |
Camillo Bruni
2017/03/13 09:02:30
sorry, my comment wasn't clear enough :P, we don't
rongjie
2017/03/13 10:46:40
Done.
|
+ } |
+ elements->Set(array, k, *cast_value); |
+ } |
+ return *array; |
+} |
+ |
static Maybe<bool> IncludesValueSlowPath(Isolate* isolate, |
Handle<JSObject> receiver, |
Handle<Object> value, |
@@ -1191,6 +1213,17 @@ 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 +2848,38 @@ class TypedElementsAccessor |
return Just(true); |
} |
+ 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); |
+ |
+ if (!obj_value->IsNumber()) { |
+ return FillNumberSlowPath(isolate, array, obj_value, start, end); |
+ } |
+ |
+ ctype value = 0; |
+ |
+ if (obj_value->IsSmi()) { |
+ value = static_cast<ctype>(Smi::cast(*obj_value)->value()); |
+ } else { |
+ DCHECK(obj_value->IsHeapNumber()); |
+ value = static_cast<ctype>(HeapNumber::cast(*obj_value)->value()); |
+ } |
+ if (array->type() == kExternalUint8ClampedArray) { |
+ value = std::min<ctype>(std::max<ctype>(0, value), 255); |
+ } |
+ |
+ // Ensure indexes are within array bounds |
+ DCHECK_LE(0, start); |
+ DCHECK_LE(start, end); |
+ DCHECK_LE(end, array->length_value()); |
+ |
+ Handle<FixedTypedArrayBase> elements( |
+ FixedTypedArrayBase::cast(array->elements())); |
Camillo Bruni
2017/03/13 09:02:30
Another nit: you can avoid a handle allocation her
rongjie
2017/03/13 10:46:40
Done.
|
+ 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, |