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