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 1702 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1713 | 1713 |
1714 void JSObject::initialize_elements() { | 1714 void JSObject::initialize_elements() { |
1715 if (map()->has_fast_smi_or_object_elements() || | 1715 if (map()->has_fast_smi_or_object_elements() || |
1716 map()->has_fast_double_elements()) { | 1716 map()->has_fast_double_elements()) { |
1717 ASSERT(!GetHeap()->InNewSpace(GetHeap()->empty_fixed_array())); | 1717 ASSERT(!GetHeap()->InNewSpace(GetHeap()->empty_fixed_array())); |
1718 WRITE_FIELD(this, kElementsOffset, GetHeap()->empty_fixed_array()); | 1718 WRITE_FIELD(this, kElementsOffset, GetHeap()->empty_fixed_array()); |
1719 } else if (map()->has_external_array_elements()) { | 1719 } else if (map()->has_external_array_elements()) { |
1720 ExternalArray* empty_array = GetHeap()->EmptyExternalArrayForMap(map()); | 1720 ExternalArray* empty_array = GetHeap()->EmptyExternalArrayForMap(map()); |
1721 ASSERT(!GetHeap()->InNewSpace(empty_array)); | 1721 ASSERT(!GetHeap()->InNewSpace(empty_array)); |
1722 WRITE_FIELD(this, kElementsOffset, empty_array); | 1722 WRITE_FIELD(this, kElementsOffset, empty_array); |
| 1723 } else if (map()->has_fixed_typed_array_elements()) { |
| 1724 FixedTypedArrayBase* empty_array = |
| 1725 GetHeap()->EmptyFixedTypedArrayForMap(map()); |
| 1726 ASSERT(!GetHeap()->InNewSpace(empty_array)); |
| 1727 WRITE_FIELD(this, kElementsOffset, empty_array); |
1723 } else { | 1728 } else { |
1724 UNREACHABLE(); | 1729 UNREACHABLE(); |
1725 } | 1730 } |
1726 } | 1731 } |
1727 | 1732 |
1728 | 1733 |
1729 MaybeObject* JSObject::ResetElements() { | 1734 MaybeObject* JSObject::ResetElements() { |
1730 if (map()->is_observed()) { | 1735 if (map()->is_observed()) { |
1731 // Maintain invariant that observed elements are always in dictionary mode. | 1736 // Maintain invariant that observed elements are always in dictionary mode. |
1732 SeededNumberDictionary* dictionary; | 1737 SeededNumberDictionary* dictionary; |
(...skipping 1891 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3624 } | 3629 } |
3625 | 3630 |
3626 | 3631 |
3627 void ExternalFloat64Array::set(int index, double value) { | 3632 void ExternalFloat64Array::set(int index, double value) { |
3628 ASSERT((index >= 0) && (index < this->length())); | 3633 ASSERT((index >= 0) && (index < this->length())); |
3629 double* ptr = static_cast<double*>(external_pointer()); | 3634 double* ptr = static_cast<double*>(external_pointer()); |
3630 ptr[index] = value; | 3635 ptr[index] = value; |
3631 } | 3636 } |
3632 | 3637 |
3633 | 3638 |
| 3639 void* FixedTypedArrayBase::DataPtr() { |
| 3640 return FIELD_ADDR(this, kDataOffset); |
| 3641 } |
| 3642 |
| 3643 |
| 3644 int FixedTypedArrayBase::DataSize() { |
| 3645 InstanceType instance_type = map()->instance_type(); |
| 3646 int element_size; |
| 3647 switch (instance_type) { |
| 3648 #define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \ |
| 3649 case FIXED_##TYPE##_ARRAY_TYPE: \ |
| 3650 element_size = size; \ |
| 3651 break; |
| 3652 |
| 3653 TYPED_ARRAYS(TYPED_ARRAY_CASE) |
| 3654 #undef TYPED_ARRAY_CASE |
| 3655 default: |
| 3656 UNREACHABLE(); |
| 3657 return 0; |
| 3658 } |
| 3659 return length() * element_size; |
| 3660 } |
| 3661 |
| 3662 |
3634 int FixedTypedArrayBase::size() { | 3663 int FixedTypedArrayBase::size() { |
3635 InstanceType instance_type = map()->instance_type(); | 3664 return OBJECT_POINTER_ALIGN(kDataOffset + DataSize()); |
3636 int element_size; | |
3637 switch (instance_type) { | |
3638 case FIXED_UINT8_ARRAY_TYPE: | |
3639 case FIXED_INT8_ARRAY_TYPE: | |
3640 case FIXED_UINT8_CLAMPED_ARRAY_TYPE: | |
3641 element_size = 1; | |
3642 break; | |
3643 case FIXED_UINT16_ARRAY_TYPE: | |
3644 case FIXED_INT16_ARRAY_TYPE: | |
3645 element_size = 2; | |
3646 break; | |
3647 case FIXED_UINT32_ARRAY_TYPE: | |
3648 case FIXED_INT32_ARRAY_TYPE: | |
3649 case FIXED_FLOAT32_ARRAY_TYPE: | |
3650 element_size = 4; | |
3651 break; | |
3652 case FIXED_FLOAT64_ARRAY_TYPE: | |
3653 element_size = 8; | |
3654 break; | |
3655 default: | |
3656 UNREACHABLE(); | |
3657 return 0; | |
3658 } | |
3659 return OBJECT_POINTER_ALIGN(kDataOffset + length() * element_size); | |
3660 } | 3665 } |
3661 | 3666 |
3662 | 3667 |
| 3668 uint8_t Uint8ArrayTraits::defaultValue() { return 0; } |
| 3669 |
| 3670 |
| 3671 uint8_t Uint8ClampedArrayTraits::defaultValue() { return 0; } |
| 3672 |
| 3673 |
| 3674 int8_t Int8ArrayTraits::defaultValue() { return 0; } |
| 3675 |
| 3676 |
| 3677 uint16_t Uint16ArrayTraits::defaultValue() { return 0; } |
| 3678 |
| 3679 |
| 3680 int16_t Int16ArrayTraits::defaultValue() { return 0; } |
| 3681 |
| 3682 |
| 3683 uint32_t Uint32ArrayTraits::defaultValue() { return 0; } |
| 3684 |
| 3685 |
| 3686 int32_t Int32ArrayTraits::defaultValue() { return 0; } |
| 3687 |
| 3688 |
| 3689 float Float32ArrayTraits::defaultValue() { |
| 3690 return static_cast<double>(OS::nan_value()); |
| 3691 } |
| 3692 |
| 3693 |
| 3694 double Float64ArrayTraits::defaultValue() { return OS::nan_value(); } |
| 3695 |
| 3696 |
3663 template <class Traits> | 3697 template <class Traits> |
3664 typename Traits::ElementType FixedTypedArray<Traits>::get_scalar(int index) { | 3698 typename Traits::ElementType FixedTypedArray<Traits>::get_scalar(int index) { |
3665 ASSERT((index >= 0) && (index < this->length())); | 3699 ASSERT((index >= 0) && (index < this->length())); |
3666 ElementType* ptr = reinterpret_cast<ElementType*>( | 3700 ElementType* ptr = reinterpret_cast<ElementType*>( |
3667 FIELD_ADDR(this, kDataOffset)); | 3701 FIELD_ADDR(this, kDataOffset)); |
3668 return ptr[index]; | 3702 return ptr[index]; |
3669 } | 3703 } |
3670 | 3704 |
3671 | 3705 |
3672 template<> inline | 3706 template<> inline |
(...skipping 15 matching lines...) Expand all Loading... |
3688 | 3722 |
3689 template<> inline | 3723 template<> inline |
3690 void FixedTypedArray<Float64ArrayTraits>::set( | 3724 void FixedTypedArray<Float64ArrayTraits>::set( |
3691 int index, Float64ArrayTraits::ElementType value) { | 3725 int index, Float64ArrayTraits::ElementType value) { |
3692 ASSERT((index >= 0) && (index < this->length())); | 3726 ASSERT((index >= 0) && (index < this->length())); |
3693 WRITE_DOUBLE_FIELD(this, ElementOffset(index), value); | 3727 WRITE_DOUBLE_FIELD(this, ElementOffset(index), value); |
3694 } | 3728 } |
3695 | 3729 |
3696 | 3730 |
3697 template <class Traits> | 3731 template <class Traits> |
| 3732 typename Traits::ElementType FixedTypedArray<Traits>::from_int(int value) { |
| 3733 return static_cast<ElementType>(value); |
| 3734 } |
| 3735 |
| 3736 |
| 3737 template <> inline |
| 3738 uint8_t FixedTypedArray<Uint8ClampedArrayTraits>::from_int(int value) { |
| 3739 if (value < 0) return 0; |
| 3740 if (value > 0xFF) return 0xFF; |
| 3741 return static_cast<uint8_t>(value); |
| 3742 } |
| 3743 |
| 3744 |
| 3745 template <class Traits> |
| 3746 typename Traits::ElementType FixedTypedArray<Traits>::from_double( |
| 3747 double value) { |
| 3748 return static_cast<ElementType>(DoubleToInt32(value)); |
| 3749 } |
| 3750 |
| 3751 |
| 3752 template<> inline |
| 3753 uint8_t FixedTypedArray<Uint8ClampedArrayTraits>::from_double(double value) { |
| 3754 if (value < 0) return 0; |
| 3755 if (value > 0xFF) return 0xFF; |
| 3756 return static_cast<uint8_t>(lrint(value)); |
| 3757 } |
| 3758 |
| 3759 |
| 3760 template<> inline |
| 3761 float FixedTypedArray<Float32ArrayTraits>::from_double(double value) { |
| 3762 return static_cast<float>(value); |
| 3763 } |
| 3764 |
| 3765 |
| 3766 template<> inline |
| 3767 double FixedTypedArray<Float64ArrayTraits>::from_double(double value) { |
| 3768 return value; |
| 3769 } |
| 3770 |
| 3771 |
| 3772 template <class Traits> |
3698 MaybeObject* FixedTypedArray<Traits>::get(int index) { | 3773 MaybeObject* FixedTypedArray<Traits>::get(int index) { |
3699 return Traits::ToObject(GetHeap(), get_scalar(index)); | 3774 return Traits::ToObject(GetHeap(), get_scalar(index)); |
3700 } | 3775 } |
3701 | 3776 |
3702 template <class Traits> | 3777 template <class Traits> |
3703 MaybeObject* FixedTypedArray<Traits>::SetValue(uint32_t index, Object* value) { | 3778 MaybeObject* FixedTypedArray<Traits>::SetValue(uint32_t index, Object* value) { |
3704 ElementType cast_value = Traits::defaultValue(); | 3779 ElementType cast_value = Traits::defaultValue(); |
3705 if (index < static_cast<uint32_t>(length())) { | 3780 if (index < static_cast<uint32_t>(length())) { |
3706 if (value->IsSmi()) { | 3781 if (value->IsSmi()) { |
3707 int int_value = Smi::cast(value)->value(); | 3782 int int_value = Smi::cast(value)->value(); |
3708 cast_value = static_cast<ElementType>(int_value); | 3783 cast_value = from_int(int_value); |
3709 } else if (value->IsHeapNumber()) { | 3784 } else if (value->IsHeapNumber()) { |
3710 double double_value = HeapNumber::cast(value)->value(); | 3785 double double_value = HeapNumber::cast(value)->value(); |
3711 cast_value = static_cast<ElementType>(DoubleToInt32(double_value)); | 3786 cast_value = from_double(double_value); |
3712 } else { | 3787 } else { |
3713 // Clamp undefined to the default value. All other types have been | 3788 // Clamp undefined to the default value. All other types have been |
3714 // converted to a number type further up in the call chain. | 3789 // converted to a number type further up in the call chain. |
3715 ASSERT(value->IsUndefined()); | 3790 ASSERT(value->IsUndefined()); |
3716 } | 3791 } |
3717 set(index, cast_value); | 3792 set(index, cast_value); |
3718 } | 3793 } |
3719 return Traits::ToObject(GetHeap(), cast_value); | 3794 return Traits::ToObject(GetHeap(), cast_value); |
3720 } | 3795 } |
3721 | 3796 |
(...skipping 2242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5964 #undef EXTERNAL_ELEMENTS_CHECK | 6039 #undef EXTERNAL_ELEMENTS_CHECK |
5965 | 6040 |
5966 | 6041 |
5967 bool JSObject::HasFixedTypedArrayElements() { | 6042 bool JSObject::HasFixedTypedArrayElements() { |
5968 HeapObject* array = elements(); | 6043 HeapObject* array = elements(); |
5969 ASSERT(array != NULL); | 6044 ASSERT(array != NULL); |
5970 return array->IsFixedTypedArrayBase(); | 6045 return array->IsFixedTypedArrayBase(); |
5971 } | 6046 } |
5972 | 6047 |
5973 | 6048 |
| 6049 #define FIXED_TYPED_ELEMENTS_CHECK(Type, type, TYPE, ctype, size) \ |
| 6050 bool JSObject::HasFixed##Type##Elements() { \ |
| 6051 HeapObject* array = elements(); \ |
| 6052 ASSERT(array != NULL); \ |
| 6053 if (!array->IsHeapObject()) \ |
| 6054 return false; \ |
| 6055 return array->map()->instance_type() == FIXED_##TYPE##_ARRAY_TYPE; \ |
| 6056 } |
| 6057 |
| 6058 TYPED_ARRAYS(FIXED_TYPED_ELEMENTS_CHECK) |
| 6059 |
| 6060 #undef FIXED_TYPED_ELEMENTS_CHECK |
| 6061 |
| 6062 |
5974 bool JSObject::HasNamedInterceptor() { | 6063 bool JSObject::HasNamedInterceptor() { |
5975 return map()->has_named_interceptor(); | 6064 return map()->has_named_interceptor(); |
5976 } | 6065 } |
5977 | 6066 |
5978 | 6067 |
5979 bool JSObject::HasIndexedInterceptor() { | 6068 bool JSObject::HasIndexedInterceptor() { |
5980 return map()->has_indexed_interceptor(); | 6069 return map()->has_indexed_interceptor(); |
5981 } | 6070 } |
5982 | 6071 |
5983 | 6072 |
(...skipping 746 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6730 #undef READ_UINT32_FIELD | 6819 #undef READ_UINT32_FIELD |
6731 #undef WRITE_UINT32_FIELD | 6820 #undef WRITE_UINT32_FIELD |
6732 #undef READ_SHORT_FIELD | 6821 #undef READ_SHORT_FIELD |
6733 #undef WRITE_SHORT_FIELD | 6822 #undef WRITE_SHORT_FIELD |
6734 #undef READ_BYTE_FIELD | 6823 #undef READ_BYTE_FIELD |
6735 #undef WRITE_BYTE_FIELD | 6824 #undef WRITE_BYTE_FIELD |
6736 | 6825 |
6737 } } // namespace v8::internal | 6826 } } // namespace v8::internal |
6738 | 6827 |
6739 #endif // V8_OBJECTS_INL_H_ | 6828 #endif // V8_OBJECTS_INL_H_ |
OLD | NEW |