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/elements.h" | 9 #include "src/elements.h" |
10 #include "src/objects-inl.h" | 10 #include "src/objects-inl.h" |
(...skipping 446 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
457 } // namespace | 457 } // namespace |
458 | 458 |
459 BUILTIN(TypedArrayPrototypeCopyWithin) { | 459 BUILTIN(TypedArrayPrototypeCopyWithin) { |
460 HandleScope scope(isolate); | 460 HandleScope scope(isolate); |
461 | 461 |
462 Handle<JSTypedArray> array; | 462 Handle<JSTypedArray> array; |
463 const char* method = "%TypedArray%.prototype.copyWithin"; | 463 const char* method = "%TypedArray%.prototype.copyWithin"; |
464 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | 464 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
465 isolate, array, JSTypedArray::Validate(isolate, args.receiver(), method)); | 465 isolate, array, JSTypedArray::Validate(isolate, args.receiver(), method)); |
466 | 466 |
467 if (V8_UNLIKELY(array->WasNeutered())) return *array; | |
468 | |
469 int64_t len = array->length_value(); | 467 int64_t len = array->length_value(); |
470 int64_t to = 0; | 468 int64_t to = 0; |
471 int64_t from = 0; | 469 int64_t from = 0; |
472 int64_t final = len; | 470 int64_t final = len; |
473 | 471 |
474 if (V8_LIKELY(args.length() > 1)) { | 472 if (V8_LIKELY(args.length() > 1)) { |
475 Handle<Object> num; | 473 Handle<Object> num; |
476 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | 474 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
477 isolate, num, Object::ToInteger(isolate, args.at<Object>(1))); | 475 isolate, num, Object::ToInteger(isolate, args.at<Object>(1))); |
478 to = CapRelativeIndex(num, 0, len); | 476 to = CapRelativeIndex(num, 0, len); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
514 to = to * element_size; | 512 to = to * element_size; |
515 from = from * element_size; | 513 from = from * element_size; |
516 count = count * element_size; | 514 count = count * element_size; |
517 | 515 |
518 uint8_t* data = static_cast<uint8_t*>(elements->DataPtr()); | 516 uint8_t* data = static_cast<uint8_t*>(elements->DataPtr()); |
519 std::memmove(data + to, data + from, count); | 517 std::memmove(data + to, data + from, count); |
520 | 518 |
521 return *array; | 519 return *array; |
522 } | 520 } |
523 | 521 |
522 BUILTIN(TypedArrayPrototypeFill) { | |
523 HandleScope scope(isolate); | |
524 | |
525 Handle<JSTypedArray> array; | |
526 const char* method = "%TypedArray%.prototype.fill"; | |
527 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | |
528 isolate, array, JSTypedArray::Validate(isolate, args.receiver(), method)); | |
529 | |
530 int64_t len = array->length_value(); | |
531 int64_t start = 0; | |
532 int64_t end = len; | |
533 | |
534 if (args.length() > 2) { | |
535 Handle<Object> num = args.atOrUndefined(isolate, 2); | |
536 if (!num->IsUndefined(isolate)) { | |
537 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | |
538 isolate, num, Object::ToInteger(isolate, num)); | |
539 start = CapRelativeIndex(num, 0, len); | |
540 | |
541 num = args.atOrUndefined(isolate, 3); | |
542 if (!num->IsUndefined(isolate)) { | |
543 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | |
544 isolate, num, Object::ToInteger(isolate, num)); | |
545 end = CapRelativeIndex(num, 0, len); | |
546 } | |
547 } | |
548 } | |
549 | |
550 int64_t count = end - start; | |
551 if (count <= 0) return *array; | |
552 | |
553 if (V8_UNLIKELY(array->WasNeutered())) return *array; | |
554 | |
555 // Ensure processed indexes are within array bounds | |
556 DCHECK_GE(start, 0); | |
557 DCHECK_LT(start, len); | |
558 DCHECK_GE(end, 0); | |
559 DCHECK_LE(end, len); | |
560 DCHECK_LE(count, len); | |
561 | |
562 Handle<Object> obj_value = args.at<Object>(1); | |
563 | |
564 if (obj_value->IsNumber()) { | |
565 double value = 0.0; | |
566 | |
567 if (obj_value->IsSmi()) { | |
568 value = Smi::cast(*obj_value)->value(); | |
569 } else { | |
570 DCHECK(obj_value->IsHeapNumber()); | |
571 value = HeapNumber::cast(*obj_value)->value(); | |
572 } | |
573 if (array->type() == kExternalUint8ClampedArray) { | |
574 value = std::min<double>(std::max<double>(0, value), 255); | |
575 } | |
576 | |
577 Handle<FixedTypedArrayBase> elements( | |
578 FixedTypedArrayBase::cast(array->elements())); | |
579 switch (array->type()) { | |
580 #define TYPED_ARRAY_FILL(Type, type, TYPE, ctype, size) \ | |
581 case kExternal##Type##Array: { \ | |
582 ctype* data = static_cast<ctype*>(elements->DataPtr()); \ | |
583 ctype cast_value = static_cast<ctype>(value); \ | |
584 std::fill(data + start, data + end, cast_value); \ | |
585 break; \ | |
586 } | |
587 | |
588 TYPED_ARRAYS(TYPED_ARRAY_FILL) | |
589 #undef TYPED_ARRAY_FILL | |
590 } | |
591 } else { | |
592 ElementsAccessor* elements = array->GetElementsAccessor(); | |
593 | |
Camillo Bruni
2017/03/09 12:11:08
Could you put the following code inside the Elemen
| |
594 Handle<Object> cast_value; | |
595 uint32_t start32 = static_cast<uint32_t>(start); | |
596 uint32_t end32 = static_cast<uint32_t>(end); | |
597 | |
598 for (uint32_t k = start32; k < end32; ++k) { | |
599 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | |
600 isolate, cast_value, Object::ToNumber(obj_value)); | |
601 // IntegerIndexedElementSet step 5 | |
602 if (V8_UNLIKELY(array->WasNeutered())) { | |
603 THROW_NEW_ERROR_RETURN_FAILURE( | |
604 isolate, NewTypeError(MessageTemplate::kDetachedOperation, | |
605 isolate->factory()->NewStringFromAsciiChecked( | |
606 method))); | |
607 } | |
608 elements->Set(array, k, *cast_value); | |
609 } | |
610 } | |
611 | |
612 return *array; | |
613 } | |
614 | |
524 BUILTIN(TypedArrayPrototypeIncludes) { | 615 BUILTIN(TypedArrayPrototypeIncludes) { |
525 HandleScope scope(isolate); | 616 HandleScope scope(isolate); |
526 | 617 |
527 Handle<JSTypedArray> array; | 618 Handle<JSTypedArray> array; |
528 const char* method = "%TypedArray%.prototype.includes"; | 619 const char* method = "%TypedArray%.prototype.includes"; |
529 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | 620 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
530 isolate, array, JSTypedArray::Validate(isolate, args.receiver(), method)); | 621 isolate, array, JSTypedArray::Validate(isolate, args.receiver(), method)); |
531 | 622 |
532 if (args.length() < 2) return isolate->heap()->false_value(); | 623 if (args.length() < 2) return isolate->heap()->false_value(); |
533 | 624 |
(...skipping 12 matching lines...) Expand all Loading... | |
546 Handle<Object> search_element = args.at<Object>(1); | 637 Handle<Object> search_element = args.at<Object>(1); |
547 ElementsAccessor* elements = array->GetElementsAccessor(); | 638 ElementsAccessor* elements = array->GetElementsAccessor(); |
548 Maybe<bool> result = elements->IncludesValue(isolate, array, search_element, | 639 Maybe<bool> result = elements->IncludesValue(isolate, array, search_element, |
549 static_cast<uint32_t>(index), | 640 static_cast<uint32_t>(index), |
550 static_cast<uint32_t>(len)); | 641 static_cast<uint32_t>(len)); |
551 return *isolate->factory()->ToBoolean(result.FromJust()); | 642 return *isolate->factory()->ToBoolean(result.FromJust()); |
552 } | 643 } |
553 | 644 |
554 } // namespace internal | 645 } // namespace internal |
555 } // namespace v8 | 646 } // namespace v8 |
OLD | NEW |