Chromium Code Reviews| Index: runtime/vm/object.h |
| diff --git a/runtime/vm/object.h b/runtime/vm/object.h |
| index b10a26689a326be397cec18f1ec48f5cfe2654cb..c91738c1bf4dde874c63661e129a4a9feb40dfad 100644 |
| --- a/runtime/vm/object.h |
| +++ b/runtime/vm/object.h |
| @@ -178,7 +178,7 @@ class Object { |
| // Class testers. |
| #define DEFINE_CLASS_TESTER(clazz) \ |
| virtual bool Is##clazz() const { return false; } |
| -CLASS_LIST_NO_OBJECT(DEFINE_CLASS_TESTER); |
| +CLASS_LIST_NO_OBJECT_WITH_STRING(DEFINE_CLASS_TESTER); |
| #undef DEFINE_CLASS_TESTER |
| bool IsNull() const { return raw_ == null_; } |
| @@ -715,6 +715,9 @@ class Class : public Object { |
| const String& name, |
| int num_fields); |
| + // Allocate the raw string classes. |
| + static RawClass* NewStringClass(intptr_t class_id); |
| + |
| // Allocate a class representing a function signature described by |
| // signature_function, which must be a closure function or a signature |
| // function. |
| @@ -3659,9 +3662,9 @@ class String : public Instance { |
| static intptr_t Hash(const uint16_t* characters, intptr_t len); |
| static intptr_t Hash(const uint32_t* characters, intptr_t len); |
| - virtual int32_t CharAt(intptr_t index) const; |
| + int32_t CharAt(intptr_t index) const; |
| - virtual intptr_t CharSize() const; |
| + intptr_t CharSize() const; |
| inline bool Equals(const String& str) const; |
| inline bool Equals(const String& str, |
| @@ -3682,12 +3685,28 @@ class String : public Instance { |
| bool IsSymbol() const { return raw()->IsCanonical(); } |
| - virtual bool IsExternal() const { return false; } |
| - virtual void* GetPeer() const { |
| - UNREACHABLE(); |
| - return NULL; |
| + bool IsOneByteString() const { |
| + return raw()->GetClassId() == kOneByteStringCid; |
| + } |
| + |
| + bool IsTwoByteString() const { |
| + return raw()->GetClassId() == kTwoByteStringCid; |
| + } |
| + |
| + bool IsExternalOneByteString() const { |
| + return raw()->GetClassId() == kExternalOneByteStringCid; |
| + } |
| + |
| + bool IsExternalTwoByteString() const { |
| + return raw()->GetClassId() == kExternalTwoByteStringCid; |
| + } |
| + |
| + bool IsExternal() const { |
| + return RawObject::IsExternalStringClassId(raw()->GetClassId()); |
| } |
| + void* GetPeer() const; |
| + |
| void ToUTF8(uint8_t* utf8_array, intptr_t array_len) const; |
| // Creates a new String object from a C string that is assumed to contain |
| @@ -3791,30 +3810,33 @@ class String : public Instance { |
| template<typename HandleType, typename ElementType> |
|
siva
2012/11/05 19:23:38
HandleType should probably be called StringType as
|
| static void ReadFromImpl(SnapshotReader* reader, |
| - HandleType* str_obj, |
| + String* str_obj, |
| intptr_t len, |
| intptr_t tags, |
| Snapshot::Kind kind); |
| HEAP_OBJECT_IMPLEMENTATION(String, Instance); |
| + friend class Class; |
| friend class Symbols; |
| + friend class OneByteString; |
| + friend class TwoByteString; |
| + friend class ExternalOneByteString; |
| + friend class ExternalTwoByteString; |
| }; |
| -class OneByteString : public String { |
| +class OneByteString : public AllStatic { |
| public: |
| - virtual int32_t CharAt(intptr_t index) const { |
| - return *CharAddr(index); |
| + static int32_t CharAt(const String& str, intptr_t index) { |
| + return *CharAddr(str, index); |
| } |
| - virtual intptr_t CharSize() const { |
| - return kOneByteChar; |
| - } |
| + static RawOneByteString* EscapeSpecialCharacters(const String& str, |
| + bool raw_str); |
| - RawOneByteString* EscapeSpecialCharacters(bool raw_str) const; |
| - |
| - bool EqualsIgnoringPrivateKey(const OneByteString& str) const; |
| + static bool EqualsIgnoringPrivateKey(const String& str1, |
| + const String& str2); |
| // We use the same maximum elements for all strings. |
| static const intptr_t kBytesPerElement = 1; |
| @@ -3828,9 +3850,9 @@ class OneByteString : public String { |
| } |
| static intptr_t InstanceSize(intptr_t len) { |
| - ASSERT(sizeof(RawOneByteString) == kSizeofRawString); |
| + ASSERT(sizeof(RawOneByteString) == String::kSizeofRawString); |
| ASSERT(0 <= len && len <= kMaxElements); |
| - return RoundedAllocationSize( |
| + return String::RoundedAllocationSize( |
| sizeof(RawOneByteString) + (len * kBytesPerElement)); |
| } |
| @@ -3851,7 +3873,7 @@ class OneByteString : public String { |
| static RawOneByteString* New(const uint32_t* characters, |
| intptr_t len, |
| Heap::Space space); |
| - static RawOneByteString* New(const OneByteString& str, |
| + static RawOneByteString* New(const String& str, |
| Heap::Space space); |
| static RawOneByteString* Concat(const String& str1, |
| @@ -3865,30 +3887,47 @@ class OneByteString : public String { |
| const String& str, |
| Heap::Space space); |
| + static const ClassId kClassId = kOneByteStringCid; |
| + |
| + static RawOneByteString* null() { |
| + return reinterpret_cast<RawOneByteString*>(Object::null()); |
| + } |
| + |
| private: |
| - uint8_t* CharAddr(intptr_t index) const { |
| - // TODO(iposva): Determine if we should throw an exception here. |
| - ASSERT((index >= 0) && (index < Length())); |
| - return &raw_ptr()->data_[index]; |
| + static RawOneByteString* raw(const String& str) { |
| + return reinterpret_cast<RawOneByteString*>(str.raw()); |
| + } |
| + |
| + static RawOneByteString* raw_ptr(const String& str) { |
| + return reinterpret_cast<RawOneByteString*>(str.raw_ptr()); |
| + } |
| + |
| + static uint8_t* CharAddr(const String& str, intptr_t index) { |
| + ASSERT((index >= 0) && (index < str.Length())); |
| + ASSERT(str.IsOneByteString()); |
| + NoGCScope no_gc; |
| + return &raw_ptr(str)->data_[index]; |
| } |
| - HEAP_OBJECT_IMPLEMENTATION(OneByteString, String); |
| + static RawOneByteString* ReadFrom(SnapshotReader* reader, |
| + intptr_t object_id, |
| + intptr_t tags, |
| + Snapshot::Kind kind); |
| + |
| friend class Class; |
| friend class String; |
| + friend class SnapshotReader; |
| }; |
| -class TwoByteString : public String { |
| +class TwoByteString : public AllStatic { |
| public: |
| - virtual int32_t CharAt(intptr_t index) const { |
| - return *CharAddr(index); |
| + static int32_t CharAt(const String& str, intptr_t index) { |
| + return *CharAddr(str, index); |
| } |
| - virtual intptr_t CharSize() const { |
| - return kTwoByteChar; |
| - } |
| - |
| - RawTwoByteString* EscapeSpecialCharacters(bool raw_str) const; |
| + static RawTwoByteString* EscapeSpecialCharacters(const String& str, |
| + bool raw_str); |
| // We use the same maximum elements for all strings. |
| static const intptr_t kBytesPerElement = 2; |
| @@ -3902,9 +3941,9 @@ class TwoByteString : public String { |
| } |
| static intptr_t InstanceSize(intptr_t len) { |
| - ASSERT(sizeof(RawTwoByteString) == kSizeofRawString); |
| + ASSERT(sizeof(RawTwoByteString) == String::kSizeofRawString); |
| ASSERT(0 <= len && len <= kMaxElements); |
| - return RoundedAllocationSize( |
| + return String::RoundedAllocationSize( |
| sizeof(RawTwoByteString) + (len * kBytesPerElement)); |
| } |
| @@ -3917,7 +3956,7 @@ class TwoByteString : public String { |
| const uint32_t* characters, |
| intptr_t len, |
| Heap::Space space); |
| - static RawTwoByteString* New(const TwoByteString& str, |
| + static RawTwoByteString* New(const String& str, |
| Heap::Space space); |
| static RawTwoByteString* Concat(const String& str1, |
| @@ -3931,31 +3970,53 @@ class TwoByteString : public String { |
| const String& str, |
| Heap::Space space); |
| + static RawTwoByteString* null() { |
| + return reinterpret_cast<RawTwoByteString*>(Object::null()); |
| + } |
| + |
| + |
| + static const ClassId kClassId = kTwoByteStringCid; |
| + |
| private: |
| - uint16_t* CharAddr(intptr_t index) const { |
| - ASSERT((index >= 0) && (index < Length())); |
| - return &raw_ptr()->data_[index]; |
| + static RawTwoByteString* raw(const String& str) { |
| + return reinterpret_cast<RawTwoByteString*>(str.raw()); |
| + } |
| + |
| + static RawTwoByteString* raw_ptr(const String& str) { |
| + return reinterpret_cast<RawTwoByteString*>(str.raw_ptr()); |
| } |
| - HEAP_OBJECT_IMPLEMENTATION(TwoByteString, String); |
| + static uint16_t* CharAddr(const String& str, intptr_t index) { |
| + ASSERT((index >= 0) && (index < str.Length())); |
| + ASSERT(str.IsTwoByteString()); |
| + NoGCScope no_gc; |
| + return &raw_ptr(str)->data_[index]; |
| + } |
| + |
| + static RawTwoByteString* ReadFrom(SnapshotReader* reader, |
| + intptr_t object_id, |
| + intptr_t tags, |
| + Snapshot::Kind kind); |
| + |
| friend class Class; |
| friend class String; |
| + friend class SnapshotReader; |
| }; |
| -class ExternalOneByteString : public String { |
| - public: |
| - virtual int32_t CharAt(intptr_t index) const { |
| - return *CharAddr(index); |
| +class ExternalOneByteString : public AllStatic { |
| + private: |
| + static RawExternalOneByteString* raw_str(const String& str) { |
| + return reinterpret_cast<RawExternalOneByteString*>(str.raw_ptr()); |
| } |
|
siva
2012/11/05 19:23:38
Normally we put private methods after all the publ
Tom Ball
2012/11/05 21:22:41
This method was added before the other methods wer
|
| - virtual intptr_t CharSize() const { |
| - return kOneByteChar; |
| + public: |
| + static int32_t CharAt(const String& str, intptr_t index) { |
| + return *CharAddr(str, index); |
| } |
| - virtual bool IsExternal() const { return true; } |
| - virtual void* GetPeer() const { |
| - return raw_ptr()->external_data_->peer(); |
| + static void* GetPeer(const String& str) { |
| + return raw_str(str)->external_data_->peer(); |
| } |
| // We use the same maximum elements for all strings. |
| @@ -3963,7 +4024,7 @@ class ExternalOneByteString : public String { |
| static const intptr_t kMaxElements = String::kMaxElements; |
| static intptr_t InstanceSize() { |
| - return RoundedAllocationSize(sizeof(RawExternalOneByteString)); |
| + return String::RoundedAllocationSize(sizeof(RawExternalOneByteString)); |
| } |
| static RawExternalOneByteString* New(const uint8_t* characters, |
| @@ -3972,38 +4033,57 @@ class ExternalOneByteString : public String { |
| Dart_PeerFinalizer callback, |
| Heap::Space space); |
| + static RawExternalOneByteString* null() { |
| + return reinterpret_cast<RawExternalOneByteString*>(Object::null()); |
| + } |
| + |
| + static const ClassId kClassId = kExternalOneByteStringCid; |
| + |
| private: |
| - const uint8_t* CharAddr(intptr_t index) const { |
| - // TODO(iposva): Determine if we should throw an exception here. |
| - ASSERT((index >= 0) && (index < Length())); |
| - return &(raw_ptr()->external_data_->data()[index]); |
| + static RawExternalOneByteString* raw(const String& str) { |
| + return reinterpret_cast<RawExternalOneByteString*>(str.raw()); |
| } |
| - void SetExternalData(ExternalStringData<uint8_t>* data) { |
| - raw_ptr()->external_data_ = data; |
| + static const uint8_t* CharAddr(const String& str, intptr_t index) { |
| + ASSERT((index >= 0) && (index < str.Length())); |
| + ASSERT(str.IsExternalOneByteString()); |
| + NoGCScope no_gc; |
| + return &(raw_str(str)->external_data_->data()[index]); |
| + } |
| + |
| + static void SetExternalData(const String& str, |
| + ExternalStringData<uint8_t>* data) { |
| + ASSERT(str.IsExternalOneByteString()); |
| + NoGCScope no_gc; |
| + raw_str(str)->external_data_ = data; |
| } |
| static void Finalize(Dart_Handle handle, void* peer); |
| - HEAP_OBJECT_IMPLEMENTATION(ExternalOneByteString, String); |
| + static RawExternalOneByteString* ReadFrom(SnapshotReader* reader, |
| + intptr_t object_id, |
| + intptr_t tags, |
| + Snapshot::Kind kind); |
| + |
| friend class Class; |
| friend class String; |
| + friend class SnapshotReader; |
| }; |
| -class ExternalTwoByteString : public String { |
| - public: |
| - virtual int32_t CharAt(intptr_t index) const { |
| - return *CharAddr(index); |
| +class ExternalTwoByteString : public AllStatic { |
| + private: |
| + static RawExternalTwoByteString* raw_str(const String& str) { |
| + return reinterpret_cast<RawExternalTwoByteString*>(str.raw_ptr()); |
| } |
|
siva
2012/11/05 19:23:38
Ditto comment here regarding private method and th
Tom Ball
2012/11/05 21:22:41
Done.
|
| - virtual intptr_t CharSize() const { |
| - return kTwoByteChar; |
| + public: |
| + static int32_t CharAt(const String& str, intptr_t index) { |
| + return *CharAddr(str, index); |
| } |
| - virtual bool IsExternal() const { return true; } |
| - virtual void* GetPeer() const { |
| - return raw_ptr()->external_data_->peer(); |
| + static void* GetPeer(const String& str) { |
| + return raw_str(str)->external_data_->peer(); |
| } |
| // We use the same maximum elements for all strings. |
| @@ -4011,7 +4091,7 @@ class ExternalTwoByteString : public String { |
| static const intptr_t kMaxElements = String::kMaxElements; |
| static intptr_t InstanceSize() { |
| - return RoundedAllocationSize(sizeof(RawExternalTwoByteString)); |
| + return String::RoundedAllocationSize(sizeof(RawExternalTwoByteString)); |
| } |
| static RawExternalTwoByteString* New(const uint16_t* characters, |
| @@ -4020,22 +4100,41 @@ class ExternalTwoByteString : public String { |
| Dart_PeerFinalizer callback, |
| Heap::Space space = Heap::kNew); |
| + static RawExternalTwoByteString* null() { |
| + return reinterpret_cast<RawExternalTwoByteString*>(Object::null()); |
| + } |
| + |
| + static const ClassId kClassId = kExternalTwoByteStringCid; |
| + |
| private: |
| - const uint16_t* CharAddr(intptr_t index) const { |
| - // TODO(iposva): Determine if we should throw an exception here. |
| - ASSERT((index >= 0) && (index < Length())); |
| - return &(raw_ptr()->external_data_->data()[index]); |
| + static RawExternalTwoByteString* raw(const String& str) { |
| + return reinterpret_cast<RawExternalTwoByteString*>(str.raw()); |
| } |
| - void SetExternalData(ExternalStringData<uint16_t>* data) { |
| - raw_ptr()->external_data_ = data; |
| + static const uint16_t* CharAddr(const String& str, intptr_t index) { |
| + ASSERT((index >= 0) && (index < str.Length())); |
| + ASSERT(str.IsExternalTwoByteString()); |
| + NoGCScope no_gc; |
| + return &(raw_str(str)->external_data_->data()[index]); |
| + } |
| + |
| + static void SetExternalData(const String& str, |
| + ExternalStringData<uint16_t>* data) { |
| + ASSERT(str.IsExternalTwoByteString()); |
| + NoGCScope no_gc; |
| + raw_str(str)->external_data_ = data; |
| } |
| static void Finalize(Dart_Handle handle, void* peer); |
| - HEAP_OBJECT_IMPLEMENTATION(ExternalTwoByteString, String); |
| + static RawExternalTwoByteString* ReadFrom(SnapshotReader* reader, |
| + intptr_t object_id, |
| + intptr_t tags, |
| + Snapshot::Kind kind); |
| + |
| friend class Class; |
| friend class String; |
| + friend class SnapshotReader; |
| }; |