Skip to main content

v8/
property_attribute.rs

1#[repr(C)]
2#[derive(Debug, Eq, PartialEq)]
3pub struct PropertyAttribute(u32);
4
5impl PropertyAttribute {
6  /// No property attributes.
7  pub const NONE: Self = Self(0);
8
9  /// Not writable. Corresponds to
10  /// `Object.defineProperty(o, "p", { writable: false })`.
11  pub const READ_ONLY: Self = Self(1 << 0);
12
13  /// Not enumerable. Corresponds to
14  /// `Object.defineProperty(o, "p", { enumerable: false })`.
15  pub const DONT_ENUM: Self = Self(1 << 1);
16
17  /// Not configurable. Corresponds to
18  /// `Object.defineProperty(o, "p", { configurable: false })`.
19  pub const DONT_DELETE: Self = Self(1 << 2);
20
21  /// Test if no property attributes are set.
22  #[inline(always)]
23  pub fn is_none(&self) -> bool {
24    *self == PropertyAttribute::NONE
25  }
26
27  /// Test if the read-only property attribute is set.
28  #[inline(always)]
29  pub fn is_read_only(&self) -> bool {
30    self.has(Self::READ_ONLY)
31  }
32
33  /// Test if the non-enumerable property attribute is set.
34  #[inline(always)]
35  pub fn is_dont_enum(&self) -> bool {
36    self.has(Self::DONT_ENUM)
37  }
38
39  /// Test if the non-configurable property attribute is set.
40  #[inline(always)]
41  pub fn is_dont_delete(&self) -> bool {
42    self.has(Self::DONT_DELETE)
43  }
44
45  #[inline(always)]
46  fn has(&self, that: Self) -> bool {
47    let Self(lhs) = self;
48    let Self(rhs) = that;
49    0 != lhs & rhs
50  }
51
52  pub fn as_u32(&self) -> u32 {
53    let Self(bits) = self;
54    *bits
55  }
56}
57
58// Identical to #[derive(Default)] but arguably clearer when made explicit.
59impl Default for PropertyAttribute {
60  fn default() -> Self {
61    Self::NONE
62  }
63}
64
65impl std::ops::BitOr for PropertyAttribute {
66  type Output = Self;
67
68  fn bitor(self, Self(rhs): Self) -> Self {
69    let Self(lhs) = self;
70    Self(lhs | rhs)
71  }
72}
73
74#[test]
75fn test_attr() {
76  assert!(PropertyAttribute::NONE.is_none());
77  assert!(!PropertyAttribute::NONE.is_read_only());
78  assert!(!PropertyAttribute::NONE.is_dont_enum());
79  assert!(!PropertyAttribute::NONE.is_dont_delete());
80
81  assert!(!PropertyAttribute::READ_ONLY.is_none());
82  assert!(PropertyAttribute::READ_ONLY.is_read_only());
83  assert!(!PropertyAttribute::READ_ONLY.is_dont_enum());
84  assert!(!PropertyAttribute::READ_ONLY.is_dont_delete());
85
86  assert!(!PropertyAttribute::DONT_ENUM.is_none());
87  assert!(!PropertyAttribute::DONT_ENUM.is_read_only());
88  assert!(PropertyAttribute::DONT_ENUM.is_dont_enum());
89  assert!(!PropertyAttribute::DONT_ENUM.is_dont_delete());
90
91  assert!(!PropertyAttribute::DONT_DELETE.is_none());
92  assert!(!PropertyAttribute::DONT_DELETE.is_read_only());
93  assert!(!PropertyAttribute::DONT_DELETE.is_dont_enum());
94  assert!(PropertyAttribute::DONT_DELETE.is_dont_delete());
95
96  assert_eq!(PropertyAttribute::NONE, Default::default());
97  assert_eq!(
98    PropertyAttribute::READ_ONLY,
99    PropertyAttribute::NONE | PropertyAttribute::READ_ONLY
100  );
101
102  let attr = PropertyAttribute::READ_ONLY | PropertyAttribute::DONT_ENUM;
103  assert!(!attr.is_none());
104  assert!(attr.is_read_only());
105  assert!(attr.is_dont_enum());
106  assert!(!attr.is_dont_delete());
107
108  let attr = PropertyAttribute::READ_ONLY
109    | PropertyAttribute::READ_ONLY
110    | PropertyAttribute::DONT_ENUM;
111  assert!(!attr.is_none());
112  assert!(attr.is_read_only());
113  assert!(attr.is_dont_enum());
114  assert!(!attr.is_dont_delete());
115}