OLD | NEW |
---|---|
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 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/builtins/builtins-utils.h" | 5 #include "src/builtins/builtins-utils.h" |
6 #include "src/builtins/builtins.h" | 6 #include "src/builtins/builtins.h" |
7 #include "src/code-stub-assembler.h" | 7 #include "src/code-stub-assembler.h" |
8 #include "src/counters.h" | 8 #include "src/counters.h" |
9 #include "src/objects-inl.h" | 9 #include "src/objects-inl.h" |
10 | 10 |
(...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
392 } // namespace | 392 } // namespace |
393 | 393 |
394 BUILTIN(TypedArrayPrototypeCopyWithin) { | 394 BUILTIN(TypedArrayPrototypeCopyWithin) { |
395 HandleScope scope(isolate); | 395 HandleScope scope(isolate); |
396 | 396 |
397 Handle<JSTypedArray> array; | 397 Handle<JSTypedArray> array; |
398 const char* method = "%TypedArray%.prototype.copyWithin"; | 398 const char* method = "%TypedArray%.prototype.copyWithin"; |
399 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | 399 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
400 isolate, array, JSTypedArray::Validate(isolate, args.receiver(), method)); | 400 isolate, array, JSTypedArray::Validate(isolate, args.receiver(), method)); |
401 | 401 |
402 if (V8_UNLIKELY(array->WasNeutered())) return *array; | |
403 | |
404 int64_t len = array->length_value(); | 402 int64_t len = array->length_value(); |
405 int64_t to = 0; | 403 int64_t to = 0; |
406 int64_t from = 0; | 404 int64_t from = 0; |
407 int64_t final = len; | 405 int64_t final = len; |
408 | 406 |
409 if (V8_LIKELY(args.length() > 1)) { | 407 if (V8_LIKELY(args.length() > 1)) { |
410 Handle<Object> num; | 408 Handle<Object> num; |
411 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | 409 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
412 isolate, num, Object::ToInteger(isolate, args.at<Object>(1))); | 410 isolate, num, Object::ToInteger(isolate, args.at<Object>(1))); |
413 to = CapRelativeIndex(num, 0, len); | 411 to = CapRelativeIndex(num, 0, len); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
449 to = to * element_size; | 447 to = to * element_size; |
450 from = from * element_size; | 448 from = from * element_size; |
451 count = count * element_size; | 449 count = count * element_size; |
452 | 450 |
453 uint8_t* data = static_cast<uint8_t*>(elements->DataPtr()); | 451 uint8_t* data = static_cast<uint8_t*>(elements->DataPtr()); |
454 std::memmove(data + to, data + from, count); | 452 std::memmove(data + to, data + from, count); |
455 | 453 |
456 return *array; | 454 return *array; |
457 } | 455 } |
458 | 456 |
457 BUILTIN(TypedArrayPrototypeFill) { | |
458 HandleScope scope(isolate); | |
459 | |
460 Handle<JSTypedArray> array; | |
461 const char* method = "%TypedArray%.prototype.fill"; | |
462 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | |
463 isolate, array, JSTypedArray::Validate(isolate, args.receiver(), method)); | |
464 | |
465 int64_t len = array->length_value(); | |
466 int64_t start = 0; | |
467 int64_t end = len; | |
468 double value = 0.0; | |
469 | |
470 if (V8_LIKELY(args.length() > 1)) { | |
471 Handle<Object> num; | |
472 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | |
473 isolate, num, Object::ToNumber(args.at<Object>(1))); | |
474 | |
475 if (num->IsSmi()) { | |
476 value = Smi::cast(*num)->value(); | |
477 } else { | |
478 DCHECK(num->IsHeapNumber()); | |
479 value = HeapNumber::cast(*num)->value(); | |
480 } | |
481 if (array->type() == kExternalUint8ClampedArray) { | |
482 value = std::min<double>(std::max<double>(0, value), 255); | |
483 } | |
484 | |
485 Handle<Object> optional = args.atOrUndefined(isolate, 2); | |
486 if (!optional->IsUndefined(isolate)) { | |
487 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | |
488 isolate, num, Object::ToInteger(isolate, optional)); | |
489 start = CapRelativeIndex(num, 0, len); | |
490 | |
491 optional = args.atOrUndefined(isolate, 3); | |
492 if (!optional->IsUndefined(isolate)) { | |
493 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | |
494 isolate, num, Object::ToInteger(isolate, optional)); | |
495 end = CapRelativeIndex(num, 0, len); | |
496 } | |
497 } | |
498 } | |
499 | |
500 int64_t count = end - start; | |
501 if (count <= 0) return *array; | |
502 | |
503 if (V8_UNLIKELY(array->WasNeutered())) return *array; | |
504 | |
505 // Ensure processed indexes are within array bounds | |
506 DCHECK_GE(start, 0); | |
507 DCHECK_LT(start, len); | |
508 DCHECK_GE(end, 0); | |
509 DCHECK_LE(end, len); | |
510 DCHECK_LE(count, len); | |
511 | |
512 switch (array->type()) { | |
513 #define TYPED_ARRAY_FILL(Type, type, TYPE, ctype, size) \ | |
514 case kExternal##Type##Array: { \ | |
515 ctype* backing_store = \ | |
516 static_cast<ctype*>(array->GetBuffer()->backing_store()); \ | |
Choongwoo Han
2017/03/08 08:18:30
You should consider byte_offset for backing_store.
| |
517 ctype cast_value = static_cast<ctype>(value); \ | |
518 std::fill(backing_store + start, backing_store + end, cast_value); \ | |
519 break; \ | |
520 } | |
521 | |
522 TYPED_ARRAYS(TYPED_ARRAY_FILL) | |
523 #undef TYPED_ARRAY_FILL | |
524 } | |
525 | |
526 return *array; | |
527 } | |
528 | |
459 } // namespace internal | 529 } // namespace internal |
460 } // namespace v8 | 530 } // namespace v8 |
OLD | NEW |