OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #ifndef VM_OBJECT_H_ | 5 #ifndef VM_OBJECT_H_ |
6 #define VM_OBJECT_H_ | 6 #define VM_OBJECT_H_ |
7 | 7 |
8 #include "include/dart_api.h" | 8 #include "include/dart_api.h" |
9 #include "platform/assert.h" | 9 #include "platform/assert.h" |
10 #include "platform/utils.h" | 10 #include "platform/utils.h" |
(...skipping 3589 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3600 | 3600 |
3601 // String may not be '\0' terminated. | 3601 // String may not be '\0' terminated. |
3602 class String : public Instance { | 3602 class String : public Instance { |
3603 public: | 3603 public: |
3604 // We use 30 bits for the hash code so that we consistently use a | 3604 // We use 30 bits for the hash code so that we consistently use a |
3605 // 32bit Smi representation for the hash code on all architectures. | 3605 // 32bit Smi representation for the hash code on all architectures. |
3606 static const intptr_t kHashBits = 30; | 3606 static const intptr_t kHashBits = 30; |
3607 | 3607 |
3608 static const intptr_t kOneByteChar = 1; | 3608 static const intptr_t kOneByteChar = 1; |
3609 static const intptr_t kTwoByteChar = 2; | 3609 static const intptr_t kTwoByteChar = 2; |
3610 static const intptr_t kFourByteChar = 4; | |
3611 | 3610 |
3612 // All strings share the same maximum element count to keep things | 3611 // All strings share the same maximum element count to keep things |
3613 // simple. We choose a value that will prevent integer overflow for | 3612 // simple. We choose a value that will prevent integer overflow for |
3614 // 4 byte strings, since it is the worst case. | 3613 // 2 byte strings, since it is the worst case. |
3615 static const intptr_t kSizeofRawString = sizeof(RawObject) + (2 * kWordSize); | 3614 static const intptr_t kSizeofRawString = sizeof(RawObject) + (2 * kWordSize); |
3616 static const intptr_t kMaxElements = kSmiMax / kFourByteChar; | 3615 static const intptr_t kMaxElements = kSmiMax / kTwoByteChar; |
3617 | 3616 |
3618 intptr_t Length() const { return Smi::Value(raw_ptr()->length_); } | 3617 intptr_t Length() const { return Smi::Value(raw_ptr()->length_); } |
3619 static intptr_t length_offset() { return OFFSET_OF(RawString, length_); } | 3618 static intptr_t length_offset() { return OFFSET_OF(RawString, length_); } |
3620 | 3619 |
3621 virtual intptr_t Hash() const; | 3620 virtual intptr_t Hash() const; |
3622 static intptr_t hash_offset() { return OFFSET_OF(RawString, hash_); } | 3621 static intptr_t hash_offset() { return OFFSET_OF(RawString, hash_); } |
3623 static intptr_t Hash(const String& str, intptr_t begin_index, intptr_t len); | 3622 static intptr_t Hash(const String& str, intptr_t begin_index, intptr_t len); |
3624 static intptr_t Hash(const uint8_t* characters, intptr_t len); | 3623 static intptr_t Hash(const uint8_t* characters, intptr_t len); |
3625 static intptr_t Hash(const uint16_t* characters, intptr_t len); | 3624 static intptr_t Hash(const uint16_t* characters, intptr_t len); |
3626 static intptr_t Hash(const uint32_t* characters, intptr_t len); | 3625 static intptr_t Hash(const uint32_t* characters, intptr_t len); |
(...skipping 20 matching lines...) Expand all Loading... |
3647 virtual RawInstance* Canonicalize() const; | 3646 virtual RawInstance* Canonicalize() const; |
3648 | 3647 |
3649 bool IsSymbol() const { return raw()->IsCanonical(); } | 3648 bool IsSymbol() const { return raw()->IsCanonical(); } |
3650 | 3649 |
3651 virtual bool IsExternal() const { return false; } | 3650 virtual bool IsExternal() const { return false; } |
3652 virtual void* GetPeer() const { | 3651 virtual void* GetPeer() const { |
3653 UNREACHABLE(); | 3652 UNREACHABLE(); |
3654 return NULL; | 3653 return NULL; |
3655 } | 3654 } |
3656 | 3655 |
3657 static RawString* New(const char* str, Heap::Space space = Heap::kNew); | 3656 void ToUTF8(uint8_t* utf8_array, intptr_t array_len) const; |
3658 static RawString* New(const uint8_t* characters, | 3657 |
3659 intptr_t len, | 3658 // Creates a new String object from a C string that is assumed to contain |
| 3659 // UTF-8 encoded characters and '\0' is considered a termination character. |
| 3660 static RawString* New(const char* cstr, Heap::Space space = Heap::kNew); |
| 3661 |
| 3662 // Creates a new String object from an array of UTF-8 encoded characters. |
| 3663 static RawString* New(const uint8_t* utf8_array, |
| 3664 intptr_t array_len, |
3660 Heap::Space space = Heap::kNew); | 3665 Heap::Space space = Heap::kNew); |
3661 static RawString* New(const uint16_t* characters, | 3666 |
3662 intptr_t len, | 3667 // Creates a new String object from an array of UTF-16 encoded characters. |
| 3668 static RawString* New(const uint16_t* utf16_array, |
| 3669 intptr_t array_len, |
3663 Heap::Space space = Heap::kNew); | 3670 Heap::Space space = Heap::kNew); |
3664 static RawString* New(const uint32_t* characters, | 3671 |
3665 intptr_t len, | 3672 // Creates a new String object from an array of UTF-32 encoded characters. |
| 3673 static RawString* New(const uint32_t* utf32_array, |
| 3674 intptr_t array_len, |
3666 Heap::Space space = Heap::kNew); | 3675 Heap::Space space = Heap::kNew); |
| 3676 |
| 3677 // Create a new String object from another Dart String instance. |
3667 static RawString* New(const String& str, Heap::Space space = Heap::kNew); | 3678 static RawString* New(const String& str, Heap::Space space = Heap::kNew); |
3668 | 3679 |
3669 static RawString* NewExternal(const uint8_t* characters, | 3680 // Creates a new External String object using the specified array of |
3670 intptr_t len, | 3681 // UTF-8 encoded characters as the external reference. |
| 3682 static RawString* NewExternal(const uint8_t* utf8_array, |
| 3683 intptr_t array_len, |
3671 void* peer, | 3684 void* peer, |
3672 Dart_PeerFinalizer callback, | 3685 Dart_PeerFinalizer callback, |
3673 Heap::Space = Heap::kNew); | 3686 Heap::Space = Heap::kNew); |
3674 static RawString* NewExternal(const uint16_t* characters, | 3687 |
3675 intptr_t len, | 3688 // Creates a new External String object using the specified array of |
3676 void* peer, | 3689 // UTF-16 encoded characters as the external reference. |
3677 Dart_PeerFinalizer callback, | 3690 static RawString* NewExternal(const uint16_t* utf16_array, |
3678 Heap::Space = Heap::kNew); | 3691 intptr_t array_len, |
3679 static RawString* NewExternal(const uint32_t* characters, | |
3680 intptr_t len, | |
3681 void* peer, | 3692 void* peer, |
3682 Dart_PeerFinalizer callback, | 3693 Dart_PeerFinalizer callback, |
3683 Heap::Space = Heap::kNew); | 3694 Heap::Space = Heap::kNew); |
3684 | 3695 |
3685 static void Copy(const String& dst, | 3696 static void Copy(const String& dst, |
3686 intptr_t dst_offset, | 3697 intptr_t dst_offset, |
3687 const uint8_t* characters, | 3698 const uint8_t* characters, |
3688 intptr_t len); | 3699 intptr_t len); |
3689 static void Copy(const String& dst, | 3700 static void Copy(const String& dst, |
3690 intptr_t dst_offset, | 3701 intptr_t dst_offset, |
3691 const uint16_t* characters, | 3702 const uint16_t* characters, |
3692 intptr_t len); | 3703 intptr_t len); |
3693 static void Copy(const String& dst, | 3704 static void Copy(const String& dst, |
3694 intptr_t dst_offset, | 3705 intptr_t dst_offset, |
3695 const uint32_t* characters, | |
3696 intptr_t len); | |
3697 static void Copy(const String& dst, | |
3698 intptr_t dst_offset, | |
3699 const String& src, | 3706 const String& src, |
3700 intptr_t src_offset, | 3707 intptr_t src_offset, |
3701 intptr_t len); | 3708 intptr_t len); |
3702 | 3709 |
3703 static RawString* EscapeSpecialCharacters(const String& str, bool raw_str); | 3710 static RawString* EscapeSpecialCharacters(const String& str, bool raw_str); |
3704 | 3711 |
3705 static RawString* Concat(const String& str1, | 3712 static RawString* Concat(const String& str1, |
3706 const String& str2, | 3713 const String& str2, |
3707 Heap::Space space = Heap::kNew); | 3714 Heap::Space space = Heap::kNew); |
3708 static RawString* ConcatAll(const Array& strings, | 3715 static RawString* ConcatAll(const Array& strings, |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3862 ASSERT(0 <= len && len <= kMaxElements); | 3869 ASSERT(0 <= len && len <= kMaxElements); |
3863 return RoundedAllocationSize( | 3870 return RoundedAllocationSize( |
3864 sizeof(RawTwoByteString) + (len * kBytesPerElement)); | 3871 sizeof(RawTwoByteString) + (len * kBytesPerElement)); |
3865 } | 3872 } |
3866 | 3873 |
3867 static RawTwoByteString* New(intptr_t len, | 3874 static RawTwoByteString* New(intptr_t len, |
3868 Heap::Space space); | 3875 Heap::Space space); |
3869 static RawTwoByteString* New(const uint16_t* characters, | 3876 static RawTwoByteString* New(const uint16_t* characters, |
3870 intptr_t len, | 3877 intptr_t len, |
3871 Heap::Space space); | 3878 Heap::Space space); |
3872 static RawTwoByteString* New(const uint32_t* characters, | 3879 static RawTwoByteString* New(intptr_t utf16_len, |
| 3880 const uint32_t* characters, |
3873 intptr_t len, | 3881 intptr_t len, |
3874 Heap::Space space); | 3882 Heap::Space space); |
3875 static RawTwoByteString* New(const TwoByteString& str, | 3883 static RawTwoByteString* New(const TwoByteString& str, |
3876 Heap::Space space); | 3884 Heap::Space space); |
3877 | 3885 |
3878 static RawTwoByteString* Concat(const String& str1, | 3886 static RawTwoByteString* Concat(const String& str1, |
3879 const String& str2, | 3887 const String& str2, |
3880 Heap::Space space); | 3888 Heap::Space space); |
3881 static RawTwoByteString* ConcatAll(const Array& strings, | 3889 static RawTwoByteString* ConcatAll(const Array& strings, |
3882 intptr_t len, | 3890 intptr_t len, |
3883 Heap::Space space); | 3891 Heap::Space space); |
3884 | 3892 |
3885 static RawTwoByteString* Transform(int32_t (*mapping)(int32_t ch), | 3893 static RawTwoByteString* Transform(int32_t (*mapping)(int32_t ch), |
3886 const String& str, | 3894 const String& str, |
3887 Heap::Space space); | 3895 Heap::Space space); |
3888 | 3896 |
3889 private: | 3897 private: |
3890 uint16_t* CharAddr(intptr_t index) const { | 3898 uint16_t* CharAddr(intptr_t index) const { |
3891 ASSERT((index >= 0) && (index < Length())); | 3899 ASSERT((index >= 0) && (index < Length())); |
3892 return &raw_ptr()->data_[index]; | 3900 return &raw_ptr()->data_[index]; |
3893 } | 3901 } |
3894 | 3902 |
3895 HEAP_OBJECT_IMPLEMENTATION(TwoByteString, String); | 3903 HEAP_OBJECT_IMPLEMENTATION(TwoByteString, String); |
3896 friend class Class; | 3904 friend class Class; |
3897 friend class String; | 3905 friend class String; |
3898 }; | 3906 }; |
3899 | 3907 |
3900 | 3908 |
3901 class FourByteString : public String { | |
3902 public: | |
3903 virtual int32_t CharAt(intptr_t index) const { | |
3904 return *CharAddr(index); | |
3905 } | |
3906 | |
3907 virtual intptr_t CharSize() const { | |
3908 return kFourByteChar; | |
3909 } | |
3910 | |
3911 RawFourByteString* EscapeSpecialCharacters(bool raw_str) const; | |
3912 | |
3913 static const intptr_t kBytesPerElement = 4; | |
3914 static const intptr_t kMaxElements = String::kMaxElements; | |
3915 | |
3916 static intptr_t InstanceSize() { | |
3917 ASSERT(sizeof(RawFourByteString) == OFFSET_OF(RawFourByteString, data_)); | |
3918 return 0; | |
3919 } | |
3920 | |
3921 static intptr_t InstanceSize(intptr_t len) { | |
3922 ASSERT(sizeof(RawTwoByteString) == kSizeofRawString); | |
3923 ASSERT(0 <= len && len <= kMaxElements); | |
3924 return RoundedAllocationSize( | |
3925 sizeof(RawFourByteString) + (len * kBytesPerElement)); | |
3926 } | |
3927 | |
3928 static RawFourByteString* New(intptr_t len, | |
3929 Heap::Space space); | |
3930 static RawFourByteString* New(const uint32_t* characters, | |
3931 intptr_t len, | |
3932 Heap::Space space); | |
3933 static RawFourByteString* New(const FourByteString& str, | |
3934 Heap::Space space); | |
3935 | |
3936 static RawFourByteString* Concat(const String& str1, | |
3937 const String& str2, | |
3938 Heap::Space space); | |
3939 static RawFourByteString* ConcatAll(const Array& strings, | |
3940 intptr_t len, | |
3941 Heap::Space space); | |
3942 | |
3943 static RawFourByteString* Transform(int32_t (*mapping)(int32_t ch), | |
3944 const String& str, | |
3945 Heap::Space space); | |
3946 | |
3947 private: | |
3948 uint32_t* CharAddr(intptr_t index) const { | |
3949 ASSERT((index >= 0) && (index < Length())); | |
3950 return &raw_ptr()->data_[index]; | |
3951 } | |
3952 | |
3953 HEAP_OBJECT_IMPLEMENTATION(FourByteString, String); | |
3954 friend class Class; | |
3955 friend class String; | |
3956 }; | |
3957 | |
3958 | |
3959 class ExternalOneByteString : public String { | 3909 class ExternalOneByteString : public String { |
3960 public: | 3910 public: |
3961 virtual int32_t CharAt(intptr_t index) const { | 3911 virtual int32_t CharAt(intptr_t index) const { |
3962 return *CharAddr(index); | 3912 return *CharAddr(index); |
3963 } | 3913 } |
3964 | 3914 |
3965 virtual intptr_t CharSize() const { | 3915 virtual intptr_t CharSize() const { |
3966 return kOneByteChar; | 3916 return kOneByteChar; |
3967 } | 3917 } |
3968 | 3918 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4045 } | 3995 } |
4046 | 3996 |
4047 static void Finalize(Dart_Handle handle, void* peer); | 3997 static void Finalize(Dart_Handle handle, void* peer); |
4048 | 3998 |
4049 HEAP_OBJECT_IMPLEMENTATION(ExternalTwoByteString, String); | 3999 HEAP_OBJECT_IMPLEMENTATION(ExternalTwoByteString, String); |
4050 friend class Class; | 4000 friend class Class; |
4051 friend class String; | 4001 friend class String; |
4052 }; | 4002 }; |
4053 | 4003 |
4054 | 4004 |
4055 class ExternalFourByteString : public String { | |
4056 public: | |
4057 virtual int32_t CharAt(intptr_t index) const { | |
4058 return *CharAddr(index); | |
4059 } | |
4060 | |
4061 virtual intptr_t CharSize() const { | |
4062 return kFourByteChar; | |
4063 } | |
4064 | |
4065 virtual bool IsExternal() const { return true; } | |
4066 virtual void* GetPeer() const { | |
4067 return raw_ptr()->external_data_->peer(); | |
4068 } | |
4069 | |
4070 // We use the same maximum elements for all strings. | |
4071 static const intptr_t kBytesPerElement = 4; | |
4072 static const intptr_t kMaxElements = String::kMaxElements; | |
4073 | |
4074 static intptr_t InstanceSize() { | |
4075 return RoundedAllocationSize(sizeof(RawExternalFourByteString)); | |
4076 } | |
4077 | |
4078 static RawExternalFourByteString* New(const uint32_t* characters, | |
4079 intptr_t len, | |
4080 void* peer, | |
4081 Dart_PeerFinalizer callback, | |
4082 Heap::Space space = Heap::kNew); | |
4083 | |
4084 private: | |
4085 const uint32_t* CharAddr(intptr_t index) const { | |
4086 // TODO(iposva): Determine if we should throw an exception here. | |
4087 ASSERT((index >= 0) && (index < Length())); | |
4088 return &(raw_ptr()->external_data_->data()[index]); | |
4089 } | |
4090 | |
4091 void SetExternalData(ExternalStringData<uint32_t>* data) { | |
4092 raw_ptr()->external_data_ = data; | |
4093 } | |
4094 | |
4095 static void Finalize(Dart_Handle handle, void* peer); | |
4096 | |
4097 HEAP_OBJECT_IMPLEMENTATION(ExternalFourByteString, String); | |
4098 friend class Class; | |
4099 friend class String; | |
4100 }; | |
4101 | |
4102 | |
4103 // Class Bool implements Dart core class bool. | 4005 // Class Bool implements Dart core class bool. |
4104 class Bool : public Instance { | 4006 class Bool : public Instance { |
4105 public: | 4007 public: |
4106 bool value() const { | 4008 bool value() const { |
4107 return raw_ptr()->value_; | 4009 return raw_ptr()->value_; |
4108 } | 4010 } |
4109 | 4011 |
4110 static intptr_t InstanceSize() { | 4012 static intptr_t InstanceSize() { |
4111 return RoundedAllocationSize(sizeof(RawBool)); | 4013 return RoundedAllocationSize(sizeof(RawBool)); |
4112 } | 4014 } |
(...skipping 1657 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5770 if (this->CharAt(i) != str.CharAt(begin_index + i)) { | 5672 if (this->CharAt(i) != str.CharAt(begin_index + i)) { |
5771 return false; | 5673 return false; |
5772 } | 5674 } |
5773 } | 5675 } |
5774 return true; | 5676 return true; |
5775 } | 5677 } |
5776 | 5678 |
5777 } // namespace dart | 5679 } // namespace dart |
5778 | 5680 |
5779 #endif // VM_OBJECT_H_ | 5681 #endif // VM_OBJECT_H_ |
OLD | NEW |