| 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 2678 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2689 void set_value(double value) const; | 2689 void set_value(double value) const; |
| 2690 | 2690 |
| 2691 HEAP_OBJECT_IMPLEMENTATION(Double, Number); | 2691 HEAP_OBJECT_IMPLEMENTATION(Double, Number); |
| 2692 friend class Class; | 2692 friend class Class; |
| 2693 }; | 2693 }; |
| 2694 | 2694 |
| 2695 | 2695 |
| 2696 // String may not be '\0' terminated. | 2696 // String may not be '\0' terminated. |
| 2697 class String : public Instance { | 2697 class String : public Instance { |
| 2698 public: | 2698 public: |
| 2699 typedef void (*PeerFinalizer)(void *peer); | |
| 2700 | |
| 2701 // We use 30 bits for the hash code so that we consistently use a | 2699 // We use 30 bits for the hash code so that we consistently use a |
| 2702 // 32bit Smi representation for the hash code on all architectures. | 2700 // 32bit Smi representation for the hash code on all architectures. |
| 2703 static const intptr_t kHashBits = 30; | 2701 static const intptr_t kHashBits = 30; |
| 2704 | 2702 |
| 2705 static const intptr_t kOneByteChar = 1; | 2703 static const intptr_t kOneByteChar = 1; |
| 2706 static const intptr_t kTwoByteChar = 2; | 2704 static const intptr_t kTwoByteChar = 2; |
| 2707 static const intptr_t kFourByteChar = 4; | 2705 static const intptr_t kFourByteChar = 4; |
| 2708 | 2706 |
| 2709 intptr_t Length() const { return Smi::Value(raw_ptr()->length_); } | 2707 intptr_t Length() const { return Smi::Value(raw_ptr()->length_); } |
| 2710 static intptr_t length_offset() { return OFFSET_OF(RawString, length_); } | 2708 static intptr_t length_offset() { return OFFSET_OF(RawString, length_); } |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2759 intptr_t len, | 2757 intptr_t len, |
| 2760 Heap::Space space = Heap::kNew); | 2758 Heap::Space space = Heap::kNew); |
| 2761 static RawString* New(const uint32_t* characters, | 2759 static RawString* New(const uint32_t* characters, |
| 2762 intptr_t len, | 2760 intptr_t len, |
| 2763 Heap::Space space = Heap::kNew); | 2761 Heap::Space space = Heap::kNew); |
| 2764 static RawString* New(const String& str, Heap::Space space = Heap::kNew); | 2762 static RawString* New(const String& str, Heap::Space space = Heap::kNew); |
| 2765 | 2763 |
| 2766 static RawString* NewExternal(const uint8_t* characters, | 2764 static RawString* NewExternal(const uint8_t* characters, |
| 2767 intptr_t len, | 2765 intptr_t len, |
| 2768 void* peer, | 2766 void* peer, |
| 2769 PeerFinalizer callback, | 2767 Dart_PeerFinalizer callback, |
| 2770 Heap::Space = Heap::kNew); | 2768 Heap::Space = Heap::kNew); |
| 2771 static RawString* NewExternal(const uint16_t* characters, | 2769 static RawString* NewExternal(const uint16_t* characters, |
| 2772 intptr_t len, | 2770 intptr_t len, |
| 2773 void* peer, | 2771 void* peer, |
| 2774 PeerFinalizer callback, | 2772 Dart_PeerFinalizer callback, |
| 2775 Heap::Space = Heap::kNew); | 2773 Heap::Space = Heap::kNew); |
| 2776 static RawString* NewExternal(const uint32_t* characters, | 2774 static RawString* NewExternal(const uint32_t* characters, |
| 2777 intptr_t len, | 2775 intptr_t len, |
| 2778 void* peer, | 2776 void* peer, |
| 2779 PeerFinalizer callback, | 2777 Dart_PeerFinalizer callback, |
| 2780 Heap::Space = Heap::kNew); | 2778 Heap::Space = Heap::kNew); |
| 2781 | 2779 |
| 2782 static void Copy(const String& dst, | 2780 static void Copy(const String& dst, |
| 2783 intptr_t dst_offset, | 2781 intptr_t dst_offset, |
| 2784 const uint8_t* characters, | 2782 const uint8_t* characters, |
| 2785 intptr_t len); | 2783 intptr_t len); |
| 2786 static void Copy(const String& dst, | 2784 static void Copy(const String& dst, |
| 2787 intptr_t dst_offset, | 2785 intptr_t dst_offset, |
| 2788 const uint16_t* characters, | 2786 const uint16_t* characters, |
| 2789 intptr_t len); | 2787 intptr_t len); |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3023 virtual int32_t CharAt(intptr_t index) const { | 3021 virtual int32_t CharAt(intptr_t index) const { |
| 3024 return *CharAddr(index); | 3022 return *CharAddr(index); |
| 3025 } | 3023 } |
| 3026 | 3024 |
| 3027 virtual intptr_t CharSize() const { | 3025 virtual intptr_t CharSize() const { |
| 3028 return kOneByteChar; | 3026 return kOneByteChar; |
| 3029 } | 3027 } |
| 3030 | 3028 |
| 3031 virtual bool IsExternal() const { return true; } | 3029 virtual bool IsExternal() const { return true; } |
| 3032 virtual void* GetPeer() const { | 3030 virtual void* GetPeer() const { |
| 3033 return raw_ptr()->external_data_->peer_; | 3031 return raw_ptr()->external_data_->peer(); |
| 3034 } | 3032 } |
| 3035 | 3033 |
| 3036 static intptr_t InstanceSize() { | 3034 static intptr_t InstanceSize() { |
| 3037 return RoundedAllocationSize(sizeof(RawExternalOneByteString)); | 3035 return RoundedAllocationSize(sizeof(RawExternalOneByteString)); |
| 3038 } | 3036 } |
| 3039 | 3037 |
| 3040 static RawExternalOneByteString* New(const uint8_t* characters, | 3038 static RawExternalOneByteString* New(const uint8_t* characters, |
| 3041 intptr_t len, | 3039 intptr_t len, |
| 3042 void* peer, | 3040 void* peer, |
| 3043 PeerFinalizer callback, | 3041 Dart_PeerFinalizer callback, |
| 3044 Heap::Space space); | 3042 Heap::Space space); |
| 3045 | 3043 |
| 3046 private: | 3044 private: |
| 3047 const uint8_t* CharAddr(intptr_t index) const { | 3045 const uint8_t* CharAddr(intptr_t index) const { |
| 3048 // TODO(iposva): Determine if we should throw an exception here. | 3046 // TODO(iposva): Determine if we should throw an exception here. |
| 3049 ASSERT((index >= 0) && (index < Length())); | 3047 ASSERT((index >= 0) && (index < Length())); |
| 3050 return &(raw_ptr()->external_data_->data_[index]); | 3048 return &(raw_ptr()->external_data_->data()[index]); |
| 3051 } | 3049 } |
| 3052 | 3050 |
| 3053 void SetExternalData(ExternalStringData<uint8_t>* data) { | 3051 void SetExternalData(ExternalStringData<uint8_t>* data) { |
| 3054 raw_ptr()->external_data_ = data; | 3052 raw_ptr()->external_data_ = data; |
| 3055 } | 3053 } |
| 3056 | 3054 |
| 3055 static void Finalize(Dart_Handle handle, void* peer); |
| 3056 |
| 3057 HEAP_OBJECT_IMPLEMENTATION(ExternalOneByteString, String); | 3057 HEAP_OBJECT_IMPLEMENTATION(ExternalOneByteString, String); |
| 3058 friend class Class; | 3058 friend class Class; |
| 3059 friend class String; | 3059 friend class String; |
| 3060 }; | 3060 }; |
| 3061 | 3061 |
| 3062 | 3062 |
| 3063 class ExternalTwoByteString : public String { | 3063 class ExternalTwoByteString : public String { |
| 3064 public: | 3064 public: |
| 3065 virtual int32_t CharAt(intptr_t index) const { | 3065 virtual int32_t CharAt(intptr_t index) const { |
| 3066 return *CharAddr(index); | 3066 return *CharAddr(index); |
| 3067 } | 3067 } |
| 3068 | 3068 |
| 3069 virtual intptr_t CharSize() const { | 3069 virtual intptr_t CharSize() const { |
| 3070 return kTwoByteChar; | 3070 return kTwoByteChar; |
| 3071 } | 3071 } |
| 3072 | 3072 |
| 3073 virtual bool IsExternal() const { return true; } | 3073 virtual bool IsExternal() const { return true; } |
| 3074 virtual void* GetPeer() const { | 3074 virtual void* GetPeer() const { |
| 3075 return raw_ptr()->external_data_->peer_; | 3075 return raw_ptr()->external_data_->peer(); |
| 3076 } | 3076 } |
| 3077 | 3077 |
| 3078 static intptr_t InstanceSize() { | 3078 static intptr_t InstanceSize() { |
| 3079 return RoundedAllocationSize(sizeof(RawExternalTwoByteString)); | 3079 return RoundedAllocationSize(sizeof(RawExternalTwoByteString)); |
| 3080 } | 3080 } |
| 3081 | 3081 |
| 3082 static RawExternalTwoByteString* New(const uint16_t* characters, | 3082 static RawExternalTwoByteString* New(const uint16_t* characters, |
| 3083 intptr_t len, | 3083 intptr_t len, |
| 3084 void* peer, | 3084 void* peer, |
| 3085 PeerFinalizer callback, | 3085 Dart_PeerFinalizer callback, |
| 3086 Heap::Space space = Heap::kNew); | 3086 Heap::Space space = Heap::kNew); |
| 3087 | 3087 |
| 3088 private: | 3088 private: |
| 3089 const uint16_t* CharAddr(intptr_t index) const { | 3089 const uint16_t* CharAddr(intptr_t index) const { |
| 3090 // TODO(iposva): Determine if we should throw an exception here. | 3090 // TODO(iposva): Determine if we should throw an exception here. |
| 3091 ASSERT((index >= 0) && (index < Length())); | 3091 ASSERT((index >= 0) && (index < Length())); |
| 3092 return &(raw_ptr()->external_data_->data_[index]); | 3092 return &(raw_ptr()->external_data_->data()[index]); |
| 3093 } | 3093 } |
| 3094 | 3094 |
| 3095 void SetExternalData(ExternalStringData<uint16_t>* data) { | 3095 void SetExternalData(ExternalStringData<uint16_t>* data) { |
| 3096 raw_ptr()->external_data_ = data; | 3096 raw_ptr()->external_data_ = data; |
| 3097 } | 3097 } |
| 3098 | 3098 |
| 3099 static void Finalize(Dart_Handle handle, void* peer); |
| 3100 |
| 3099 HEAP_OBJECT_IMPLEMENTATION(ExternalTwoByteString, String); | 3101 HEAP_OBJECT_IMPLEMENTATION(ExternalTwoByteString, String); |
| 3100 friend class Class; | 3102 friend class Class; |
| 3101 friend class String; | 3103 friend class String; |
| 3102 }; | 3104 }; |
| 3103 | 3105 |
| 3104 | 3106 |
| 3105 class ExternalFourByteString : public String { | 3107 class ExternalFourByteString : public String { |
| 3106 public: | 3108 public: |
| 3107 virtual int32_t CharAt(intptr_t index) const { | 3109 virtual int32_t CharAt(intptr_t index) const { |
| 3108 return *CharAddr(index); | 3110 return *CharAddr(index); |
| 3109 } | 3111 } |
| 3110 | 3112 |
| 3111 virtual intptr_t CharSize() const { | 3113 virtual intptr_t CharSize() const { |
| 3112 return kFourByteChar; | 3114 return kFourByteChar; |
| 3113 } | 3115 } |
| 3114 | 3116 |
| 3115 virtual bool IsExternal() const { return true; } | 3117 virtual bool IsExternal() const { return true; } |
| 3116 virtual void* GetPeer() const { | 3118 virtual void* GetPeer() const { |
| 3117 return raw_ptr()->external_data_->peer_; | 3119 return raw_ptr()->external_data_->peer(); |
| 3118 } | 3120 } |
| 3119 | 3121 |
| 3120 static intptr_t InstanceSize() { | 3122 static intptr_t InstanceSize() { |
| 3121 return RoundedAllocationSize(sizeof(RawExternalFourByteString)); | 3123 return RoundedAllocationSize(sizeof(RawExternalFourByteString)); |
| 3122 } | 3124 } |
| 3123 | 3125 |
| 3124 static RawExternalFourByteString* New(const uint32_t* characters, | 3126 static RawExternalFourByteString* New(const uint32_t* characters, |
| 3125 intptr_t len, | 3127 intptr_t len, |
| 3126 void* peer, | 3128 void* peer, |
| 3127 PeerFinalizer callback, | 3129 Dart_PeerFinalizer callback, |
| 3128 Heap::Space space = Heap::kNew); | 3130 Heap::Space space = Heap::kNew); |
| 3129 | 3131 |
| 3130 private: | 3132 private: |
| 3131 const uint32_t* CharAddr(intptr_t index) const { | 3133 const uint32_t* CharAddr(intptr_t index) const { |
| 3132 // TODO(iposva): Determine if we should throw an exception here. | 3134 // TODO(iposva): Determine if we should throw an exception here. |
| 3133 ASSERT((index >= 0) && (index < Length())); | 3135 ASSERT((index >= 0) && (index < Length())); |
| 3134 return &(raw_ptr()->external_data_->data_[index]); | 3136 return &(raw_ptr()->external_data_->data()[index]); |
| 3135 } | 3137 } |
| 3136 | 3138 |
| 3137 void SetExternalData(ExternalStringData<uint32_t>* data) { | 3139 void SetExternalData(ExternalStringData<uint32_t>* data) { |
| 3138 raw_ptr()->external_data_ = data; | 3140 raw_ptr()->external_data_ = data; |
| 3139 } | 3141 } |
| 3140 | 3142 |
| 3143 static void Finalize(Dart_Handle handle, void* peer); |
| 3144 |
| 3141 HEAP_OBJECT_IMPLEMENTATION(ExternalFourByteString, String); | 3145 HEAP_OBJECT_IMPLEMENTATION(ExternalFourByteString, String); |
| 3142 friend class Class; | 3146 friend class Class; |
| 3143 friend class String; | 3147 friend class String; |
| 3144 }; | 3148 }; |
| 3145 | 3149 |
| 3146 | 3150 |
| 3147 class Bool : public Instance { | 3151 class Bool : public Instance { |
| 3148 public: | 3152 public: |
| 3149 bool value() const { | 3153 bool value() const { |
| 3150 return raw_ptr()->value_; | 3154 return raw_ptr()->value_; |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3374 }; | 3378 }; |
| 3375 | 3379 |
| 3376 | 3380 |
| 3377 class ExternalByteArray : public ByteArray { | 3381 class ExternalByteArray : public ByteArray { |
| 3378 public: | 3382 public: |
| 3379 intptr_t Length() const { | 3383 intptr_t Length() const { |
| 3380 ASSERT(!IsNull()); | 3384 ASSERT(!IsNull()); |
| 3381 return Smi::Value(raw_ptr()->length_); | 3385 return Smi::Value(raw_ptr()->length_); |
| 3382 } | 3386 } |
| 3383 | 3387 |
| 3388 void* GetPeer() const { |
| 3389 return raw_ptr()->external_data_->peer(); |
| 3390 } |
| 3391 |
| 3384 template<typename T> | 3392 template<typename T> |
| 3385 T At(intptr_t byte_offset) const { | 3393 T At(intptr_t byte_offset) const { |
| 3386 T* addr = Addr<T>(byte_offset); | 3394 T* addr = Addr<T>(byte_offset); |
| 3387 ASSERT(Utils::IsAligned(reinterpret_cast<intptr_t>(addr), sizeof(T))); | 3395 ASSERT(Utils::IsAligned(reinterpret_cast<intptr_t>(addr), sizeof(T))); |
| 3388 return *addr; | 3396 return *addr; |
| 3389 } | 3397 } |
| 3390 | 3398 |
| 3391 template<typename T> | 3399 template<typename T> |
| 3392 void SetAt(intptr_t byte_offset, T value) const { | 3400 void SetAt(intptr_t byte_offset, T value) const { |
| 3393 T* addr = Addr<T>(byte_offset); | 3401 T* addr = Addr<T>(byte_offset); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 3406 void SetUnalignedAt(intptr_t byte_offset, T value) const { | 3414 void SetUnalignedAt(intptr_t byte_offset, T value) const { |
| 3407 memmove(Addr<T>(byte_offset), &value, sizeof(T)); | 3415 memmove(Addr<T>(byte_offset), &value, sizeof(T)); |
| 3408 } | 3416 } |
| 3409 | 3417 |
| 3410 static intptr_t InstanceSize() { | 3418 static intptr_t InstanceSize() { |
| 3411 return RoundedAllocationSize(sizeof(RawExternalByteArray)); | 3419 return RoundedAllocationSize(sizeof(RawExternalByteArray)); |
| 3412 } | 3420 } |
| 3413 | 3421 |
| 3414 static RawExternalByteArray* New(uint8_t* data, | 3422 static RawExternalByteArray* New(uint8_t* data, |
| 3415 intptr_t len, | 3423 intptr_t len, |
| 3424 void* peer, |
| 3425 Dart_PeerFinalizer callback, |
| 3416 Heap::Space space = Heap::kNew); | 3426 Heap::Space space = Heap::kNew); |
| 3417 | 3427 |
| 3418 private: | 3428 private: |
| 3419 uint8_t* ByteAddr(intptr_t byte_offset) const { | 3429 uint8_t* ByteAddr(intptr_t byte_offset) const { |
| 3420 return Addr<uint8_t>(byte_offset); | 3430 return Addr<uint8_t>(byte_offset); |
| 3421 } | 3431 } |
| 3422 | 3432 |
| 3423 template<typename T> | 3433 template<typename T> |
| 3424 T* Addr(intptr_t byte_offset) const { | 3434 T* Addr(intptr_t byte_offset) const { |
| 3425 intptr_t limit = byte_offset + sizeof(T); | 3435 intptr_t limit = byte_offset + sizeof(T); |
| 3426 // TODO(iposva): Determine if we should throw an exception here. | 3436 // TODO(iposva): Determine if we should throw an exception here. |
| 3427 ASSERT((byte_offset >= 0) && (limit <= Length())); | 3437 ASSERT((byte_offset >= 0) && (limit <= Length())); |
| 3428 uint8_t* addr = &raw_ptr()->data_[byte_offset]; | 3438 uint8_t* addr = &raw_ptr()->external_data_->data()[byte_offset]; |
| 3429 return reinterpret_cast<T*>(addr); | 3439 return reinterpret_cast<T*>(addr); |
| 3430 } | 3440 } |
| 3431 | 3441 |
| 3432 void SetLength(intptr_t value) { | 3442 void SetLength(intptr_t value) { |
| 3433 raw_ptr()->length_ = Smi::New(value); | 3443 raw_ptr()->length_ = Smi::New(value); |
| 3434 } | 3444 } |
| 3435 | 3445 |
| 3436 void SetData(uint8_t* data) const { | 3446 void SetExternalData(ExternalByteArrayData* data) { |
| 3437 raw_ptr()->data_ = data; | 3447 raw_ptr()->external_data_ = data; |
| 3438 } | 3448 } |
| 3439 | 3449 |
| 3450 static void Finalize(Dart_Handle handle, void* peer); |
| 3451 |
| 3440 HEAP_OBJECT_IMPLEMENTATION(ExternalByteArray, ByteArray); | 3452 HEAP_OBJECT_IMPLEMENTATION(ExternalByteArray, ByteArray); |
| 3441 friend class Class; | 3453 friend class Class; |
| 3442 }; | 3454 }; |
| 3443 | 3455 |
| 3444 | 3456 |
| 3445 class Closure : public Instance { | 3457 class Closure : public Instance { |
| 3446 public: | 3458 public: |
| 3447 RawFunction* function() const { return raw_ptr()->function_; } | 3459 RawFunction* function() const { return raw_ptr()->function_; } |
| 3448 static intptr_t function_offset() { | 3460 static intptr_t function_offset() { |
| 3449 return OFFSET_OF(RawClosure, function_); | 3461 return OFFSET_OF(RawClosure, function_); |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3648 } | 3660 } |
| 3649 | 3661 |
| 3650 | 3662 |
| 3651 void Context::SetAt(intptr_t index, const Instance& value) const { | 3663 void Context::SetAt(intptr_t index, const Instance& value) const { |
| 3652 StorePointer(InstanceAddr(index), value.raw()); | 3664 StorePointer(InstanceAddr(index), value.raw()); |
| 3653 } | 3665 } |
| 3654 | 3666 |
| 3655 } // namespace dart | 3667 } // namespace dart |
| 3656 | 3668 |
| 3657 #endif // VM_OBJECT_H_ | 3669 #endif // VM_OBJECT_H_ |
| OLD | NEW |