Chromium Code Reviews| Index: src/builtins/builtins-typedarray.cc |
| diff --git a/src/builtins/builtins-typedarray.cc b/src/builtins/builtins-typedarray.cc |
| index 8a8bde4be027a1329ccaaea4dfe92d75c32957a3..96b129eec1268815991ddb6eb018b045acf704cf 100644 |
| --- a/src/builtins/builtins-typedarray.cc |
| +++ b/src/builtins/builtins-typedarray.cc |
| @@ -399,8 +399,6 @@ BUILTIN(TypedArrayPrototypeCopyWithin) { |
| ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| isolate, array, JSTypedArray::Validate(isolate, args.receiver(), method)); |
| - if (V8_UNLIKELY(array->WasNeutered())) return *array; |
| - |
| int64_t len = array->length_value(); |
| int64_t to = 0; |
| int64_t from = 0; |
| @@ -456,5 +454,77 @@ BUILTIN(TypedArrayPrototypeCopyWithin) { |
| return *array; |
| } |
| +BUILTIN(TypedArrayPrototypeFill) { |
| + HandleScope scope(isolate); |
| + |
| + Handle<JSTypedArray> array; |
| + const char* method = "%TypedArray%.prototype.fill"; |
| + ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| + isolate, array, JSTypedArray::Validate(isolate, args.receiver(), method)); |
| + |
| + int64_t len = array->length_value(); |
| + int64_t start = 0; |
| + int64_t end = len; |
| + double value = 0.0; |
| + |
| + if (V8_LIKELY(args.length() > 1)) { |
| + Handle<Object> num; |
| + ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| + isolate, num, Object::ToNumber(args.at<Object>(1))); |
| + |
| + if (num->IsSmi()) { |
| + value = Smi::cast(*num)->value(); |
| + } else { |
| + DCHECK(num->IsHeapNumber()); |
| + value = HeapNumber::cast(*num)->value(); |
| + } |
| + if (array->type() == kExternalUint8ClampedArray) { |
| + value = std::min<double>(std::max<double>(0, value), 255); |
| + } |
| + |
| + Handle<Object> optional = args.atOrUndefined(isolate, 2); |
| + if (!optional->IsUndefined(isolate)) { |
| + ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| + isolate, num, Object::ToInteger(isolate, optional)); |
| + start = CapRelativeIndex(num, 0, len); |
| + |
| + optional = args.atOrUndefined(isolate, 3); |
| + if (!optional->IsUndefined(isolate)) { |
| + ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| + isolate, num, Object::ToInteger(isolate, optional)); |
| + end = CapRelativeIndex(num, 0, len); |
| + } |
| + } |
| + } |
| + |
| + int64_t count = end - start; |
| + if (count <= 0) return *array; |
| + |
| + if (V8_UNLIKELY(array->WasNeutered())) return *array; |
| + |
| + // Ensure processed indexes are within array bounds |
| + DCHECK_GE(start, 0); |
| + DCHECK_LT(start, len); |
| + DCHECK_GE(end, 0); |
| + DCHECK_LE(end, len); |
| + DCHECK_LE(count, len); |
| + |
| + switch (array->type()) { |
| +#define TYPED_ARRAY_FILL(Type, type, TYPE, ctype, size) \ |
| + case kExternal##Type##Array: { \ |
| + ctype* backing_store = \ |
| + static_cast<ctype*>(array->GetBuffer()->backing_store()); \ |
|
Choongwoo Han
2017/03/08 08:18:30
You should consider byte_offset for backing_store.
|
| + ctype cast_value = static_cast<ctype>(value); \ |
| + std::fill(backing_store + start, backing_store + end, cast_value); \ |
| + break; \ |
| + } |
| + |
| + TYPED_ARRAYS(TYPED_ARRAY_FILL) |
| +#undef TYPED_ARRAY_FILL |
| + } |
| + |
| + return *array; |
| +} |
| + |
| } // namespace internal |
| } // namespace v8 |