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/isolate.h"
12 
13 namespace v8 {
14 namespace internal {
15 
16 // Abstraction for elements in instance-descriptor arrays.
17 //
18 // Each descriptor has a key, property attributes, property type,
19 // property index (in the actual instance-descriptor array) and
20 // optionally a piece of data.
21 class Descriptor BASE_EMBEDDED {
22  public:
GetKey()23   Handle<Name> GetKey() const { return key_; }
GetValue()24   Handle<Object> GetValue() const { return value_; }
GetDetails()25   PropertyDetails GetDetails() const { return details_; }
26 
SetSortedKeyIndex(int index)27   void SetSortedKeyIndex(int index) { details_ = details_.set_pointer(index); }
28 
29  private:
30   Handle<Name> key_;
31   Handle<Object> value_;
32   PropertyDetails details_;
33 
34  protected:
Descriptor()35   Descriptor() : details_(Smi::kZero) {}
36 
Init(Handle<Name> key,Handle<Object> value,PropertyDetails details)37   void Init(Handle<Name> key, Handle<Object> value, PropertyDetails details) {
38     DCHECK(key->IsUniqueName());
39     DCHECK_IMPLIES(key->IsPrivate(), !details.IsEnumerable());
40     key_ = key;
41     value_ = value;
42     details_ = details;
43   }
44 
Descriptor(Handle<Name> key,Handle<Object> value,PropertyDetails details)45   Descriptor(Handle<Name> key, Handle<Object> value, PropertyDetails details)
46       : key_(key), value_(value), details_(details) {
47     DCHECK(key->IsUniqueName());
48     DCHECK_IMPLIES(key->IsPrivate(), !details_.IsEnumerable());
49   }
50 
51   Descriptor(Handle<Name> key, Handle<Object> value,
52              PropertyAttributes attributes, PropertyType type,
53              Representation representation, int field_index = 0)
key_(key)54       : key_(key),
55         value_(value),
56         details_(attributes, type, representation, field_index) {
57     DCHECK(key->IsUniqueName());
58     DCHECK_IMPLIES(key->IsPrivate(), !details_.IsEnumerable());
59   }
60 
61   friend class DescriptorArray;
62   friend class Map;
63 };
64 
65 
66 std::ostream& operator<<(std::ostream& os, const Descriptor& d);
67 
68 
69 class DataDescriptor final : public Descriptor {
70  public:
71   DataDescriptor(Handle<Name> key, int field_index,
72                  PropertyAttributes attributes, Representation representation);
73   // 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)74   DataDescriptor(Handle<Name> key, int field_index,
75                  Handle<Object> wrapped_field_type,
76                  PropertyAttributes attributes, Representation representation)
77       : Descriptor(key, wrapped_field_type, attributes, DATA, representation,
78                    field_index) {
79     DCHECK(wrapped_field_type->IsSmi() || wrapped_field_type->IsWeakCell());
80   }
81 };
82 
83 
84 class DataConstantDescriptor final : public Descriptor {
85  public:
DataConstantDescriptor(Handle<Name> key,Handle<Object> value,PropertyAttributes attributes)86   DataConstantDescriptor(Handle<Name> key, Handle<Object> value,
87                          PropertyAttributes attributes)
88       : Descriptor(key, value, attributes, DATA_CONSTANT,
89                    value->OptimalRepresentation()) {}
90 };
91 
92 
93 class AccessorConstantDescriptor final : public Descriptor {
94  public:
AccessorConstantDescriptor(Handle<Name> key,Handle<Object> foreign,PropertyAttributes attributes)95   AccessorConstantDescriptor(Handle<Name> key, Handle<Object> foreign,
96                              PropertyAttributes attributes)
97       : Descriptor(key, foreign, attributes, ACCESSOR_CONSTANT,
98                    Representation::Tagged()) {}
99 };
100 
101 
102 }  // namespace internal
103 }  // namespace v8
104 
105 #endif  // V8_PROPERTY_H_
106