Home | History | Annotate | Download | only in src
      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:
     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
     30   static bool IsAccessorDescriptor(PropertyDescriptor* desc) {
     31     return desc->has_get() || desc->has_set();
     32   }
     33 
     34   // ES6 6.2.4.2
     35   static bool IsDataDescriptor(PropertyDescriptor* desc) {
     36     return desc->has_value() || desc->has_writable();
     37   }
     38 
     39   // ES6 6.2.4.3
     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 
     55   bool is_empty() const {
     56     return !has_enumerable() && !has_configurable() && !has_writable() &&
     57            !has_value() && !has_get() && !has_set();
     58   }
     59 
     60   bool enumerable() const { return enumerable_; }
     61   void set_enumerable(bool enumerable) {
     62     enumerable_ = enumerable;
     63     has_enumerable_ = true;
     64   }
     65   bool has_enumerable() const { return has_enumerable_; }
     66 
     67   bool configurable() const { return configurable_; }
     68   void set_configurable(bool configurable) {
     69     configurable_ = configurable;
     70     has_configurable_ = true;
     71   }
     72   bool has_configurable() const { return has_configurable_; }
     73 
     74   Handle<Object> value() const { return value_; }
     75   void set_value(Handle<Object> value) { value_ = value; }
     76   bool has_value() const { return !value_.is_null(); }
     77 
     78   bool writable() const { return writable_; }
     79   void set_writable(bool writable) {
     80     writable_ = writable;
     81     has_writable_ = true;
     82   }
     83   bool has_writable() const { return has_writable_; }
     84 
     85   Handle<Object> get() const { return get_; }
     86   void set_get(Handle<Object> get) { get_ = get; }
     87   bool has_get() const { return !get_.is_null(); }
     88 
     89   Handle<Object> set() const { return set_; }
     90   void set_set(Handle<Object> set) { set_ = set; }
     91   bool has_set() const { return !set_.is_null(); }
     92 
     93   Handle<Object> name() const { return name_; }
     94   void set_name(Handle<Object> name) { name_ = name; }
     95 
     96   PropertyAttributes ToAttributes() {
     97     return static_cast<PropertyAttributes>(
     98         (has_enumerable() && !enumerable() ? DONT_ENUM : NONE) |
     99         (has_configurable() && !configurable() ? DONT_DELETE : NONE) |
    100         (has_writable() && !writable() ? READ_ONLY : NONE));
    101   }
    102 
    103  private:
    104   bool enumerable_ : 1;
    105   bool has_enumerable_ : 1;
    106   bool configurable_ : 1;
    107   bool has_configurable_ : 1;
    108   bool writable_ : 1;
    109   bool has_writable_ : 1;
    110   Handle<Object> value_;
    111   Handle<Object> get_;
    112   Handle<Object> set_;
    113   Handle<Object> name_;
    114 
    115   // Some compilers (Xcode 5.1, ARM GCC 4.9) insist on having a copy
    116   // constructor for std::vector<PropertyDescriptor>, so we can't
    117   // DISALLOW_COPY_AND_ASSIGN(PropertyDescriptor); here.
    118 };
    119 
    120 }  // namespace internal
    121 }  // namespace v8
    122 
    123 #endif  // V8_PROPERTY_DESCRIPTOR_H_
    124