1 // Copyright 2015 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_DESCRIPTOR_H_
6 #define V8_PROPERTY_DESCRIPTOR_H_
7 
8 
9 #include "src/handles.h"
10 #include "src/property-details.h"
11 
12 
13 namespace v8 {
14 namespace internal {
15 
16 class Isolate;
17 class Object;
18 
19 class PropertyDescriptor {
20  public:
PropertyDescriptor()21   PropertyDescriptor()
22       : enumerable_(false),
23         has_enumerable_(false),
24         configurable_(false),
25         has_configurable_(false),
26         writable_(false),
27         has_writable_(false) {}
28 
29   // ES6 6.2.4.1
IsAccessorDescriptor(PropertyDescriptor * desc)30   static bool IsAccessorDescriptor(PropertyDescriptor* desc) {
31     return desc->has_get() || desc->has_set();
32   }
33 
34   // ES6 6.2.4.2
IsDataDescriptor(PropertyDescriptor * desc)35   static bool IsDataDescriptor(PropertyDescriptor* desc) {
36     return desc->has_value() || desc->has_writable();
37   }
38 
39   // ES6 6.2.4.3
IsGenericDescriptor(PropertyDescriptor * desc)40   static bool IsGenericDescriptor(PropertyDescriptor* desc) {
41     return !IsAccessorDescriptor(desc) && !IsDataDescriptor(desc);
42   }
43 
44   // ES6 6.2.4.4
45   Handle<Object> ToObject(Isolate* isolate);
46 
47   // ES6 6.2.4.5
48   static bool ToPropertyDescriptor(Isolate* isolate, Handle<Object> obj,
49                                    PropertyDescriptor* desc);
50 
51   // ES6 6.2.4.6
52   static void CompletePropertyDescriptor(Isolate* isolate,
53                                          PropertyDescriptor* desc);
54 
is_empty()55   bool is_empty() const {
56     return !has_enumerable() && !has_configurable() && !has_writable() &&
57            !has_value() && !has_get() && !has_set();
58   }
59 
IsRegularAccessorProperty()60   bool IsRegularAccessorProperty() const {
61     return has_configurable() && has_enumerable() && !has_value() &&
62            !has_writable() && has_get() && has_set();
63   }
64 
IsRegularDataProperty()65   bool IsRegularDataProperty() const {
66     return has_configurable() && has_enumerable() && has_value() &&
67            has_writable() && !has_get() && !has_set();
68   }
69 
enumerable()70   bool enumerable() const { return enumerable_; }
set_enumerable(bool enumerable)71   void set_enumerable(bool enumerable) {
72     enumerable_ = enumerable;
73     has_enumerable_ = true;
74   }
has_enumerable()75   bool has_enumerable() const { return has_enumerable_; }
76 
configurable()77   bool configurable() const { return configurable_; }
set_configurable(bool configurable)78   void set_configurable(bool configurable) {
79     configurable_ = configurable;
80     has_configurable_ = true;
81   }
has_configurable()82   bool has_configurable() const { return has_configurable_; }
83 
value()84   Handle<Object> value() const { return value_; }
set_value(Handle<Object> value)85   void set_value(Handle<Object> value) { value_ = value; }
has_value()86   bool has_value() const { return !value_.is_null(); }
87 
writable()88   bool writable() const { return writable_; }
set_writable(bool writable)89   void set_writable(bool writable) {
90     writable_ = writable;
91     has_writable_ = true;
92   }
has_writable()93   bool has_writable() const { return has_writable_; }
94 
get()95   Handle<Object> get() const { return get_; }
set_get(Handle<Object> get)96   void set_get(Handle<Object> get) { get_ = get; }
has_get()97   bool has_get() const { return !get_.is_null(); }
98 
set()99   Handle<Object> set() const { return set_; }
set_set(Handle<Object> set)100   void set_set(Handle<Object> set) { set_ = set; }
has_set()101   bool has_set() const { return !set_.is_null(); }
102 
name()103   Handle<Object> name() const { return name_; }
set_name(Handle<Object> name)104   void set_name(Handle<Object> name) { name_ = name; }
105 
ToAttributes()106   PropertyAttributes ToAttributes() {
107     return static_cast<PropertyAttributes>(
108         (has_enumerable() && !enumerable() ? DONT_ENUM : NONE) |
109         (has_configurable() && !configurable() ? DONT_DELETE : NONE) |
110         (has_writable() && !writable() ? READ_ONLY : NONE));
111   }
112 
113  private:
114   bool enumerable_ : 1;
115   bool has_enumerable_ : 1;
116   bool configurable_ : 1;
117   bool has_configurable_ : 1;
118   bool writable_ : 1;
119   bool has_writable_ : 1;
120   Handle<Object> value_;
121   Handle<Object> get_;
122   Handle<Object> set_;
123   Handle<Object> name_;
124 
125   // Some compilers (Xcode 5.1, ARM GCC 4.9) insist on having a copy
126   // constructor for std::vector<PropertyDescriptor>, so we can't
127   // DISALLOW_COPY_AND_ASSIGN(PropertyDescriptor); here.
128 };
129 
130 }  // namespace internal
131 }  // namespace v8
132 
133 #endif  // V8_PROPERTY_DESCRIPTOR_H_
134