Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #ifndef V8_PROPERTY_H_ |
| 29 | #define V8_PROPERTY_H_ |
| 30 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 31 | #include "allocation.h" |
| 32 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 33 | namespace v8 { |
| 34 | namespace internal { |
| 35 | |
| 36 | |
| 37 | // Abstraction for elements in instance-descriptor arrays. |
| 38 | // |
| 39 | // Each descriptor has a key, property attributes, property type, |
| 40 | // property index (in the actual instance-descriptor array) and |
| 41 | // optionally a piece of data. |
| 42 | // |
| 43 | |
| 44 | class Descriptor BASE_EMBEDDED { |
| 45 | public: |
| 46 | static int IndexFromValue(Object* value) { |
| 47 | return Smi::cast(value)->value(); |
| 48 | } |
| 49 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 50 | MUST_USE_RESULT MaybeObject* KeyToSymbol() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 51 | if (!StringShape(key_).IsSymbol()) { |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 52 | Object* result; |
| 53 | { MaybeObject* maybe_result = HEAP->LookupSymbol(key_); |
| 54 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 55 | } |
| 56 | key_ = String::cast(result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 57 | } |
| 58 | return key_; |
| 59 | } |
| 60 | |
| 61 | String* GetKey() { return key_; } |
| 62 | Object* GetValue() { return value_; } |
| 63 | PropertyDetails GetDetails() { return details_; } |
| 64 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 65 | #ifdef OBJECT_PRINT |
| 66 | void Print(FILE* out); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 67 | #endif |
| 68 | |
| 69 | void SetEnumerationIndex(int index) { |
| 70 | ASSERT(PropertyDetails::IsValidIndex(index)); |
| 71 | details_ = PropertyDetails(details_.attributes(), details_.type(), index); |
| 72 | } |
| 73 | |
| 74 | private: |
| 75 | String* key_; |
| 76 | Object* value_; |
| 77 | PropertyDetails details_; |
| 78 | |
| 79 | protected: |
| 80 | Descriptor() : details_(Smi::FromInt(0)) {} |
| 81 | |
| 82 | void Init(String* key, Object* value, PropertyDetails details) { |
| 83 | key_ = key; |
| 84 | value_ = value; |
| 85 | details_ = details; |
| 86 | } |
| 87 | |
| 88 | Descriptor(String* key, Object* value, PropertyDetails details) |
| 89 | : key_(key), |
| 90 | value_(value), |
| 91 | details_(details) { } |
| 92 | |
| 93 | Descriptor(String* key, |
| 94 | Object* value, |
| 95 | PropertyAttributes attributes, |
| 96 | PropertyType type, |
| 97 | int index = 0) |
| 98 | : key_(key), |
| 99 | value_(value), |
| 100 | details_(attributes, type, index) { } |
| 101 | |
| 102 | friend class DescriptorArray; |
| 103 | }; |
| 104 | |
| 105 | // A pointer from a map to the new map that is created by adding |
| 106 | // a named property. These are key to the speed and functioning of V8. |
| 107 | // The two maps should always have the same prototype, since |
| 108 | // MapSpace::CreateBackPointers depends on this. |
| 109 | class MapTransitionDescriptor: public Descriptor { |
| 110 | public: |
| 111 | MapTransitionDescriptor(String* key, Map* map, PropertyAttributes attributes) |
| 112 | : Descriptor(key, map, attributes, MAP_TRANSITION) { } |
| 113 | }; |
| 114 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 115 | class ElementsTransitionDescriptor: public Descriptor { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 116 | public: |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 117 | ElementsTransitionDescriptor(String* key, |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 118 | Map* map, |
| 119 | ElementsKind elements_kind) |
| 120 | : Descriptor(key, map, PropertyDetails(NONE, |
| 121 | ELEMENTS_TRANSITION, |
| 122 | elements_kind)) { } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 123 | }; |
| 124 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 125 | // Marks a field name in a map so that adding the field is guaranteed |
| 126 | // to create a FIELD descriptor in the new map. Used after adding |
| 127 | // a constant function the first time, creating a CONSTANT_FUNCTION |
| 128 | // descriptor in the new map. This avoids creating multiple maps with |
| 129 | // the same CONSTANT_FUNCTION field. |
| 130 | class ConstTransitionDescriptor: public Descriptor { |
| 131 | public: |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 132 | explicit ConstTransitionDescriptor(String* key, Map* map) |
| 133 | : Descriptor(key, map, NONE, CONSTANT_TRANSITION) { } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | |
| 137 | class FieldDescriptor: public Descriptor { |
| 138 | public: |
| 139 | FieldDescriptor(String* key, |
| 140 | int field_index, |
| 141 | PropertyAttributes attributes, |
| 142 | int index = 0) |
| 143 | : Descriptor(key, Smi::FromInt(field_index), attributes, FIELD, index) {} |
| 144 | }; |
| 145 | |
| 146 | |
| 147 | class ConstantFunctionDescriptor: public Descriptor { |
| 148 | public: |
| 149 | ConstantFunctionDescriptor(String* key, |
| 150 | JSFunction* function, |
| 151 | PropertyAttributes attributes, |
| 152 | int index = 0) |
| 153 | : Descriptor(key, function, attributes, CONSTANT_FUNCTION, index) {} |
| 154 | }; |
| 155 | |
| 156 | |
| 157 | class CallbacksDescriptor: public Descriptor { |
| 158 | public: |
| 159 | CallbacksDescriptor(String* key, |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 160 | Object* foreign, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 161 | PropertyAttributes attributes, |
| 162 | int index = 0) |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 163 | : Descriptor(key, foreign, attributes, CALLBACKS, index) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | |
| 167 | class LookupResult BASE_EMBEDDED { |
| 168 | public: |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 169 | LookupResult() |
| 170 | : lookup_type_(NOT_FOUND), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 171 | cacheable_(true), |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 172 | details_(NONE, NORMAL) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 173 | |
| 174 | void DescriptorResult(JSObject* holder, PropertyDetails details, int number) { |
| 175 | lookup_type_ = DESCRIPTOR_TYPE; |
| 176 | holder_ = holder; |
| 177 | details_ = details; |
| 178 | number_ = number; |
| 179 | } |
| 180 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 181 | void DescriptorResult(JSObject* holder, Smi* details, int number) { |
| 182 | lookup_type_ = DESCRIPTOR_TYPE; |
| 183 | holder_ = holder; |
| 184 | details_ = PropertyDetails(details); |
| 185 | number_ = number; |
| 186 | } |
| 187 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 188 | void ConstantResult(JSObject* holder) { |
| 189 | lookup_type_ = CONSTANT_TYPE; |
| 190 | holder_ = holder; |
| 191 | details_ = |
| 192 | PropertyDetails(static_cast<PropertyAttributes>(DONT_ENUM | |
| 193 | DONT_DELETE), |
| 194 | CALLBACKS); |
| 195 | number_ = -1; |
| 196 | } |
| 197 | |
| 198 | void DictionaryResult(JSObject* holder, int entry) { |
| 199 | lookup_type_ = DICTIONARY_TYPE; |
| 200 | holder_ = holder; |
| 201 | details_ = holder->property_dictionary()->DetailsAt(entry); |
| 202 | number_ = entry; |
| 203 | } |
| 204 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 205 | void HandlerResult() { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 206 | lookup_type_ = HANDLER_TYPE; |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 207 | holder_ = NULL; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 208 | details_ = PropertyDetails(NONE, HANDLER); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 209 | cacheable_ = false; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 212 | void InterceptorResult(JSObject* holder) { |
| 213 | lookup_type_ = INTERCEPTOR_TYPE; |
| 214 | holder_ = holder; |
| 215 | details_ = PropertyDetails(NONE, INTERCEPTOR); |
| 216 | } |
| 217 | |
| 218 | void NotFound() { |
| 219 | lookup_type_ = NOT_FOUND; |
| 220 | } |
| 221 | |
| 222 | JSObject* holder() { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 223 | ASSERT(IsFound()); |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 224 | return holder_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | PropertyType type() { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 228 | ASSERT(IsFound()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 229 | return details_.type(); |
| 230 | } |
| 231 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 232 | PropertyAttributes GetAttributes() { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 233 | ASSERT(IsFound()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 234 | return details_.attributes(); |
| 235 | } |
| 236 | |
| 237 | PropertyDetails GetPropertyDetails() { |
| 238 | return details_; |
| 239 | } |
| 240 | |
| 241 | bool IsReadOnly() { return details_.IsReadOnly(); } |
| 242 | bool IsDontDelete() { return details_.IsDontDelete(); } |
| 243 | bool IsDontEnum() { return details_.IsDontEnum(); } |
| 244 | bool IsDeleted() { return details_.IsDeleted(); } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 245 | bool IsFound() { return lookup_type_ != NOT_FOUND; } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 246 | bool IsHandler() { return lookup_type_ == HANDLER_TYPE; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 247 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 248 | // Is the result is a property excluding transitions and the null |
| 249 | // descriptor? |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 250 | bool IsProperty() { |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 251 | return IsFound() && (type() < FIRST_PHANTOM_PROPERTY_TYPE); |
| 252 | } |
| 253 | |
| 254 | // Is the result a property or a transition? |
| 255 | bool IsPropertyOrTransition() { |
| 256 | return IsFound() && (type() != NULL_DESCRIPTOR); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 259 | bool IsCacheable() { return cacheable_; } |
| 260 | void DisallowCaching() { cacheable_ = false; } |
| 261 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 262 | Object* GetLazyValue() { |
| 263 | switch (type()) { |
| 264 | case FIELD: |
| 265 | return holder()->FastPropertyAt(GetFieldIndex()); |
| 266 | case NORMAL: { |
| 267 | Object* value; |
| 268 | value = holder()->property_dictionary()->ValueAt(GetDictionaryEntry()); |
| 269 | if (holder()->IsGlobalObject()) { |
| 270 | value = JSGlobalPropertyCell::cast(value)->value(); |
| 271 | } |
| 272 | return value; |
| 273 | } |
| 274 | case CONSTANT_FUNCTION: |
| 275 | return GetConstantFunction(); |
| 276 | default: |
| 277 | return Smi::FromInt(0); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | Map* GetTransitionMap() { |
| 282 | ASSERT(lookup_type_ == DESCRIPTOR_TYPE); |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 283 | ASSERT(type() == MAP_TRANSITION || type() == CONSTANT_TRANSITION || |
| 284 | type() == ELEMENTS_TRANSITION); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 285 | return Map::cast(GetValue()); |
| 286 | } |
| 287 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 288 | Map* GetTransitionMapFromMap(Map* map) { |
| 289 | ASSERT(lookup_type_ == DESCRIPTOR_TYPE); |
| 290 | ASSERT(type() == MAP_TRANSITION); |
| 291 | return Map::cast(map->instance_descriptors()->GetValue(number_)); |
| 292 | } |
| 293 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 294 | int GetFieldIndex() { |
| 295 | ASSERT(lookup_type_ == DESCRIPTOR_TYPE); |
| 296 | ASSERT(type() == FIELD); |
| 297 | return Descriptor::IndexFromValue(GetValue()); |
| 298 | } |
| 299 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 300 | int GetLocalFieldIndexFromMap(Map* map) { |
| 301 | ASSERT(lookup_type_ == DESCRIPTOR_TYPE); |
| 302 | ASSERT(type() == FIELD); |
| 303 | return Descriptor::IndexFromValue( |
| 304 | map->instance_descriptors()->GetValue(number_)) - |
| 305 | map->inobject_properties(); |
| 306 | } |
| 307 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 308 | int GetDictionaryEntry() { |
| 309 | ASSERT(lookup_type_ == DICTIONARY_TYPE); |
| 310 | return number_; |
| 311 | } |
| 312 | |
| 313 | JSFunction* GetConstantFunction() { |
| 314 | ASSERT(type() == CONSTANT_FUNCTION); |
| 315 | return JSFunction::cast(GetValue()); |
| 316 | } |
| 317 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 318 | JSFunction* GetConstantFunctionFromMap(Map* map) { |
| 319 | ASSERT(lookup_type_ == DESCRIPTOR_TYPE); |
| 320 | ASSERT(type() == CONSTANT_FUNCTION); |
| 321 | return JSFunction::cast(map->instance_descriptors()->GetValue(number_)); |
| 322 | } |
| 323 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 324 | Object* GetCallbackObject() { |
| 325 | if (lookup_type_ == CONSTANT_TYPE) { |
| 326 | // For now we only have the __proto__ as constant type. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 327 | return HEAP->prototype_accessors(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 328 | } |
| 329 | return GetValue(); |
| 330 | } |
| 331 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 332 | #ifdef OBJECT_PRINT |
| 333 | void Print(FILE* out); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 334 | #endif |
| 335 | |
| 336 | Object* GetValue() { |
| 337 | if (lookup_type_ == DESCRIPTOR_TYPE) { |
| 338 | DescriptorArray* descriptors = holder()->map()->instance_descriptors(); |
| 339 | return descriptors->GetValue(number_); |
| 340 | } |
| 341 | // In the dictionary case, the data is held in the value field. |
| 342 | ASSERT(lookup_type_ == DICTIONARY_TYPE); |
| 343 | return holder()->GetNormalizedProperty(this); |
| 344 | } |
| 345 | |
| 346 | private: |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 347 | // Where did we find the result; |
| 348 | enum { |
| 349 | NOT_FOUND, |
| 350 | DESCRIPTOR_TYPE, |
| 351 | DICTIONARY_TYPE, |
| 352 | HANDLER_TYPE, |
| 353 | INTERCEPTOR_TYPE, |
| 354 | CONSTANT_TYPE |
| 355 | } lookup_type_; |
| 356 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 357 | JSObject* holder_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 358 | int number_; |
| 359 | bool cacheable_; |
| 360 | PropertyDetails details_; |
| 361 | }; |
| 362 | |
| 363 | |
| 364 | } } // namespace v8::internal |
| 365 | |
| 366 | #endif // V8_PROPERTY_H_ |