| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 3726 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3737 ASSERT((index >= 0) && (index < this->length())); | 3737 ASSERT((index >= 0) && (index < this->length())); |
| 3738 WRITE_DOUBLE_FIELD(this, ElementOffset(index), value); | 3738 WRITE_DOUBLE_FIELD(this, ElementOffset(index), value); |
| 3739 } | 3739 } |
| 3740 | 3740 |
| 3741 | 3741 |
| 3742 template <class Traits> | 3742 template <class Traits> |
| 3743 MaybeObject* FixedTypedArray<Traits>::get(int index) { | 3743 MaybeObject* FixedTypedArray<Traits>::get(int index) { |
| 3744 return Traits::ToObject(GetHeap(), get_scalar(index)); | 3744 return Traits::ToObject(GetHeap(), get_scalar(index)); |
| 3745 } | 3745 } |
| 3746 | 3746 |
| 3747 |
| 3748 inline uint8_t clampObjectToUint8(Object* value) { |
| 3749 uint8_t clamped_value = 0; |
| 3750 if (value->IsSmi()) { |
| 3751 int int_value = Smi::cast(value)->value(); |
| 3752 if (int_value < 0) { |
| 3753 clamped_value = 0; |
| 3754 } else if (int_value > 255) { |
| 3755 clamped_value = 255; |
| 3756 } else { |
| 3757 clamped_value = static_cast<uint8_t>(int_value); |
| 3758 } |
| 3759 } else if (value->IsHeapNumber()) { |
| 3760 double double_value = HeapNumber::cast(value)->value(); |
| 3761 if (!(double_value > 0)) { |
| 3762 // NaN and less than zero clamp to zero. |
| 3763 clamped_value = 0; |
| 3764 } else if (double_value > 255) { |
| 3765 // Greater than 255 clamp to 255. |
| 3766 clamped_value = 255; |
| 3767 } else { |
| 3768 // Other doubles are rounded to the nearest integer. |
| 3769 clamped_value = static_cast<uint8_t>(lrint(double_value)); |
| 3770 } |
| 3771 } else { |
| 3772 // Clamp undefined to zero (default). All other types have been |
| 3773 // converted to a number type further up in the call chain. |
| 3774 ASSERT(value->IsUndefined()); |
| 3775 } |
| 3776 return clamped_value; |
| 3777 } |
| 3778 |
| 3779 |
| 3780 template<> inline |
| 3781 uint8_t FixedTypedArray<Uint8ClampedArrayTraits>::castObject(Object* value) { |
| 3782 return clampObjectToUint8(value); |
| 3783 } |
| 3784 |
| 3785 |
| 3786 template <class Traits> |
| 3787 typename Traits::ElementType FixedTypedArray<Traits>::castObject( |
| 3788 Object* value) { |
| 3789 ElementType result = Traits::defaultValue(); |
| 3790 if (value->IsSmi()) { |
| 3791 int int_value = Smi::cast(value)->value(); |
| 3792 result = static_cast<ElementType>(int_value); |
| 3793 } else if (value->IsHeapNumber()) { |
| 3794 double double_value = HeapNumber::cast(value)->value(); |
| 3795 result = static_cast<ElementType>(DoubleToInt32(double_value)); |
| 3796 } else { |
| 3797 // Clamp undefined to the default value. All other types have been |
| 3798 // converted to a number type further up in the call chain. |
| 3799 ASSERT(value->IsUndefined()); |
| 3800 } |
| 3801 return result; |
| 3802 } |
| 3803 |
| 3804 |
| 3747 template <class Traits> | 3805 template <class Traits> |
| 3748 MaybeObject* FixedTypedArray<Traits>::SetValue(uint32_t index, Object* value) { | 3806 MaybeObject* FixedTypedArray<Traits>::SetValue(uint32_t index, Object* value) { |
| 3749 ElementType cast_value = Traits::defaultValue(); | 3807 ElementType cast_value = Traits::defaultValue(); |
| 3750 if (index < static_cast<uint32_t>(length())) { | 3808 if (index < static_cast<uint32_t>(length())) { |
| 3751 if (value->IsSmi()) { | 3809 cast_value = castObject(value); |
| 3752 int int_value = Smi::cast(value)->value(); | |
| 3753 cast_value = static_cast<ElementType>(int_value); | |
| 3754 } else if (value->IsHeapNumber()) { | |
| 3755 double double_value = HeapNumber::cast(value)->value(); | |
| 3756 cast_value = static_cast<ElementType>(DoubleToInt32(double_value)); | |
| 3757 } else { | |
| 3758 // Clamp undefined to the default value. All other types have been | |
| 3759 // converted to a number type further up in the call chain. | |
| 3760 ASSERT(value->IsUndefined()); | |
| 3761 } | |
| 3762 set(index, cast_value); | 3810 set(index, cast_value); |
| 3763 } | 3811 } |
| 3764 return Traits::ToObject(GetHeap(), cast_value); | 3812 return Traits::ToObject(GetHeap(), cast_value); |
| 3765 } | 3813 } |
| 3766 | 3814 |
| 3767 template <class Traits> | 3815 template <class Traits> |
| 3768 Handle<Object> FixedTypedArray<Traits>::SetValue( | 3816 Handle<Object> FixedTypedArray<Traits>::SetValue( |
| 3769 Handle<FixedTypedArray<Traits> > array, | 3817 Handle<FixedTypedArray<Traits> > array, |
| 3770 uint32_t index, | 3818 uint32_t index, |
| 3771 Handle<Object> value) { | 3819 Handle<Object> value) { |
| (...skipping 3017 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6789 #undef READ_UINT32_FIELD | 6837 #undef READ_UINT32_FIELD |
| 6790 #undef WRITE_UINT32_FIELD | 6838 #undef WRITE_UINT32_FIELD |
| 6791 #undef READ_SHORT_FIELD | 6839 #undef READ_SHORT_FIELD |
| 6792 #undef WRITE_SHORT_FIELD | 6840 #undef WRITE_SHORT_FIELD |
| 6793 #undef READ_BYTE_FIELD | 6841 #undef READ_BYTE_FIELD |
| 6794 #undef WRITE_BYTE_FIELD | 6842 #undef WRITE_BYTE_FIELD |
| 6795 | 6843 |
| 6796 } } // namespace v8::internal | 6844 } } // namespace v8::internal |
| 6797 | 6845 |
| 6798 #endif // V8_OBJECTS_INL_H_ | 6846 #endif // V8_OBJECTS_INL_H_ |
| OLD | NEW |