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
11namespace v8 {
12namespace internal {
13
14class Isolate;
15class Object;
16class PropertyDescriptorObject;
17
18class PropertyDescriptor {
19 public:
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
29  static bool IsAccessorDescriptor(PropertyDescriptor* desc) {
30    return desc->has_get() || desc->has_set();
31  }
32
33  // ES6 6.2.4.2
34  static bool IsDataDescriptor(PropertyDescriptor* desc) {
35    return desc->has_value() || desc->has_writable();
36  }
37
38  // ES6 6.2.4.3
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
56  bool is_empty() const {
57    return !has_enumerable() && !has_configurable() && !has_writable() &&
58           !has_value() && !has_get() && !has_set();
59  }
60
61  bool IsRegularAccessorProperty() const {
62    return has_configurable() && has_enumerable() && !has_value() &&
63           !has_writable() && has_get() && has_set();
64  }
65
66  bool IsRegularDataProperty() const {
67    return has_configurable() && has_enumerable() && has_value() &&
68           has_writable() && !has_get() && !has_set();
69  }
70
71  bool enumerable() const { return enumerable_; }
72  void set_enumerable(bool enumerable) {
73    enumerable_ = enumerable;
74    has_enumerable_ = true;
75  }
76  bool has_enumerable() const { return has_enumerable_; }
77
78  bool configurable() const { return configurable_; }
79  void set_configurable(bool configurable) {
80    configurable_ = configurable;
81    has_configurable_ = true;
82  }
83  bool has_configurable() const { return has_configurable_; }
84
85  Handle<Object> value() const { return value_; }
86  void set_value(Handle<Object> value) { value_ = value; }
87  bool has_value() const { return !value_.is_null(); }
88
89  bool writable() const { return writable_; }
90  void set_writable(bool writable) {
91    writable_ = writable;
92    has_writable_ = true;
93  }
94  bool has_writable() const { return has_writable_; }
95
96  Handle<Object> get() const { return get_; }
97  void set_get(Handle<Object> get) { get_ = get; }
98  bool has_get() const { return !get_.is_null(); }
99
100  Handle<Object> set() const { return set_; }
101  void set_set(Handle<Object> set) { set_ = set; }
102  bool has_set() const { return !set_.is_null(); }
103
104  Handle<Object> name() const { return name_; }
105  void set_name(Handle<Object> name) { name_ = name; }
106
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