Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
| 5 | #ifndef V8_PROPERTY_H_ |
| 6 | #define V8_PROPERTY_H_ |
| 7 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 8 | #include <iosfwd> |
| 9 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10 | #include "src/factory.h" |
| 11 | #include "src/field-index.h" |
| 12 | #include "src/field-index-inl.h" |
| 13 | #include "src/isolate.h" |
| 14 | #include "src/types.h" |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 15 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16 | namespace v8 { |
| 17 | namespace internal { |
| 18 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19 | // Abstraction for elements in instance-descriptor arrays. |
| 20 | // |
| 21 | // Each descriptor has a key, property attributes, property type, |
| 22 | // property index (in the actual instance-descriptor array) and |
| 23 | // optionally a piece of data. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 24 | class Descriptor BASE_EMBEDDED { |
| 25 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 26 | void KeyToUniqueName() { |
| 27 | if (!key_->IsUniqueName()) { |
| 28 | key_ = key_->GetIsolate()->factory()->InternalizeString( |
| 29 | Handle<String>::cast(key_)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 30 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 33 | Handle<Name> GetKey() const { return key_; } |
| 34 | Handle<Object> GetValue() const { return value_; } |
| 35 | PropertyDetails GetDetails() const { return details_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 36 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 37 | void SetSortedKeyIndex(int index) { details_ = details_.set_pointer(index); } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 38 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 39 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 40 | Handle<Name> key_; |
| 41 | Handle<Object> value_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 42 | PropertyDetails details_; |
| 43 | |
| 44 | protected: |
| 45 | Descriptor() : details_(Smi::FromInt(0)) {} |
| 46 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 47 | void Init(Handle<Name> key, Handle<Object> value, PropertyDetails details) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 48 | key_ = key; |
| 49 | value_ = value; |
| 50 | details_ = details; |
| 51 | } |
| 52 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 53 | Descriptor(Handle<Name> key, Handle<Object> value, PropertyDetails details) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 54 | : key_(key), |
| 55 | value_(value), |
| 56 | details_(details) { } |
| 57 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 58 | Descriptor(Handle<Name> key, |
| 59 | Handle<Object> value, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 60 | PropertyAttributes attributes, |
| 61 | PropertyType type, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 62 | Representation representation, |
| 63 | int field_index = 0) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 64 | : key_(key), |
| 65 | value_(value), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 66 | details_(attributes, type, representation, field_index) { } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 67 | |
| 68 | friend class DescriptorArray; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 69 | friend class Map; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 73 | std::ostream& operator<<(std::ostream& os, const Descriptor& d); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 74 | |
| 75 | |
| 76 | class FieldDescriptor FINAL : public Descriptor { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 77 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 78 | FieldDescriptor(Handle<Name> key, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 79 | int field_index, |
| 80 | PropertyAttributes attributes, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 81 | Representation representation) |
| 82 | : Descriptor(key, HeapType::Any(key->GetIsolate()), attributes, |
| 83 | FIELD, representation, field_index) {} |
| 84 | FieldDescriptor(Handle<Name> key, |
| 85 | int field_index, |
| 86 | Handle<HeapType> field_type, |
| 87 | PropertyAttributes attributes, |
| 88 | Representation representation) |
| 89 | : Descriptor(key, field_type, attributes, FIELD, |
| 90 | representation, field_index) { } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 94 | class ConstantDescriptor FINAL : public Descriptor { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 95 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 96 | ConstantDescriptor(Handle<Name> key, |
| 97 | Handle<Object> value, |
| 98 | PropertyAttributes attributes) |
| 99 | : Descriptor(key, value, attributes, CONSTANT, |
| 100 | value->OptimalRepresentation()) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 104 | class CallbacksDescriptor FINAL : public Descriptor { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 105 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 106 | CallbacksDescriptor(Handle<Name> key, |
| 107 | Handle<Object> foreign, |
| 108 | PropertyAttributes attributes) |
| 109 | : Descriptor(key, foreign, attributes, CALLBACKS, |
| 110 | Representation::Tagged()) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 114 | class LookupResult FINAL BASE_EMBEDDED { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 115 | public: |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 116 | explicit LookupResult(Isolate* isolate) |
| 117 | : isolate_(isolate), |
| 118 | next_(isolate->top_lookup_result()), |
| 119 | lookup_type_(NOT_FOUND), |
| 120 | holder_(NULL), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 121 | transition_(NULL), |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 122 | details_(NONE, FIELD, Representation::None()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 123 | isolate->set_top_lookup_result(this); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | ~LookupResult() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 127 | DCHECK(isolate()->top_lookup_result() == this); |
| 128 | isolate()->set_top_lookup_result(next_); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 129 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 130 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 131 | Isolate* isolate() const { return isolate_; } |
| 132 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 133 | void DescriptorResult(JSObject* holder, PropertyDetails details, int number) { |
| 134 | lookup_type_ = DESCRIPTOR_TYPE; |
| 135 | holder_ = holder; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 136 | transition_ = NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 137 | details_ = details; |
| 138 | number_ = number; |
| 139 | } |
| 140 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 141 | void TransitionResult(JSObject* holder, Map* target) { |
| 142 | lookup_type_ = TRANSITION_TYPE; |
| 143 | number_ = target->LastAdded(); |
| 144 | details_ = target->instance_descriptors()->GetDetails(number_); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 145 | holder_ = holder; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 146 | transition_ = target; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void NotFound() { |
| 150 | lookup_type_ = NOT_FOUND; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 151 | details_ = PropertyDetails(NONE, FIELD, 0); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 152 | holder_ = NULL; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 153 | transition_ = NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 156 | Representation representation() const { |
| 157 | DCHECK(IsFound()); |
| 158 | return details_.representation(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 159 | } |
| 160 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 161 | // Property callbacks does not include transitions to callbacks. |
| 162 | bool IsPropertyCallbacks() const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 163 | return !IsTransition() && details_.type() == CALLBACKS; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 166 | bool IsReadOnly() const { |
| 167 | DCHECK(IsFound()); |
| 168 | return details_.IsReadOnly(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 171 | bool IsField() const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 172 | return lookup_type_ == DESCRIPTOR_TYPE && details_.type() == FIELD; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 175 | bool IsConstant() const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 176 | return lookup_type_ == DESCRIPTOR_TYPE && details_.type() == CONSTANT; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 179 | bool IsConfigurable() const { return details_.IsConfigurable(); } |
| 180 | bool IsFound() const { return lookup_type_ != NOT_FOUND; } |
| 181 | bool IsTransition() const { return lookup_type_ == TRANSITION_TYPE; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 182 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 183 | // Is the result is a property excluding transitions and the null descriptor? |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 184 | bool IsProperty() const { |
| 185 | return IsFound() && !IsTransition(); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 188 | Map* GetTransitionTarget() const { |
| 189 | DCHECK(IsTransition()); |
| 190 | return transition_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 193 | bool IsTransitionToField() const { |
| 194 | return IsTransition() && details_.type() == FIELD; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 197 | int GetLocalFieldIndexFromMap(Map* map) const { |
| 198 | return GetFieldIndexFromMap(map) - map->inobject_properties(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 199 | } |
| 200 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 201 | Object* GetConstantFromMap(Map* map) const { |
| 202 | DCHECK(details_.type() == CONSTANT); |
| 203 | return GetValueFromMap(map); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 206 | Object* GetValueFromMap(Map* map) const { |
| 207 | DCHECK(lookup_type_ == DESCRIPTOR_TYPE || |
| 208 | lookup_type_ == TRANSITION_TYPE); |
| 209 | DCHECK(number_ < map->NumberOfOwnDescriptors()); |
| 210 | return map->instance_descriptors()->GetValue(number_); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 211 | } |
| 212 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 213 | int GetFieldIndexFromMap(Map* map) const { |
| 214 | DCHECK(lookup_type_ == DESCRIPTOR_TYPE || |
| 215 | lookup_type_ == TRANSITION_TYPE); |
| 216 | DCHECK(number_ < map->NumberOfOwnDescriptors()); |
| 217 | return map->instance_descriptors()->GetFieldIndex(number_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 220 | HeapType* GetFieldTypeFromMap(Map* map) const { |
| 221 | DCHECK_NE(NOT_FOUND, lookup_type_); |
| 222 | DCHECK(number_ < map->NumberOfOwnDescriptors()); |
| 223 | return map->instance_descriptors()->GetFieldType(number_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 226 | Map* GetFieldOwnerFromMap(Map* map) const { |
| 227 | DCHECK(lookup_type_ == DESCRIPTOR_TYPE || |
| 228 | lookup_type_ == TRANSITION_TYPE); |
| 229 | DCHECK(number_ < map->NumberOfOwnDescriptors()); |
| 230 | return map->FindFieldOwner(number_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 233 | void Iterate(ObjectVisitor* visitor); |
| 234 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 235 | private: |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 236 | Isolate* isolate_; |
| 237 | LookupResult* next_; |
| 238 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 239 | // Where did we find the result; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 240 | enum { NOT_FOUND, DESCRIPTOR_TYPE, TRANSITION_TYPE } lookup_type_; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 241 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 242 | JSReceiver* holder_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 243 | Map* transition_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 244 | int number_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 245 | PropertyDetails details_; |
| 246 | }; |
| 247 | |
| 248 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 249 | std::ostream& operator<<(std::ostream& os, const LookupResult& r); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 250 | } } // namespace v8::internal |
| 251 | |
| 252 | #endif // V8_PROPERTY_H_ |