1 // Copyright 2014 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef V8_PROPERTY_H_ 6 #define V8_PROPERTY_H_ 7 8 #include <iosfwd> 9 10 #include "src/factory.h" 11 #include "src/field-index.h" 12 #include "src/isolate.h" 13 #include "src/types.h" 14 15 namespace v8 { 16 namespace internal { 17 18 // Abstraction for elements in instance-descriptor arrays. 19 // 20 // Each descriptor has a key, property attributes, property type, 21 // property index (in the actual instance-descriptor array) and 22 // optionally a piece of data. 23 class Descriptor BASE_EMBEDDED { 24 public: KeyToUniqueName()25 void KeyToUniqueName() { 26 if (!key_->IsUniqueName()) { 27 key_ = key_->GetIsolate()->factory()->InternalizeString( 28 Handle<String>::cast(key_)); 29 } 30 } 31 GetKey()32 Handle<Name> GetKey() const { return key_; } GetValue()33 Handle<Object> GetValue() const { return value_; } GetDetails()34 PropertyDetails GetDetails() const { return details_; } 35 SetSortedKeyIndex(int index)36 void SetSortedKeyIndex(int index) { details_ = details_.set_pointer(index); } 37 38 private: 39 Handle<Name> key_; 40 Handle<Object> value_; 41 PropertyDetails details_; 42 43 protected: Descriptor()44 Descriptor() : details_(Smi::FromInt(0)) {} 45 Init(Handle<Name> key,Handle<Object> value,PropertyDetails details)46 void Init(Handle<Name> key, Handle<Object> value, PropertyDetails details) { 47 key_ = key; 48 value_ = value; 49 details_ = details; 50 } 51 Descriptor(Handle<Name> key,Handle<Object> value,PropertyDetails details)52 Descriptor(Handle<Name> key, Handle<Object> value, PropertyDetails details) 53 : key_(key), 54 value_(value), 55 details_(details) { } 56 57 Descriptor(Handle<Name> key, 58 Handle<Object> value, 59 PropertyAttributes attributes, 60 PropertyType type, 61 Representation representation, 62 int field_index = 0) key_(key)63 : key_(key), 64 value_(value), 65 details_(attributes, type, representation, field_index) { } 66 67 friend class DescriptorArray; 68 friend class Map; 69 }; 70 71 72 std::ostream& operator<<(std::ostream& os, const Descriptor& d); 73 74 75 class DataDescriptor final : public Descriptor { 76 public: DataDescriptor(Handle<Name> key,int field_index,PropertyAttributes attributes,Representation representation)77 DataDescriptor(Handle<Name> key, int field_index, 78 PropertyAttributes attributes, Representation representation) 79 : Descriptor(key, HeapType::Any(key->GetIsolate()), attributes, DATA, 80 representation, field_index) {} 81 // The field type is either a simple type or a map wrapped in a weak cell. DataDescriptor(Handle<Name> key,int field_index,Handle<Object> wrapped_field_type,PropertyAttributes attributes,Representation representation)82 DataDescriptor(Handle<Name> key, int field_index, 83 Handle<Object> wrapped_field_type, 84 PropertyAttributes attributes, Representation representation) 85 : Descriptor(key, wrapped_field_type, attributes, DATA, representation, 86 field_index) { 87 DCHECK(wrapped_field_type->IsSmi() || wrapped_field_type->IsWeakCell()); 88 } 89 }; 90 91 92 class DataConstantDescriptor final : public Descriptor { 93 public: DataConstantDescriptor(Handle<Name> key,Handle<Object> value,PropertyAttributes attributes)94 DataConstantDescriptor(Handle<Name> key, Handle<Object> value, 95 PropertyAttributes attributes) 96 : Descriptor(key, value, attributes, DATA_CONSTANT, 97 value->OptimalRepresentation()) {} 98 }; 99 100 101 class AccessorConstantDescriptor final : public Descriptor { 102 public: AccessorConstantDescriptor(Handle<Name> key,Handle<Object> foreign,PropertyAttributes attributes)103 AccessorConstantDescriptor(Handle<Name> key, Handle<Object> foreign, 104 PropertyAttributes attributes) 105 : Descriptor(key, foreign, attributes, ACCESSOR_CONSTANT, 106 Representation::Tagged()) {} 107 }; 108 109 110 } // namespace internal 111 } // namespace v8 112 113 #endif // V8_PROPERTY_H_ 114