property-descriptor.h 3.78 KB
Newer Older
1 2 3 4
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
#ifndef V8_OBJECTS_PROPERTY_DESCRIPTOR_H_
#define V8_OBJECTS_PROPERTY_DESCRIPTOR_H_
7

8
#include "src/handles/handles.h"
9
#include "src/objects/property-details.h"
10 11 12 13 14 15

namespace v8 {
namespace internal {

class Isolate;
class Object;
16
class PropertyDescriptorObject;
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

class PropertyDescriptor {
 public:
  PropertyDescriptor()
      : enumerable_(false),
        has_enumerable_(false),
        configurable_(false),
        has_configurable_(false),
        writable_(false),
        has_writable_(false) {}

  // ES6 6.2.4.1
  static bool IsAccessorDescriptor(PropertyDescriptor* desc) {
    return desc->has_get() || desc->has_set();
  }

  // ES6 6.2.4.2
  static bool IsDataDescriptor(PropertyDescriptor* desc) {
    return desc->has_value() || desc->has_writable();
  }

  // ES6 6.2.4.3
  static bool IsGenericDescriptor(PropertyDescriptor* desc) {
    return !IsAccessorDescriptor(desc) && !IsDataDescriptor(desc);
  }

43 44 45
  // ES6 6.2.4.4
  Handle<Object> ToObject(Isolate* isolate);

46 47
  Handle<PropertyDescriptorObject> ToPropertyDescriptorObject(Isolate* isolate);

48 49 50 51 52 53 54 55
  // ES6 6.2.4.5
  static bool ToPropertyDescriptor(Isolate* isolate, Handle<Object> obj,
                                   PropertyDescriptor* desc);

  // ES6 6.2.4.6
  static void CompletePropertyDescriptor(Isolate* isolate,
                                         PropertyDescriptor* desc);

56 57 58
  bool is_empty() const {
    return !has_enumerable() && !has_configurable() && !has_writable() &&
           !has_value() && !has_get() && !has_set();
59 60 61 62 63 64 65 66 67 68
  }

  bool IsRegularAccessorProperty() const {
    return has_configurable() && has_enumerable() && !has_value() &&
           !has_writable() && has_get() && has_set();
  }

  bool IsRegularDataProperty() const {
    return has_configurable() && has_enumerable() && has_value() &&
           has_writable() && !has_get() && !has_set();
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
  }

  bool enumerable() const { return enumerable_; }
  void set_enumerable(bool enumerable) {
    enumerable_ = enumerable;
    has_enumerable_ = true;
  }
  bool has_enumerable() const { return has_enumerable_; }

  bool configurable() const { return configurable_; }
  void set_configurable(bool configurable) {
    configurable_ = configurable;
    has_configurable_ = true;
  }
  bool has_configurable() const { return has_configurable_; }

  Handle<Object> value() const { return value_; }
  void set_value(Handle<Object> value) { value_ = value; }
  bool has_value() const { return !value_.is_null(); }

  bool writable() const { return writable_; }
  void set_writable(bool writable) {
    writable_ = writable;
    has_writable_ = true;
  }
  bool has_writable() const { return has_writable_; }

  Handle<Object> get() const { return get_; }
  void set_get(Handle<Object> get) { get_ = get; }
  bool has_get() const { return !get_.is_null(); }

  Handle<Object> set() const { return set_; }
  void set_set(Handle<Object> set) { set_ = set; }
  bool has_set() const { return !set_.is_null(); }

  Handle<Object> name() const { return name_; }
  void set_name(Handle<Object> name) { name_ = name; }

  PropertyAttributes ToAttributes() {
    return static_cast<PropertyAttributes>(
        (has_enumerable() && !enumerable() ? DONT_ENUM : NONE) |
        (has_configurable() && !configurable() ? DONT_DELETE : NONE) |
        (has_writable() && !writable() ? READ_ONLY : NONE));
  }

 private:
  bool enumerable_ : 1;
  bool has_enumerable_ : 1;
  bool configurable_ : 1;
  bool has_configurable_ : 1;
  bool writable_ : 1;
  bool has_writable_ : 1;
  Handle<Object> value_;
  Handle<Object> get_;
  Handle<Object> set_;
  Handle<Object> name_;
};

}  // namespace internal
}  // namespace v8

130
#endif  // V8_OBJECTS_PROPERTY_DESCRIPTOR_H_